def close(self): """Close this end of a connection.""" def fin(): self.__sched_send(TcpPacket(self.local, self.remote, seq_num=len(self.out), fin=True)) self.ack_event.wait(self.timeout) if self.state == "ESTABLISHED" or self.state == "SYN_RCVD": self.state = "FIN_WAIT_1" self._log("tcp-state", "ESTABLISHED : FIN -> FIN_WAIT_1") while self.out_ack_i < len(self.out): self.ack_event.wait() attempt(fin, 10) if self.state == "FIN_WAIT_1": self.state = "FIN_WAIT_2" self._log("tcp-state", "FIN_WAIT_1 <- ACK : FIN_WAIT_2") self.fin_event.wait() self.state = "TIME_WAIT" self._log("tcp-state", "FIN_WAIT_2 <- FIN : ACK -> TIME_WAIT") elif self.state == "CLOSING": self.state = "TIME_WAIT" self._log("tcp-state", "CLOSING <- ACK : TIME_WAIT") sim.sleep(3 * self.timeout) self._log("tcp-state", "TIME_WAIT : CLOSED") elif self.state == "CLOSE_WAIT": self.state = "LAST_ACK" self._log("tcp-state", "CLOSE_WAIT : FIN -> LAST_ACK") attempt(fin, 10) self._log("tcp-state", "LAST_ACK <- ACK : CLOSED")
def generate(self, function, args, avg_delay, duration): """Generate events with exponential distribution.""" frequency = 1 / avg_delay end_time = duration + sim.time() sim.sleep(random.expovariate(frequency)) while end_time <= sim.time(): sim.sleep(random.expovariate(frequency)) function(*args)
def send(self, dest, message_len, rate, duration): """Sends random messages for the specified duration.""" stop_time = sim.time() + duration sim.sleep(random.expovariate(rate)) while stop_time > sim.time(): message = ''.join(random.choice(string.letters) for _ in xrange(message_len)) self.host.sched_send(Packet(self.host.ip, dest, message)) sim.sleep(random.expovariate(rate))
def loss(start=self.out_i, end=end, timeout=self.timeout): time = sim.time() sim.sleep(self.timeout) if start <= self.out_ack_i < end and self.last_loss < time and self.congestion.min_ack < end: self.congestion.min_ack = end self.out_i = start self.last_loss = time self.timeout *= 2 self._log("tcp-loss", "timeout %d-%d %.4f", start, end - 1, timeout) self.congestion.timeout() self.ack_event.notify()
def send(): self.__log('queue-start %d', packet.id) self.__mutex.lock() self.__log('queue-end %d', packet.id) self.__log('transmit-start %d', packet.id) sim.sleep(packet.size / self.bandwidth) self.__log('transmit-end %d', packet.id) self.__mutex.unlock() self.__log('propogate-start %d', packet.id) sim.sleep(self.prop_delay) self.__log('propogate-end %d', packet.id) self.dest.received(packet)
def send(self): self.__mutex.lock() priority, packet = heapq.heappop(self.__queue) self.__log("queue-end %d", packet.id) self.__log("transmit-start %d", packet.id) sim.sleep(len(packet) / self.bandwidth) self.__log("transmit-end %d", packet.id) self.__mutex.unlock() self.__log("propogate-start %d", packet.id) sim.sleep(self.prop_delay) self.__log("propogate-end %d", packet.id) self.dest.received(packet, self)