예제 #1
0
    def _send_method(self, channel_number, method, content=None):
        """
        Constructs a RPC method frame and then sends it to the broker.
        """
        self._send_frame(pika.frame.Method(channel_number, method))

        if isinstance(content, tuple):
            props = content[0]
            body = content[1]
        else:
            props = None
            body = content

        if props:
            length = 0
            if body:
                length = len(body)
            self._send_frame(pika.frame.Header(channel_number, length, props))

        if body:
            max_piece = (self.parameters.frame_max - \
                         spec.FRAME_HEADER_SIZE - \
                         spec.FRAME_END_SIZE)
            body_buf = simplebuffer.SimpleBuffer(body)
            frames_sent = 0
            frames = len(body) / spec.FRAME_MAX_SIZE
            while body_buf:
                piece_len = min(len(body_buf), max_piece)
                piece = body_buf.read_and_consume(piece_len)
                self._send_frame(pika.frame.Body(channel_number, piece))
                frames_sent += 1
예제 #2
0
    def _init_connection_state(self):
        """
        Initialize or reset all of our internal state variables for a given
        connection. If we disconnect and reconnect, all of our state needs to
        be wiped.
        """
        # Outbound buffer for buffering writes until we're able to send them
        self.outbound_buffer = simplebuffer.SimpleBuffer()

        # Inbound buffer for decoding frames
        self._frame_buffer = ''

        # Connection state, server properties and channels all change on
        # each connection
        self.server_properties = None
        self._channels = dict()

        # Data used for Heartbeat checking and back-pressure detection
        self.bytes_sent = 0
        self.bytes_received = 0
        self.frames_sent = 0
        self.frames_received = 0
        self.heartbeat = None

        # Default back-pressure multiplier value
        self._backpressure = 10

        # Connection state
        self.connection_state = CONNECTION_CLOSED

        # When closing, hold reason why
        self.closing = 0, None

        # Our starting point once connected, first frame received
        self.callbacks.add(0, spec.Connection.Start, self._on_connection_start)
예제 #3
0
 def test_send_to_socket_offset_reset(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     with mock.patch('socket.socket') as mock_socket:
         mock_socket.send = mock.Mock(return_value=10)
         obj.offset = 1000000
         with mock.patch.object(obj, 'consume') as consume:
             obj.send_to_socket(mock_socket)
             consume.assert_called_once_with(0)
예제 #4
0
파일: connection.py 프로젝트: smglab/pika
    def _send_method(self, channel_number, method_frame, content=None):
        """Constructs a RPC method frame and then sends it to the broker.

        :param int channel_number: The channel number for the frame
        :param pika.object.Method method_frame: The method frame to send
        :param tuple content: If set, is a content frame, is tuple of
                              properties and body.

        """
        LOGGER.debug('Sending on channel %i: %r', channel_number, method_frame)
        self._send_frame(frame.Method(channel_number, method_frame))
        if isinstance(content, tuple):
            self._send_frame(frame.Header(channel_number,
                                          len(content[1]),
                                           content[0]))
            if content[1]:
                body_buf = simplebuffer.SimpleBuffer(content[1])
                while body_buf:
                    piece_len = min(len(body_buf), self._body_max_length)
                    piece = body_buf.read_and_consume(piece_len)
                    self._send_frame(frame.Body(channel_number, piece))
예제 #5
0
 def test_simple_buffer_read_none(self):
     obj = simplebuffer.SimpleBuffer('Foo Bar')
     self.assertEqual(obj.read(), 'Foo Bar')
예제 #6
0
 def test_simple_buffer_write_empty_size(self):
     obj = simplebuffer.SimpleBuffer('Foo Bar')
     obj.write('')
     self.assertEqual(obj.size, 7)
예제 #7
0
 def test_simple_buffer_write_empty(self):
     obj = simplebuffer.SimpleBuffer('Foo Bar')
     obj.write('')
     obj.buf.seek(0)
     self.assertEqual(obj.buf.read(), 'Foo Bar')
예제 #8
0
 def test_read_and_consume_offset(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     obj.read_and_consume(5)
     self.assertEqual(obj.offset, 5)
예제 #9
0
 def test_simple_buffer_consume_cleanup(self):
     obj = simplebuffer.SimpleBuffer()
     obj.offset = 65538
     obj.size = 0
     obj.consume(0)
     self.assertEqual(obj.offset, 0)
예제 #10
0
 def test_simple_buffer_consume(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     obj.consume(5)
     self.assertEqual(obj.size, 5)
예제 #11
0
 def test_len(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     self.assertEqual(len(obj), 10)
예제 #12
0
 def test_nonzero(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     self.assertFalse(not obj)
예제 #13
0
 def test_flush_offset(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     obj.flush()
     self.assertEqual(obj.offset, 10)
예제 #14
0
 def test_flush(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     obj.flush()
     self.assertEqual(obj.size, 0)
예제 #15
0
 def test_send_to_socket_offset(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     with mock.patch('socket.socket') as mock_socket:
         mock_socket.send = mock.Mock(return_value=5)
         obj.send_to_socket(mock_socket)
         self.assertEqual(obj.offset, 5)
예제 #16
0
 def test_simple_buffer_read_no_bytes(self):
     obj = simplebuffer.SimpleBuffer()
     self.assertEqual(obj.read(0), '')
예제 #17
0
 def test_simple_buffer_read_offset_sized(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     obj.offset = 5
     self.assertEqual(obj.read(3), '567')
예제 #18
0
 def test_str(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     self.assertEqual(str(obj),
                      "<SimpleBuffer of 10 bytes, 10 total size, "
                      "'0123456789'>")
예제 #19
0
 def test_simple_buffer_consume_read(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     obj.consume(5)
     self.assertEqual(obj.read(5), '56789')
예제 #20
0
 def test_simple_buffer_init_buf_is_stringio(self):
     obj = simplebuffer.SimpleBuffer()
     self.assertIsInstance(obj.buf, cStringIO.OutputType)
예제 #21
0
 def test_read_and_consume_content(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     self.assertEqual(obj.read_and_consume(5), '01234')
예제 #22
0
 def test_simple_buffer_init_buf_position(self):
     # Position should be set to the end
     obj = simplebuffer.SimpleBuffer('Foo Bar')
     self.assertEqual(obj.buf.tell(), 7)
예제 #23
0
 def test_send_to_socket_content(self):
     obj = simplebuffer.SimpleBuffer('0123456789')
     with mock.patch('socket.socket') as mock_socket:
         obj.send_to_socket(mock_socket)
         mock_socket.send.called_once_with('0123456789')
예제 #24
0
 def test_simple_buffer_write(self):
     # Position should be set to the end
     obj = simplebuffer.SimpleBuffer('Foo Bar')
     obj.write(' Baz')
     obj.buf.seek(0)
     self.assertEqual(obj.buf.read(), 'Foo Bar Baz')