mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 11:34:18 +00:00
5.0b9
This commit is contained in:
parent
7a0986033b
commit
074ca063fe
6 changed files with 36 additions and 44 deletions
20
royalherald/broadcast.py
Normal file
20
royalherald/broadcast.py
Normal file
|
@ -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})"
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -104,7 +104,7 @@ class Server:
|
|||
if package.destination == "<none>":
|
||||
return []
|
||||
# Is it all possible destinations?
|
||||
if package.destination == "<all>":
|
||||
if package.destination == "*":
|
||||
return self.identified_clients
|
||||
# Is it a valid nid?
|
||||
try:
|
||||
|
|
2
setup.py
2
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",
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue