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 format(self, record): if record.levelno == logging.DEBUG: self._fmt = '%(name)s - %(message)s' else: record.time = sim.time() self._fmt = '%(time)7.4f %(message)s' return super(Formatter, self).format(record)
def __data(self, packet): """Handle a data packet.""" self.inc += (None,) * (packet.seq_num - len(self.inc)) self.inc[packet.seq_num : packet.seq_num + len(packet.message)] = packet.message self.inc_i = next( (i for i, b in enumerate(self.inc[self.inc_i :], start=self.inc_i) if b is None), len(self.inc) ) print packet.timestamp, sim.time() self.__sched_send(TcpPacket(self.local, self.remote, ack_num=self.inc_i, timestamp=packet.timestamp)) self.data_event.notify()
def __ack(self, packet): """Handle an ACK packet.""" self.timeout = (self.timeout + 2.5 * (sim.time() - packet.timestamp)) / 2 # print sim.time(), packet.timestamp if self.state == "SYN_RCVD": self.state = "ESTABLISHED" self._log("tcp-state", "SYN_RCVD <- ACK : ESTABLISHED") if packet.ack_num <= len(self.out): self.congestion.ack(packet.ack_num) self.ack_event.notify()
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 __init__(self, origin, dest, message=None, seq_num=None, ack_num=None, syn=False, fin=False, timestamp=None): """Create a TCP packet.""" self.origin = origin self.dest = dest self.message = message self.seq_num = seq_num self.ack_num = ack_num self.ack = ack_num is not None self.syn = syn self.fin = fin self.timestamp = timestamp if timestamp is not None else sim.time()
def recv(self, packet, link): type, value = pickle.loads(packet.message) if type == RoutingPacket.PING: self.__incoming_to_outgoing[link].enqueue(RoutingPacket) self.__matrix[packet.ip][link] = (sim.time() - value) / 2 vector = {ip : max(d.iteritems(), key=operator.itemgetter(1))[0] for ip, d in self.__matrix}