From 41a3c5fd7fa2d2de040b39f93b721cb80b3da9e2 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Sun, 26 Jul 2020 11:45:40 -0700 Subject: [PATCH] pygui: added wrapper class for sessions returned by grpc GetSession --- daemon/core/gui/coreclient.py | 9 ++++----- daemon/core/gui/graph/graph.py | 13 +++++++------ daemon/core/gui/wrappers.py | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/daemon/core/gui/coreclient.py b/daemon/core/gui/coreclient.py index fd1abc34..5ddc28d7 100644 --- a/daemon/core/gui/coreclient.py +++ b/daemon/core/gui/coreclient.py @@ -48,6 +48,7 @@ from core.gui.wrappers import ( NodeServiceData, NodeType, Position, + Session, SessionLocation, SessionState, ThroughputsEvent, @@ -311,8 +312,8 @@ class CoreClient: # get session data try: response = self.client.get_session(self.session_id) - session = response.session - self.state = SessionState(session.state) + session = Session.from_proto(response.session) + self.state = session.state self.handling_events = self.client.events( self.session_id, self.handle_events ) @@ -349,9 +350,7 @@ class CoreClient: self.ifaces_manager.joined(session.links) # draw session - nodes = [Node.from_proto(x) for x in session.nodes] - links = [Link.from_proto(x) for x in session.links] - self.app.canvas.reset_and_redraw(nodes, links) + self.app.canvas.reset_and_redraw(session) # get mobility configs response = self.client.get_mobility_configs(self.session_id) diff --git a/daemon/core/gui/graph/graph.py b/daemon/core/gui/graph/graph.py index ae0b00c0..81e0d1c6 100644 --- a/daemon/core/gui/graph/graph.py +++ b/daemon/core/gui/graph/graph.py @@ -22,7 +22,7 @@ from core.gui.graph.shape import Shape from core.gui.graph.shapeutils import ShapeType, is_draw_shape, is_marker from core.gui.images import ImageEnum, TypeToImage from core.gui.nodeutils import NodeDraw, NodeUtils -from core.gui.wrappers import Interface, Link, LinkType, Node, ThroughputsEvent +from core.gui.wrappers import Interface, Link, LinkType, Node, Session, ThroughputsEvent if TYPE_CHECKING: from core.gui.app import Application @@ -127,7 +127,7 @@ class CanvasGraph(tk.Canvas): ) self.configure(scrollregion=self.bbox(tk.ALL)) - def reset_and_redraw(self, nodes: List[Node], links: List[Link]) -> None: + def reset_and_redraw(self, session: Session) -> None: # reset view options to default state self.show_node_labels.set(True) self.show_link_labels.set(True) @@ -152,7 +152,7 @@ class CanvasGraph(tk.Canvas): self.wireless_edges.clear() self.wireless_network.clear() self.drawing_edge = None - self.draw_session(nodes, links) + self.draw_session(session) def setup_bindings(self) -> None: """ @@ -325,19 +325,20 @@ class CanvasGraph(tk.Canvas): self.nodes[node.id] = node self.core.canvas_nodes[core_node.id] = node - def draw_session(self, nodes: List[Node], links: List[Link]) -> None: + def draw_session(self, session: Session) -> None: """ Draw existing session. """ # draw existing nodes - for core_node in nodes: + for core_node in session.nodes: + logging.debug("drawing node: %s", core_node) # peer to peer node is not drawn on the GUI if NodeUtils.is_ignore_node(core_node.type): continue self.add_core_node(core_node) # draw existing links - for link in links: + for link in session.links: logging.debug("drawing link: %s", link) canvas_node1 = self.core.canvas_nodes[link.node1_id] canvas_node2 = self.core.canvas_nodes[link.node2_id] diff --git a/daemon/core/gui/wrappers.py b/daemon/core/gui/wrappers.py index 5fb12837..612c7646 100644 --- a/daemon/core/gui/wrappers.py +++ b/daemon/core/gui/wrappers.py @@ -545,6 +545,27 @@ class Node: ) +@dataclass +class Session: + id: int + state: SessionState + nodes: List[Node] + links: List[Link] + dir: str + + @classmethod + def from_proto(cls, proto: core_pb2.Session) -> "Session": + nodes = [Node.from_proto(x) for x in proto.nodes] + links = [Link.from_proto(x) for x in proto.links] + return Session( + id=proto.id, + state=SessionState(proto.state), + nodes=nodes, + links=links, + dir=proto.dir, + ) + + @dataclass class LinkEvent: message_type: MessageType