Ejemplo n.º 1
0
 def __init__(self, transport):
     self.reactor = transport.reactor
     self.transport = PacketTransport(transport)
     self.seq = itertools.count()
     self.replies = {}
     self.reactor.call(self._process_incoming)
     self.api = RemotingNamespace(weakref.proxy(self))
Ejemplo n.º 2
0
class RemotingClient(object):
    def __init__(self, transport):
        self.reactor = transport.reactor
        self.transport = PacketTransport(transport)
        self.seq = itertools.count()
        self.replies = {}
        self.reactor.call(self._process_incoming)
        self.api = RemotingNamespace(weakref.proxy(self))

    @classmethod
    @reactive
    def connect(cls, reactor, host, port):
        trns = yield reactor.net.connect_tcp(host, port)
        rreturn(cls(trns))

    @reactive
    def close(self):
        yield self.transport.close()
    
    @reactive
    def _process_incoming(self):
        while True:
            try:
                data = yield self.transport.recv()
            except (EOFError, TransportClosed):
                break
            try:
                seq, isexc, obj = self.unpack(data)
            except (TypeError, ValueError) as ex:
                print "PROTOCOL ERROR", ex
                yield self.close()
                break
            if seq not in self.replies:
                print "INVALID SEQ", seq
                yield self.close()
                break
            
            dfr = self.replies.pop(seq)
            if isexc:
                dfr.throw(obj)
            else:
                dfr.set(obj)
    
    @reactive
    def call(self, funcname, *args, **kwargs):
        dfr = yield self._call(funcname, args, kwargs)
        obj = yield dfr
        rreturn(obj)

    @reactive
    def _call(self, funcname, args, kwargs):
        seq = self.seq.next()
        data = self.pack((seq, funcname, args, kwargs))
        yield self.transport.send(data)
        dfr = ReactorDeferred(self.reactor)
        self.replies[seq] = dfr
        rreturn(dfr)

    def unpack(self, data):
        return pickle.loads(data)
    def pack(self, obj):
        return pickle.dumps(obj)
Ejemplo n.º 3
0
 def __init__(self, service_factory, transport, owner = None):
     BaseHandler.__init__(self, PacketTransport(transport), owner)
     self.service = service_factory(self.reactor)