1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
This commit is contained in:
Steffo 2019-10-20 18:28:47 +02:00
parent 074ca063fe
commit a45be79d02
4 changed files with 27 additions and 11 deletions

View file

@ -1,6 +1,12 @@
import typing
class Broadcast: class Broadcast:
def __init__(self, handler: str, data: dict): def __init__(self, handler: str, data: dict, msg_type: typing.Optional[str] = None):
super().__init__() super().__init__()
if msg_type is not None:
assert msg_type == self.__class__.__name__
self.msg_type = self.__class__.__name__
self.handler: str = handler self.handler: str = handler
self.data: dict = data self.data: dict = data

View file

@ -59,8 +59,8 @@ class Link:
self.nid: str = str(uuid.uuid4()) self.nid: str = str(uuid.uuid4())
self.secret: str = secret self.secret: str = secret
self.websocket: typing.Optional[websockets.WebSocketClientProtocol] = None self.websocket: typing.Optional[websockets.WebSocketClientProtocol] = None
# Not sure on the type annotation here self.request_handler: typing.Callable[[typing.Union[Request, Broadcast]],
self.request_handler: typing.Callable[[Request], typing.Awaitable[Response]] = request_handler typing.Awaitable[Response]] = request_handler
self._pending_requests: typing.Dict[str, PendingRequest] = {} self._pending_requests: typing.Dict[str, PendingRequest] = {}
if loop is None: if loop is None:
self._loop = asyncio.get_event_loop() self._loop = asyncio.get_event_loop()
@ -170,9 +170,13 @@ class Link:
request.set(package.data) request.set(package.data)
continue continue
# Package is a request # Package is a request
assert isinstance(package, Package) elif package.data["msg_type"] == "Request":
log.debug(f"Received request {package.source_conv_id}: {package}") log.debug(f"Received request {package.source_conv_id}: {package}")
response: Response = await self.request_handler(Request.from_dict(package.data)) response: Response = await self.request_handler(Request.from_dict(package.data))
response_package: Package = package.reply(response.to_dict()) response_package: Package = package.reply(response.to_dict())
await self.send(response_package) await self.send(response_package)
log.debug(f"Replied to request {response_package.source_conv_id}: {response_package}") log.debug(f"Replied to request {response_package.source_conv_id}: {response_package}")
# Package is a broadcast
elif package.data["msg_type"] == "Broadcast":
log.debug(f"Received broadcast {package.source_conv_id}: {package}")
await self.request_handler(Broadcast.from_dict(package.data))

View file

@ -1,10 +1,16 @@
import typing
class Request: class Request:
"""A request sent from a :py:class:`Link` to another. """A request sent from a :py:class:`Link` to another.
It contains the name of the requested handler, in addition to the data.""" It contains the name of the requested handler, in addition to the data."""
def __init__(self, handler: str, data: dict): def __init__(self, handler: str, data: dict, msg_type: typing.Optional[str] = None):
super().__init__() super().__init__()
if msg_type is not None:
assert msg_type == self.__class__.__name__
self.msg_type = self.__class__.__name__
self.handler: str = handler self.handler: str = handler
self.data: dict = data self.data: dict = data

View file

@ -5,7 +5,7 @@ with open("README.md", "r") as f:
setuptools.setup( setuptools.setup(
name="royalherald", name="royalherald",
version="5.0b9", version="5.1b1",
author="Stefano Pigozzi", author="Stefano Pigozzi",
author_email="ste.pigozzi@gmail.com", author_email="ste.pigozzi@gmail.com",
description="A websocket communication protocol", description="A websocket communication protocol",