From e2490dee4a0a35a82904b18c653fb830c8eda057 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Wed, 15 Apr 2020 15:41:37 -0700 Subject: [PATCH] modified wireless links to obtain colors based on connected network from the session, LinkData will no provide a color itself --- daemon/core/api/grpc/events.py | 1 + daemon/core/api/grpc/grpcutils.py | 1 + daemon/core/api/grpc/server.py | 2 ++ daemon/core/emane/linkmonitor.py | 2 ++ daemon/core/emulator/data.py | 1 + daemon/core/emulator/session.py | 17 +++++++++++++++++ daemon/core/gui/graph/graph.py | 2 ++ daemon/core/location/mobility.py | 2 ++ daemon/core/plugins/sdt.py | 26 ++++---------------------- daemon/proto/core/api/grpc/core.proto | 1 + 10 files changed, 33 insertions(+), 22 deletions(-) diff --git a/daemon/core/api/grpc/events.py b/daemon/core/api/grpc/events.py index 7505f1b4..a77f50cf 100644 --- a/daemon/core/api/grpc/events.py +++ b/daemon/core/api/grpc/events.py @@ -86,6 +86,7 @@ def handle_link_event(event: LinkData) -> core_pb2.LinkEvent: options=options, network_id=event.network_id, label=event.label, + color=event.color, ) return core_pb2.LinkEvent(message_type=event.message_type.value, link=link) diff --git a/daemon/core/api/grpc/grpcutils.py b/daemon/core/api/grpc/grpcutils.py index adc2e28d..6c5623ba 100644 --- a/daemon/core/api/grpc/grpcutils.py +++ b/daemon/core/api/grpc/grpcutils.py @@ -372,6 +372,7 @@ def convert_link(session: Session, link_data: LinkData) -> core_pb2.Link: options=options, network_id=link_data.network_id, label=link_data.label, + color=link_data.color, ) diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index c559f8f2..b14412ee 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -1494,12 +1494,14 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): flag = MessageFlags.ADD else: flag = MessageFlags.DELETE + color = session.get_link_color(emane_one.id) link = LinkData( message_type=flag, link_type=LinkTypes.WIRELESS, node1_id=node_one.id, node2_id=node_two.id, network_id=emane_one.id, + color=color, ) session.broadcast_link(link) return EmaneLinkResponse(result=True) diff --git a/daemon/core/emane/linkmonitor.py b/daemon/core/emane/linkmonitor.py index e161ada2..7eb903fd 100644 --- a/daemon/core/emane/linkmonitor.py +++ b/daemon/core/emane/linkmonitor.py @@ -314,6 +314,7 @@ class EmaneLinkMonitor: node_two: int, emane_id: int, ) -> None: + color = self.emane_manager.session.get_link_color(emane_id) link_data = LinkData( message_type=message_type, label=label, @@ -321,6 +322,7 @@ class EmaneLinkMonitor: node2_id=node_two, network_id=emane_id, link_type=LinkTypes.WIRELESS, + color=color, ) self.emane_manager.session.broadcast_link(link_data) diff --git a/daemon/core/emulator/data.py b/daemon/core/emulator/data.py index c7141541..7dff6be0 100644 --- a/daemon/core/emulator/data.py +++ b/daemon/core/emulator/data.py @@ -129,3 +129,4 @@ class LinkData: interface2_ip6: str = None interface2_ip6_mask: int = None opaque: str = None + color: str = None diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index 15dcf7d1..80538fc3 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -76,6 +76,7 @@ NODES = { } NODES_TYPE = {NODES[x]: x for x in NODES} CTRL_NET_ID = 9001 +LINK_COLORS = ["green", "blue", "orange", "purple", "turquoise"] class Session: @@ -105,6 +106,7 @@ class Session: self.thumbnail = None self.user = None self.event_loop = EventLoop() + self.link_colors = {} # dict of nodes: all nodes and nets self.node_id_gen = IdGen() @@ -927,6 +929,7 @@ class Session: self.location.reset() self.services.reset() self.mobility.config_reset() + self.link_colors.clear() def start_events(self) -> None: """ @@ -1956,3 +1959,17 @@ class Session: else: node = self.get_node(node_id) node.cmd(data, wait=False) + + def get_link_color(self, network_id: int) -> str: + """ + Assign a color for links associated with a network. + + :param network_id: network to get a link color for + :return: link color + """ + color = self.link_colors.get(network_id) + if not color: + index = len(self.link_colors) % len(LINK_COLORS) + color = LINK_COLORS[index] + self.link_colors[network_id] = color + return color diff --git a/daemon/core/gui/graph/graph.py b/daemon/core/gui/graph/graph.py index c26d3701..19fde443 100644 --- a/daemon/core/gui/graph/graph.py +++ b/daemon/core/gui/graph/graph.py @@ -212,6 +212,8 @@ class CanvasGraph(tk.Canvas): edge = CanvasWirelessEdge(self, src.id, dst.id, src_pos, dst_pos, token) if link.label: edge.middle_label_text(link.label) + if link.color: + edge.color = link.color self.wireless_edges[token] = edge src.wireless_edges.add(edge) dst.wireless_edges.add(edge) diff --git a/daemon/core/location/mobility.py b/daemon/core/location/mobility.py index b5a76507..05a6ac3e 100644 --- a/daemon/core/location/mobility.py +++ b/daemon/core/location/mobility.py @@ -488,12 +488,14 @@ class BasicRangeModel(WirelessModel): :param message_type: link message type :return: link data """ + color = self.session.get_link_color(self.wlan.id) return LinkData( message_type=message_type, node1_id=interface1.node.id, node2_id=interface2.node.id, network_id=self.wlan.id, link_type=LinkTypes.WIRELESS, + color=color, ) def sendlinkmsg( diff --git a/daemon/core/plugins/sdt.py b/daemon/core/plugins/sdt.py index 68be2147..93663052 100644 --- a/daemon/core/plugins/sdt.py +++ b/daemon/core/plugins/sdt.py @@ -33,7 +33,6 @@ NODE_LAYER = "CORE::Nodes" LINK_LAYER = "CORE::Links" CORE_LAYERS = [CORE_LAYER, LINK_LAYER, NODE_LAYER] DEFAULT_LINK_COLOR = "red" -LINK_COLORS = ["green", "blue", "orange", "purple", "white"] class Sdt: @@ -73,7 +72,6 @@ class Sdt: self.url = self.DEFAULT_SDT_URL self.address = None self.protocol = None - self.colors = {} self.network_layers = set() self.session.node_handlers.append(self.handle_node_update) self.session.link_handlers.append(self.handle_link_update) @@ -180,7 +178,6 @@ class Sdt: self.cmd(f"delete layer,{layer}") self.disconnect() self.network_layers.clear() - self.colors.clear() def cmd(self, cmdstr: str) -> bool: """ @@ -353,24 +350,6 @@ class Sdt: pass return result - def get_link_line(self, network_id: int) -> str: - """ - Retrieve link line color based on network. - - :param network_id: network id of link, None for wired links - :return: link line configuration - """ - network = self.session.nodes.get(network_id) - if network: - color = self.colors.get(network_id) - if not color: - index = len(self.colors) % len(LINK_COLORS) - color = LINK_COLORS[index] - self.colors[network_id] = color - else: - color = DEFAULT_LINK_COLOR - return f"{color},2" - def add_link( self, node_one: int, node_two: int, network_id: int = None, label: str = None ) -> None: @@ -388,7 +367,10 @@ class Sdt: return if self.wireless_net_check(node_one) or self.wireless_net_check(node_two): return - line = self.get_link_line(network_id) + color = DEFAULT_LINK_COLOR + if network_id: + color = self.session.get_link_color(network_id) + line = f"{color},2" link_id = get_link_id(node_one, node_two, network_id) layer = LINK_LAYER if network_id: diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index 8394acd5..997d5287 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -688,6 +688,7 @@ message Link { LinkOptions options = 6; int32 network_id = 7; string label = 8; + string color = 9; } message LinkOptions {