added grpc call to execute python script, to replicate prior gui functionality

This commit is contained in:
Blake Harnden 2020-02-28 12:28:41 -08:00
parent c630d911fe
commit 9535d40b70
3 changed files with 38 additions and 0 deletions

View file

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

View file

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

View file

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