class EndpointsRoot(object): endpoints = [] rqex = RequestExtractor() parser = Parser() collector = None def __init__(self, collector): self.collector = collector self.logger = logging.getLogger() def register(self, endpoint): if isinstance(endpoint, Endpoint): self.endpoints.append(endpoint) else: raise TypeError("Not a Endpoint type") def do_calls(self): dl = [] while True: line = self.collector.get() if line is None: break uid, ip, request = line d = self.call(request, from_ip=ip) d.addCallback(self._reply, uid) @d.addErrback def _eb(failure): self.logger.warn("\033[31mRequest call failed: %s\033[0m" % str(failure)) dl.append(d) return DeferredList(dl) def _reply(self, result, uid): self.logger.debug("\033[34mResult for session uid: %d: %s\033[0m" % (uid, str(result))) self.collector.release(uid, self.parser.encode(result)) def call(self, request, from_ip): prefix, method, args = self.rqex.extract(request) for endpoint in self.endpoints: if endpoint.prefix == prefix: return endpoint.call_method(method, args, from_ip) else: raise MethodNotFound(method)
def test03_encode_decode_marshal(self): """ test with pickle serializer """ parser = Parser("marshal") pack = parser.encode("hello") self.assertEqual(parser.decode(pack), "hello")
def test01_encode_decode_json(self): """ test with json serializer """ parser = Parser("json") pack = parser.encode("hello") self.assertEqual(parser.decode(pack), "hello")
def test00_assign_invalid_parser_type(self): "try to assign invalid serializer type" parser = Parser() self.assertRaises(TypeError, parser._set_backend, "toto")