From 6d4434bc1274c5aa7612ff289a97b646f6b477cc Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Thu, 16 Jul 2020 22:51:26 -0700 Subject: [PATCH] grpc: added set session user call, updated mobility to look for files within new gui as well, fixed pygui issue when start session has a grpc exceptions, showing and empty error window --- daemon/core/api/grpc/client.py | 14 ++++++++++ daemon/core/api/grpc/server.py | 15 ++++++++++ daemon/core/gui/coreclient.py | 5 ++++ daemon/core/gui/data/xmls/sample1.xml | 2 +- daemon/core/gui/toolbar.py | 2 +- daemon/core/location/mobility.py | 40 +++++++++++++-------------- daemon/proto/core/api/grpc/core.proto | 11 ++++++++ 7 files changed, 66 insertions(+), 23 deletions(-) diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index 20e193eb..3e974233 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -414,6 +414,20 @@ class CoreGrpcClient: request = core_pb2.SetSessionStateRequest(session_id=session_id, state=state) return self.stub.SetSessionState(request) + def set_session_user( + self, session_id: int, user: str + ) -> core_pb2.SetSessionUserResponse: + """ + Set session user, used for helping to find files without full paths. + + :param session_id: id of session + :param user: user to set for session + :return: response with result of success or failure + :raises grpc.RpcError: when session doesn't exist + """ + request = core_pb2.SetSessionUserRequest(session_id=session_id, user=user) + return self.stub.SetSessionUser(request) + def add_session_server( self, session_id: int, name: str, host: str ) -> core_pb2.AddSessionServerResponse: diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 5bdebac6..da2d53c3 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -448,6 +448,21 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): return core_pb2.SetSessionStateResponse(result=result) + def SetSessionUser( + self, request: core_pb2.SetSessionUserRequest, context: ServicerContext + ) -> core_pb2.SetSessionUserResponse: + """ + Sets the user for a session. + + :param request: set session user request + :param context: context object + :return: set session user response + """ + logging.debug("set session user: %s", request) + session = self.get_session(request.session_id, context) + session.user = request.user + return core_pb2.SetSessionUserResponse(result=True) + def GetSessionOptions( self, request: core_pb2.GetSessionOptionsRequest, context: ServicerContext ) -> core_pb2.GetSessionOptionsResponse: diff --git a/daemon/core/gui/coreclient.py b/daemon/core/gui/coreclient.py index 255192be..52023e14 100644 --- a/daemon/core/gui/coreclient.py +++ b/daemon/core/gui/coreclient.py @@ -1,6 +1,7 @@ """ Incorporate grpc into python tkinter GUI """ +import getpass import json import logging import os @@ -71,6 +72,7 @@ class CoreClient: self.default_services: Dict[NodeType, Set[str]] = {} self.emane_models: List[str] = [] self.observer: Optional[str] = None + self.user = getpass.getuser() # loaded configuration data self.servers: Dict[str, CoreServer] = {} @@ -289,6 +291,9 @@ class CoreClient: self.session_id, self.handle_events ) + # set session user + self.client.set_session_user(self.session_id, self.user) + # get session service defaults response = self.client.get_service_defaults(self.session_id) self.default_services = { diff --git a/daemon/core/gui/data/xmls/sample1.xml b/daemon/core/gui/data/xmls/sample1.xml index afec8874..5055c225 100644 --- a/daemon/core/gui/data/xmls/sample1.xml +++ b/daemon/core/gui/data/xmls/sample1.xml @@ -188,7 +188,7 @@ - + diff --git a/daemon/core/gui/toolbar.py b/daemon/core/gui/toolbar.py index c3e9067f..406a88ca 100644 --- a/daemon/core/gui/toolbar.py +++ b/daemon/core/gui/toolbar.py @@ -305,7 +305,7 @@ class Toolbar(ttk.Frame): self.set_runtime() self.app.core.set_metadata() self.app.core.show_mobility_players() - else: + elif response.exceptions: enable_buttons(self.design_frame, enabled=True) message = "\n".join(response.exceptions) self.app.show_error("Start Session Error", message) diff --git a/daemon/core/location/mobility.py b/daemon/core/location/mobility.py index f2e0f470..e982c5c1 100644 --- a/daemon/core/location/mobility.py +++ b/daemon/core/location/mobility.py @@ -5,10 +5,10 @@ mobility.py: mobility helpers for moving nodes and calculating wireless range. import heapq import logging import math -import os import threading import time from functools import total_ordering +from pathlib import Path from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple from core import utils @@ -1030,30 +1030,28 @@ class Ns2ScriptedMobility(WayPointMobility): def findfile(self, file_name: str) -> str: """ Locate a script file. If the specified file doesn't exist, look in the - same directory as the scenario file, or in the default - configs directory (~/.core/configs). This allows for sample files without - absolute path names. + same directory as the scenario file, or in gui directories. :param file_name: file name to find :return: absolute path to the file + :raises CoreError: when file is not found """ - if os.path.exists(file_name): - return file_name - - if self.session.file_name is not None: - d = os.path.dirname(self.session.file_name) - sessfn = os.path.join(d, file_name) - if os.path.exists(sessfn): - return sessfn - - if self.session.user is not None: - userfn = os.path.join( - "/home", self.session.user, ".core", "configs", file_name - ) - if os.path.exists(userfn): - return userfn - - return file_name + file_path = Path(file_name).expanduser() + if file_path.exists(): + return str(file_path) + if self.session.file_name: + file_path = Path(self.session.file_name).parent / file_name + if file_path.exists(): + return str(file_path) + if self.session.user: + user_path = Path(f"~{self.session.user}").expanduser() + file_path = user_path / ".core" / "configs" / file_name + if file_path.exists(): + return str(file_path) + file_path = user_path / ".coregui" / "mobility" / file_name + if file_path.exists(): + return str(file_path) + raise CoreError(f"invalid file: {file_name}") def parsemap(self, mapstr: str) -> None: """ diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index f01fca50..5ca4812c 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -39,6 +39,8 @@ service CoreApi { } rpc SetSessionState (SetSessionStateRequest) returns (SetSessionStateResponse) { } + rpc SetSessionUser (SetSessionUserRequest) returns (SetSessionUserResponse) { + } rpc AddSessionServer (AddSessionServerRequest) returns (AddSessionServerResponse) { } @@ -297,6 +299,15 @@ message SetSessionStateResponse { bool result = 1; } +message SetSessionUserRequest { + int32 session_id = 1; + string user = 2; +} + +message SetSessionUserResponse { + bool result = 1; +} + message AddSessionServerRequest { int32 session_id = 1; string name = 2;