grpc/pygui: updated add_node source support, updated pygui to handle add_node events

This commit is contained in:
Blake Harnden 2020-06-26 22:11:36 -07:00
parent e79645013b
commit 5eae67aac5
5 changed files with 37 additions and 18 deletions

View file

@ -468,17 +468,20 @@ class CoreGrpcClient:
return stream
def add_node(
self, session_id: int, node: core_pb2.Node
self, session_id: int, node: core_pb2.Node, source: str = None
) -> core_pb2.AddNodeResponse:
"""
Add node to session.
:param session_id: session id
:param node: node to add
:param source: source application
:return: response with node id
:raises grpc.RpcError: when session doesn't exist
"""
request = core_pb2.AddNodeRequest(session_id=session_id, node=node)
request = core_pb2.AddNodeRequest(
session_id=session_id, node=node, source=source
)
return self.stub.AddNode(request)
def get_node(self, session_id: int, node_id: int) -> core_pb2.GetNodeResponse:

View file

@ -668,6 +668,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
_type, _id, options = grpcutils.add_node_data(request.node)
_class = session.get_node_class(_type)
node = session.add_node(_class, _id, options)
source = request.source if request.source else None
session.broadcast_node(node, MessageFlags.ADD, source)
return core_pb2.AddNodeResponse(node_id=node.id)
def GetNode(

View file

@ -225,16 +225,18 @@ class CoreClient:
def handle_node_event(self, event: NodeEvent) -> None:
logging.debug("node event: %s", event)
node_id = event.node.id
canvas_node = self.canvas_nodes[node_id]
if event.message_type == MessageType.NONE:
canvas_node = self.canvas_nodes[event.node.id]
x = event.node.position.x
y = event.node.position.y
canvas_node.move(x, y)
elif event.message_type == MessageType.DELETE:
canvas_node = self.canvas_nodes[event.node.id]
self.app.canvas.clear_selection()
self.app.canvas.select_object(canvas_node.id)
self.app.canvas.delete_selected_objects()
elif event.message_type == MessageType.ADD:
self.app.canvas.add_core_node(event.node)
else:
logging.warning("unknown node event: %s", event)

View file

@ -7,7 +7,14 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple
from PIL import Image
from PIL.ImageTk import PhotoImage
from core.api.grpc.core_pb2 import Interface, Link, LinkType, Session, ThroughputsEvent
from core.api.grpc.core_pb2 import (
Interface,
Link,
LinkType,
Node,
Session,
ThroughputsEvent,
)
from core.gui.dialogs.shapemod import ShapeDialog
from core.gui.graph import tags
from core.gui.graph.edges import (
@ -315,29 +322,33 @@ class CanvasGraph(tk.Canvas):
edge = self.wireless_edges[token]
edge.middle_label_text(link.label)
def add_core_node(self, core_node: Node) -> None:
if core_node.id in self.core.canvas_nodes:
logging.error("core node already exists: %s", core_node)
return
logging.debug("adding node %s", core_node)
# if the gui can't find node's image, default to the "edit-node" image
image = NodeUtils.node_image(core_node, self.app.guiconfig, self.app.app_scale)
if not image:
image = self.app.get_icon(ImageEnum.EDITNODE, ICON_SIZE)
x = core_node.position.x
y = core_node.position.y
node = CanvasNode(self.app, x, y, core_node, image)
self.nodes[node.id] = node
self.core.canvas_nodes[core_node.id] = node
def draw_session(self, session: Session) -> None:
"""
Draw existing session.
"""
# draw existing 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
image = NodeUtils.node_image(
core_node, self.app.guiconfig, self.app.app_scale
)
# if the gui can't find node's image, default to the "edit-node" image
if not image:
image = self.app.get_icon(ImageEnum.EDITNODE, ICON_SIZE)
x = core_node.position.x
y = core_node.position.y
node = CanvasNode(self.app, x, y, core_node, image)
self.nodes[node.id] = node
self.core.canvas_nodes[core_node.id] = node
self.add_core_node(core_node)
# draw existing links
# draw existing links
for link in session.links:
logging.debug("drawing link: %s", link)
canvas_node1 = self.core.canvas_nodes[link.node1_id]

View file

@ -404,6 +404,7 @@ message FileEvent {
message AddNodeRequest {
int32 session_id = 1;
Node node = 2;
string source = 3;
}
message AddNodeResponse {