239 lines
7.1 KiB
Python
239 lines
7.1 KiB
Python
"""MuxRPC"""
|
|
|
|
from typing import Any, AsyncIterator, Callable, Dict, Generator, List, Literal, Optional, Union
|
|
|
|
from typing_extensions import Self
|
|
|
|
from .packet_stream import PacketStream, PSMessage, PSMessageType, PSRequestHandler, PSStreamHandler
|
|
|
|
MuxRPCJSON = Dict[str, Any]
|
|
MuxRPCCallType = Literal["async", "duplex", "sink", "source", "sync"]
|
|
MuxRPCRequestHandlerType = Callable[[PacketStream, "MuxRPCRequest"], None]
|
|
MuxRPCRequestParam = Union[bytes, str, MuxRPCJSON] # pylint: disable=invalid-name
|
|
|
|
|
|
class MuxRPCAPIException(Exception):
|
|
"""Exception to raise on MuxRPC API errors"""
|
|
|
|
|
|
class MuxRPCHandler: # pylint: disable=too-few-public-methods
|
|
"""Base MuxRPC handler class"""
|
|
|
|
def check_message(self, msg: PSMessage) -> None:
|
|
"""Check message validity"""
|
|
|
|
body = msg.body
|
|
|
|
if isinstance(body, dict) and "name" in body and body["name"] == "Error":
|
|
raise MuxRPCAPIException(body["message"])
|
|
|
|
def __await__(self) -> Generator[Optional[PSMessage], None, None]:
|
|
raise NotImplementedError()
|
|
|
|
def __aiter__(self) -> AsyncIterator[Optional[PSMessage]]:
|
|
raise NotImplementedError()
|
|
|
|
async def __anext__(self) -> Optional[PSMessage]:
|
|
raise NotImplementedError()
|
|
|
|
def send(self, msg: Any, msg_type: PSMessageType = PSMessageType.JSON, end: bool = False) -> None:
|
|
"""Send a message through the stream"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
class MuxRPCRequestHandler(MuxRPCHandler): # pylint: disable=abstract-method
|
|
"""Base class for MuxRPC request handlers"""
|
|
|
|
def __init__(self, ps_handler: PSRequestHandler):
|
|
self.ps_handler = ps_handler
|
|
|
|
def __aiter__(self) -> AsyncIterator[Optional[PSMessage]]:
|
|
return self
|
|
|
|
async def __anext__(self) -> Optional[PSMessage]:
|
|
msg = await self.ps_handler.__anext__()
|
|
|
|
assert msg
|
|
|
|
self.check_message(msg)
|
|
|
|
return msg
|
|
|
|
|
|
class MuxRPCSourceHandler(MuxRPCHandler): # pylint: disable=abstract-method
|
|
"""MuxRPC handler for sources"""
|
|
|
|
def __init__(self, ps_handler: PSStreamHandler):
|
|
self.ps_handler = ps_handler
|
|
|
|
def __aiter__(self) -> AsyncIterator[Optional[PSMessage]]:
|
|
return self
|
|
|
|
async def __anext__(self) -> Optional[PSMessage]:
|
|
msg = await self.ps_handler.__anext__()
|
|
|
|
assert msg
|
|
|
|
self.check_message(msg)
|
|
|
|
return msg
|
|
|
|
|
|
class MuxRPCSinkHandlerMixin: # pylint: disable=too-few-public-methods
|
|
"""Mixin for sink-type MuxRPC handlers"""
|
|
|
|
connection: Optional[PacketStream]
|
|
req: Optional[int]
|
|
|
|
def send(self, msg: Any, msg_type: PSMessageType = PSMessageType.JSON, end: bool = False) -> None:
|
|
"""Send a message through the stream"""
|
|
|
|
assert self.connection
|
|
|
|
self.connection.send(msg, stream=True, msg_type=msg_type, req=self.req, end_err=end)
|
|
|
|
|
|
class MuxRPCDuplexHandler(MuxRPCSinkHandlerMixin, MuxRPCSourceHandler): # pylint: disable=abstract-method
|
|
"""MuxRPC handler for duplex streams"""
|
|
|
|
def __init__(self, ps_handler: PSStreamHandler, connection: PacketStream, req: int):
|
|
super().__init__(ps_handler)
|
|
|
|
self.connection = connection
|
|
self.req = req
|
|
|
|
|
|
class MuxRPCSinkHandler(MuxRPCHandler, MuxRPCSinkHandlerMixin): # pylint: disable=abstract-method
|
|
"""MuxRPC handler for sinks"""
|
|
|
|
def __init__(self, connection: PacketStream, req: int):
|
|
self.connection = connection
|
|
self.req = req
|
|
|
|
|
|
def _get_appropriate_api_handler(
|
|
type_: MuxRPCCallType, connection: PacketStream, ps_handler: Union[PSRequestHandler, PSStreamHandler], req: int
|
|
) -> MuxRPCHandler:
|
|
"""Find the appropriate MuxRPC handler"""
|
|
|
|
if type_ in {"sync", "async"}:
|
|
assert isinstance(ps_handler, PSRequestHandler)
|
|
return MuxRPCRequestHandler(ps_handler)
|
|
|
|
if type_ == "source":
|
|
assert isinstance(ps_handler, PSStreamHandler)
|
|
return MuxRPCSourceHandler(ps_handler)
|
|
|
|
if type_ == "sink":
|
|
return MuxRPCSinkHandler(connection, req)
|
|
|
|
if type_ == "duplex":
|
|
assert isinstance(ps_handler, PSStreamHandler)
|
|
return MuxRPCDuplexHandler(ps_handler, connection, req)
|
|
|
|
raise TypeError(f"Unknown request type {type_}")
|
|
|
|
|
|
class MuxRPCRequest:
|
|
"""MuxRPC request"""
|
|
|
|
@classmethod
|
|
def from_message(cls, message: PSMessage) -> Self:
|
|
"""Initialise a request from a raw packet stream message"""
|
|
|
|
body = message.body
|
|
|
|
return cls(".".join(body["name"]), body["args"])
|
|
|
|
def __init__(self, name: str, args: List[MuxRPCRequestParam]):
|
|
self.name = name
|
|
self.args = args
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<MuxRPCRequest {self.name} {self.args}>"
|
|
|
|
|
|
class MuxRPCMessage:
|
|
"""MuxRPC message"""
|
|
|
|
@classmethod
|
|
def from_message(cls, message: PSMessage) -> Self:
|
|
"""Initialise a MuxRPC message from a raw packet stream message"""
|
|
|
|
return cls(message.body)
|
|
|
|
def __init__(self, body: PSMessage):
|
|
self.body = body
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<MuxRPCMessage {self.body}>"
|
|
|
|
|
|
class MuxRPCAPI:
|
|
"""Generit MuxRPC API"""
|
|
|
|
def __init__(self) -> None:
|
|
self.handlers: Dict[str, MuxRPCRequestHandlerType] = {}
|
|
self.connection: Optional[PacketStream] = None
|
|
|
|
def __aiter__(self) -> AsyncIterator[None]:
|
|
return self
|
|
|
|
async def __anext__(self) -> None:
|
|
assert self.connection
|
|
|
|
req_message = await self.connection.__anext__()
|
|
|
|
if req_message is None:
|
|
raise StopAsyncIteration()
|
|
|
|
body = req_message.body
|
|
|
|
if isinstance(body, dict) and body.get("name"):
|
|
self.process(self.connection, MuxRPCRequest.from_message(req_message))
|
|
|
|
def __await__(self) -> Generator[None, None, None]:
|
|
yield from self.__anext__().__await__()
|
|
|
|
def add_connection(self, connection: PacketStream) -> None:
|
|
"""Set the packet stream connection of this RPC API"""
|
|
|
|
self.connection = connection
|
|
|
|
def define(self, name: str) -> Callable[[MuxRPCRequestHandlerType], MuxRPCRequestHandlerType]:
|
|
"""Decorator to define an RPC method handler"""
|
|
|
|
def _handle(f: MuxRPCRequestHandlerType) -> MuxRPCRequestHandlerType:
|
|
self.handlers[name] = f
|
|
|
|
return f
|
|
|
|
return _handle
|
|
|
|
def process(self, connection: PacketStream, request: MuxRPCRequest) -> None:
|
|
"""Process an incoming request"""
|
|
|
|
handler = self.handlers.get(request.name)
|
|
|
|
if not handler:
|
|
raise MuxRPCAPIException(f"Method {request.name} not found!")
|
|
|
|
handler(connection, request)
|
|
|
|
def call(self, name: str, args: List[MuxRPCRequestParam], type_: MuxRPCCallType = "sync") -> MuxRPCHandler:
|
|
"""Call an RPC method"""
|
|
|
|
assert self.connection
|
|
|
|
if not self.connection.is_connected:
|
|
raise Exception("not connected") # pylint: disable=broad-exception-raised
|
|
|
|
old_counter = self.connection.req_counter
|
|
ps_handler = self.connection.send(
|
|
{"name": name.split("."), "args": args, "type": type_},
|
|
stream=type_ in {"sink", "source", "duplex"},
|
|
)
|
|
|
|
return _get_appropriate_api_handler(type_, self.connection, ps_handler, old_counter)
|