2023-11-01 04:57:08 +00:00
|
|
|
"""MuxRPC"""
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
from ssb.packet_stream import PSMessageType
|
|
|
|
|
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
class MuxRPCAPIException(Exception):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Exception to raise on MuxRPC API errors"""
|
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
|
2023-11-01 04:57:08 +00:00
|
|
|
class MuxRPCHandler: # pylint: disable=too-few-public-methods
|
|
|
|
"""Base MuxRPC handler class"""
|
2017-07-30 12:08:18 +00:00
|
|
|
|
|
|
|
def check_message(self, msg):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Check message validity"""
|
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
body = msg.body
|
2023-11-01 04:04:43 +00:00
|
|
|
|
|
|
|
if isinstance(body, dict) and "name" in body and body["name"] == "Error":
|
|
|
|
raise MuxRPCAPIException(body["message"])
|
2017-07-30 12:08:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MuxRPCRequestHandler(MuxRPCHandler):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Base class for MuxRPC request handlers"""
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
def __init__(self, ps_handler):
|
|
|
|
self.ps_handler = ps_handler
|
|
|
|
|
|
|
|
def __await__(self):
|
2023-11-01 04:04:43 +00:00
|
|
|
msg = yield from self.ps_handler.__await__()
|
2017-07-30 12:08:18 +00:00
|
|
|
self.check_message(msg)
|
|
|
|
return msg
|
2017-07-30 08:14:26 +00:00
|
|
|
|
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
class MuxRPCSourceHandler(MuxRPCHandler):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""MuxRPC handler for sources"""
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
def __init__(self, ps_handler):
|
|
|
|
self.ps_handler = ps_handler
|
|
|
|
|
2023-11-01 05:03:06 +00:00
|
|
|
def __aiter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
async def __anext__(self):
|
|
|
|
msg = await self.ps_handler.__anext__()
|
|
|
|
self.check_message(msg)
|
|
|
|
|
|
|
|
return msg
|
2017-07-30 08:14:26 +00:00
|
|
|
|
|
|
|
|
2023-11-01 04:57:08 +00:00
|
|
|
class MuxRPCSinkHandlerMixin: # pylint: disable=too-few-public-methods
|
|
|
|
"""Mixin for sink-type MuxRPC handlers"""
|
|
|
|
|
2017-07-30 08:52:04 +00:00
|
|
|
def send(self, msg, msg_type=PSMessageType.JSON, end=False):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Send a message through the stream"""
|
|
|
|
|
2017-07-30 08:52:04 +00:00
|
|
|
self.connection.send(msg, stream=True, msg_type=msg_type, req=self.req, end_err=end)
|
2017-07-30 08:14:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MuxRPCDuplexHandler(MuxRPCSinkHandlerMixin, MuxRPCSourceHandler):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""MuxRPC handler for duplex streams"""
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
def __init__(self, ps_handler, connection, req):
|
2023-11-01 04:57:08 +00:00
|
|
|
super().__init__(ps_handler)
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
self.connection = connection
|
|
|
|
self.req = req
|
|
|
|
|
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
class MuxRPCSinkHandler(MuxRPCHandler, MuxRPCSinkHandlerMixin):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""MuxRPC handler for sinks"""
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
def __init__(self, connection, req):
|
|
|
|
self.connection = connection
|
|
|
|
self.req = req
|
|
|
|
|
|
|
|
|
|
|
|
def _get_appropriate_api_handler(type_, connection, ps_handler, req):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Find the appropriate MuxRPC handler"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
if type_ in {"sync", "async"}:
|
2017-07-30 08:14:26 +00:00
|
|
|
return MuxRPCRequestHandler(ps_handler)
|
2023-11-01 04:57:08 +00:00
|
|
|
|
|
|
|
if type_ == "source":
|
2017-07-30 08:14:26 +00:00
|
|
|
return MuxRPCSourceHandler(ps_handler)
|
2023-11-01 04:57:08 +00:00
|
|
|
|
|
|
|
if type_ == "sink":
|
2017-07-30 08:14:26 +00:00
|
|
|
return MuxRPCSinkHandler(connection, req)
|
2023-11-01 04:57:08 +00:00
|
|
|
|
|
|
|
if type_ == "duplex":
|
2017-07-30 08:14:26 +00:00
|
|
|
return MuxRPCDuplexHandler(ps_handler, connection, req)
|
|
|
|
|
2023-11-01 04:57:08 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class MuxRPCRequest:
|
|
|
|
"""MuxRPC request"""
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_message(cls, message):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Initialise a request from a raw packet stream message"""
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
body = message.body
|
2023-11-01 04:57:08 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
return cls(".".join(body["name"]), body["args"])
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
def __init__(self, name, args):
|
|
|
|
self.name = name
|
|
|
|
self.args = args
|
|
|
|
|
|
|
|
def __repr__(self):
|
2023-11-01 04:57:08 +00:00
|
|
|
return f"<MuxRPCRequest {self.name} {self.args}>"
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
|
2023-11-01 04:57:08 +00:00
|
|
|
class MuxRPCMessage:
|
|
|
|
"""MuxRPC message"""
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
@classmethod
|
|
|
|
def from_message(cls, message):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Initialise a MuxRPC message from a raw packet stream message"""
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
return cls(message.body)
|
|
|
|
|
|
|
|
def __init__(self, body):
|
|
|
|
self.body = body
|
|
|
|
|
|
|
|
def __repr__(self):
|
2023-11-01 04:57:08 +00:00
|
|
|
return f"<MuxRPCMessage {self.body}>"
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
|
2023-11-01 04:57:08 +00:00
|
|
|
class MuxRPCAPI:
|
|
|
|
"""Generit MuxRPC API"""
|
2017-07-30 08:14:26 +00:00
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.handlers = {}
|
|
|
|
self.connection = None
|
|
|
|
|
|
|
|
async def __await__(self):
|
|
|
|
async for req_message in self.connection:
|
|
|
|
if req_message is None:
|
|
|
|
return
|
2023-11-01 04:04:43 +00:00
|
|
|
|
2023-11-02 05:15:17 +00:00
|
|
|
body = req_message.body
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
if isinstance(body, dict) and body.get("name"):
|
2017-07-30 08:14:26 +00:00
|
|
|
self.process(self.connection, MuxRPCRequest.from_message(req_message))
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
def add_connection(self, connection):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Set the packet stream connection of this RPC API"""
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
self.connection = connection
|
|
|
|
|
|
|
|
def define(self, name):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Decorator to define an RPC method handler"""
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
def _handle(f):
|
|
|
|
self.handlers[name] = f
|
|
|
|
|
|
|
|
@wraps(f)
|
|
|
|
def _f(*args, **kwargs):
|
|
|
|
return f(*args, **kwargs)
|
2023-11-01 04:04:43 +00:00
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
return f
|
2023-11-01 04:04:43 +00:00
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
return _handle
|
|
|
|
|
|
|
|
def process(self, connection, request):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Process an incoming request"""
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
handler = self.handlers.get(request.name)
|
2023-11-01 04:57:08 +00:00
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
if not handler:
|
2023-11-01 04:57:08 +00:00
|
|
|
raise MuxRPCAPIException(f"Method {request.name} not found!")
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
handler(connection, request)
|
2017-07-30 08:14:26 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
def call(self, name, args, type_="sync"):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Call an RPC method"""
|
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
if not self.connection.is_connected:
|
2023-11-01 04:57:08 +00:00
|
|
|
raise Exception("not connected") # pylint: disable=broad-exception-raised
|
2023-11-01 04:04:43 +00:00
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
old_counter = self.connection.req_counter
|
2023-11-01 04:04:43 +00:00
|
|
|
ps_handler = self.connection.send(
|
|
|
|
{"name": name.split("."), "args": args, "type": type_},
|
|
|
|
stream=type_ in {"sink", "source", "duplex"},
|
|
|
|
)
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
return _get_appropriate_api_handler(type_, self.connection, ps_handler, old_counter)
|