def add_wired_edge(self, src: CanvasNode, dst: CanvasNode, link: Link) -> None: token = create_edge_token(src.id, dst.id) if token in self.edges and link.options.unidirectional: edge = self.edges[token] edge.asymmetric_link = link elif token not in self.edges: node1 = src.core_node node2 = dst.core_node src_pos = (node1.position.x, node1.position.y) dst_pos = (node2.position.x, node2.position.y) edge = CanvasEdge(self, src.id, src_pos, dst_pos) edge.token = token edge.dst = dst.id edge.set_link(link) edge.check_wireless() src.edges.add(edge) dst.edges.add(edge) self.edges[edge.token] = edge self.core.links[edge.token] = edge if link.HasField("iface1"): iface1 = link.iface1 self.core.iface_to_edge[(node1.id, iface1.id)] = token src.ifaces[iface1.id] = iface1 edge.src_iface = iface1 if link.HasField("iface2"): iface2 = link.iface2 self.core.iface_to_edge[(node2.id, iface2.id)] = edge.token dst.ifaces[iface2.id] = iface2 edge.dst_iface = iface2
def start_session(self) -> StartSessionResponse: self.ifaces_manager.reset_mac() nodes = [x.core_node for x in self.canvas_nodes.values()] links = [] for edge in self.links.values(): link = Link() link.CopyFrom(edge.link) if link.HasField("iface1") and not link.iface1.mac: link.iface1.mac = self.ifaces_manager.next_mac() if link.HasField("iface2") and not link.iface2.mac: link.iface2.mac = self.ifaces_manager.next_mac() links.append(link) wlan_configs = self.get_wlan_configs_proto() mobility_configs = self.get_mobility_configs_proto() emane_model_configs = self.get_emane_model_configs_proto() hooks = list(self.hooks.values()) service_configs = self.get_service_configs_proto() file_configs = self.get_service_file_configs_proto() asymmetric_links = [ x.asymmetric_link for x in self.links.values() if x.asymmetric_link ] config_service_configs = self.get_config_service_configs_proto() if self.emane_config: emane_config = {x: self.emane_config[x].value for x in self.emane_config} else: emane_config = None response = StartSessionResponse(result=False) try: self.send_servers() response = self.client.start_session( self.session_id, nodes, links, self.location, hooks, emane_config, emane_model_configs, wlan_configs, mobility_configs, service_configs, file_configs, asymmetric_links, config_service_configs, ) logging.info( "start session(%s), result: %s", self.session_id, response.result ) if response.result: self.set_metadata() except grpc.RpcError as e: self.app.show_grpc_exception("Start Session Error", e) return response
def from_proto(cls, proto: core_pb2.Link) -> "Link": iface1 = None if proto.HasField("iface1"): iface1 = Interface.from_proto(proto.iface1) iface2 = None if proto.HasField("iface2"): iface2 = Interface.from_proto(proto.iface2) options = None if proto.HasField("options"): options = LinkOptions.from_proto(proto.options) return Link( type=LinkType(proto.type), node1_id=proto.node1_id, node2_id=proto.node2_id, iface1=iface1, iface2=iface2, options=options, network_id=proto.network_id, label=proto.label, color=proto.color, )
def create_link( self, edge: CanvasEdge, canvas_src_node: CanvasNode, canvas_dst_node: CanvasNode ) -> None: """ Create core link for a pair of canvas nodes, with token referencing the canvas edge. """ src_node = canvas_src_node.core_node dst_node = canvas_dst_node.core_node # determine subnet self.ifaces_manager.determine_subnets(canvas_src_node, canvas_dst_node) src_iface = None if NodeUtils.is_container_node(src_node.type): src_iface = self.create_iface(canvas_src_node) self.iface_to_edge[(src_node.id, src_iface.id)] = edge.token dst_iface = None if NodeUtils.is_container_node(dst_node.type): dst_iface = self.create_iface(canvas_dst_node) self.iface_to_edge[(dst_node.id, dst_iface.id)] = edge.token link = Link( type=LinkType.WIRED, node1_id=src_node.id, node2_id=dst_node.id, iface1=src_iface, iface2=dst_iface, ) # assign after creating link proto, since interfaces are copied if src_iface: iface1 = link.iface1 edge.src_iface = iface1 canvas_src_node.ifaces[iface1.id] = iface1 if dst_iface: iface2 = link.iface2 edge.dst_iface = iface2 canvas_dst_node.ifaces[iface2.id] = iface2 edge.set_link(link) self.links[edge.token] = edge logging.info("Add link between %s and %s", src_node.name, dst_node.name)
def paste(self) -> None: if self.core.is_runtime(): logging.debug("paste is disabled during runtime state") return # maps original node canvas id to copy node canvas id copy_map = {} # the edges that will be copy over to_copy_edges = [] for canvas_node in self.to_copy: core_node = canvas_node.core_node actual_x = core_node.position.x + 50 actual_y = core_node.position.y + 50 scaled_x, scaled_y = self.get_scaled_coords(actual_x, actual_y) copy = self.core.create_node(actual_x, actual_y, core_node.type, core_node.model) if not copy: continue node = CanvasNode(self.app, scaled_x, scaled_y, copy, canvas_node.image) # copy configurations and services node.core_node.services[:] = canvas_node.core_node.services node.core_node.config_services[:] = canvas_node.core_node.config_services node.emane_model_configs = deepcopy( canvas_node.emane_model_configs) node.wlan_config = deepcopy(canvas_node.wlan_config) node.mobility_config = deepcopy(canvas_node.mobility_config) node.service_configs = deepcopy(canvas_node.service_configs) node.service_file_configs = deepcopy( canvas_node.service_file_configs) node.config_service_configs = deepcopy( canvas_node.config_service_configs) copy_map[canvas_node.id] = node.id self.core.canvas_nodes[copy.id] = node self.nodes[node.id] = node for edge in canvas_node.edges: if edge.src not in self.to_copy or edge.dst not in self.to_copy: if canvas_node.id == edge.src: dst_node = self.nodes[edge.dst] self.create_edge(node, dst_node) elif canvas_node.id == edge.dst: src_node = self.nodes[edge.src] self.create_edge(src_node, node) else: to_copy_edges.append(edge) # copy link and link config for edge in to_copy_edges: src_node_id = copy_map[edge.token[0]] dst_node_id = copy_map[edge.token[1]] src_node_copy = self.nodes[src_node_id] dst_node_copy = self.nodes[dst_node_id] self.create_edge(src_node_copy, dst_node_copy) token = create_edge_token(src_node_copy.id, dst_node_copy.id) copy_edge = self.edges[token] copy_link = copy_edge.link options = edge.link.options copy_link.options.CopyFrom(options) iface1_id = None if copy_link.HasField("iface1"): iface1_id = copy_link.iface1.id iface2_id = None if copy_link.HasField("iface2"): iface2_id = copy_link.iface2.id if not options.unidirectional: copy_edge.asymmetric_link = None else: asym_iface1 = None if iface1_id: asym_iface1 = Interface(id=iface1_id) asym_iface2 = None if iface2_id: asym_iface2 = Interface(id=iface2_id) copy_edge.asymmetric_link = Link( node1_id=copy_link.node2_id, node2_id=copy_link.node1_id, iface1=asym_iface1, iface2=asym_iface2, options=edge.asymmetric_link.options, ) self.itemconfig( copy_edge.id, width=self.itemcget(edge.id, "width"), fill=self.itemcget(edge.id, "fill"), ) self.tag_raise(tags.NODE)