def flood(self, frame): each(self.other_ports).send(frame) self.host.report( '{} -> {}', self.index, ','.join(map(str, each(self.other_ports).index)), )
def populate(self, models): self.devices = [] self.links = [] self.items = [] tip2device = {} for model in models: item = create(model) if item: log.debug('adding {}'.format(model)) self.items.append(item) if isinstance(item, Device): log.debug('update {}\'s tips'.format(model)) tip2device.update({tip: item for tip in model.tips}) self.devices.append(item) elif isinstance(item, Link): item.setZValue(-1) self.links.append(item) log.debug('adding finished: {}'.format( map(str, each(self.items).model))) self.load_ok = True for link in self.links: peers = each(link.model.tips).peer try: link.devices = tuple(tip2device[tip] for tip in peers) except KeyError: log.error('{}\'s endpoint is not added to scene'.format( link.model)) self.ok = False if self.load_ok: for item in self.items: self.addItem(item) self.addItem(item.console)
def __init__(self, n_interfaces=3): super(Switch, self).__init__(n_interfaces) self.mac2port = CacheTable(entry_duration=1000) self.ports = [ Port( host=self, nic=nic, mac2port=self.mac2port, index=i, ) for i, nic in enumerate(self.nics) ] each(self.ports).calc_other_ports(self.ports)
def __init__(self, n_tips=3): super(Switch, self).__init__(n_tips) self.nics = [NIC(tip) for tip in self.tips] mac2port = CacheTable(entry_duration=1000) self.ports = [ Port( host=self, nic=nic, mac2port=mac2port, index=i, ) for i, nic in enumerate(self.nics) ] each(self.ports).calc_other_ports(self.ports) mint.worker(self.run, priority=simulation.SWITCH_PRIORITY)
def __init__(self, a, b, latency=0): super(Link, self).__init__(n_interfaces=2) a.peer_with(self.tips[0], by=self) self.tips[1].peer_with(b, by=self) self.latency = latency self.pipes = [deque([0] * self.latency) for _ in xrange(2)] self.endpoint_names = map(str, each(self.tips).peer.host)
def __init__(self, a, b, latency=0): super(Link, self).__init__(n_tips=2) a.peer_with(self.tips[0]); self.tips[1].peer_with(b) self.latency = latency self.pipes = [deque([0] * self.latency) for _ in xrange(2)] self.endpoint_names = map(str, each(self.tips).peer.host) mint.worker(self.run, priority=simulation.LINK_PRIORITY)
def track_device(self): old_line = self.line() new_line = QLineF(*each(self.devices).scenePos()) old_mid_pt = old_line.pointAt(0.5) new_mid_pt = new_line.pointAt(0.5) offset = new_mid_pt - old_mid_pt self.console.moveBy(offset.x(), offset.y()) self.setLine(new_line)
def __init__(self, a, b, latency=0): super(Link, self).__init__(n_tips=2) a.peer_with(self.tips[0], by=self) self.tips[1].peer_with(b, by=self) self.latency = latency self.pipes = [deque([0] * self.latency) for _ in xrange(2)] self.endpoint_names = map(str, each(self.tips).peer.host) mint.worker(self.run, priority=simulation.LINK_PRIORITY)
def run(self): if hasattr(self, 'timer'): self.stop() elif not self.sim.finished: each(self.scene().devices).highlighted = False self.timer = QTimer() self.timer.timeout.connect(self.step_and_refresh) self.timer.start(self.ms_per_step) else: QMessageBox.warning(self, 'Error', 'Simulation finished.\n' + 'Please restart.')
def keyPressEvent(self, ev): ch = ev.text() if ch == config.key['Step']: self.step() elif ch == config.key['Run']: self.run() elif ch == config.key['Toggle console']: self.toggle_console() elif ch == config.key['Toggle console frame']: self.scene().toggle('console frame') elif ch == config.key['Toggle mode']: self.toggle_mode() elif ch == config.key['Next mode']: self.next_mode() elif ch == config.key['Clear']: each(self.scene().devices).highlighted = False self.refresh() elif ch == '?': self.help()
def step(self): sim = self.sim each(self.scene().devices).highlighted = False if sim.finished and self.mode == self.MODE_EVENT: self.mode = self.MODE_TIK_TOK if self.mode == self.MODE_EVENT: # stop if self.thread and not self.thread.isFinished: self.thread.stopped = True self.thread.wait() # start else: self.thread = StepUntilSthHappend(sim) self.thread.finished.connect(self.refresh) self.thread.start() elif self.mode == self.MODE_TIK_TOK: sim.step() elif self.mode == self.MODE_PHASE: sim.step(by=sim.Phase) else: log.error('{} mode not recognized'.format(self.mode)) self.refresh()
def devices(self, val): self._devices = val each(self._devices).links.append(self) self.track_device()
def flood(self, frame): each(self.other_ports).send(frame)
def run(self): while True: each(self.ports).forward() mint.elapse(1)
def itemChange(self, change, val): if change == self.ItemPositionHasChanged: each(self.links).track_device() return super(Device, self).itemChange(change, val)
def __repr__(self): return '\n'.join(map(repr, each(self.layers).container))
def refresh(self): each(self.views).refresh()
def gen_name(self): roles = [m.role for m in each(self.items).model] name = ' '.join('{}{}'.format(len(tuple(g)), role.lower()) for role, g in itertools.groupby(sorted(roles))) return name + '.topo'
def toggle(self, what): each(self.items).toggle(what)
def update_status(self): each(self.items).refresh()
def run(self): while True: isum = sum(each(self.tips).isymbol)
def run(self): while True: each(self.interfaces).do_send() each(self.interfaces).do_recv() mint.elapse(1)
def __init__(self, n_interfaces=3): super(Router, self).__init__(n_interfaces) self.interfaces = [Interface(self, nic) for nic in self.nics] each(self.interfaces).report.connect(self.report) each(self.interfaces).on_ipv4.connect(self.on_ipv4) self.routes = RouteTable()
def __repr__(self): return repr(list(each(self.a).e))