def __transmit(self): """Transmit packet.""" packet = self.queue.pop() log('queue-end %d %d' % (self.id, packet.id)) log('transmit-start %d %d' % (self.id, packet.id)) self.busy = True yield sleep(packet.size / self.bandwidth) log('transmit-end %d %d' % (self.id, packet.id)) self.busy = False if self.queue: simulator.new_thread(self.__transmit()) log('propogate-start %d %d' % (self.id, packet.id)) yield sleep(self.prop_delay) log('propogate-end %d %d' % (self.id, packet.id)) yield self.dest.received(packet)
self.rate = rate def send(self, duration): """Sends random messages for specified duration.""" stop_time = simulator.scheduler.get_time() + duration while stop_time > simulator.scheduler.get_time(): message = ''.join(random.choice(string.letters) for _ in xrange(self.message_len)) self.host.send(Packet(self.host.ip, self.dest, message)) yield sleep(random.expovariate(self.rate)) if __name__ == '__main__': """Main method.""" # set up network host1 = Host('123.0.0.0') host2 = Host('101.0.0.0') link1 = Link(host1, host2, prop_delay=0.5, bandwidth=1000) link2 = Link(host2, host1, prop_delay=0.5, bandwidth=1000) # set up usage mlen = 128 usage = .90 rs1 = RandomSender(host1, host2.ip, message_len=mlen, rate=usage*link1.bandwidth/mlen) rs2 = RandomSender(host2, host1.ip, message_len=mlen, rate=usage*link1.bandwidth/mlen) # run simulator.new_thread(rs1.send(duration=10)) simulator.new_thread(rs2.send(duration=10)) logger.level = 4 simulator.run()
def enqueue(self, packet): """Called to place this packet in the queue.""" log('queue-start %d %d' % (self.id, packet.id)) self.queue.appendleft(packet) if not self.busy: simulator.new_thread(self.__transmit())