Skip to content

Commit

Permalink
Optional async-backend interface (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia authored Apr 25, 2024
1 parent 733adfd commit 2c89efa
Show file tree
Hide file tree
Showing 40 changed files with 268 additions and 116 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import logging
from collections.abc import AsyncGenerator
from typing import Any, TypeAlias

from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer
from easynetwork.servers import AsyncTCPNetworkServer
Expand Down Expand Up @@ -101,14 +100,13 @@ async def main() -> None:
port = 9000
protocol = JSONProtocol()
handler = EchoRequestHandler()
backend = AsyncIOBackend()

logging.basicConfig(
level=logging.INFO,
format="[ %(levelname)s ] [ %(name)s ] %(message)s",
)

async with AsyncTCPNetworkServer(host, port, protocol, handler, backend) as server:
async with AsyncTCPNetworkServer(host, port, protocol, handler) as server:
try:
await server.serve_forever()
except asyncio.CancelledError:
Expand Down Expand Up @@ -154,12 +152,11 @@ if __name__ == "__main__":
import asyncio

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend

...

async def main() -> None:
async with AsyncTCPNetworkClient(("localhost", 9000), JSONProtocol(), AsyncIOBackend()) as client:
async with AsyncTCPNetworkClient(("localhost", 9000), JSONProtocol()) as client:
await client.send_packet({"data": {"my_body": ["as json"]}})
response = await client.recv_packet() # response should be the sent dictionary
print(response) # prints {'data': {'my_body': ['as json']}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import asyncio

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer


async def main() -> None:
protocol = StreamProtocol(JSONSerializer())
address = ("localhost", 9000)
backend = AsyncIOBackend()

async with AsyncTCPNetworkClient(address, protocol, backend) as client:
async with AsyncTCPNetworkClient(address, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
import asyncio

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer


async def main() -> None:
protocol = StreamProtocol(JSONSerializer())
address = ("localhost", 9000)
backend = AsyncIOBackend()

try:
async with asyncio.timeout(30):
client = AsyncTCPNetworkClient(address, protocol, backend)
client = AsyncTCPNetworkClient(address, protocol)
await client.wait_connected()
except TimeoutError:
print(f"Could not connect to {address} after 30 seconds")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import socket

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer

Expand All @@ -20,9 +19,8 @@ async def obtain_a_connected_socket() -> socket.socket:
async def main() -> None:
protocol = StreamProtocol(JSONSerializer())
sock = await obtain_a_connected_socket()
backend = AsyncIOBackend()

async with AsyncTCPNetworkClient(sock, protocol, backend) as client:
async with AsyncTCPNetworkClient(sock, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from easynetwork.clients import AsyncTCPNetworkClient, TCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer

Expand All @@ -22,13 +21,11 @@ def ssl_shared_lock_for_sync_client() -> None:
async def ssl_shared_lock_for_async_client() -> None:
remote_address = ("remote_address", 12345)
protocol = StreamProtocol(JSONSerializer())
backend = AsyncIOBackend()

# [start]
client = AsyncTCPNetworkClient(
remote_address,
protocol,
backend,
ssl=True,
ssl_shared_lock=False,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.exceptions import StreamProtocolParseError
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer

Expand Down Expand Up @@ -78,21 +77,19 @@ async def socket_proxy_example(client: AsyncTCPNetworkClient[Any, Any]) -> None:
async def max_recv_size_example() -> None:
address = ("remote_address", 12345)
protocol = StreamProtocol(JSONSerializer())
backend = AsyncIOBackend()

# [start]
async with AsyncTCPNetworkClient(address, protocol, backend, max_recv_size=1024) as client:
async with AsyncTCPNetworkClient(address, protocol, max_recv_size=1024) as client:
# Only do socket.recv(1024) calls
packet = await client.recv_packet()


async def ssl_default_context_example() -> None:
address = ("remote_address", 12345)
protocol = StreamProtocol(JSONSerializer())
backend = AsyncIOBackend()

# [start]
async with AsyncTCPNetworkClient(address, protocol, backend, ssl=True) as client:
async with AsyncTCPNetworkClient(address, protocol, ssl=True) as client:
await client.send_packet({"data": 42})

packet = await client.recv_packet()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import asyncio
from collections.abc import AsyncGenerator

from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.servers import AsyncTCPNetworkServer
from easynetwork.servers.handlers import AsyncStreamClient, AsyncStreamRequestHandler
Expand Down Expand Up @@ -36,10 +35,9 @@ async def main() -> None:
host, port = "localhost", 9000
protocol = ServerProtocol()
handler = MyRequestHandler()
backend = AsyncIOBackend()

# Create the server, binding to localhost on port 9000
async with AsyncTCPNetworkServer(host, port, protocol, handler, backend) as server:
async with AsyncTCPNetworkServer(host, port, protocol, handler) as server:
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
await server.serve_forever()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Any

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer
from easynetwork.servers import AsyncTCPNetworkServer
Expand All @@ -30,11 +29,10 @@ async def handle(
await client.send_packet({"task": current_task.get_name(), "request": request})


async def client(host: str, port: int, message: str, backend: AsyncIOBackend) -> None:
async def client(host: str, port: int, message: str) -> None:
async with AsyncTCPNetworkClient(
(host, port),
JSONProtocol(),
backend,
) as client:
await client.send_packet({"message": message})
response = await client.recv_packet()
Expand All @@ -45,14 +43,12 @@ async def main() -> None:
host, port = "localhost", 9000
protocol = JSONProtocol()
handler = MyRequestHandler()
backend = AsyncIOBackend()

server = AsyncTCPNetworkServer(
host,
port,
protocol,
handler,
backend,
)

async with server:
Expand All @@ -62,9 +58,9 @@ async def main() -> None:

print(f"Server loop running in task: {server_task.get_name()}")

await client(host, port, "Hello world 1", backend)
await client(host, port, "Hello world 2", backend)
await client(host, port, "Hello world 3", backend)
await client(host, port, "Hello world 1")
await client(host, port, "Hello world 2")
await client(host, port, "Hello world 3")

await server.shutdown()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Any

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import StreamProtocol
from easynetwork.serializers import JSONSerializer
from easynetwork.servers import AsyncTCPNetworkServer
Expand All @@ -31,11 +30,10 @@ async def handle(
await client.send_packet({"task": current_task.get_name(), "request": request})


async def client(host: str, port: int, message: str, backend: AsyncIOBackend) -> None:
async def client(host: str, port: int, message: str) -> None:
async with AsyncTCPNetworkClient(
(host, port),
JSONProtocol(),
backend,
ssl=True,
) as client:
await client.send_packet({"message": message})
Expand All @@ -47,7 +45,6 @@ async def main() -> None:
host, port = "localhost", 9000
protocol = JSONProtocol()
handler = MyRequestHandler()
backend = AsyncIOBackend()

ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(
Expand All @@ -59,7 +56,6 @@ async def main() -> None:
port,
protocol,
handler,
backend,
ssl=ssl_context,
)

Expand All @@ -70,9 +66,9 @@ async def main() -> None:

print(f"Server loop running in task: {server_task.get_name()}")

await client(host, port, "Hello world 1", backend)
await client(host, port, "Hello world 2", backend)
await client(host, port, "Hello world 3", backend)
await client(host, port, "Hello world 1")
await client(host, port, "Hello world 2")
await client(host, port, "Hello world 3")

await server.shutdown()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import asyncio

from easynetwork.clients import AsyncUDPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import DatagramProtocol
from easynetwork.serializers import JSONSerializer


async def main() -> None:
protocol = DatagramProtocol(JSONSerializer())
address = ("localhost", 9000)
backend = AsyncIOBackend()

async with AsyncUDPNetworkClient(address, protocol, backend) as client:
async with AsyncUDPNetworkClient(address, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import socket

from easynetwork.clients import AsyncUDPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import DatagramProtocol
from easynetwork.serializers import JSONSerializer

Expand All @@ -20,9 +19,8 @@ async def obtain_a_connected_socket() -> socket.socket:
async def main() -> None:
protocol = DatagramProtocol(JSONSerializer())
sock = await obtain_a_connected_socket()
backend = AsyncIOBackend()

async with AsyncUDPNetworkClient(sock, protocol, backend) as client:
async with AsyncUDPNetworkClient(sock, protocol) as client:
print(f"Remote address: {client.get_remote_address()}")

...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import asyncio
from collections.abc import AsyncGenerator

from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import DatagramProtocol
from easynetwork.servers import AsyncUDPNetworkServer
from easynetwork.servers.handlers import AsyncDatagramClient, AsyncDatagramRequestHandler
Expand Down Expand Up @@ -36,10 +35,9 @@ async def main() -> None:
host, port = "localhost", 9000
protocol = ServerProtocol()
handler = MyRequestHandler()
backend = AsyncIOBackend()

# Create the server, binding to localhost on port 9000
async with AsyncUDPNetworkServer(host, port, protocol, handler, backend) as server:
async with AsyncUDPNetworkServer(host, port, protocol, handler) as server:
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
await server.serve_forever()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Any

from easynetwork.clients import AsyncUDPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend
from easynetwork.protocol import DatagramProtocol
from easynetwork.serializers import JSONSerializer
from easynetwork.servers import AsyncUDPNetworkServer
Expand All @@ -30,8 +29,8 @@ async def handle(
await client.send_packet({"task": current_task.get_name(), "request": request})


async def client(host: str, port: int, message: str, backend: AsyncIOBackend) -> None:
async with AsyncUDPNetworkClient((host, port), JSONProtocol(), backend) as client:
async def client(host: str, port: int, message: str) -> None:
async with AsyncUDPNetworkClient((host, port), JSONProtocol()) as client:
await client.send_packet({"message": message})
response = await client.recv_packet()
print(f"From server: {response}")
Expand All @@ -41,14 +40,12 @@ async def main() -> None:
host, port = "localhost", 9000
protocol = JSONProtocol()
handler = MyRequestHandler()
backend = AsyncIOBackend()

server = AsyncUDPNetworkServer(
host,
port,
protocol,
handler,
backend,
)

async with server:
Expand All @@ -58,9 +55,9 @@ async def main() -> None:

print(f"Server loop running in task: {server_task.get_name()}")

await client(host, port, "Hello world 1", backend)
await client(host, port, "Hello world 2", backend)
await client(host, port, "Hello world 3", backend)
await client(host, port, "Hello world 1")
await client(host, port, "Hello world 2")
await client(host, port, "Hello world 3")

await server.shutdown()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sys

from easynetwork.clients import AsyncTCPNetworkClient
from easynetwork.lowlevel.std_asyncio import AsyncIOBackend

from json_protocol import JSONProtocol

Expand All @@ -13,10 +12,9 @@ async def main() -> None:
host = "localhost"
port = 9000
protocol = JSONProtocol()
backend = AsyncIOBackend()

# Connect to server
async with AsyncTCPNetworkClient((host, port), protocol, backend) as client:
async with AsyncTCPNetworkClient((host, port), protocol) as client:
# Send data
request = {"command-line arguments": sys.argv[1:]}
await client.send_packet(request)
Expand Down
Loading

0 comments on commit 2c89efa

Please sign in to comment.