2020-05-02 07:36:33 +01:00
|
|
|
import functools
|
2020-01-30 00:08:36 +00:00
|
|
|
import logging
|
2019-12-05 19:12:25 +00:00
|
|
|
import tkinter as tk
|
2020-01-13 23:31:41 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2019-12-05 19:12:25 +00:00
|
|
|
|
2019-12-10 06:50:26 +00:00
|
|
|
import grpc
|
|
|
|
|
2019-12-11 22:42:00 +00:00
|
|
|
from core.api.grpc import core_pb2
|
2019-12-06 17:10:50 +00:00
|
|
|
from core.api.grpc.core_pb2 import NodeType
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui import themes
|
|
|
|
from core.gui.dialogs.emaneconfig import EmaneConfigDialog
|
|
|
|
from core.gui.dialogs.mobilityconfig import MobilityConfigDialog
|
|
|
|
from core.gui.dialogs.nodeconfig import NodeConfigDialog
|
2020-01-20 20:17:11 +00:00
|
|
|
from core.gui.dialogs.nodeconfigservice import NodeConfigServiceDialog
|
2019-12-19 17:50:58 +00:00
|
|
|
from core.gui.dialogs.nodeservice import NodeServiceDialog
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui.dialogs.wlanconfig import WlanConfigDialog
|
|
|
|
from core.gui.errors import show_grpc_error
|
|
|
|
from core.gui.graph import tags
|
2020-05-02 07:36:33 +01:00
|
|
|
from core.gui.graph.edges import CanvasEdge
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui.graph.tooltip import CanvasTooltip
|
2020-02-17 23:14:52 +00:00
|
|
|
from core.gui.images import ImageEnum, Images
|
2020-02-24 18:58:01 +00:00
|
|
|
from core.gui.nodeutils import ANTENNA_SIZE, NodeUtils
|
2019-12-05 19:12:25 +00:00
|
|
|
|
2020-01-13 23:31:41 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from core.gui.app import Application
|
|
|
|
from PIL.ImageTk import PhotoImage
|
|
|
|
|
2019-12-05 19:12:25 +00:00
|
|
|
NODE_TEXT_OFFSET = 5
|
|
|
|
|
|
|
|
|
|
|
|
class CanvasNode:
|
2020-01-13 23:31:41 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
app: "Application",
|
|
|
|
x: float,
|
|
|
|
y: float,
|
|
|
|
core_node: core_pb2.Node,
|
|
|
|
image: "PhotoImage",
|
|
|
|
):
|
2019-12-05 19:12:25 +00:00
|
|
|
self.app = app
|
|
|
|
self.canvas = app.canvas
|
|
|
|
self.image = image
|
|
|
|
self.core_node = core_node
|
|
|
|
self.id = self.canvas.create_image(
|
2019-12-09 22:13:21 +00:00
|
|
|
x, y, anchor=tk.CENTER, image=self.image, tags=tags.NODE
|
2019-12-05 19:12:25 +00:00
|
|
|
)
|
2019-12-10 22:33:52 +00:00
|
|
|
label_y = self._get_label_y()
|
2019-12-05 19:12:25 +00:00
|
|
|
self.text_id = self.canvas.create_text(
|
|
|
|
x,
|
2019-12-10 22:33:52 +00:00
|
|
|
label_y,
|
2019-12-05 19:12:25 +00:00
|
|
|
text=self.core_node.name,
|
2020-04-19 23:47:07 +01:00
|
|
|
tags=tags.NODE_LABEL,
|
2020-02-12 22:13:28 +00:00
|
|
|
font=self.app.icon_text_font,
|
2019-12-05 19:12:25 +00:00
|
|
|
fill="#0000CD",
|
2020-04-19 23:47:07 +01:00
|
|
|
state=self.canvas.show_node_labels.state(),
|
2019-12-05 19:12:25 +00:00
|
|
|
)
|
|
|
|
self.tooltip = CanvasTooltip(self.canvas)
|
2019-12-06 17:10:50 +00:00
|
|
|
self.edges = set()
|
|
|
|
self.interfaces = []
|
|
|
|
self.wireless_edges = set()
|
2020-02-17 23:14:52 +00:00
|
|
|
self.antennas = []
|
|
|
|
self.antenna_images = {}
|
2020-04-21 08:38:36 +01:00
|
|
|
# possible configurations
|
|
|
|
self.emane_model_configs = {}
|
|
|
|
self.wlan_config = {}
|
|
|
|
self.mobility_config = {}
|
2020-04-21 18:31:20 +01:00
|
|
|
self.service_configs = {}
|
|
|
|
self.service_file_configs = {}
|
2020-04-21 19:13:41 +01:00
|
|
|
self.config_service_configs = {}
|
2019-12-06 17:10:50 +00:00
|
|
|
self.setup_bindings()
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context = tk.Menu(self.canvas)
|
|
|
|
themes.style_menu(self.context)
|
2019-12-06 17:10:50 +00:00
|
|
|
|
|
|
|
def setup_bindings(self):
|
2019-12-05 19:12:25 +00:00
|
|
|
self.canvas.tag_bind(self.id, "<Double-Button-1>", self.double_click)
|
|
|
|
self.canvas.tag_bind(self.id, "<Enter>", self.on_enter)
|
|
|
|
self.canvas.tag_bind(self.id, "<Leave>", self.on_leave)
|
2020-05-02 17:20:36 +01:00
|
|
|
self.canvas.tag_bind(self.id, "<ButtonRelease-3>", self.show_context)
|
2019-12-06 00:37:48 +00:00
|
|
|
|
2019-12-06 01:01:48 +00:00
|
|
|
def delete(self):
|
2020-01-30 00:08:36 +00:00
|
|
|
logging.debug("Delete canvas node for %s", self.core_node)
|
2019-12-06 01:01:48 +00:00
|
|
|
self.canvas.delete(self.id)
|
|
|
|
self.canvas.delete(self.text_id)
|
2020-02-24 18:58:01 +00:00
|
|
|
self.delete_antennas()
|
2019-12-06 01:01:48 +00:00
|
|
|
|
2019-12-06 00:37:48 +00:00
|
|
|
def add_antenna(self):
|
|
|
|
x, y = self.canvas.coords(self.id)
|
2020-02-17 23:14:52 +00:00
|
|
|
offset = len(self.antennas) * 8 * self.app.app_scale
|
|
|
|
img = Images.get(ImageEnum.ANTENNA, int(ANTENNA_SIZE * self.app.app_scale))
|
2019-12-06 00:37:48 +00:00
|
|
|
antenna_id = self.canvas.create_image(
|
|
|
|
x - 16 + offset,
|
2020-02-17 23:14:52 +00:00
|
|
|
y - int(23 * self.app.app_scale),
|
2019-12-06 00:37:48 +00:00
|
|
|
anchor=tk.CENTER,
|
2020-02-17 23:14:52 +00:00
|
|
|
image=img,
|
2019-12-09 22:13:21 +00:00
|
|
|
tags=tags.ANTENNA,
|
2019-12-06 00:37:48 +00:00
|
|
|
)
|
2020-02-17 23:14:52 +00:00
|
|
|
self.antennas.append(antenna_id)
|
|
|
|
self.antenna_images[antenna_id] = img
|
2019-12-06 00:37:48 +00:00
|
|
|
|
|
|
|
def delete_antenna(self):
|
|
|
|
"""
|
|
|
|
delete one antenna
|
|
|
|
"""
|
2020-01-30 00:08:36 +00:00
|
|
|
logging.debug("Delete an antenna on %s", self.core_node.name)
|
2020-02-17 23:14:52 +00:00
|
|
|
if self.antennas:
|
|
|
|
antenna_id = self.antennas.pop()
|
2019-12-06 00:37:48 +00:00
|
|
|
self.canvas.delete(antenna_id)
|
2020-02-17 23:14:52 +00:00
|
|
|
self.antenna_images.pop(antenna_id, None)
|
2019-12-06 00:37:48 +00:00
|
|
|
|
2020-01-30 00:08:36 +00:00
|
|
|
def delete_antennas(self):
|
2019-12-06 00:37:48 +00:00
|
|
|
"""
|
|
|
|
delete all antennas
|
|
|
|
"""
|
2020-01-30 00:08:36 +00:00
|
|
|
logging.debug("Remove all antennas for %s", self.core_node.name)
|
2020-02-17 23:14:52 +00:00
|
|
|
for antenna_id in self.antennas:
|
2019-12-06 00:37:48 +00:00
|
|
|
self.canvas.delete(antenna_id)
|
2020-02-17 23:14:52 +00:00
|
|
|
self.antennas.clear()
|
|
|
|
self.antenna_images.clear()
|
2019-12-06 00:37:48 +00:00
|
|
|
|
2019-12-05 19:12:25 +00:00
|
|
|
def redraw(self):
|
|
|
|
self.canvas.itemconfig(self.id, image=self.image)
|
|
|
|
self.canvas.itemconfig(self.text_id, text=self.core_node.name)
|
2020-03-04 19:38:24 +00:00
|
|
|
for edge in self.edges:
|
|
|
|
edge.redraw()
|
2019-12-05 19:12:25 +00:00
|
|
|
|
2019-12-10 22:33:52 +00:00
|
|
|
def _get_label_y(self):
|
|
|
|
image_box = self.canvas.bbox(self.id)
|
|
|
|
return image_box[3] + NODE_TEXT_OFFSET
|
|
|
|
|
2020-02-13 20:15:56 +00:00
|
|
|
def scale_text(self):
|
|
|
|
text_bound = self.canvas.bbox(self.text_id)
|
|
|
|
prev_y = (text_bound[3] + text_bound[1]) / 2
|
|
|
|
new_y = self._get_label_y()
|
|
|
|
self.canvas.move(self.text_id, 0, new_y - prev_y)
|
|
|
|
|
2020-01-11 00:22:21 +00:00
|
|
|
def move(self, x: int, y: int):
|
2019-12-12 19:06:52 +00:00
|
|
|
x, y = self.canvas.get_scaled_coords(x, y)
|
|
|
|
current_x, current_y = self.canvas.coords(self.id)
|
|
|
|
x_offset = x - current_x
|
|
|
|
y_offset = y - current_y
|
2019-12-10 22:33:52 +00:00
|
|
|
self.motion(x_offset, y_offset, update=False)
|
|
|
|
|
2020-01-11 00:22:21 +00:00
|
|
|
def motion(self, x_offset: int, y_offset: int, update: bool = True):
|
2019-12-13 17:28:51 +00:00
|
|
|
original_position = self.canvas.coords(self.id)
|
2019-12-05 19:12:25 +00:00
|
|
|
self.canvas.move(self.id, x_offset, y_offset)
|
2020-04-15 20:51:35 +01:00
|
|
|
pos = self.canvas.coords(self.id)
|
2019-12-13 17:28:51 +00:00
|
|
|
|
|
|
|
# check new position
|
|
|
|
bbox = self.canvas.bbox(self.id)
|
|
|
|
if not self.canvas.valid_position(*bbox):
|
|
|
|
self.canvas.coords(self.id, original_position)
|
|
|
|
return
|
|
|
|
|
|
|
|
# move test and selection box
|
2019-12-05 19:12:25 +00:00
|
|
|
self.canvas.move(self.text_id, x_offset, y_offset)
|
2019-12-10 22:33:52 +00:00
|
|
|
self.canvas.move_selection(self.id, x_offset, y_offset)
|
|
|
|
|
|
|
|
# move antennae
|
2020-02-17 23:14:52 +00:00
|
|
|
for antenna_id in self.antennas:
|
2019-12-10 22:33:52 +00:00
|
|
|
self.canvas.move(antenna_id, x_offset, y_offset)
|
|
|
|
|
|
|
|
# move edges
|
2019-12-05 19:12:25 +00:00
|
|
|
for edge in self.edges:
|
2020-04-15 20:51:35 +01:00
|
|
|
edge.move_node(self.id, pos)
|
2019-12-05 19:12:25 +00:00
|
|
|
for edge in self.wireless_edges:
|
2020-04-15 20:51:35 +01:00
|
|
|
edge.move_node(self.id, pos)
|
2019-12-10 22:33:52 +00:00
|
|
|
|
2019-12-12 19:06:52 +00:00
|
|
|
# set actual coords for node and update core is running
|
2020-04-15 20:51:35 +01:00
|
|
|
real_x, real_y = self.canvas.get_actual_coords(*pos)
|
2019-12-13 18:47:23 +00:00
|
|
|
self.core_node.position.x = real_x
|
|
|
|
self.core_node.position.y = real_y
|
2019-12-05 19:12:25 +00:00
|
|
|
if self.app.core.is_runtime() and update:
|
2019-12-10 22:33:52 +00:00
|
|
|
self.app.core.edit_node(self.core_node)
|
2019-12-05 19:12:25 +00:00
|
|
|
|
2020-01-11 00:22:21 +00:00
|
|
|
def on_enter(self, event: tk.Event):
|
2019-12-05 19:12:25 +00:00
|
|
|
if self.app.core.is_runtime() and self.app.core.observer:
|
|
|
|
self.tooltip.text.set("waiting...")
|
|
|
|
self.tooltip.on_enter(event)
|
2019-12-10 06:50:26 +00:00
|
|
|
try:
|
|
|
|
output = self.app.core.run(self.core_node.id)
|
|
|
|
self.tooltip.text.set(output)
|
|
|
|
except grpc.RpcError as e:
|
2020-02-05 23:09:33 +00:00
|
|
|
show_grpc_error(e, self.app, self.app)
|
2019-12-05 19:12:25 +00:00
|
|
|
|
2020-01-11 00:22:21 +00:00
|
|
|
def on_leave(self, event: tk.Event):
|
2019-12-05 19:12:25 +00:00
|
|
|
self.tooltip.on_leave(event)
|
|
|
|
|
2020-01-11 00:22:21 +00:00
|
|
|
def double_click(self, event: tk.Event):
|
2019-12-05 19:12:25 +00:00
|
|
|
if self.app.core.is_runtime():
|
|
|
|
self.canvas.core.launch_terminal(self.core_node.id)
|
|
|
|
else:
|
|
|
|
self.show_config()
|
|
|
|
|
2020-05-02 17:20:36 +01:00
|
|
|
def show_context(self, event: tk.Event) -> None:
|
|
|
|
# clear existing menu
|
|
|
|
self.context.delete(0, tk.END)
|
2019-12-06 17:10:50 +00:00
|
|
|
is_wlan = self.core_node.type == NodeType.WIRELESS_LAN
|
|
|
|
is_emane = self.core_node.type == NodeType.EMANE
|
|
|
|
if self.app.core.is_runtime():
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(label="Configure", command=self.show_config)
|
2019-12-13 19:48:36 +00:00
|
|
|
if is_wlan:
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(
|
|
|
|
label="WLAN Config", command=self.show_wlan_config
|
|
|
|
)
|
2019-12-06 17:10:50 +00:00
|
|
|
if is_wlan and self.core_node.id in self.app.core.mobility_players:
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(
|
2019-12-06 17:10:50 +00:00
|
|
|
label="Mobility Player", command=self.show_mobility_player
|
|
|
|
)
|
|
|
|
else:
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(label="Configure", command=self.show_config)
|
2019-12-06 19:17:05 +00:00
|
|
|
if NodeUtils.is_container_node(self.core_node.type):
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(label="Services", command=self.show_services)
|
|
|
|
self.context.add_command(
|
2020-01-20 20:17:11 +00:00
|
|
|
label="Config Services", command=self.show_config_services
|
|
|
|
)
|
2019-12-06 17:10:50 +00:00
|
|
|
if is_emane:
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(
|
2019-12-06 17:10:50 +00:00
|
|
|
label="EMANE Config", command=self.show_emane_config
|
|
|
|
)
|
|
|
|
if is_wlan:
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(
|
|
|
|
label="WLAN Config", command=self.show_wlan_config
|
|
|
|
)
|
|
|
|
self.context.add_command(
|
2019-12-06 17:10:50 +00:00
|
|
|
label="Mobility Config", command=self.show_mobility_config
|
|
|
|
)
|
2019-12-06 17:13:58 +00:00
|
|
|
if NodeUtils.is_wireless_node(self.core_node.type):
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_command(
|
2019-12-11 22:42:00 +00:00
|
|
|
label="Link To Selected", command=self.wireless_link_selected
|
|
|
|
)
|
2020-05-02 17:20:36 +01:00
|
|
|
unlink_menu = tk.Menu(self.context)
|
2020-05-02 07:36:33 +01:00
|
|
|
for edge in self.edges:
|
|
|
|
other_id = edge.src
|
|
|
|
if self.id == other_id:
|
|
|
|
other_id = edge.dst
|
|
|
|
other_node = self.canvas.nodes[other_id]
|
|
|
|
func_unlink = functools.partial(self.click_unlink, edge)
|
|
|
|
unlink_menu.add_command(
|
|
|
|
label=other_node.core_node.name, command=func_unlink
|
|
|
|
)
|
|
|
|
themes.style_menu(unlink_menu)
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_cascade(label="Unlink", menu=unlink_menu)
|
|
|
|
edit_menu = tk.Menu(self.context)
|
2020-03-27 05:24:23 +00:00
|
|
|
themes.style_menu(edit_menu)
|
2020-04-21 07:20:39 +01:00
|
|
|
edit_menu.add_command(label="Cut", command=self.click_cut)
|
2020-03-27 05:24:23 +00:00
|
|
|
edit_menu.add_command(label="Copy", command=self.canvas_copy)
|
|
|
|
edit_menu.add_command(label="Delete", command=self.canvas_delete)
|
2020-05-02 17:20:36 +01:00
|
|
|
self.context.add_cascade(label="Edit", menu=edit_menu)
|
|
|
|
self.context.tk_popup(event.x_root, event.y_root)
|
2019-12-06 17:10:50 +00:00
|
|
|
|
2020-04-21 07:20:39 +01:00
|
|
|
def click_cut(self) -> None:
|
|
|
|
self.canvas_copy()
|
|
|
|
self.canvas_delete()
|
|
|
|
|
2020-05-02 07:36:33 +01:00
|
|
|
def click_unlink(self, edge: CanvasEdge) -> None:
|
|
|
|
self.canvas.delete_edge(edge)
|
|
|
|
|
2020-03-27 05:24:23 +00:00
|
|
|
def canvas_delete(self) -> None:
|
|
|
|
self.canvas.clear_selection()
|
|
|
|
self.canvas.selection[self.id] = self
|
|
|
|
self.canvas.delete_selected_objects()
|
|
|
|
|
|
|
|
def canvas_copy(self) -> None:
|
|
|
|
self.canvas.clear_selection()
|
|
|
|
self.canvas.selection[self.id] = self
|
|
|
|
self.canvas.copy()
|
|
|
|
|
2019-12-05 19:12:25 +00:00
|
|
|
def show_config(self):
|
|
|
|
dialog = NodeConfigDialog(self.app, self.app, self)
|
|
|
|
dialog.show()
|
|
|
|
|
|
|
|
def show_wlan_config(self):
|
|
|
|
dialog = WlanConfigDialog(self.app, self.app, self)
|
2020-02-05 23:53:14 +00:00
|
|
|
if not dialog.has_error:
|
2020-02-05 23:09:33 +00:00
|
|
|
dialog.show()
|
2019-12-05 19:12:25 +00:00
|
|
|
|
|
|
|
def show_mobility_config(self):
|
|
|
|
dialog = MobilityConfigDialog(self.app, self.app, self)
|
2020-02-05 23:09:33 +00:00
|
|
|
if not dialog.has_error:
|
|
|
|
dialog.show()
|
2019-12-05 19:12:25 +00:00
|
|
|
|
|
|
|
def show_mobility_player(self):
|
|
|
|
mobility_player = self.app.core.mobility_players[self.core_node.id]
|
|
|
|
mobility_player.show()
|
|
|
|
|
|
|
|
def show_emane_config(self):
|
|
|
|
dialog = EmaneConfigDialog(self.app, self.app, self)
|
|
|
|
dialog.show()
|
2019-12-10 21:23:03 +00:00
|
|
|
|
|
|
|
def show_services(self):
|
2020-05-03 07:51:42 +01:00
|
|
|
dialog = NodeServiceDialog(self.app, self.app, self)
|
2019-12-10 21:23:03 +00:00
|
|
|
dialog.show()
|
2019-12-11 22:42:00 +00:00
|
|
|
|
2020-01-20 20:17:11 +00:00
|
|
|
def show_config_services(self):
|
2020-05-03 07:51:42 +01:00
|
|
|
dialog = NodeConfigServiceDialog(self.app, self.app, self)
|
2020-01-20 20:17:11 +00:00
|
|
|
dialog.show()
|
|
|
|
|
2020-01-14 19:06:52 +00:00
|
|
|
def has_emane_link(self, interface_id: int) -> core_pb2.Node:
|
2019-12-17 04:57:46 +00:00
|
|
|
result = None
|
|
|
|
for edge in self.edges:
|
|
|
|
if self.id == edge.src:
|
|
|
|
other_id = edge.dst
|
|
|
|
edge_interface_id = edge.src_interface.id
|
|
|
|
else:
|
|
|
|
other_id = edge.src
|
|
|
|
edge_interface_id = edge.dst_interface.id
|
|
|
|
if edge_interface_id != interface_id:
|
|
|
|
continue
|
|
|
|
other_node = self.canvas.nodes[other_id]
|
|
|
|
if other_node.core_node.type == NodeType.EMANE:
|
|
|
|
result = other_node.core_node
|
|
|
|
break
|
|
|
|
return result
|
|
|
|
|
2019-12-11 22:42:00 +00:00
|
|
|
def wireless_link_selected(self):
|
|
|
|
for canvas_nid in [
|
|
|
|
x for x in self.canvas.selection if "node" in self.canvas.gettags(x)
|
|
|
|
]:
|
|
|
|
core_node = self.canvas.nodes[canvas_nid].core_node
|
|
|
|
if core_node.type == core_pb2.NodeType.DEFAULT and core_node.model == "mdr":
|
|
|
|
self.canvas.create_edge(self, self.canvas.nodes[canvas_nid])
|
|
|
|
self.canvas.clear_selection()
|
2020-02-18 21:59:23 +00:00
|
|
|
|
2020-02-17 23:14:52 +00:00
|
|
|
def scale_antennas(self):
|
|
|
|
for i in range(len(self.antennas)):
|
|
|
|
antenna_id = self.antennas[i]
|
|
|
|
image = Images.get(
|
|
|
|
ImageEnum.ANTENNA, int(ANTENNA_SIZE * self.app.app_scale)
|
|
|
|
)
|
|
|
|
self.canvas.itemconfig(antenna_id, image=image)
|
|
|
|
self.antenna_images[antenna_id] = image
|
|
|
|
node_x, node_y = self.canvas.coords(self.id)
|
|
|
|
x, y = self.canvas.coords(antenna_id)
|
|
|
|
dx = node_x - 16 + (i * 8 * self.app.app_scale) - x
|
|
|
|
dy = node_y - int(23 * self.app.app_scale) - y
|
|
|
|
self.canvas.move(antenna_id, dx, dy)
|