grpc added delete session and set session state apis

This commit is contained in:
bharnden 2019-02-24 08:15:33 -08:00
parent fcff9e4551
commit ad7522b5bc
3 changed files with 88 additions and 8 deletions

View file

@ -24,6 +24,11 @@ class CoreApiClient(object):
def create_session(self):
return self.stub.CreateSession(core_pb2.CreateSessionRequest())
def delete_session(self, _id):
request = core_pb2.DeleteSessionRequest()
request.id = _id
return self.stub.DeleteSession(request)
def get_sessions(self):
return self.stub.GetSessions(core_pb2.SessionsRequest())
@ -49,6 +54,12 @@ class CoreApiClient(object):
update_proto(request, scale=scale)
return self.stub.SetSessionLocation(request)
def set_session_state(self, _id, state):
request = core_pb2.SetSessionStateRequest()
request.id = _id
request.state = state
return self.stub.SetSessionState(request)
@contextmanager
def connect(self):
channel = grpc.insecure_channel(self.address)
@ -76,20 +87,25 @@ def main():
response = client.set_session_location(
session_data.id,
x=0, y=0, z=None,
lat=47.57917, lon=-122.13232, alt=2.0,
lat=47.57917, lon=-122.13232, alt=3.0,
scale=150000.0
)
print("set location response: %s" % response)
# get session
session = client.get_session(session_data.id)
print(session)
# get options
print(client.get_session_options(session_data.id))
print("get options: %s" % client.get_session_options(session_data.id))
# get location
print(client.get_session_location(session_data.id))
print("get location: %s" % client.get_session_location(session_data.id))
# change session state
print("set session state: %s" % client.set_session_state(session_data.id, core_pb2.INSTANTIATION))
# get session
print("get session: %s" % client.get_session(session_data.id))
# delete session
print("delete session: %s" % client.delete_session(session_data.id))
if __name__ == "__main__":

View file

@ -1,3 +1,5 @@
import os
from core.enumerations import NodeTypes, EventTypes
from concurrent import futures
@ -102,6 +104,11 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
response.state = session.state
return response
def DeleteSession(self, request, context):
response = core_pb2.DeleteSessionResponse()
response.result = self.coreemu.delete_session(request.id)
return response
def GetSessions(self, request, context):
response = core_pb2.SessionsResponse()
for session_id in self.coreemu.sessions:
@ -138,6 +145,32 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return core_pb2.SetSessionLocationResponse()
def SetSessionState(self, request, context):
response = core_pb2.SetSessionStateResponse()
session = self.coreemu.sessions.get(request.id)
try:
state = EventTypes(request.state)
session.set_state(state)
if state == EventTypes.INSTANTIATION_STATE:
# create session directory if it does not exist
if not os.path.exists(session.session_dir):
os.mkdir(session.session_dir)
session.instantiate()
elif state == EventTypes.SHUTDOWN_STATE:
session.shutdown()
elif state == EventTypes.DATACOLLECT_STATE:
session.data_collect()
elif state == EventTypes.DEFINITION_STATE:
session.clear()
response.result = True
except KeyError:
response.result = False
return response
def GetSessionOptions(self, request, context):
session = self.coreemu.sessions.get(request.id)
config = session.options.get_configs()
@ -169,6 +202,8 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
pass
response = core_pb2.SessionResponse()
response.state = session.state
for node_id in session.objects:
node = session.objects[node_id]

View file

@ -5,6 +5,8 @@ package core;
service CoreApi {
rpc CreateSession (CreateSessionRequest) returns (CreateSessionResponse) {
}
rpc DeleteSession (DeleteSessionRequest) returns (DeleteSessionResponse) {
}
rpc GetSessions (SessionsRequest) returns (SessionsResponse) {
}
rpc GetSession (SessionRequest) returns (SessionResponse) {
@ -14,7 +16,8 @@ service CoreApi {
rpc GetSessionLocation (GetSessionLocationRequest) returns (GetSessionLocationResponse) {
}
rpc SetSessionLocation (SetSessionLocationRequest) returns (SetSessionLocationResponse) {
}
rpc SetSessionState (SetSessionStateRequest) returns (SetSessionStateResponse) {
}
}
@ -28,6 +31,14 @@ message CreateSessionResponse {
int32 state = 2;
}
message DeleteSessionRequest {
int32 id = 1;
}
message DeleteSessionResponse {
bool result = 1;
}
message SessionsRequest {
}
@ -69,10 +80,28 @@ message SetSessionLocationRequest {
}
message SetSessionLocationResponse {
}
message SetSessionStateRequest {
int32 id = 1;
SessionState state = 2;
}
message SetSessionStateResponse {
bool result = 1;
}
// data structures for messages below
enum SessionState {
NONE = 0;
DEFINITION = 1;
CONFIGURATION = 2;
INSTANTIATION = 3;
RUNTIME = 4;
DATACOLLECT = 5;
SHUTDOWN = 6;
}
message ConfigGroup {
string name = 1;
repeated ConfigOption options = 2;