Exemplo n.º 1
0
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)
Exemplo n.º 2
0
Arquivo: parse.py Projeto: inkhey/mmc
    def test03_encode_decode_marshal(self):
        """ test with pickle serializer """

        parser = Parser("marshal")
        pack = parser.encode("hello")
        self.assertEqual(parser.decode(pack), "hello")
Exemplo n.º 3
0
Arquivo: parse.py Projeto: inkhey/mmc
 def test01_encode_decode_json(self):
     """ test with json serializer """
     parser = Parser("json")
     pack = parser.encode("hello")
     self.assertEqual(parser.decode(pack), "hello")
Exemplo n.º 4
0
    def test03_encode_decode_marshal(self):
        """ test with pickle serializer """

        parser = Parser("marshal")
        pack = parser.encode("hello")
        self.assertEqual(parser.decode(pack), "hello")
Exemplo n.º 5
0
 def test01_encode_decode_json(self):
     """ test with json serializer """
     parser = Parser("json")
     pack = parser.encode("hello")
     self.assertEqual(parser.decode(pack), "hello")
Exemplo n.º 6
0
    def test00_assign_invalid_parser_type(self):
        "try to assign invalid serializer type"

        parser = Parser()
        self.assertRaises(TypeError, parser._set_backend, "toto")