def handle_packet(self,packet): # handle ACK (Sender) # if the ACK number is greater than our send base... # update the acknowledged segment counter, restart timer and send if possible if packet.ack_number > self.send_base: Sim.trace("ACK RCVD: %d <=== %d : %d" % (packet.destination_address,packet.source_address,packet.ack_number)) bytes_acked = packet.ack_number - self.send_base self.not_yet_acknowledged_segments -= bytes_acked if self.cwnd >= self.ssthreshold: self.cwnd += (self.mss * bytes_acked) / self.cwnd else: self.cwnd += bytes_acked self.cancel_timer() self.timer = Sim.scheduler.add(delay=self.timeout, event='new_ack_data', handler=self.retransmit) self.send_if_possible() self.send_base = packet.ack_number # print congestion window if self.source_port > 3: Sim.trace_custom("%f" % (self.cwnd/self.mss), 'cwnd', self.source_port) # handle data (Receiver) if packet.length > 0: Sim.trace("SEGMENT RCVD: %d <=== %d : %d" % (packet.destination_address,packet.source_address,packet.sequence)) self.receive_temp_buffer.add(packet.sequence) self.receive_packet_buffer.append(packet) self.send_cumulative_ack()
def handle_packet(self,packet): # handle ACK if packet.ack_number > self.window_start and packet.ack_number <= self.sequence: # 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)) 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"
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) # 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) # 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_ack(self, packet): Sim.trace("%d received ReliableTransport ACK from %d for %d" % (packet.destination_address,packet.source_address,packet.ack_number)) self.unacked_packet_count -= ((packet.ack_number - self.received_ack_number) / self.mss) self.cancel_timer() self.timer = Sim.scheduler.add(delay=self.timeout, event='new_ack_data', handler=self.retransmit) self.received_ack_number = packet.ack_number self.send_if_possible()
def retransmit(self,event): if self.send_base >= len(self.send_buffer): return Sim.trace("%d's retransmission timer fired" % (self.source_address)) self.loss_detected() packet = self.send_one_packet(self.send_base) self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit)
def receive_data(self,data): global original_size global received_size Sim.trace('AppHandler',"application got %d bytes" % (len(data))) self.f.write(data) received_size += len(data) self.f.flush()
def send_ack(self): packet = TCPPacket(source_address=self.source_address, source_port=self.source_port, destination_address=self.destination_address, destination_port=self.destination_port, sequence=self.sequence,ack_number=self.ack, flow_id=int(self.file_prefix.split("_")[-1])) # send the packet Sim.trace("%d sending My_RTP ACK to %d for %d" % (self.source_address,self.destination_address,packet.ack_number)) self.transport.send_packet(packet)
def send_ack(self): packet = TCPPacket(source_address=self.source_address, source_port=self.source_port, destination_address=self.destination_address, destination_port=self.destination_port, sequence=self.sequence,ack_number=self.ack) # send the packet Sim.trace("%d sending ReliableTransport ACK to %d for %d" % (self.source_address,self.destination_address,packet.ack_number)) self.transport.send_packet(packet)
def send_ack(self): packet = TCPPacket(source_address=self.source_address, source_port=self.source_port, destination_address=self.destination_address, destination_port=self.destination_port, sequence=self.next_sequence_num,ack_number=self.ack) # since we are sending ACK, we're not waiting to send cumulative ACK anymore self.next_inorder_seg_pending = False # send the packet Sim.trace("SEND ACK: %d ===> %d : %d" % (self.source_address,self.destination_address,packet.ack_number)) self.transport.send_packet(packet)
def run(self, janela, perda, fila, fast): self.filename = fila loss = perda Sim.trace('Trans', "It is %s that I am using fast transmit." % (fast)) # parameters Sim.scheduler.reset() Sim.set_debug('AppHandler') Sim.set_debug('TCP') Sim.set_debug('Trans') # setup network net = Network('./network.txt') net.loss(loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') net.set_queue(n1.links[0], "100") net.set_queue(n2.links[0], "100") n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, janela, fast) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, janela, fast) # send a file with open(self.filename, 'rb') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # run the simulation Sim.scheduler.run() self.diff() self.filename = None return Sim.scheduler.current_time()
def handle_packet(self,packet): # handle ACK (Sender) # if the ACK number is greater than our send base... # update the acknowledged segment counter, restart timer and send if possible if packet.ack_number > self.send_base: Sim.trace("ACK RCVD: %d <=== %d : %d" % (packet.destination_address,packet.source_address,packet.ack_number)) self.not_yet_acknowledged_segments -= ((packet.ack_number - self.send_base) / self.mss) self.cancel_timer() self.timer = Sim.scheduler.add(delay=self.timeout, event='new_ack_data', handler=self.retransmit) self.send_base = packet.ack_number self.send_if_possible() else: self.triple_dup_ack_counter+=1 if self.triple_dup_ack_counter == 3: Sim.trace("FAST RETRANSMIT: Duplicate ACK: %d"%(packet.ack_number)) Sim.trace("Outstanding acks: %d" % self.not_yet_acknowledged_segments) self.send_one_packet(packet.ack_number) self.triple_dup_ack_counter = 0 # handle data (Receiver) if packet.length > 0: Sim.trace("SEGMENT RCVD: %d <=== %d : %d" % (packet.destination_address,packet.source_address,packet.sequence)) self.receive_temp_buffer.add(packet.sequence) self.receive_packet_buffer.append(packet) self.send_cumulative_ack()
def retransmit(self,event): self.timer_set = False if self.packets_outstanding <= 0: return self.ssthresh = int(max(math.ceil(self.window_size / 2.0), 1)) self.window_size = 1 self.bytes_acked = 0 self.packets_outstanding = 1 Sim.trace("%d retransmission timer fired" % (self.source_address)) packet = self.send_one_packet(self.window_start) if packet: self.sequence = self.window_start + packet.length self.window_file.write(str(Sim.scheduler.current_time()) + " " + str(self.window_size) + "\n")
def receive_data(self,data): if self.plot: global original_size global received_size Sim.trace('AppHandler',"application got %d bytes" % (len(data))) self.f.write(data) received_size[self.identifier] += len(data) self.f.flush() turn_timer_off = True for identifier in received_size.keys(): if received_size[identifier] != original_size: turn_timer_off = False break if turn_timer_off: global decisecondEvent Sim.scheduler.cancel(decisecondEvent)
def handle_sequence(self, packet): # print "We want sequence number:%d\nGot sequence number:%d\n" % (self.ack, packet.sequence) Sim.trace("%d received ReliableTransport segment from %d for %d" % (packet.destination_address,packet.source_address,packet.sequence)) ReliableTransport.stats.add(packet.queueing_delay) self.received_sequences.add(packet.sequence) self.receive_buffer.append(packet) # cumulative ack sequence_list = sorted(self.received_sequences) for i in range(self.ack/self.mss, len(sequence_list)): if sequence_list[i] == self.ack: tempPacket = [p for p in self.receive_buffer if p.sequence == self.ack][0] self.increment_ack(tempPacket.sequence + tempPacket.length) self.app.handle_packet(tempPacket) self.send_ack()
def handle_sequence(self, packet): print >> self.sequence_file, Sim.scheduler.current_time(), packet.ack_number, packet.length Sim.trace("%d received TcpTahoe segment from %d for %d" % (packet.destination_address,packet.source_address,packet.sequence)) self.received_sequences.add(packet.sequence) self.receive_buffer.append(packet) # cumulative ack sequence_list = sorted(self.received_sequences) for i in range(self.ack/self.mss, len(sequence_list)): if sequence_list[i] == self.ack: tempPacket = [p for p in self.receive_buffer if p.sequence == self.ack][0] self.increment_ack(tempPacket.sequence + tempPacket.length) print >> self.rate_file, Sim.scheduler.current_time(), self.ack, tempPacket.length self.app.handle_packet(tempPacket) self.send_ack()
def send_one_packet(self, sequence): # get one packet worth of data body = self.send_buffer[sequence:(sequence + self.mss)] 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) # send the packet Sim.trace("%d sending ReliableTransport segment to %d for %d" % (self.source_address,self.destination_address,packet.sequence)) self.transport.send_packet(packet) # set a timer if it's not already going if not self.timer: self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit) return packet
def handle_ack(self, packet): print >> self.ack_file, Sim.scheduler.current_time(), packet.ack_number, packet.length Sim.trace("%d received TcpTahoe ACK from %d for %d" % (packet.destination_address,packet.source_address,packet.ack_number)) new_bytes = packet.ack_number - self.received_ack_number self.unacked_packet_count -= new_bytes if self.cwnd >= self.threshold: self.cwnd += (self.mss*new_bytes)/self.cwnd self.threshold = self.cwnd else: self.cwnd += new_bytes self.cancel_timer() self.timer = Sim.scheduler.add(delay=self.timeout, event='new_ack_data', handler=self.retransmit) self.send_if_possible() self.received_ack_number = packet.ack_number
def send_one_packet(self,sequence): # get one packet worth of data body = self.send_buffer[sequence:(sequence + self.mss)] 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) # send the packet Sim.trace("SEND SEGMENT: %d ===> %d : %d at %d" % (self.source_address,self.destination_address,packet.sequence,packet.enter_queue)) self.transport.send_packet(packet) Sim.trace("SENT AT: %d" % packet.enter_queue) if not self.timer: self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit) return packet
def receive_data(self,data): if self.plot: global original_size global received_size Sim.trace('AppHandler',"application got %d bytes" % (len(data))) self.f.write(data) received_size[self.identifier] += len(data) self.f.flush() turn_timer_off = True for identifier in received_size.keys(): if received_size[identifier] != original_size: turn_timer_off = False break if turn_timer_off: global decisecondEvent Sim.scheduler.cancel(decisecondEvent) global previous_decisecond_stored_size bytes_per_second = (original_size - previous_decisecond_stored_size[self.identifier]) / Sim.scheduler.current_time() #print "Bytes per second: ", bytes_per_second self.add_plot_data(Sim.scheduler.current_time(),bytes_per_second,'ReceiverRate',self.identifier)
def loss_event(self): Sim.trace("Before loss event: cwnd = %d. Source port = %d" % ((self.cwnd/self.mss), self.source_port)) self.threshold = max(self.cwnd/2, self.mss) self.cwnd = self.mss Sim.trace("After loss event: cwnd = %d. Source port = %d" % ((self.cwnd/self.mss), self.source_port))
def retransmit(self,event): if self.received_ack_number < len(self.send_buffer): Sim.trace("%d retransmission timer fired" % (self.source_address)) packet = self.send_one_packet(self.received_ack_number) self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit)
def handle_packet(self,packet): Sim.trace("application got packet with %d bytes" % (packet.length)) self.f.write(packet.body) self.f.flush()
def receive_data(self, data): Sim.trace('AppHandler', "application got %d bytes" % (len(data))) self.f.write(data) self.f.flush()
def trace(self, message): """ Print debugging messages. """ Sim.trace("TCP", message)
def retransmit(self,event): self.timer_set = False if self.packets_outstanding <= 0: return Sim.trace("%d retransmission timer fired" % (self.source_address)) packet = self.send_one_packet(self.window_start)
def handle_packet(self,packet): Sim.trace("application got packet with %d bytes" % (packet.length)) self.f.write(packet.body) self.f.flush() self.total_queuing_delay += packet.queueing_delay self.total_packets_sent += 1
def receive_packet(self,packet): Sim.trace(str(self.node.hostname) + " packet ID: " + str(packet.ident))
def trace(self, message): ''' Print debugging messages. ''' Sim.trace("TCP", message)
def trace(self, message, protocol=""): Sim.trace("Node" + protocol, message + " (" + protocol + ")")
def trace(self,message): ''' Print debugging messages. ''' Sim.trace("TCP",message)
def receive_data(self,data): Sim.trace('AppHandler',"application got %d bytes" % (len(data))) self.f.write(data) self.f.flush()
def trace(self,message,protocol=""): Sim.trace("Node"+protocol,message + " ("+protocol+")")