refactoring to change core_grpc to core, and update CoreGrpc to be CoreClient and have core now be client, also refactored usages of application to be just app to keep it short

This commit is contained in:
bharnden 2019-11-01 13:42:49 -07:00
parent 1f230146a6
commit 6fa3beb1c1
14 changed files with 176 additions and 194 deletions

View file

@ -1,7 +1,7 @@
import logging import logging
import tkinter as tk import tkinter as tk
from coretk.coregrpc import CoreGrpc from coretk.coreclient import CoreClient
from coretk.coremenubar import CoreMenubar from coretk.coremenubar import CoreMenubar
from coretk.coretoolbar import CoreToolbar from coretk.coretoolbar import CoreToolbar
from coretk.graph import CanvasGraph from coretk.graph import CanvasGraph
@ -25,12 +25,12 @@ class Application(tk.Frame):
self.radiovar = tk.IntVar(value=1) self.radiovar = tk.IntVar(value=1)
self.show_grid_var = tk.IntVar(value=1) self.show_grid_var = tk.IntVar(value=1)
self.adjust_to_dim_var = tk.IntVar(value=0) self.adjust_to_dim_var = tk.IntVar(value=0)
self.core_grpc = CoreGrpc(self) self.core = CoreClient(self)
self.setup_app() self.setup_app()
self.create_menu() self.create_menu()
self.create_widgets() self.create_widgets()
self.draw_canvas() self.draw_canvas()
self.core_grpc.set_up() self.core.set_up()
def setup_app(self): def setup_app(self):
self.master.title("CORE") self.master.title("CORE")
@ -54,7 +54,7 @@ class Application(tk.Frame):
def draw_canvas(self): def draw_canvas(self):
self.canvas = CanvasGraph( self.canvas = CanvasGraph(
self, self.core_grpc, background="#cccccc", scrollregion=(0, 0, 1200, 1000) self, self.core, background="#cccccc", scrollregion=(0, 0, 1200, 1000)
) )
self.canvas.pack(fill=tk.BOTH, expand=True) self.canvas.pack(fill=tk.BOTH, expand=True)

View file

@ -56,13 +56,13 @@ class Edge:
self.interface_2 = None self.interface_2 = None
class CoreGrpc: class CoreClient:
def __init__(self, app, sid=None): def __init__(self, app):
""" """
Create a CoreGrpc instance Create a CoreGrpc instance
""" """
self.core = client.CoreGrpcClient() self.client = client.CoreGrpcClient()
self.session_id = sid self.session_id = None
self.node_ids = [] self.node_ids = []
self.app = app self.app = app
self.master = app.master self.master = app.master
@ -99,9 +99,9 @@ class CoreGrpc:
def join_session(self, session_id): def join_session(self, session_id):
# query session and set as current session # query session and set as current session
self.session_id = session_id self.session_id = session_id
response = self.core.get_session(self.session_id) response = self.client.get_session(self.session_id)
logging.info("joining session(%s): %s", self.session_id, response) logging.info("joining session(%s): %s", self.session_id, response)
self.core.events(self.session_id, self.handle_events) self.client.events(self.session_id, self.handle_events)
# set title to session # set title to session
self.master.title(f"CORE Session({self.session_id})") self.master.title(f"CORE Session({self.session_id})")
@ -129,7 +129,7 @@ class CoreGrpc:
:return: nothing :return: nothing
""" """
response = self.core.create_session() response = self.client.create_session()
logging.info("created session: %s", response) logging.info("created session: %s", response)
self.join_session(response.session_id) self.join_session(response.session_id)
@ -138,15 +138,15 @@ class CoreGrpc:
sid = self.session_id sid = self.session_id
else: else:
sid = custom_sid sid = custom_sid
response = self.core.delete_session(sid) response = self.client.delete_session(sid)
logging.info("Deleted session result: %s", response) logging.info("Deleted session result: %s", response)
def terminate_session(self, custom_sid=None): def shutdown_session(self, custom_sid=None):
if custom_sid is None: if custom_sid is None:
sid = self.session_id sid = self.session_id
else: else:
sid = custom_sid sid = custom_sid
s = self.core.get_session(sid).session s = self.client.get_session(sid).session
# delete links and nodes from running session # delete links and nodes from running session
if s.state == core_pb2.SessionState.RUNTIME: if s.state == core_pb2.SessionState.RUNTIME:
self.set_session_state("datacollect", sid) self.set_session_state("datacollect", sid)
@ -160,8 +160,8 @@ class CoreGrpc:
:return: existing sessions :return: existing sessions
""" """
self.core.connect() self.client.connect()
response = self.core.get_sessions() response = self.client.get_sessions()
# if there are no sessions, create a new session, else join a session # if there are no sessions, create a new session, else join a session
sessions = response.sessions sessions = response.sessions
@ -172,7 +172,7 @@ class CoreGrpc:
dialog.show() dialog.show()
def get_session_state(self): def get_session_state(self):
response = self.core.get_session(self.session_id) response = self.client.get_session(self.session_id)
# logging.info("get session: %s", response) # logging.info("get session: %s", response)
return response.session.state return response.session.state
@ -190,27 +190,29 @@ class CoreGrpc:
response = None response = None
if state == "configuration": if state == "configuration":
response = self.core.set_session_state( response = self.client.set_session_state(
sid, core_pb2.SessionState.CONFIGURATION sid, core_pb2.SessionState.CONFIGURATION
) )
elif state == "instantiation": elif state == "instantiation":
response = self.core.set_session_state( response = self.client.set_session_state(
sid, core_pb2.SessionState.INSTANTIATION sid, core_pb2.SessionState.INSTANTIATION
) )
elif state == "datacollect": elif state == "datacollect":
response = self.core.set_session_state( response = self.client.set_session_state(
sid, core_pb2.SessionState.DATACOLLECT sid, core_pb2.SessionState.DATACOLLECT
) )
elif state == "shutdown": elif state == "shutdown":
response = self.core.set_session_state(sid, core_pb2.SessionState.SHUTDOWN) response = self.client.set_session_state(
sid, core_pb2.SessionState.SHUTDOWN
)
elif state == "runtime": elif state == "runtime":
response = self.core.set_session_state(sid, core_pb2.SessionState.RUNTIME) response = self.client.set_session_state(sid, core_pb2.SessionState.RUNTIME)
elif state == "definition": elif state == "definition":
response = self.core.set_session_state( response = self.client.set_session_state(
sid, core_pb2.SessionState.DEFINITION sid, core_pb2.SessionState.DEFINITION
) )
elif state == "none": elif state == "none":
response = self.core.set_session_state(sid, core_pb2.SessionState.NONE) response = self.client.set_session_state(sid, core_pb2.SessionState.NONE)
else: else:
logging.error("coregrpc.py: set_session_state: INVALID STATE") logging.error("coregrpc.py: set_session_state: INVALID STATE")
@ -218,7 +220,7 @@ class CoreGrpc:
def edit_node(self, node_id, x, y): def edit_node(self, node_id, x, y):
position = core_pb2.Position(x=x, y=y) position = core_pb2.Position(x=x, y=y)
response = self.core.edit_node(self.session_id, node_id, position) response = self.client.edit_node(self.session_id, node_id, position)
logging.info("updated node id %s: %s", node_id, response) logging.info("updated node id %s: %s", node_id, response)
def delete_nodes(self, delete_session=None): def delete_nodes(self, delete_session=None):
@ -226,8 +228,8 @@ class CoreGrpc:
sid = self.session_id sid = self.session_id
else: else:
sid = delete_session sid = delete_session
for node in self.core.get_session(sid).session.nodes: for node in self.client.get_session(sid).session.nodes:
response = self.core.delete_node(self.session_id, node.id) response = self.client.delete_node(self.session_id, node.id)
logging.info("delete nodes %s", response) logging.info("delete nodes %s", response)
def delete_links(self, delete_session=None): def delete_links(self, delete_session=None):
@ -237,8 +239,8 @@ class CoreGrpc:
else: else:
sid = delete_session sid = delete_session
for link in self.core.get_session(sid).session.links: for link in self.client.get_session(sid).session.links:
response = self.core.delete_link( response = self.client.delete_link(
self.session_id, self.session_id,
link.node_one_id, link.node_one_id,
link.node_two_id, link.node_two_id,
@ -259,7 +261,7 @@ class CoreGrpc:
wlan_configs=None, wlan_configs=None,
mobility_configs=None, mobility_configs=None,
): ):
response = self.core.start_session( response = self.client.start_session(
session_id=self.session_id, session_id=self.session_id,
nodes=nodes, nodes=nodes,
links=links, links=links,
@ -268,7 +270,7 @@ class CoreGrpc:
logging.debug("Start session %s, result: %s", self.session_id, response.result) logging.debug("Start session %s, result: %s", self.session_id, response.result)
def stop_session(self): def stop_session(self):
response = self.core.stop_session(session_id=self.session_id) response = self.client.stop_session(session_id=self.session_id)
logging.debug("coregrpc.py Stop session, result: %s", response.result) logging.debug("coregrpc.py Stop session, result: %s", response.result)
# TODO no need, might get rid of this # TODO no need, might get rid of this
@ -285,11 +287,11 @@ class CoreGrpc:
""" """
if1 = self.create_interface(type1, edge.interface_1) if1 = self.create_interface(type1, edge.interface_1)
if2 = self.create_interface(type2, edge.interface_2) if2 = self.create_interface(type2, edge.interface_2)
response = self.core.add_link(self.session_id, id1, id2, if1, if2) response = self.client.add_link(self.session_id, id1, id2, if1, if2)
logging.info("created link: %s", response) logging.info("created link: %s", response)
def launch_terminal(self, node_id): def launch_terminal(self, node_id):
response = self.core.get_node_terminal(self.session_id, node_id) response = self.client.get_node_terminal(self.session_id, node_id)
logging.info("get terminal %s", response.terminal) logging.info("get terminal %s", response.terminal)
os.system("xterm -e %s &" % response.terminal) os.system("xterm -e %s &" % response.terminal)
@ -300,9 +302,9 @@ class CoreGrpc:
:param str file_path: file path that user pick :param str file_path: file path that user pick
:return: nothing :return: nothing
""" """
response = self.core.save_xml(self.session_id, file_path) response = self.client.save_xml(self.session_id, file_path)
logging.info("coregrpc.py save xml %s", response) logging.info("coregrpc.py save xml %s", response)
self.core.events(self.session_id, self.handle_events) self.client.events(self.session_id, self.handle_events)
def open_xml(self, file_path): def open_xml(self, file_path):
""" """
@ -311,7 +313,7 @@ class CoreGrpc:
:param str file_path: file to open :param str file_path: file to open
:return: session id :return: session id
""" """
response = self.core.open_xml(file_path) response = self.client.open_xml(file_path)
logging.debug("open xml: %s", response) logging.debug("open xml: %s", response)
self.join_session(response.session_id) self.join_session(response.session_id)
@ -322,7 +324,7 @@ class CoreGrpc:
:return: nothing :return: nothing
""" """
logging.debug("Close grpc") logging.debug("Close grpc")
self.core.close() self.client.close()
def peek_id(self): def peek_id(self):
""" """
@ -353,7 +355,7 @@ class CoreGrpc:
position = core_pb2.Position(x=x, y=y) position = core_pb2.Position(x=x, y=y)
node = core_pb2.Node(id=node_id, type=node_type, position=position, model=model) node = core_pb2.Node(id=node_id, type=node_type, position=position, model=model)
self.node_ids.append(node_id) self.node_ids.append(node_id)
response = self.core.add_node(self.session_id, node) response = self.client.add_node(self.session_id, node)
logging.info("created node: %s", response) logging.info("created node: %s", response)
if node_type == core_pb2.NodeType.WIRELESS_LAN: if node_type == core_pb2.NodeType.WIRELESS_LAN:
d = OrderedDict() d = OrderedDict()
@ -362,7 +364,7 @@ class CoreGrpc:
d["jitter"] = "0" d["jitter"] = "0"
d["delay"] = "20000" d["delay"] = "20000"
d["error"] = "0" d["error"] = "0"
r = self.core.set_wlan_config(self.session_id, node_id, d) r = self.client.set_wlan_config(self.session_id, node_id, d)
logging.debug("set wlan config %s", r) logging.debug("set wlan config %s", r)
return response.node_id return response.node_id

View file

@ -9,19 +9,19 @@ class CoreMenubar(object):
Core menubar Core menubar
""" """
def __init__(self, application, master, menubar): def __init__(self, app, master, menubar):
""" """
Create a CoreMenubar instance Create a CoreMenubar instance
:param master: :param master:
:param tkinter.Menu menubar: menubar object :param tkinter.Menu menubar: menubar object
:param coretk.app.Application application: application object :param coretk.app.Application app: application object
""" """
self.menubar = menubar self.menubar = menubar
self.master = master self.master = master
self.application = application self.app = app
self.menuaction = action.MenuAction(application, master) self.menuaction = action.MenuAction(app, master)
self.menu_action = MenuAction(self.application, self.master) self.menu_action = MenuAction(self.app, self.master)
# def on_quit(self): # def on_quit(self):
# """ # """
@ -649,23 +649,23 @@ class CoreMenubar(object):
:return: nothing :return: nothing
""" """
self.application.bind_all("<Control-n>", action.file_new_shortcut) self.app.bind_all("<Control-n>", action.file_new_shortcut)
self.application.bind_all("<Control-o>", action.file_open_shortcut) self.app.bind_all("<Control-o>", action.file_open_shortcut)
self.application.bind_all("<Control-s>", action.file_save_shortcut) self.app.bind_all("<Control-s>", action.file_save_shortcut)
self.application.bind_all("<Control-z>", action.edit_undo_shortcut) self.app.bind_all("<Control-z>", action.edit_undo_shortcut)
self.application.bind_all("<Control-y>", action.edit_redo_shortcut) self.app.bind_all("<Control-y>", action.edit_redo_shortcut)
self.application.bind_all("<Control-x>", action.edit_cut_shortcut) self.app.bind_all("<Control-x>", action.edit_cut_shortcut)
self.application.bind_all("<Control-c>", action.edit_copy_shortcut) self.app.bind_all("<Control-c>", action.edit_copy_shortcut)
self.application.bind_all("<Control-v>", action.edit_paste_shortcut) self.app.bind_all("<Control-v>", action.edit_paste_shortcut)
self.application.bind_all("<Control-a>", action.edit_select_all_shortcut) self.app.bind_all("<Control-a>", action.edit_select_all_shortcut)
self.application.bind_all("<Control-j>", action.edit_select_adjacent_shortcut) self.app.bind_all("<Control-j>", action.edit_select_adjacent_shortcut)
self.application.bind_all("<Control-f>", action.edit_find_shortcut) self.app.bind_all("<Control-f>", action.edit_find_shortcut)
self.application.bind_all("<Prior>", action.canvas_previous_shortcut) self.app.bind_all("<Prior>", action.canvas_previous_shortcut)
self.application.bind_all("<Next>", action.canvas_next_shortcut) self.app.bind_all("<Next>", action.canvas_next_shortcut)
self.application.bind_all("<Home>", action.canvas_first_shortcut) self.app.bind_all("<Home>", action.canvas_first_shortcut)
self.application.bind_all("<End>", action.canvas_last_shortcut) self.app.bind_all("<End>", action.canvas_last_shortcut)
self.application.bind_all("<Control-Shift-plus>", action.view_zoom_in_shortcut) self.app.bind_all("<Control-Shift-plus>", action.view_zoom_in_shortcut)
self.application.bind_all("<Control-minus>", action.view_zoom_out_shortcut) self.app.bind_all("<Control-minus>", action.view_zoom_out_shortcut)
def create_core_menubar(self): def create_core_menubar(self):
""" """

View file

@ -25,14 +25,14 @@ class CoreToolbar(object):
Core toolbar class Core toolbar class
""" """
def __init__(self, application, edit_frame, menubar): def __init__(self, app, edit_frame, menubar):
""" """
Create a CoreToolbar instance Create a CoreToolbar instance
:param tkinter.Frame edit_frame: edit frame :param tkinter.Frame edit_frame: edit frame
""" """
self.application = application self.app = app
self.master = application.master self.master = app.master
self.edit_frame = edit_frame self.edit_frame = edit_frame
self.menubar = menubar self.menubar = menubar
self.radio_value = tk.IntVar() self.radio_value = tk.IntVar()
@ -161,7 +161,7 @@ class CoreToolbar(object):
:return: nothing :return: nothing
""" """
logging.debug("Click START STOP SESSION button") logging.debug("Click START STOP SESSION button")
helper = CoreToolbarHelp(self.application) helper = CoreToolbarHelp(self.app)
self.destroy_children_widgets() self.destroy_children_widgets()
self.canvas.mode = GraphMode.SELECT self.canvas.mode = GraphMode.SELECT
@ -593,11 +593,7 @@ class CoreToolbar(object):
""" """
logging.debug("Click on STOP button ") logging.debug("Click on STOP button ")
self.destroy_children_widgets() self.destroy_children_widgets()
self.app.core.stop_session()
# self.canvas.core_grpc.set_session_state(SessionStateEnum.DATACOLLECT.value)
# self.canvas.core_grpc.delete_links()
# self.canvas.core_grpc.delete_nodes()
self.canvas.core_grpc.stop_session()
self.create_toolbar() self.create_toolbar()
def click_run_button(self): def click_run_button(self):

View file

@ -15,8 +15,7 @@ class CoreToolbarHelp:
:return: nothing :return: nothing
""" """
nodes = [] nodes = []
core = self.app.core_grpc for node in self.app.core.nodes.values():
for node in core.nodes.values():
pos = core_pb2.Position(x=int(node.x), y=int(node.y)) pos = core_pb2.Position(x=int(node.x), y=int(node.y))
n = core_pb2.Node( n = core_pb2.Node(
id=node.node_id, type=node.type, position=pos, model=node.model id=node.node_id, type=node.type, position=pos, model=node.model
@ -32,14 +31,9 @@ class CoreToolbarHelp:
:return: list of protobuf links :return: list of protobuf links
""" """
links = [] links = []
core = self.app.core_grpc for edge in self.app.core.edges.values():
for edge in core.edges.values(): interface_one = self.app.core.create_interface(edge.type1, edge.interface_1)
interface_one = self.app.core_grpc.create_interface( interface_two = self.app.core.create_interface(edge.type2, edge.interface_2)
edge.type1, edge.interface_1
)
interface_two = self.app.core_grpc.create_interface(
edge.type2, edge.interface_2
)
# TODO for now only consider the basic cases # TODO for now only consider the basic cases
if ( if (
edge.type1 == core_pb2.NodeType.WIRELESS_LAN edge.type1 == core_pb2.NodeType.WIRELESS_LAN
@ -61,8 +55,7 @@ class CoreToolbarHelp:
def get_wlan_configuration_list(self): def get_wlan_configuration_list(self):
configs = [] configs = []
core = self.app.core_grpc manager_configs = self.app.core.wlanconfig_management.configurations
manager_configs = core.wlanconfig_management.configurations
for key in manager_configs: for key in manager_configs:
cnf = core_pb2.WlanConfig(node_id=key, config=manager_configs[key]) cnf = core_pb2.WlanConfig(node_id=key, config=manager_configs[key])
configs.append(cnf) configs.append(cnf)
@ -72,4 +65,4 @@ class CoreToolbarHelp:
nodes = self.get_node_list() nodes = self.get_node_list()
links = self.get_link_list() links = self.get_link_list()
wlan_configs = self.get_wlan_configuration_list() wlan_configs = self.get_wlan_configuration_list()
self.app.core_grpc.start_session(nodes, links, wlan_configs=wlan_configs) self.app.core.start_session(nodes, links, wlan_configs=wlan_configs)

View file

@ -18,8 +18,8 @@ class SessionOptionsDialog(Dialog):
self.draw() self.draw()
def draw(self): def draw(self):
session_id = self.master.core_grpc.session_id session_id = self.app.core.session_id
response = self.master.core_grpc.core.get_session_options(session_id) response = self.app.core.client.get_session_options(session_id)
logging.info("session options: %s", response) logging.info("session options: %s", response)
self.options = response.config self.options = response.config
self.values = configutils.create_config(self, self.options, PAD_X, PAD_Y) self.values = configutils.create_config(self, self.options, PAD_X, PAD_Y)
@ -28,7 +28,7 @@ class SessionOptionsDialog(Dialog):
def save(self): def save(self):
config = configutils.parse_config(self.options, self.values) config = configutils.parse_config(self.options, self.values)
session_id = self.master.core_grpc.session_id session_id = self.app.core.session_id
response = self.master.core_grpc.core.set_session_options(session_id, config) response = self.app.core.client.set_session_options(session_id, config)
logging.info("saved session config: %s", response) logging.info("saved session config: %s", response)
self.destroy() self.destroy()

View file

@ -12,7 +12,7 @@ class SessionsDialog(Dialog):
""" """
create session table instance create session table instance
:param coretk.coregrpc.CoreGrpc grpc: coregrpc :param coretk.coreclient.CoreClient grpc: coregrpc
:param root.master master: :param root.master master:
""" """
super().__init__(master, app, "Sessions", modal=True) super().__init__(master, app, "Sessions", modal=True)
@ -51,7 +51,7 @@ class SessionsDialog(Dialog):
self.tree.column("nodes", stretch=tk.YES) self.tree.column("nodes", stretch=tk.YES)
self.tree.heading("nodes", text="Node Count") self.tree.heading("nodes", text="Node Count")
response = self.app.core_grpc.core.get_sessions() response = self.app.core.client.get_sessions()
logging.info("sessions: %s", response) logging.info("sessions: %s", response)
for index, session in enumerate(response.sessions): for index, session in enumerate(response.sessions):
state_name = core_pb2.SessionState.Enum.Name(session.state) state_name = core_pb2.SessionState.Enum.Name(session.state)
@ -105,7 +105,7 @@ class SessionsDialog(Dialog):
b.grid(row=0, column=3, padx=2, sticky="ew") b.grid(row=0, column=3, padx=2, sticky="ew")
def click_new(self): def click_new(self):
self.app.core_grpc.create_new_session() self.app.core.create_new_session()
self.destroy() self.destroy()
def click_select(self, event): def click_select(self, event):
@ -142,7 +142,7 @@ class SessionsDialog(Dialog):
logging.error("querysessiondrawing.py invalid state") logging.error("querysessiondrawing.py invalid state")
def join_session(self, session_id): def join_session(self, session_id):
self.app.core_grpc.join_session(session_id) self.app.core.join_session(session_id)
self.destroy() self.destroy()
def on_selected(self, event): def on_selected(self, event):
@ -151,6 +151,6 @@ class SessionsDialog(Dialog):
self.join_session(sid) self.join_session(sid)
def shutdown_session(self, sid): def shutdown_session(self, sid):
self.app.core_grpc.terminate_session(sid) self.app.core.shutdown_session(sid)
self.click_new() self.click_new()
self.destroy() self.destroy()

View file

@ -26,7 +26,7 @@ CORE_EMANE = ["emane"]
class CanvasGraph(tk.Canvas): class CanvasGraph(tk.Canvas):
def __init__(self, master, core_grpc, cnf=None, **kwargs): def __init__(self, master, core, cnf=None, **kwargs):
if cnf is None: if cnf is None:
cnf = {} cnf = {}
kwargs["highlightthickness"] = 0 kwargs["highlightthickness"] = 0
@ -45,10 +45,10 @@ class CanvasGraph(tk.Canvas):
self.setup_menus() self.setup_menus()
self.setup_bindings() self.setup_bindings()
self.draw_grid() self.draw_grid()
self.core_grpc = core_grpc self.core = core
self.helper = GraphHelper(self, core_grpc) self.helper = GraphHelper(self, core)
self.throughput_draw = Throughput(self, core_grpc) self.throughput_draw = Throughput(self, core)
self.wireless_draw = WirelessConnection(self, core_grpc) self.wireless_draw = WirelessConnection(self, core)
self.is_node_context_opened = False self.is_node_context_opened = False
def setup_menus(self): def setup_menus(self):
@ -147,7 +147,7 @@ class CanvasGraph(tk.Canvas):
core_id_to_canvas_id[node.id] = n.id core_id_to_canvas_id[node.id] = n.id
# store the node in grpc manager # store the node in grpc manager
self.core_grpc.add_preexisting_node(n, session.id, node, name) self.core.add_preexisting_node(n, session.id, node, name)
# draw existing links # draw existing links
for link in session.links: for link in session.links:
@ -179,7 +179,7 @@ class CanvasGraph(tk.Canvas):
n1.edges.add(e) n1.edges.add(e)
n2.edges.add(e) n2.edges.add(e)
self.edges[e.token] = e self.edges[e.token] = e
self.core_grpc.add_edge(session.id, e.token, n1.id, n2.id) self.core.add_edge(session.id, e.token, n1.id, n2.id)
self.helper.redraw_antenna(link, n1, n2) self.helper.redraw_antenna(link, n1, n2)
@ -208,14 +208,14 @@ class CanvasGraph(tk.Canvas):
# TODO will include throughput and ipv6 in the future # TODO will include throughput and ipv6 in the future
if1 = Interface(grpc_if1.name, grpc_if1.ip4, ifid=grpc_if1.id) if1 = Interface(grpc_if1.name, grpc_if1.ip4, ifid=grpc_if1.id)
if2 = Interface(grpc_if2.name, grpc_if2.ip4, ifid=grpc_if2.id) if2 = Interface(grpc_if2.name, grpc_if2.ip4, ifid=grpc_if2.id)
self.core_grpc.edges[e.token].interface_1 = if1 self.core.edges[e.token].interface_1 = if1
self.core_grpc.edges[e.token].interface_2 = if2 self.core.edges[e.token].interface_2 = if2
self.core_grpc.nodes[ self.core.nodes[core_id_to_canvas_id[link.node_one_id]].interfaces.append(
core_id_to_canvas_id[link.node_one_id] if1
].interfaces.append(if1) )
self.core_grpc.nodes[ self.core.nodes[core_id_to_canvas_id[link.node_two_id]].interfaces.append(
core_id_to_canvas_id[link.node_two_id] if2
].interfaces.append(if2) )
# lift the nodes so they on top of the links # lift the nodes so they on top of the links
for i in self.find_withtag("node"): for i in self.find_withtag("node"):
@ -318,13 +318,13 @@ class CanvasGraph(tk.Canvas):
node_dst = self.nodes[edge.dst] node_dst = self.nodes[edge.dst]
node_dst.edges.add(edge) node_dst.edges.add(edge)
self.core_grpc.add_edge( self.core.add_edge(
self.core_grpc.session_id, edge.token, node_src.id, node_dst.id self.core.session_id, edge.token, node_src.id, node_dst.id
) )
# draw link info on the edge # draw link info on the edge
if1 = self.core_grpc.edges[edge.token].interface_1 if1 = self.core.edges[edge.token].interface_1
if2 = self.core_grpc.edges[edge.token].interface_2 if2 = self.core.edges[edge.token].interface_2
ip4_and_prefix_1 = None ip4_and_prefix_1 = None
ip4_and_prefix_2 = None ip4_and_prefix_2 = None
if if1 is not None: if if1 is not None:
@ -390,12 +390,10 @@ class CanvasGraph(tk.Canvas):
image=image, image=image,
node_type=node_name, node_type=node_name,
canvas=self, canvas=self,
core_id=self.core_grpc.peek_id(), core_id=self.core.peek_id(),
) )
self.nodes[node.id] = node self.nodes[node.id] = node
self.core_grpc.add_graph_node( self.core.add_graph_node(self.core.session_id, node.id, x, y, node_name)
self.core_grpc.session_id, node.id, x, y, node_name
)
return node return node
@ -485,10 +483,10 @@ class CanvasNode:
self.moving = None self.moving = None
def double_click(self, event): def double_click(self, event):
node_id = self.canvas.core_grpc.nodes[self.id].node_id node_id = self.canvas.core.nodes[self.id].node_id
state = self.canvas.core_grpc.get_session_state() state = self.canvas.core.get_session_state()
if state == core_pb2.SessionState.RUNTIME: if state == core_pb2.SessionState.RUNTIME:
self.canvas.core_grpc.launch_terminal(node_id) self.canvas.core.launch_terminal(node_id)
else: else:
self.canvas.canvas_action.display_configuration(self) self.canvas.canvas_action.display_configuration(self)
# if self.node_type in CORE_NODES: # if self.node_type in CORE_NODES:
@ -511,7 +509,7 @@ class CanvasNode:
def click_release(self, event): def click_release(self, event):
logging.debug(f"click release {self.name}: {event}") logging.debug(f"click release {self.name}: {event}")
self.update_coords() self.update_coords()
self.canvas.core_grpc.update_node_location(self.id, self.x_coord, self.y_coord) self.canvas.core.update_node_location(self.id, self.x_coord, self.y_coord)
self.moving = None self.moving = None
def motion(self, event): def motion(self, event):
@ -529,8 +527,8 @@ class CanvasNode:
new_x, new_y = self.canvas.coords(self.id) new_x, new_y = self.canvas.coords(self.id)
if self.canvas.core_grpc.get_session_state() == core_pb2.SessionState.RUNTIME: if self.canvas.core.get_session_state() == core_pb2.SessionState.RUNTIME:
self.canvas.core_grpc.edit_node(self.core_id, int(new_x), int(new_y)) self.canvas.core.edit_node(self.core_id, int(new_x), int(new_y))
for edge in self.edges: for edge in self.edges:
x1, y1, x2, y2 = self.canvas.coords(edge.id) x1, y1, x2, y2 = self.canvas.coords(edge.id)

View file

@ -11,12 +11,12 @@ CANVAS_COMPONENT_TAGS = ["edge", "node", "nodename", "wallpaper", "linkinfo"]
class GraphHelper: class GraphHelper:
def __init__(self, canvas, grpc): def __init__(self, canvas, core):
""" """
create an instance of GraphHelper object create an instance of GraphHelper object
""" """
self.canvas = canvas self.canvas = canvas
self.core_grpc = grpc self.core = core
def delete_canvas_components(self): def delete_canvas_components(self):
""" """

View file

@ -22,7 +22,7 @@ class LinkInfo:
self.edge = edge self.edge = edge
# self.edge_id = edge.id # self.edge_id = edge.id
self.radius = 37 self.radius = 37
self.core_grpc = self.canvas.core_grpc self.core = self.canvas.core
self.ip4_address_1 = ip4_src self.ip4_address_1 = ip4_src
self.ip6_address_1 = ip6_src self.ip6_address_1 = ip6_src
@ -104,13 +104,13 @@ class LinkInfo:
class Throughput: class Throughput:
def __init__(self, canvas, core_grpc): def __init__(self, canvas, core):
""" """
create an instance of Throughput object create an instance of Throughput object
:param coretk.app.Application app: application :param coretk.app.Application app: application
""" """
self.canvas = canvas self.canvas = canvas
self.core_grpc = core_grpc self.core = core
# edge canvas id mapped to throughput value # edge canvas id mapped to throughput value
self.tracker = {} self.tracker = {}
# map an edge canvas id to a throughput canvas id # map an edge canvas id to a throughput canvas id
@ -130,9 +130,7 @@ class Throughput:
iid = t.interface_id iid = t.interface_id
tp = t.throughput tp = t.throughput
# token = self.grpc_manager.node_id_and_interface_to_edge_token[nid, iid] # token = self.grpc_manager.node_id_and_interface_to_edge_token[nid, iid]
token = self.core_grpc.core_mapping.get_token_from_node_and_interface( token = self.core.core_mapping.get_token_from_node_and_interface(nid, iid)
nid, iid
)
print(token) print(token)
edge_id = self.canvas.edges[token].id edge_id = self.canvas.edges[token].id

View file

@ -321,10 +321,9 @@ class MenuAction:
Actions performed when choosing menu items Actions performed when choosing menu items
""" """
def __init__(self, application, master): def __init__(self, app, master):
self.master = master self.master = master
self.application = application self.app = app
self.core_grpc = application.core_grpc
def prompt_save_running_session(self): def prompt_save_running_session(self):
""" """
@ -335,21 +334,20 @@ class MenuAction:
logging.info( logging.info(
"menuaction.py: clean_nodes_links_and_set_configuration() Exiting the program" "menuaction.py: clean_nodes_links_and_set_configuration() Exiting the program"
) )
grpc = self.application.core_grpc state = self.app.core.get_session_state()
state = grpc.get_session_state()
if ( if (
state == core_pb2.SessionState.SHUTDOWN state == core_pb2.SessionState.SHUTDOWN
or state == core_pb2.SessionState.DEFINITION or state == core_pb2.SessionState.DEFINITION
): ):
grpc.delete_session() self.app.core.delete_session()
else: else:
msgbox = messagebox.askyesnocancel("stop", "Stop the running session?") msgbox = messagebox.askyesnocancel("stop", "Stop the running session?")
if msgbox or msgbox is False: if msgbox or msgbox is False:
if msgbox: if msgbox:
grpc.stop_session() self.app.core.stop_session()
grpc.delete_session() self.app.core.delete_session()
def on_quit(self): def on_quit(self):
""" """
@ -358,23 +356,21 @@ class MenuAction:
:return: nothing :return: nothing
""" """
self.prompt_save_running_session() self.prompt_save_running_session()
# self.application.core_grpc.close() self.app.quit()
self.application.quit()
def file_save_as_xml(self): def file_save_as_xml(self):
logging.info("menuaction.py file_save_as_xml()") logging.info("menuaction.py file_save_as_xml()")
grpc = self.application.core_grpc
file_path = filedialog.asksaveasfilename( file_path = filedialog.asksaveasfilename(
initialdir=SAVEDIR, initialdir=SAVEDIR,
title="Save As", title="Save As",
filetypes=(("EmulationScript XML files", "*.xml"), ("All files", "*")), filetypes=(("EmulationScript XML files", "*.xml"), ("All files", "*")),
defaultextension=".xml", defaultextension=".xml",
) )
grpc.save_xml(file_path) self.app.core.save_xml(file_path)
def file_open_xml(self): def file_open_xml(self):
logging.info("menuaction.py file_open_xml()") logging.info("menuaction.py file_open_xml()")
self.application.is_open_xml = True self.app.is_open_xml = True
file_path = filedialog.askopenfilename( file_path = filedialog.askopenfilename(
initialdir=SAVEDIR, initialdir=SAVEDIR,
title="Open", title="Open",
@ -382,17 +378,17 @@ class MenuAction:
) )
# clean up before opening a new session # clean up before opening a new session
self.prompt_save_running_session() self.prompt_save_running_session()
self.application.core_grpc.open_xml(file_path) self.app.core.open_xml(file_path)
# Todo might not need # Todo might not need
# self.application.core_editbar.destroy_children_widgets() # self.application.core_editbar.destroy_children_widgets()
# self.application.core_editbar.create_toolbar() # self.application.core_editbar.create_toolbar()
def canvas_size_and_scale(self): def canvas_size_and_scale(self):
self.application.size_and_scale = SizeAndScale(self.application) self.app.size_and_scale = SizeAndScale(self.app)
def canvas_set_wallpaper(self): def canvas_set_wallpaper(self):
self.application.set_wallpaper = CanvasWallpaper(self.application) self.app.set_wallpaper = CanvasWallpaper(self.app)
def help_core_github(self): def help_core_github(self):
webbrowser.open_new("https://github.com/coreemu/core") webbrowser.open_new("https://github.com/coreemu/core")
@ -402,10 +398,10 @@ class MenuAction:
def session_options(self): def session_options(self):
logging.debug("Click session options") logging.debug("Click session options")
dialog = SessionOptionsDialog(self.application, self.application) dialog = SessionOptionsDialog(self.app, self.app)
dialog.show() dialog.show()
def session_change_sessions(self): def session_change_sessions(self):
logging.debug("Click session change sessions") logging.debug("Click session change sessions")
dialog = SessionsDialog(self.application, self.application) dialog = SessionsDialog(self.app, self.app)
dialog.show() dialog.show()

View file

@ -22,24 +22,24 @@ class ScaleOption(enum.Enum):
class CanvasWallpaper: class CanvasWallpaper:
def __init__(self, application): def __init__(self, app):
""" """
create an instance of CanvasWallpaper object create an instance of CanvasWallpaper object
:param coretk.app.Application application: root application :param coretk.app.Application app: root application
""" """
self.application = application self.app = app
self.canvas = self.application.canvas self.canvas = self.app.canvas
self.top = tk.Toplevel() self.top = tk.Toplevel()
self.top.title("Set Canvas Wallpaper") self.top.title("Set Canvas Wallpaper")
self.radiovar = tk.IntVar() self.radiovar = tk.IntVar()
print(self.application.radiovar.get()) print(self.app.radiovar.get())
self.radiovar.set(self.application.radiovar.get()) self.radiovar.set(self.app.radiovar.get())
self.show_grid_var = tk.IntVar() self.show_grid_var = tk.IntVar()
self.show_grid_var.set(self.application.show_grid_var.get()) self.show_grid_var.set(self.app.show_grid_var.get())
self.adjust_to_dim_var = tk.IntVar() self.adjust_to_dim_var = tk.IntVar()
self.adjust_to_dim_var.set(self.application.adjust_to_dim_var.get()) self.adjust_to_dim_var.set(self.app.adjust_to_dim_var.get())
self.create_image_label() self.create_image_label()
self.create_text_label() self.create_text_label()
@ -181,7 +181,7 @@ class CanvasWallpaper:
:return: nothing :return: nothing
""" """
canvas = self.application.canvas canvas = self.app.canvas
grid = canvas.find_withtag("rectangle")[0] grid = canvas.find_withtag("rectangle")[0]
x0, y0, x1, y1 = canvas.coords(grid) x0, y0, x1, y1 = canvas.coords(grid)
canvas_w = abs(x0 - x1) canvas_w = abs(x0 - x1)
@ -215,7 +215,7 @@ class CanvasWallpaper:
cropped_tk = ImageTk.PhotoImage(cropped) cropped_tk = ImageTk.PhotoImage(cropped)
# place left corner of image to the left corner of the canvas # place left corner of image to the left corner of the canvas
self.application.croppedwallpaper = cropped_tk self.app.croppedwallpaper = cropped_tk
self.delete_canvas_components(["wallpaper"]) self.delete_canvas_components(["wallpaper"])
# self.delete_previous_wallpaper() # self.delete_previous_wallpaper()
@ -223,7 +223,7 @@ class CanvasWallpaper:
wid = self.canvas.create_image( wid = self.canvas.create_image(
(cropx / 2, cropy / 2), image=cropped_tk, tags="wallpaper" (cropx / 2, cropy / 2), image=cropped_tk, tags="wallpaper"
) )
self.application.wallpaper_id = wid self.app.wallpaper_id = wid
def center(self, img): def center(self, img):
""" """
@ -252,13 +252,13 @@ class CanvasWallpaper:
cropped_tk = ImageTk.PhotoImage(cropped) cropped_tk = ImageTk.PhotoImage(cropped)
# place the center of the image at the center of the canvas # place the center of the image at the center of the canvas
self.application.croppedwallpaper = cropped_tk self.app.croppedwallpaper = cropped_tk
self.delete_canvas_components(["wallpaper"]) self.delete_canvas_components(["wallpaper"])
# self.delete_previous_wallpaper() # self.delete_previous_wallpaper()
wid = self.canvas.create_image( wid = self.canvas.create_image(
(canvas_w / 2, canvas_h / 2), image=cropped_tk, tags="wallpaper" (canvas_w / 2, canvas_h / 2), image=cropped_tk, tags="wallpaper"
) )
self.application.wallpaper_id = wid self.app.wallpaper_id = wid
def scaled(self, img): def scaled(self, img):
""" """
@ -270,7 +270,7 @@ class CanvasWallpaper:
canvas_w, canvas_h = self.get_canvas_width_and_height() canvas_w, canvas_h = self.get_canvas_width_and_height()
resized_image = img.resize((int(canvas_w), int(canvas_h)), Image.ANTIALIAS) resized_image = img.resize((int(canvas_w), int(canvas_h)), Image.ANTIALIAS)
image_tk = ImageTk.PhotoImage(resized_image) image_tk = ImageTk.PhotoImage(resized_image)
self.application.croppedwallpaper = image_tk self.app.croppedwallpaper = image_tk
self.delete_canvas_components(["wallpaper"]) self.delete_canvas_components(["wallpaper"])
# self.delete_previous_wallpaper() # self.delete_previous_wallpaper()
@ -278,7 +278,7 @@ class CanvasWallpaper:
wid = self.canvas.create_image( wid = self.canvas.create_image(
(canvas_w / 2, canvas_h / 2), image=image_tk, tags="wallpaper" (canvas_w / 2, canvas_h / 2), image=image_tk, tags="wallpaper"
) )
self.application.wallpaper_id = wid self.app.wallpaper_id = wid
def tiled(self, img): def tiled(self, img):
return return
@ -301,15 +301,15 @@ class CanvasWallpaper:
self.delete_canvas_components(["wallpaper"]) self.delete_canvas_components(["wallpaper"])
self.draw_new_canvas(img_w, img_h) self.draw_new_canvas(img_w, img_h)
wid = self.canvas.create_image((img_w / 2, img_h / 2), image=image_tk) wid = self.canvas.create_image((img_w / 2, img_h / 2), image=image_tk)
self.application.croppedwallpaper = image_tk self.app.croppedwallpaper = image_tk
self.application.wallpaper_id = wid self.app.wallpaper_id = wid
def show_grid(self): def show_grid(self):
""" """
:return: nothing :return: nothing
""" """
self.application.adjust_to_dim_var.set(self.adjust_to_dim_var.get()) self.app.adjust_to_dim_var.set(self.adjust_to_dim_var.get())
if self.show_grid_var.get() == 0: if self.show_grid_var.get() == 0:
for i in self.canvas.find_withtag("gridline"): for i in self.canvas.find_withtag("gridline"):
@ -322,9 +322,9 @@ class CanvasWallpaper:
logging.error("setwallpaper.py show_grid invalid value") logging.error("setwallpaper.py show_grid invalid value")
def save_wallpaper_options(self): def save_wallpaper_options(self):
self.application.radiovar.set(self.radiovar.get()) self.app.radiovar.set(self.radiovar.get())
self.application.show_grid_var.set(self.show_grid_var.get()) self.app.show_grid_var.set(self.show_grid_var.get())
self.application.adjust_to_dim_var.set(self.adjust_to_dim_var.get()) self.app.adjust_to_dim_var.set(self.adjust_to_dim_var.get())
def click_apply(self): def click_apply(self):
img_link_frame = self.top.grid_slaves(2, 0)[0] img_link_frame = self.top.grid_slaves(2, 0)[0]
@ -332,23 +332,23 @@ class CanvasWallpaper:
if not filename: if not filename:
self.delete_canvas_components(["wallpaper"]) self.delete_canvas_components(["wallpaper"])
self.top.destroy() self.top.destroy()
self.application.current_wallpaper = None self.app.current_wallpaper = None
self.save_wallpaper_options() self.save_wallpaper_options()
return return
try: try:
img = Image.open(filename) img = Image.open(filename)
self.application.current_wallpaper = img self.app.current_wallpaper = img
except FileNotFoundError: except FileNotFoundError:
print("invalid filename, draw original white plot") print("invalid filename, draw original white plot")
if self.application.wallpaper_id: if self.app.wallpaper_id:
self.canvas.delete(self.application.wallpaper_id) self.canvas.delete(self.app.wallpaper_id)
self.top.destroy() self.top.destroy()
return return
self.application.adjust_to_dim_var.set(self.adjust_to_dim_var.get()) self.app.adjust_to_dim_var.set(self.adjust_to_dim_var.get())
if self.adjust_to_dim_var.get() == 0: if self.adjust_to_dim_var.get() == 0:
self.application.radiovar.set(self.radiovar.get()) self.app.radiovar.set(self.radiovar.get())
if self.radiovar.get() == ScaleOption.UPPER_LEFT.value: if self.radiovar.get() == ScaleOption.UPPER_LEFT.value:
self.upper_left(img) self.upper_left(img)

View file

@ -10,16 +10,16 @@ DRAW_OBJECT_TAGS = ["edge", "node", "nodename", "linkinfo", "antenna"]
class SizeAndScale: class SizeAndScale:
def __init__(self, application): def __init__(self, app):
""" """
create an instance for size and scale object create an instance for size and scale object
:param application: main application :param app: main application
""" """
self.application = application self.app = app
self.top = tk.Toplevel() self.top = tk.Toplevel()
self.top.title("Canvas Size and Scale") self.top.title("Canvas Size and Scale")
self.meter_per_pixel = self.application.canvas.meters_per_pixel self.meter_per_pixel = self.app.canvas.meters_per_pixel
self.size_chart() self.size_chart()
self.scale_chart() self.scale_chart()
@ -108,7 +108,7 @@ class SizeAndScale:
label = tk.Label(self.top, text="Size") label = tk.Label(self.top, text="Size")
label.grid(sticky=tk.W, padx=5) label.grid(sticky=tk.W, padx=5)
canvas = self.application.canvas canvas = self.app.canvas
plot = canvas.find_withtag("rectangle") plot = canvas.find_withtag("rectangle")
x0, y0, x1, y1 = canvas.bbox(plot[0]) x0, y0, x1, y1 = canvas.bbox(plot[0])
w = abs(x0 - x1) - 2 w = abs(x0 - x1) - 2
@ -222,7 +222,7 @@ class SizeAndScale:
:param int pixel_height: height in pixel :param int pixel_height: height in pixel
:return: nothing :return: nothing
""" """
canvas = self.application.canvas canvas = self.app.canvas
canvas.config(scrollregion=(0, 0, pixel_width + 200, pixel_height + 200)) canvas.config(scrollregion=(0, 0, pixel_width + 200, pixel_height + 200))
# delete old plot and redraw # delete old plot and redraw
@ -246,24 +246,24 @@ class SizeAndScale:
scale_frame = self.top.grid_slaves(3, 0)[0] scale_frame = self.top.grid_slaves(3, 0)[0]
meter_per_pixel = float(scale_frame.grid_slaves(0, 1)[0].get()) / 100 meter_per_pixel = float(scale_frame.grid_slaves(0, 1)[0].get()) / 100
self.application.canvas.meters_per_pixel = meter_per_pixel self.app.canvas.meters_per_pixel = meter_per_pixel
self.redraw_grid(pixel_width, pixel_height) self.redraw_grid(pixel_width, pixel_height)
print(self.application.current_wallpaper) print(self.app.current_wallpaper)
print(self.application.radiovar) print(self.app.radiovar)
# if there is a current wallpaper showing, redraw it based on current wallpaper options # if there is a current wallpaper showing, redraw it based on current wallpaper options
wallpaper_tool = self.application.set_wallpaper wallpaper_tool = self.app.set_wallpaper
current_wallpaper = self.application.current_wallpaper current_wallpaper = self.app.current_wallpaper
if current_wallpaper: if current_wallpaper:
if self.application.adjust_to_dim_var.get() == 0: if self.app.adjust_to_dim_var.get() == 0:
if self.application.radiovar.get() == ScaleOption.UPPER_LEFT.value: if self.app.radiovar.get() == ScaleOption.UPPER_LEFT.value:
wallpaper_tool.upper_left(current_wallpaper) wallpaper_tool.upper_left(current_wallpaper)
elif self.application.radiovar.get() == ScaleOption.CENTERED.value: elif self.app.radiovar.get() == ScaleOption.CENTERED.value:
wallpaper_tool.center(current_wallpaper) wallpaper_tool.center(current_wallpaper)
elif self.application.radiovar.get() == ScaleOption.SCALED.value: elif self.app.radiovar.get() == ScaleOption.SCALED.value:
wallpaper_tool.scaled(current_wallpaper) wallpaper_tool.scaled(current_wallpaper)
elif self.application.radiovar.get() == ScaleOption.TILED.value: elif self.app.radiovar.get() == ScaleOption.TILED.value:
print("not implemented") print("not implemented")
elif self.application.adjust_to_dim_var.get() == 1: elif self.app.adjust_to_dim_var.get() == 1:
wallpaper_tool.canvas_to_image_dimension(current_wallpaper) wallpaper_tool.canvas_to_image_dimension(current_wallpaper)
wallpaper_tool.show_grid() wallpaper_tool.show_grid()

View file

@ -5,10 +5,9 @@ from core.api.grpc import core_pb2
class WirelessConnection: class WirelessConnection:
def __init__(self, canvas, core_grpc): def __init__(self, canvas, core):
self.canvas = canvas self.canvas = canvas
self.core_grpc = core_grpc self.core_mapping = core.core_mapping
self.core_mapping = core_grpc.core_mapping
# map a (node_one_id, node_two_id) to a wlan canvas id # map a (node_one_id, node_two_id) to a wlan canvas id
self.map = {} self.map = {}