From 9535d40b700ab64b657b516e7ed59ed79f6d0923 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Fri, 28 Feb 2020 12:28:41 -0800 Subject: [PATCH] added grpc call to execute python script, to replicate prior gui functionality --- daemon/core/api/grpc/client.py | 6 ++++++ daemon/core/api/grpc/server.py | 22 ++++++++++++++++++++++ daemon/proto/core/api/grpc/core.proto | 10 ++++++++++ 3 files changed, 38 insertions(+) diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index f0808713..15122e67 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -27,6 +27,8 @@ from core.api.grpc.configservices_pb2 import ( SetNodeConfigServiceResponse, ) from core.api.grpc.core_pb2 import ( + ExecuteScriptRequest, + ExecuteScriptResponse, GetEmaneEventChannelRequest, GetEmaneEventChannelResponse, ) @@ -1148,6 +1150,10 @@ class CoreGrpcClient: request = GetEmaneEventChannelRequest(session_id=session_id) return self.stub.GetEmaneEventChannel(request) + def execute_script(self, script: str) -> ExecuteScriptResponse: + request = ExecuteScriptRequest(script=script) + return self.stub.ExecuteScript(request) + def connect(self) -> None: """ Open connection to server, must be closed manually. diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index f155867d..ca992481 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -3,6 +3,7 @@ import logging import os import re import tempfile +import threading import time from concurrent import futures from typing import Type @@ -10,6 +11,7 @@ from typing import Type import grpc from grpc import ServicerContext +from core import utils from core.api.grpc import ( common_pb2, configservices_pb2, @@ -33,6 +35,7 @@ from core.api.grpc.configservices_pb2 import ( SetNodeConfigServiceResponse, ) from core.api.grpc.core_pb2 import ( + ExecuteScriptResponse, GetEmaneEventChannelRequest, GetEmaneEventChannelResponse, ) @@ -1645,3 +1648,22 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): if session.emane.eventchannel: group, port, device = session.emane.eventchannel return GetEmaneEventChannelResponse(group=group, port=port, device=device) + + def ExecuteScript(self, request, context): + existing_sessions = set(self.coreemu.sessions.keys()) + thread = threading.Thread( + target=utils.execute_file, + args=( + request.script, + {"__file__": request.script, "coreemu": self.coreemu}, + ), + daemon=True, + ) + thread.start() + thread.join() + current_sessions = set(self.coreemu.sessions.keys()) + new_sessions = list(current_sessions.difference(existing_sessions)) + new_session = -1 + if new_sessions: + new_session = new_sessions[0] + return ExecuteScriptResponse(session_id=new_session) diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index e515ab2e..b89e5fb1 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -154,6 +154,8 @@ service CoreApi { } rpc EmaneLink (EmaneLinkRequest) returns (EmaneLinkResponse) { } + rpc ExecuteScript (ExecuteScriptRequest) returns (ExecuteScriptResponse) { + } } // rpc request/response messages @@ -759,6 +761,14 @@ message EmaneLinkResponse { bool result = 1; } +message ExecuteScriptRequest { + string script = 1; +} + +message ExecuteScriptResponse { + int32 session_id = 1; +} + // data structures for messages below message WlanConfig { int32 node_id = 1;