From 074ca063feb48767d3455f500aa9671af13918d0 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 20 Oct 2019 18:12:12 +0200 Subject: [PATCH] 5.0b9 --- royalherald/broadcast.py | 20 ++++++++++++++++++++ royalherald/link.py | 11 ++++++++++- royalherald/request.py | 8 ++++---- royalherald/server.py | 2 +- setup.py | 2 +- tests/test_network.py | 37 ------------------------------------- 6 files changed, 36 insertions(+), 44 deletions(-) create mode 100644 royalherald/broadcast.py diff --git a/royalherald/broadcast.py b/royalherald/broadcast.py new file mode 100644 index 00000000..7e6ae5cd --- /dev/null +++ b/royalherald/broadcast.py @@ -0,0 +1,20 @@ +class Broadcast: + def __init__(self, handler: str, data: dict): + super().__init__() + self.handler: str = handler + self.data: dict = data + + def to_dict(self): + return self.__dict__ + + @classmethod + def from_dict(cls, d: dict): + return cls(**d) + + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.handler == other.handler and self.data == other.data + return False + + def __repr__(self): + return f"{self.__class__.__qualname__}(handler={self.handler}, data={self.data})" diff --git a/royalherald/link.py b/royalherald/link.py index aef61a35..b060137d 100644 --- a/royalherald/link.py +++ b/royalherald/link.py @@ -7,6 +7,7 @@ import typing from .package import Package from .request import Request from .response import Response, ResponseSuccess, ResponseFailure +from .broadcast import Broadcast from .errors import ConnectionClosedError, InvalidServerResponseError @@ -127,13 +128,21 @@ class Link: await self.websocket.send(package.to_json_bytes()) log.debug(f"Sent package: {package}") + @requires_identification + async def broadcast(self, destination: str, broadcast: Broadcast) -> None: + package = Package(broadcast.to_dict(), source=self.nid, destination=destination) + await self.send(package) + log.debug(f"Sent broadcast: {broadcast}") + @requires_identification async def request(self, destination: str, request: Request) -> Response: + if destination.startswith("*"): + raise ValueError("requests cannot have multiple destinations") package = Package(request.to_dict(), source=self.nid, destination=destination) request = PendingRequest(loop=self._loop) self._pending_requests[package.source_conv_id] = request await self.send(package) - log.debug(f"Sent to {destination}: {request}") + log.debug(f"Sent request to {destination}: {request}") await request.event.wait() if request.data["type"] == "ResponseSuccess": response: Response = ResponseSuccess.from_dict(request.data) diff --git a/royalherald/request.py b/royalherald/request.py index 76500801..97d8e84b 100644 --- a/royalherald/request.py +++ b/royalherald/request.py @@ -11,12 +11,12 @@ class Request: def to_dict(self): return self.__dict__ - @staticmethod - def from_dict(d: dict): - return Request(**d) + @classmethod + def from_dict(cls, d: dict): + return cls(**d) def __eq__(self, other): - if isinstance(other, Request): + if isinstance(other, self.__class__): return self.handler == other.handler and self.data == other.data return False diff --git a/royalherald/server.py b/royalherald/server.py index 977782d0..716b578c 100644 --- a/royalherald/server.py +++ b/royalherald/server.py @@ -104,7 +104,7 @@ class Server: if package.destination == "": return [] # Is it all possible destinations? - if package.destination == "": + if package.destination == "*": return self.identified_clients # Is it a valid nid? try: diff --git a/setup.py b/setup.py index fc9b8207..c77692bc 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as f: setuptools.setup( name="royalherald", - version="5.0b8", + version="5.0b9", author="Stefano Pigozzi", author_email="ste.pigozzi@gmail.com", description="A websocket communication protocol", diff --git a/tests/test_network.py b/tests/test_network.py index 75a913cd..ad4cd064 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -1,24 +1,8 @@ import pytest import uuid -import asyncio -import logging import royalherald as h -log = logging.root -stream_handler = logging.StreamHandler() -stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{") -log.addHandler(stream_handler) -log.setLevel(logging.WARNING) - - -@pytest.fixture -def async_loop(): - loop = asyncio.get_event_loop() - yield loop - loop.close() - - async def echo_request_handler(message): return message @@ -37,24 +21,3 @@ def test_package_serialization(): def test_request_creation(): request = h.Request("pytest", {"testing": "is fun", "bugs": "are less fun"}) assert request == h.Request.from_dict(request.to_dict()) - - -# Broken! -# -# def test_links(async_loop: asyncio.AbstractEventLoop): -# address, port = "127.0.0.1", 1234 -# master = h.Server(address, port, "test", loop=async_loop) -# async_loop.create_task(master.run()) -# async_loop.run_until_complete(asyncio.sleep(5)) -# # Test invalid secret -# wrong_secret_link = h.Link(f"ws://{address}:{port}", "invalid", "test", echo_request_handler, loop=async_loop) -# with pytest.raises(h.ConnectionClosedError): -# async_loop.run_until_complete(wrong_secret_link.run()) -# # Test regular connection -# link1 = h.Link("ws://127.0.0.1:1235", "test", "one", echo_request_handler, loop=async_loop) -# async_loop.create_task(link1.run()) -# link2 = h.Link("ws://127.0.0.1:1235", "test", "two", echo_request_handler, loop=async_loop) -# async_loop.create_task(link2.run()) -# message = {"ciao": "ciao"} -# response = async_loop.run_until_complete(link1.request(message, "two")) -# assert message == response