コード例 #1
0
ファイル: channel.py プロジェクト: zwxyswd/gprotobuf
 def _connect(self, ):
     """
     """
     assert self._stream == None or self._stream.closed()
     if isinstance(self._address, str):
         s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     else:
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.settimeout(10)
     s.connect(self._address)
     self._stream = IOStream(s)
     self._stream.read_bytes(struct.calcsize("!I"), self._get_buffer_size)
コード例 #2
0
ファイル: channel.py プロジェクト: zwxyswd/gprotobuf
 def _connect(self,):
     """
     """
     assert self._stream == None or self._stream.closed()
     if isinstance(self._address, str):
         s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     else:
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.settimeout(10)
     s.connect(self._address)
     self._stream = IOStream(s)
     self._stream.read_bytes(struct.calcsize("!I"), self._get_buffer_size)
コード例 #3
0
ファイル: factory.py プロジェクト: zwxyswd/gprotobuf
 def _handler(self, connection, address):
     """
     
     Arguments:
     - `self`:
     - `connection`:
     - `address`:
     """
     try:
         stream = IOStream(connection)
         Handler(stream, address, self._services)
     except Exception, err:
         self._logger.error('Error in connection callback: {0}'.format(err))
コード例 #4
0
ファイル: transport.py プロジェクト: JustinTulloss/harelib
    def __init__(self, host, connect_timeout):
        if ":" in host:
            host, port = host.split(":", 1)
            port = int(port)
        else:
            port = AMQP_PORT

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.settimeout(connect_timeout)

        try:
            self.sock.connect((host, port))
        except self.socket.error:
            self.sock.close()
            raise
        self.sock.settimeout(None)

        self._setup_transport()

        self.stream = IOStream(self.sock)

        self._write(AMQP_PROTOCOL_HEADER)
コード例 #5
0
ファイル: channel.py プロジェクト: zwxyswd/gprotobuf
class Channel(BaseChannel):
    """
    """
    
    def __init__(self, ):
        """
        """
        super(BaseChannel, self).__init__()
        self._stream = None
        self._pendings = {}
        self._logger = logging.getLogger('gprotobuf.Channel')

    def connectTCP(self, host, port):
        """
        
        Arguments:
        - `self`:
        - `host`:
        - `port`:
        """
        self._address = (host, port)
        self._connect()
        self._logger.info('Connected to {0}:{1}'.format(host, port))

    def connectUnix(self, path):
        """
        
        Arguments:
        - `self`:
        - `path`:
        """
        self._address = path
        self._connect()
        self._logger.info('Connected to {0}'.format(path))

    def _connect(self, ):
        """
        """
        assert self._stream == None or self._stream.closed()
        if isinstance(self._address, str):
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        else:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(10)
        s.connect(self._address)
        self._stream = IOStream(s)
        self._stream.read_bytes(struct.calcsize("!I"), self._get_buffer_size)

    def close(self):
        ''' '''
        self._stream and self._stream.close()

    def isClosed(self):
        ''' '''
        return self._stream and self._stream.closed()
    
    def reconnect(self):
        """
        
        Arguments:
        - `self`:
        """
        assert self._address != None
        self._connect()

    def _write_request(self, buffer):
        """
        
        Arguments:
        - `self`:
        - `buffer`:
        """
        bufferLen = struct.pack("!I", len(buffer))
        buffer = bufferLen + buffer
        self._stream.write(buffer, self._after_write)

    def _after_write(self):
        """
        
        Arguments:
        - `self`:
        """
        self._logger.debug('Send RPC request finished')
        
    def _get_buffer_size(self, buffer):
        """
        
        Arguments:
        - `self`:
        - `buffer`:
        """
        bufferLen = int(struct.unpack("!I", buffer)[0])
        self._stream.read_bytes(bufferLen, self._after_read_response)

    def _after_read_response(self, data):
        """
        
        Arguments:
        - `self`:
        - `data`:
        """
        try:
            self._logger.debug('Receive RPC response finished')
            response = self.deserialize_response(data)
            uuid = response.uuid
            if uuid in self._pendings:
                if response.type == RESPONSE_OK:
                    resultClass = self._pendings[uuid][1]
                    #controller = self._pendings[uuid][2]
                    result = self.deserialize_result(response, resultClass)
                    self._pendings[uuid][0](result)
                elif response.type == RESPONSE_ERROR:
                    self._logger.error(response.error)
                del self._pendings[uuid]
        except:
            pass
        self._logger.debug('RPC invoke finished')
        if not self._stream.closed():
            self._stream.read_bytes(struct.calcsize("!I"), self._get_buffer_size)

    def CallMethod(self, methodDescriptor, controller, parameters, resultClass, done):
        ''' '''
        self._logger.debug('Start RPC invoke')
        (uuid, request) = self.serialize_request(methodDescriptor, parameters)
        try:
            self._write_request(request)
            self._pendings[uuid] = (done, resultClass, controller)
        except IOError, err:
            controller.SetFailed(str(err))
            self._logger.error('{0}'.format(err))
コード例 #6
0
ファイル: transport.py プロジェクト: JustinTulloss/harelib
class _AbstractTransport(object):
    """
    Common superclass for TCP and SSL transports

    """

    def __init__(self, host, connect_timeout):
        if ":" in host:
            host, port = host.split(":", 1)
            port = int(port)
        else:
            port = AMQP_PORT

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.settimeout(connect_timeout)

        try:
            self.sock.connect((host, port))
        except self.socket.error:
            self.sock.close()
            raise
        self.sock.settimeout(None)

        self._setup_transport()

        self.stream = IOStream(self.sock)

        self._write(AMQP_PROTOCOL_HEADER)

    def __del__(self):
        self.close()

    def _read(self, n, callback=None):
        """
        Read exactly n bytes from the peer, call back when you're done.

        """

        def cb(data):
            print "received %d bytes" % len(data)
            if callable(callback):
                callback(data)

        self.stream.read_bytes(n, cb)

    def _setup_transport(self):
        """
        Do any additional initialization of the class (used
        by the subclasses).

        """
        pass

    def _write(self, s, callback=None):
        """
        Completely write a string to the peer, call the callback on completion.

        """

        def cb():
            if callable(callback):
                callback()

        self.stream.write(s, cb)

    def close(self):
        if self.stream is not None:
            self.stream.close()
        if self.sock is not None:
            self.sock.close()

    def read_frame(self, callback=None):
        """
        Read an AMQP frame, call the callback when you're done.

        """

        """
        Python can be deeply twisted, so in async code, just keep in mind that
        the least indented code is what happens first. Just because it's written
        further down the page doesn't mean it happens later.
        """

        def received_data(data):
            frame_type, channel, size = unpack(">BHI", data)

            def received_payload(payload):
                def received_ch(ch):
                    if ch == "\xce":
                        print "successfully read shit, calling back"
                        callback(frame_type, channel, payload)
                    else:
                        raise Exception("Framing Error, received 0x%02x while expecting 0xce" % ord(ch))

                self._read(1, received_ch)

            self._read(size, received_payload)

        self._read(7, received_data)

    def write_frame(self, frame_type, channel, payload, callback=None):
        """
        Write out an AMQP frame, call the callback when you're done.

        """
        size = len(payload)
        self._write(pack(">BHI%dsB" % size, frame_type, channel, size, payload, 0xCE), callback)
コード例 #7
0
ファイル: tcpserver.py プロジェクト: pmars/tomato
 def _handle_connection(self, connection, address):
     stream = IOStream(connection, self._io_loop)
     self.handle_stream(stream, address)
コード例 #8
0
ファイル: channel.py プロジェクト: zwxyswd/gprotobuf
class Channel(BaseChannel):
    """
    """

    def __init__(self,):
        """
        """
        super(BaseChannel, self).__init__()
        self._stream = None
        self._pendings = {}
        self._logger = logging.getLogger("gprotobuf.Channel")

    def connectTCP(self, host, port):
        """
        
        Arguments:
        - `self`:
        - `host`:
        - `port`:
        """
        self._address = (host, port)
        self._connect()
        self._logger.info("Connected to {0}:{1}".format(host, port))

    def connectUnix(self, path):
        """
        
        Arguments:
        - `self`:
        - `path`:
        """
        self._address = path
        self._connect()
        self._logger.info("Connected to {0}".format(path))

    def _connect(self,):
        """
        """
        assert self._stream == None or self._stream.closed()
        if isinstance(self._address, str):
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        else:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(10)
        s.connect(self._address)
        self._stream = IOStream(s)
        self._stream.read_bytes(struct.calcsize("!I"), self._get_buffer_size)

    def close(self):
        """ """
        self._stream and self._stream.close()

    def isClosed(self):
        """ """
        return self._stream and self._stream.closed()

    def reconnect(self):
        """
        
        Arguments:
        - `self`:
        """
        assert self._address != None
        self._connect()

    def _write_request(self, buffer):
        """
        
        Arguments:
        - `self`:
        - `buffer`:
        """
        bufferLen = struct.pack("!I", len(buffer))
        buffer = bufferLen + buffer
        self._stream.write(buffer, self._after_write)

    def _after_write(self):
        """
        
        Arguments:
        - `self`:
        """
        self._logger.debug("Send RPC request finished")

    def _get_buffer_size(self, buffer):
        """
        
        Arguments:
        - `self`:
        - `buffer`:
        """
        bufferLen = int(struct.unpack("!I", buffer)[0])
        self._stream.read_bytes(bufferLen, self._after_read_response)

    def _after_read_response(self, data):
        """
        
        Arguments:
        - `self`:
        - `data`:
        """
        try:
            self._logger.debug("Receive RPC response finished")
            response = self.deserialize_response(data)
            uuid = response.uuid
            if uuid in self._pendings:
                if response.type == RESPONSE_OK:
                    resultClass = self._pendings[uuid][1]
                    # controller = self._pendings[uuid][2]
                    result = self.deserialize_result(response, resultClass)
                    self._pendings[uuid][0](result)
                elif response.type == RESPONSE_ERROR:
                    self._logger.error(response.error)
                del self._pendings[uuid]
        except:
            pass
        self._logger.debug("RPC invoke finished")
        if not self._stream.closed():
            self._stream.read_bytes(struct.calcsize("!I"), self._get_buffer_size)

    def CallMethod(self, methodDescriptor, controller, parameters, resultClass, done):
        """ """
        self._logger.debug("Start RPC invoke")
        (uuid, request) = self.serialize_request(methodDescriptor, parameters)
        try:
            self._write_request(request)
            self._pendings[uuid] = (done, resultClass, controller)
        except IOError, err:
            controller.SetFailed(str(err))
            self._logger.error("{0}".format(err))