mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 11:34:18 +00:00
5.1b1
This commit is contained in:
parent
074ca063fe
commit
a45be79d02
4 changed files with 27 additions and 11 deletions
|
@ -1,6 +1,12 @@
|
|||
import typing
|
||||
|
||||
|
||||
class Broadcast:
|
||||
def __init__(self, handler: str, data: dict):
|
||||
def __init__(self, handler: str, data: dict, msg_type: typing.Optional[str] = None):
|
||||
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.data: dict = data
|
||||
|
||||
|
|
|
@ -59,8 +59,8 @@ class Link:
|
|||
self.nid: str = str(uuid.uuid4())
|
||||
self.secret: str = secret
|
||||
self.websocket: typing.Optional[websockets.WebSocketClientProtocol] = None
|
||||
# Not sure on the type annotation here
|
||||
self.request_handler: typing.Callable[[Request], typing.Awaitable[Response]] = request_handler
|
||||
self.request_handler: typing.Callable[[typing.Union[Request, Broadcast]],
|
||||
typing.Awaitable[Response]] = request_handler
|
||||
self._pending_requests: typing.Dict[str, PendingRequest] = {}
|
||||
if loop is None:
|
||||
self._loop = asyncio.get_event_loop()
|
||||
|
@ -170,9 +170,13 @@ class Link:
|
|||
request.set(package.data)
|
||||
continue
|
||||
# Package is a request
|
||||
assert isinstance(package, Package)
|
||||
log.debug(f"Received request {package.source_conv_id}: {package}")
|
||||
response: Response = await self.request_handler(Request.from_dict(package.data))
|
||||
response_package: Package = package.reply(response.to_dict())
|
||||
await self.send(response_package)
|
||||
log.debug(f"Replied to request {response_package.source_conv_id}: {response_package}")
|
||||
elif package.data["msg_type"] == "Request":
|
||||
log.debug(f"Received request {package.source_conv_id}: {package}")
|
||||
response: Response = await self.request_handler(Request.from_dict(package.data))
|
||||
response_package: Package = package.reply(response.to_dict())
|
||||
await self.send(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))
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
import typing
|
||||
|
||||
|
||||
class Request:
|
||||
"""A request sent from a :py:class:`Link` to another.
|
||||
|
||||
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__()
|
||||
if msg_type is not None:
|
||||
assert msg_type == self.__class__.__name__
|
||||
self.msg_type = self.__class__.__name__
|
||||
self.handler: str = handler
|
||||
self.data: dict = data
|
||||
|
||||
|
|
2
setup.py
2
setup.py
|
@ -5,7 +5,7 @@ with open("README.md", "r") as f:
|
|||
|
||||
setuptools.setup(
|
||||
name="royalherald",
|
||||
version="5.0b9",
|
||||
version="5.1b1",
|
||||
author="Stefano Pigozzi",
|
||||
author_email="ste.pigozzi@gmail.com",
|
||||
description="A websocket communication protocol",
|
||||
|
|
Loading…
Reference in a new issue