def send_one_packet(self, sequence): if sequence >= len(self.send_buffer): return if sequence + self.mss > len(self.send_buffer): body = self.send_buffer[sequence : ] else: body = self.send_buffer[sequence : sequence + self.mss] # get one packet worth of data packet = TCPPacket(source_address=self.source_address, source_port=self.source_port, destination_address=self.destination_address, destination_port=self.destination_port, body=body, sequence=sequence,ack_number=self.ack, flow_id=int(self.file_prefix.split("_")[-1])) # send the packet Sim.trace("%d sending My_RTP segment to %d for %d" % (self.source_address,self.destination_address,packet.sequence)) self.transport.send_packet(packet) Sim.print_rate_info(packet) # set a timer if not self.timer_set: self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit) self.timer_set = True return packet
def handle_packet(self,packet): # handle ACK if packet.length == 0: # this acks new data, so advance the window with slide_window() Sim.trace("%d received My_RTP ACK from %d for %d" % (packet.destination_address,packet.source_address,packet.ack_number)) Sim.print_rate_info(packet) self.slide_window(packet.ack_number) # handle data if packet.length > 0: self.pkts_rcvd += 1 Sim.trace("%d received My_RTP segment from %d for %d" % (packet.destination_address,packet.source_address,packet.sequence)) # if the packet is the one we're expecting increment our # ack number and add the data to the receive buffer if packet.sequence >= self.ack: self.receive_buffer.append(packet) if packet.sequence == self.ack: self.receive_buffer = sorted(self.receive_buffer, key=lambda TCPPacket: TCPPacket.sequence) while self.receive_buffer and (self.ack == self.receive_buffer[0].sequence): pkt = self.receive_buffer.pop(0) self.increment_ack(pkt.sequence + pkt.length) # deliver data that is in order self.app.handle_packet(pkt) # always send an ACK self.send_ack() if packet.queueing_delay > self.pkt_q_delay_threshold: self.queueing_delay += packet.queueing_delay print "\n[Average Queuing Delay so far:", str(self.queueing_delay / self.pkts_rcvd) + "]" print "\n[Total Queueing Delay so far:", str(self.queueing_delay) + "]\n"