def receive_packet(self, packet): # handle ACK if self.packet_is_outstanding and packet.ack_number == self.sequence: # this acks new data, so advance the send buffer, reset # the outstanding flag, cancel the timer, and send if possible Sim.trace( "%d received StopAndWait ACK from %d for %d" % (packet.destination_address, packet.source_address, packet.ack_number) ) self.send_buffer = self.send_buffer[self.sequence - self.last_sent :] self.packet_is_outstanding = False self.cancel_timer() self.send_if_possible() # handle data if packet.length > 0: Sim.trace( "%d received StopAndWait 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.increment_ack(packet.sequence + packet.length) self.receive_buffer += packet.body # deliver data that is in order self.app.receive_packet(packet) # always send an ACK self.send_ack()
def forward_packet(self,packet): if packet.destination_address not in self.forwarding_table: Sim.trace("%d no routing entry for %d" % (self.links[0].address,packet.destination_address)) return link = self.forwarding_table[packet.destination_address] Sim.trace("%d forwarding packet to %d" % (link.address,packet.destination_address)) link.handle_packet(packet)
def trace(self,message): ''' Print debugging messages. ''' if self.destination_port != self.plot_port_number: return if not self.plot_sequence_on and not self.plot_rate_on and not self.plot_queue_on and not self.plot_window_on: Sim.trace("TCP",message)
def retransmit(self,event): if not self.send_buffer: return if not self.packet_is_outstanding: return Sim.trace("%d retransmission timer fired" % (self.source_address)) packet = self.send_one_packet(self.last_sent)
def forward_unicast_packet(self,packet): if packet.destination_address not in self.forwarding_table: Sim.trace("%s no routing entry for %d" % (self.hostname,packet.destination_address)) return link = self.forwarding_table[packet.destination_address] Sim.trace("%s forwarding packet to %d" % (self.hostname,packet.destination_address)) link.send_packet(packet)
def plot_sequence(self, sequence_number, isACK = False, dropped=False): if self.destination_port != self.plot_port_number: return message = "%i %i %d" % (sequence_number, dropped, isACK) if self.plot_sequence_on: Sim.trace("TCP", message)
def plot_window(self, size): if self.destination_port != self.plot_port_number: return message = "%i" % size if self.plot_window_on: Sim.trace("TCP", message)
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 StopAndWait ACK to %d for %d" % (self.source_address,self.destination_address,packet.ack_number)) self.transport.send_packet(packet)
def handle_packet(self,packet): # if this is the first time we have seen this packet, set its # creation timestamp if packet.created == None: packet.created = Sim.scheduler.current_time() # check if the packet is for me for link in self.links: if link.address == packet.destination_address: Sim.trace("%d received packet" % (packet.destination_address)) self.receive_packet(packet) return # forward the packet self.forward_packet(packet)
def send_one_packet(self,sequence): # get one packet worth of data body = self.send_buffer[0: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 StopAndWait segment to %d for %d" % (self.source_address,self.destination_address,packet.sequence)) self.transport.send_packet(packet) # set a timer self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit) return packet
def handle_packet(self,packet): # drop packet due to queue overflow if self.queue_size and len(self.queue) == self.queue_size: Sim.trace("! %d dropped packet due to queue overflow" % (self.address)) Sim.trace_custom('x', 'queue', self.filename) return # drop packet due to random loss if self.loss > 0 and random.random() < self.loss: Sim.trace("! %d dropped packet due to random loss" % (self.address)) return packet.enter_queue = Sim.scheduler.current_time() #Sim.trace("packet %d entered queue" % packet.sequence) if len(self.queue) == 0 and not self.busy: # packet can be sent immediately self.busy = True self.transmit(packet) else: # add packet to queue Sim.trace_custom('%d' % (len(self.queue)), 'queue', self.filename) self.queue.append(packet)
def send_packet(self,packet): # check if link is running if not self.running: return # drop packet due to queue overflow if self.queue_size and len(self.queue) == self.queue_size: Sim.trace("%d dropped packet due to queue overflow" % (self.address)) return # drop packet due to random loss if self.loss > 0 and random.random() < self.loss: Sim.trace("%d dropped packet due to random loss" % (self.address)) return packet.enter_queue = Sim.scheduler.current_time() if len(self.queue) == 0 and not self.busy: # packet can be sent immediately self.busy = True self.transmit(packet) else: # add packet to queue self.queue.append(packet)
def handle_packet(self,packet): # drop packet due to queue overflow if self.queue_size and len(self.queue) == self.queue_size: Sim.trace("%d dropped packet due to queue overflow" % (self.address)) if self.printOut: self.myfile.write(str(Sim.scheduler.current_time()) + " x\n") Sim.print_packet_loss(packet) return # drop packet due to random loss if self.loss > 0 and random.random() < self.loss: Sim.trace("%d dropped packet due to random loss" % (self.address)) if self.printOut: self.myfile.write(str(Sim.scheduler.current_time()) + " x\n") return packet.enter_queue = Sim.scheduler.current_time() if len(self.queue) == 0 and not self.busy: # packet can be sent immediately self.busy = True self.transmit(packet) else: # add packet to queue self.queue.append(packet)
def receive_packet(self,packet): # handle broadcast packets if packet.destination_address == 0: Sim.trace("%s received packet" % (self.hostname)) self.deliver_packet(packet) else: # check if unicast packet is for me for link in self.links: if link.address == packet.destination_address: Sim.trace("%s received packet" % (self.hostname)) self.deliver_packet(packet) return # decrement the TTL and drop if it has reached the last hop packet.ttl = packet.ttl - 1 if packet.ttl <= 0: Sim.trace("%s dropping packet due to TTL expired" % (self.hostname)) return # forward the packet self.forward_packet(packet)
def trace_queue(self, message): Sim.trace("Queue", 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): Sim.trace("DVR", message)
def trace(self,message): Sim.trace("Node",message)
def trace(self,message): Sim.trace("Link",message)
def trace(self, message): Sim.trace("Link", message)
def forward_broadcast_packet(self,packet): for link in self.links: Sim.trace("%s forwarding broadcast packet to %s" % (self.hostname,link.endpoint.hostname)) packet_copy = copy.deepcopy(packet) link.send_packet(packet_copy)
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()