def sendall(self, message): """Send the message. Return only when finished.""" if not hasattr(self, "remote"): raise Exception("Must call connect() first") self.out += message while self.out_ack_i < len(self.out): end = self._send_data(self.out_i) if end is not None: 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() sim.new_thread(loss) self.out_i = end else: self.ack_event.wait()
def enqueue(self, packet, priority=3): """Called to place this packet in the queue.""" if random.random() < self.loss: self.__log("packet-loss %d", packet.id) elif len(self.__queue) >= self.__max_queue_size: self.__log("queue-overflow %d", packet.id) else: heapq.heappush(self.__queue, (priority, packet)) self.__log("queue-start %d", packet.id) sim.new_thread(self.send)
def run_random(latency, bandwidth, usage, packet_len, duration): # set up network host1 = Host('123.0.0.0') host2 = Host('101.0.0.0') Link(host1, host2, prop_delay=latency, bandwidth=bandwidth) # set up network use events client = RandomSender(host=host1) rate = usage * bandwidth / packet_len sim.new_thread( lambda: client.send(dest=host2.ip, rate=rate, duration=duration, message_len=packet_len) ) #run sim.run()
def demo_client_server(host1, host2, n_client=1, n_server=1): sim.__init__() server_ip = host2.ip for i in range(0,n_client): def c(client=FileClient(host1, (server_ip, 80+i))): #sleep(i*15) client.download_file() sim.new_thread(c) for i in range(0,n_server): server = Server(host2, 80+i) sim.new_thread(server.run) #def stop(server=server): # sim.sleep(5) # server.end() #sim.new_thread(stop) sim.run()
def enqueue(self, packet): """Called to place this packet in the queue.""" 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) sim.new_thread(send)