Beispiel #1
0
    def stringReceived(self, frame):
        tmi = TTransport.TMemoryBuffer(frame)
        tmo = TTransport.TMemoryBuffer()

        iprot = self.factory.iprot_factory.getProtocol(tmi)
        oprot = self.factory.oprot_factory.getProtocol(tmo)

        server_ctx = TwistedRpcConnectionContext(self.transport.socket)
        d = self.factory.processor.process(iprot, oprot, server_ctx)
        d.addCallbacks(self.processOk, self.processError,
            callbackArgs=(tmo,))
Beispiel #2
0
    def handle(self):
        """Handle requests.

        WARNING! You must call prepare BEFORE calling handle.
        """
        assert self.prepared, "You have to call prepare before handle"
        rset, wset, xset = self._select()
        for readable in rset:
            if readable == self._read.fileno():
                # don't care i just need to clean readable flag
                self._read.recv(1024)
            elif readable in self.socket.handles:
                client_socket = self.socket.accept()
                connection = Connection(client_socket, self)
                self.clients[client_socket.fileno()] = connection
            else:
                connection = self.clients[readable]
                connection.read()
                if connection.status == WAIT_PROCESS:
                    itransport = TTransport.TMemoryBuffer(connection.message)

                    # Header protocol needs oprot == iprot. This implies the
                    # input memory buffer is reused for output too.
                    if isinstance(self.in_protocol, THeaderProtocolFactory):
                        omembuf = itransport
                        iprot = self.in_protocol.getProtocol(itransport)
                        oprot = iprot
                    else:
                        # Otherwise, assume we need a TFramedTransport.
                        omembuf = TTransport.TMemoryBuffer()
                        itransport = TTransport.TFramedTransport(itransport)
                        otransport = TTransport.TFramedTransport(omembuf)
                        iprot = self.in_protocol.getProtocol(itransport)
                        oprot = self.out_protocol.getProtocol(otransport)

                    if self.max_queue_size == 0 or \
                            self.tasks.qsize() <= self.max_queue_size:
                        self.tasks.put([
                            self.processor, iprot, oprot, omembuf, connection
                        ])
                    else:
                        logging.error(
                            "Queue max size of %d exceeded. Request rejected.",
                            self.max_queue_size)
        for writeable in wset:
            self.clients[writeable].write()
        for oob in xset:
            if oob in self.clients:
                connection = self.clients[oob]
                connection.close()
Beispiel #3
0
def serialize(protocol_factory, thr):
    # type: (Any, Any) -> AnyStr
    """Convenience method for serializing objects using the given
    protocol factory and a TMemoryBuffer."""
    transport = TTransport.TMemoryBuffer()
    protocol = protocol_factory.getProtocol(transport)
    thr.write(protocol)
    if isinstance(protocol, THeaderProtocol.THeaderProtocol):
        protocol.trans.flush()
    return transport.getvalue()
Beispiel #4
0
    def stringReceived(self, frame):
        tr = TTransport.TMemoryBuffer(frame)
        iprot = self._iprot_factory.getProtocol(tr)
        (fname, mtype, rseqid) = iprot.readMessageBegin()

        try:
            method = self.recv_map[fname]
        except KeyError:
            method = getattr(self.client, 'recv_' + fname)
            self.recv_map[fname] = method

        method(iprot, mtype, rseqid)
Beispiel #5
0
def deserialize(protocol_factory, data, thr_out):
    # type: (Any, AnyStr, T) -> T
    """Convenience method for deserializing objects using the given
    protocol factory and a TMemoryBuffer.  returns its thr_out
    argument."""
    transport = TTransport.TMemoryBuffer(data)
    try:
        protocol = protocol_factory.getProtocol(transport, thr_out.thrift_spec)  # noqa: T484
    except TypeError:
        protocol = protocol_factory.getProtocol(transport)
    if isinstance(protocol, THeaderProtocol.THeaderProtocol):
        # this reads the THeader headers to detect what the underlying
        # protocol is, as well as looking at transforms, etc.
        protocol.trans.readFrame(0)
        protocol.reset_protocol()
    thr_out.read(protocol)  # noqa: T484
    return thr_out
Beispiel #6
0
def __thrift_to_json(x):
    trans = TTransport.TMemoryBuffer()
    proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans)
    x.write(proto)
    return json.loads(trans.getvalue())