Exemplo n.º 1
0
    def process_ack(self, ack, ack_bits):
        if not len(self.pending_ack_queue.queue):
            return

        should_remove = []
        for packet in self.pending_ack_queue.queue:
            acked = False
            if packet.sequence == ack:
                acked = True
            elif not sequence_more_recent(packet.sequence, ack,
                                          self.max_sequence):
                bit_index = ReliabilitySystem.bit_index_for_sequence(
                    packet.sequence, ack, self.max_sequence)
                if bit_index <= 31:
                    acked = (ack_bits >> bit_index) & 1

            if acked:
                self.rtt += (packet._time - self.rtt
                             ) * 0.1  # exponentially smoothed moving average

                self.acked_queue.insert_sorted(packet, self.max_sequence)
                self.acks.append(packet.sequence)
                self.acked_packets += 1
                should_remove.append(packet)

        for pack in should_remove:
            self.pending_ack_queue.queue.remove(pack)
Exemplo n.º 2
0
    def _generate_ack_bits(ack, received_queue, max_sequence):
        ack_bits = 0
        for packet in received_queue.queue:
            if packet.sequence == ack or sequence_more_recent(packet.sequence, ack, max_sequence):
                break
            bit_index = ReliabilitySystem.bit_index_for_sequence(packet.sequence, ack, max_sequence)
            if bit_index <= 31:
                ack_bits |= 1 << bit_index

        return ack_bits
Exemplo n.º 3
0
 def bit_index_for_sequence(sequence, ack, max_sequence):
     assert(sequence != ack)
     assert(not sequence_more_recent(sequence, ack, max_sequence))
     if sequence > ack:
         assert(ack < 33)
         assert(max_sequence >= sequence)
         return ack + (max_sequence - sequence)
     else:
         assert(ack >= 1)
         assert(sequence <= ack - 1)
         return ack - 1 - sequence
Exemplo n.º 4
0
 def packet_received(self, sequence, size):
     self.recv_packets += 1
     if self.received_queue.exists(sequence):
         return
     packet = PacketData()
     packet.sequence = sequence
     packet._time = 0
     packet.size = size
     self.received_queue.queue.append(packet)
     if sequence_more_recent(sequence, self.remote_sequence, self.max_sequence):
         self.remote_sequence = sequence
Exemplo n.º 5
0
 def bit_index_for_sequence(sequence, ack, max_sequence):
     assert (sequence != ack)
     assert (not sequence_more_recent(sequence, ack, max_sequence))
     if sequence > ack:
         assert (ack < 33)
         assert (max_sequence >= sequence)
         return ack + (max_sequence - sequence)
     else:
         assert (ack >= 1)
         assert (sequence <= ack - 1)
         return ack - 1 - sequence
Exemplo n.º 6
0
    def _generate_ack_bits(ack, received_queue, max_sequence):
        ack_bits = 0
        for packet in received_queue.queue:
            if packet.sequence == ack or sequence_more_recent(
                    packet.sequence, ack, max_sequence):
                break
            bit_index = ReliabilitySystem.bit_index_for_sequence(
                packet.sequence, ack, max_sequence)
            if bit_index <= 31:
                ack_bits |= 1 << bit_index

        return ack_bits
Exemplo n.º 7
0
 def packet_received(self, sequence, size):
     self.recv_packets += 1
     if self.received_queue.exists(sequence):
         return
     packet = PacketData()
     packet.sequence = sequence
     packet._time = 0
     packet.size = size
     self.received_queue.queue.append(packet)
     if sequence_more_recent(sequence, self.remote_sequence,
                             self.max_sequence):
         self.remote_sequence = sequence
Exemplo n.º 8
0
    def update_queues(self):
        epsilon = 0.001

        to_remove = []
        for packet in self.sent_queue.queue:
            if packet._time <= self.rtt_max + epsilon:
                break
            to_remove.append(packet)

        for packet in to_remove:
            self.sent_queue.queue.remove(packet)

        if len(self.received_queue.queue):
            latest_sequence = self.received_queue.queue[-1].sequence
            min_sequence = latest_sequence - 34 if latest_sequence >= 34 else self.max_sequence - (
                34 - latest_sequence)

            to_remove = []
            for i, packet in enumerate(self.received_queue.queue, 1):
                if sequence_more_recent(packet.sequence, min_sequence,
                                        self.max_sequence):
                    break
                to_remove.append(packet)

            for packet in to_remove:
                self.received_queue.queue.remove(packet)

        to_remove = []
        for packet in self.acked_queue.queue:
            if packet._time <= self.rtt_max * 2 - epsilon:
                to_remove.append(packet)

        for packet in to_remove:
            self.acked_queue.queue.remove(packet)

        to_remove = []
        for packet in self.pending_ack_queue.queue:
            if packet._time <= self.rtt_max + epsilon:
                to_remove.append(packet)

        for packet in to_remove:
            self.pending_ack_queue.queue.remove(packet)
            self.lost_packets += 1
Exemplo n.º 9
0
    def update_queues(self):
        epsilon = 0.001

        to_remove = []
        for packet in self.sent_queue.queue:
            if packet._time <= self.rtt_max + epsilon:
                break
            to_remove.append(packet)

        for packet in to_remove:
            self.sent_queue.queue.remove(packet)

        if len(self.received_queue.queue):
            latest_sequence = self.received_queue.queue[-1].sequence
            min_sequence = latest_sequence - 34 if latest_sequence >= 34 else self.max_sequence - (34 - latest_sequence)

            to_remove = []
            for i, packet in enumerate(self.received_queue.queue, 1):
                if sequence_more_recent(packet.sequence, min_sequence, self.max_sequence):
                    break
                to_remove.append(packet)

            for packet in to_remove:
                self.received_queue.queue.remove(packet)

        to_remove = []
        for packet in self.acked_queue.queue:
            if packet._time <= self.rtt_max * 2 - epsilon:
                to_remove.append(packet)

        for packet in to_remove:
            self.acked_queue.queue.remove(packet)

        to_remove = []
        for packet in self.pending_ack_queue.queue:
            if packet._time <= self.rtt_max + epsilon:
                to_remove.append(packet)

        for packet in to_remove:
            self.pending_ack_queue.queue.remove(packet)
            self.lost_packets += 1
Exemplo n.º 10
0
    def process_ack(self, ack, ack_bits):
        if not len(self.pending_ack_queue.queue):
            return

        should_remove = []
        for packet in self.pending_ack_queue.queue:
            acked = False
            if packet.sequence == ack:
                acked = True
            elif not sequence_more_recent(packet.sequence, ack, self.max_sequence):
                bit_index = ReliabilitySystem.bit_index_for_sequence(packet.sequence, ack, self.max_sequence)
                if bit_index <= 31:
                    acked = (ack_bits >> bit_index) & 1

            if acked:
                self.rtt += (packet._time - self.rtt) * 0.1 # exponentially smoothed moving average

                self.acked_queue.insert_sorted(packet, self.max_sequence)
                self.acks.append(packet.sequence)
                self.acked_packets += 1
                should_remove.append(packet)
        
        for pack in should_remove:
            self.pending_ack_queue.queue.remove(pack)