def _update_node_positions(self): for node in self.nodes.itervalues(): if node.has_mobility: ns3_node = ns.network.NodeList.GetNode(node.node_index) mobility = ns3_node.GetObject(ns.mobility.MobilityModel.GetTypeId()) if mobility is not None: pos = mobility.GetPosition() x, y = transform_point_simulation_to_canvas(pos.x, pos.y) node.set_position(x, y) if node is self.follow_node: hadj = self._scrolled_window.get_hadjustment() vadj = self._scrolled_window.get_vadjustment() px, py = self.canvas.convert_to_pixels(x, y) hadj.value = px - hadj.page_size/2 vadj.value = py - vadj.page_size/2
def node_drag_motion(self, item, targe_item, event, node): self.simulation.lock.acquire() try: ns3_node = ns.network.NodeList.GetNode(node.node_index) mob = ns3_node.GetObject(ns.mobility.MobilityModel.GetTypeId()) if mob is None: return False if self.node_drag_state is None: return False x, y, dummy = self.canvas.window.get_pointer() canvas_x, canvas_y = self.canvas.convert_from_pixels(x, y) dx = (canvas_x - self.node_drag_state.canvas_x0) dy = (canvas_y - self.node_drag_state.canvas_y0) pos = mob.GetPosition() pos.x = self.node_drag_state.sim_x0 + transform_distance_canvas_to_simulation(dx) pos.y = self.node_drag_state.sim_y0 + transform_distance_canvas_to_simulation(dy) #print "SetPosition(%G, %G)" % (pos.x, pos.y) mob.SetPosition(pos) node.set_position(*transform_point_simulation_to_canvas(pos.x, pos.y)) finally: self.simulation.lock.release() return True
def scan_topology(self): print "scanning topology: %i nodes..." % (ns.network.NodeList.GetNNodes(),) graph = pygraphviz.AGraph() seen_nodes = 0 for nodeI in range(ns.network.NodeList.GetNNodes()): seen_nodes += 1 if seen_nodes == 100: print "scan topology... %i nodes visited (%.1f%%)" % (nodeI, 100*nodeI/ns.network.NodeList.GetNNodes()) seen_nodes = 0 node = ns.network.NodeList.GetNode(nodeI) node_name = "Node %i" % nodeI node_view = self.get_node(nodeI) mobility = node.GetObject(ns.mobility.MobilityModel.GetTypeId()) if mobility is not None: node_view.set_color("red") pos = mobility.GetPosition() node_view.set_position(*transform_point_simulation_to_canvas(pos.x, pos.y)) #print "node has mobility position -> ", "%f,%f" % (pos.x, pos.y) else: graph.add_node(node_name) for devI in range(node.GetNDevices()): device = node.GetDevice(devI) device_traits = lookup_netdevice_traits(type(device)) if device_traits.is_wireless: continue if device_traits.is_virtual: continue channel = device.GetChannel() if channel.GetNDevices() > 2: if REPRESENT_CHANNELS_AS_NODES: # represent channels as white nodes if mobility is None: channel_name = "Channel %s" % id(channel) graph.add_edge(node_name, channel_name) self.get_channel(channel) self.create_link(self.get_node(nodeI), self.get_channel(channel)) else: # don't represent channels, just add links between nodes in the same channel for otherDevI in range(channel.GetNDevices()): otherDev = channel.GetDevice(otherDevI) otherNode = otherDev.GetNode() otherNodeView = self.get_node(otherNode.GetId()) if otherNode is not node: if mobility is None and not otherNodeView.has_mobility: other_node_name = "Node %i" % otherNode.GetId() graph.add_edge(node_name, other_node_name) self.create_link(self.get_node(nodeI), otherNodeView) else: for otherDevI in range(channel.GetNDevices()): otherDev = channel.GetDevice(otherDevI) otherNode = otherDev.GetNode() otherNodeView = self.get_node(otherNode.GetId()) if otherNode is not node: if mobility is None and not otherNodeView.has_mobility: other_node_name = "Node %i" % otherNode.GetId() graph.add_edge(node_name, other_node_name) self.create_link(self.get_node(nodeI), otherNodeView) print "scanning topology: calling graphviz layout" graph.layout(LAYOUT_ALGORITHM) for node in graph.iternodes(): #print node, "=>", node.attr['pos'] node_type, node_id = node.split(' ') pos_x, pos_y = [float(s) for s in node.attr['pos'].split(',')] if node_type == 'Node': obj = self.nodes[int(node_id)] elif node_type == 'Channel': obj = self.channels[int(node_id)] obj.set_position(pos_x, pos_y) print "scanning topology: all done." self.emit("topology-scanned")
def hook(viz): cx1, cy1 = transform_point_simulation_to_canvas(x1, y1) cx2, cy2 = transform_point_simulation_to_canvas(x2, y2) viz.canvas.set_bounds(cx1, cy1, cx2, cy2)