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

This commit is contained in:
Blake Harnden 2020-07-16 22:51:26 -07:00
parent 36123e7aa5
commit 6d4434bc12
7 changed files with 66 additions and 23 deletions

View file

@ -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:

View file

@ -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:

View file

@ -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 = {

View file

@ -188,7 +188,7 @@
<configuration name="error" value="0"/>
</mobility_configuration>
<mobility_configuration node="10" model="ns2script">
<configuration name="file" value="/home/developer/.coretk/mobility/sample1.scen"/>
<configuration name="file" value="sample1.scen"/>
<configuration name="refresh_ms" value="50"/>
<configuration name="loop" value="1"/>
<configuration name="autostart" value="5"/>

View file

@ -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)

View file

@ -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:
"""

View file

@ -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;