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):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-11-01 05:03:06 +00:00
|
|
|
class MuxRPCHandler:
|
2017-07-30 12:08:18 +00:00
|
|
|
def check_message(self, msg):
|
|
|
|
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):
|
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):
|
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
|
|
|
|
|
|
|
|
|
|
|
class MuxRPCSinkHandlerMixin(object):
|
2017-07-30 08:52:04 +00:00
|
|
|
def send(self, msg, msg_type=PSMessageType.JSON, end=False):
|
|
|
|
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):
|
|
|
|
def __init__(self, ps_handler, connection, req):
|
|
|
|
super(MuxRPCDuplexHandler, self).__init__(ps_handler)
|
|
|
|
self.connection = connection
|
|
|
|
self.req = req
|
|
|
|
|
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
class MuxRPCSinkHandler(MuxRPCHandler, MuxRPCSinkHandlerMixin):
|
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: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:04:43 +00:00
|
|
|
elif type_ == "source":
|
2017-07-30 08:14:26 +00:00
|
|
|
return MuxRPCSourceHandler(ps_handler)
|
2023-11-01 04:04:43 +00:00
|
|
|
elif type_ == "sink":
|
2017-07-30 08:14:26 +00:00
|
|
|
return MuxRPCSinkHandler(connection, req)
|
2023-11-01 04:04:43 +00:00
|
|
|
elif type_ == "duplex":
|
2017-07-30 08:14:26 +00:00
|
|
|
return MuxRPCDuplexHandler(ps_handler, connection, req)
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
class MuxRPCRequest(object):
|
|
|
|
@classmethod
|
|
|
|
def from_message(cls, message):
|
|
|
|
body = message.body
|
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:04:43 +00:00
|
|
|
return "<MuxRPCRequest {0.name} {0.args}>".format(self)
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
class MuxRPCMessage(object):
|
|
|
|
@classmethod
|
|
|
|
def from_message(cls, message):
|
|
|
|
return cls(message.body)
|
|
|
|
|
|
|
|
def __init__(self, body):
|
|
|
|
self.body = body
|
|
|
|
|
|
|
|
def __repr__(self):
|
2023-11-01 04:04:43 +00:00
|
|
|
return "<MuxRPCMessage {0.body}}>".format(self)
|
2017-07-30 08:14:26 +00:00
|
|
|
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
class MuxRPCAPI(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.handlers = {}
|
|
|
|
self.connection = None
|
|
|
|
|
|
|
|
async def __await__(self):
|
|
|
|
async for req_message in self.connection:
|
2017-07-30 08:14:26 +00:00
|
|
|
body = req_message.body
|
2017-07-29 09:54:03 +00:00
|
|
|
if req_message is None:
|
|
|
|
return
|
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):
|
|
|
|
self.connection = connection
|
|
|
|
|
|
|
|
def define(self, name):
|
|
|
|
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):
|
|
|
|
handler = self.handlers.get(request.name)
|
|
|
|
if not handler:
|
2023-11-01 04:04:43 +00:00
|
|
|
raise MuxRPCAPIException("Method {} not found!".format(request.name))
|
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"):
|
2017-07-30 12:08:18 +00:00
|
|
|
if not self.connection.is_connected:
|
2023-11-01 04:04:43 +00:00
|
|
|
raise Exception("not connected")
|
|
|
|
|
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)
|