Ejemplo n.º 1
0
 def test_push(self):
     p1 = self._make_packet_with_seqnum(1)
     p2 = self._make_packet_with_seqnum(2)
     h = heap.Heap()
     self.assertEqual(len(h), 0)
     h.push(p1)
     self.assertEqual(len(h), 1)
     self.assertIn(p1.sequence_number, h)
     self.assertNotIn(p2.sequence_number, h)
Ejemplo n.º 2
0
    def __init__(self, proto, handler, own_addr, dest_addr, relay_addr=None):
        """
        Create a new connection and register it with the protocol.
        Args:
            proto: Handler to underlying protocol.
            handler: Upstream recipient of received messages and
                handler of other events. Should minimally implement
                `receive_message` and `handle_shutdown`.
            own_addr: Tuple of local host address (ip, port).
            dest_addr: Tuple of remote host address (ip, port).
            relay_addr: Tuple of relay host address (ip, port).
        If a relay address is specified, all outgoing packets are
        sent to that adddress, but the packets contain the address
        of their final destination. This is used for routing.
        """
        self.own_addr = self._Address(*own_addr)
        self.dest_addr = self._Address(*dest_addr)
        if relay_addr is None:
            self.relay_addr = dest_addr
        else:
            self.relay_addr = self._Address(*relay_addr)

        self.handler = handler

        self._proto = proto
        self._state = State.CONNECTING

        self._next_sequence_number = random.randrange(2**16 - 2)
        self._next_expected_seqnum = 0
        self._next_delivered_seqnum = 0

        self._segment_queue = collections.deque()
        self._sending_window = collections.OrderedDict()

        self._receive_heap = heap.Heap()

        self._looping_send = task.LoopingCall(self._dequeue_outbound_message)
        self._looping_receive = task.LoopingCall(self._pop_received_packet)

        # Initiate SYN sequence after receiving any pending SYN message.
        REACTOR.callLater(0, self._send_syn)

        # Setup and immediately cancel the ACK loop; it should only
        # be activated once the connection is in CONNECTED state.
        # However, initializing here helps avoiding `is None` checks.
        self._ack_handle = REACTOR.callLater(1, self._send_ack)
        self._ack_handle.cancel()
Ejemplo n.º 3
0
    def test_pop_all_fragments_with_fragments_missing(self):
        p1 = self._make_packet_with_seqnum(1)
        p2 = self._make_packet_with_seqnum(2)
        p4 = self._make_packet_with_seqnum(4)
        p1.more_fragments = 3
        p2.more_fragments = 2
        p4.more_fragments = 0

        h = heap.Heap()
        h.push(p2)
        h.push(p4)
        h.push(p1)

        self.assertIsNone(h.pop_min_and_all_fragments())

        self.assertEqual(len(h), 3)
        self.assertIn(1, h)
        self.assertIn(2, h)
        self.assertIn(4, h)
        self.assertNotIn(3, h)
Ejemplo n.º 4
0
    def test_pop_all_fragments_with_all_fragments_available(self):
        p1 = self._make_packet_with_seqnum(1)
        p2 = self._make_packet_with_seqnum(2)
        p3 = self._make_packet_with_seqnum(3)
        p4 = self._make_packet_with_seqnum(4)
        p1.more_fragments = 2
        p2.more_fragments = 1
        p3.more_fragments = 0

        h = heap.Heap()
        h.push(p2)
        h.push(p3)
        h.push(p4)
        h.push(p1)

        packet_list = h.pop_min_and_all_fragments()
        self.assertEqual(packet_list, (p1, p2, p3))

        self.assertEqual(len(h), 1)
        self.assertNotIn(1, h)
        self.assertNotIn(2, h)
        self.assertNotIn(3, h)
        self.assertIn(4, h)
Ejemplo n.º 5
0
 def test_init(self):
     h = heap.Heap()
     self.assertEqual(len(h), 0)
Ejemplo n.º 6
0
 def test_pop_all_fragments_from_empty_heap(self):
     h = heap.Heap()
     self.assertIsNone(h.pop_min_and_all_fragments())