grpc added listen to server class and updated server/client to specify grpc in their names

This commit is contained in:
bharnden 2019-03-21 22:56:33 -07:00
parent 16d9009c3f
commit 0c0ff95fb6
3 changed files with 26 additions and 26 deletions

View file

@ -38,7 +38,7 @@ def start_streamer(stream, handler):
thread.start()
class CoreApiClient(object):
class CoreGrpcClient(object):
def __init__(self, address="localhost:50051"):
self.address = address
self.stub = None
@ -463,7 +463,7 @@ class CoreApiClient(object):
def main():
xml_file_name = "/tmp/core.xml"
client = CoreApiClient()
client = CoreGrpcClient()
with client.context_connect():
if os.path.exists(xml_file_name):
response = client.open_xml(xml_file_name)
@ -473,12 +473,12 @@ def main():
# create session
session_data = client.create_session()
client.exception_events(session_data.id, lambda x: print(type(x)))
client.node_events(session_data.id, lambda x: print(type(x)))
client.session_events(session_data.id, lambda x: print(type(x)))
client.link_events(session_data.id, lambda x: print(type(x)))
client.file_events(session_data.id, lambda x: print(type(x)))
client.config_events(session_data.id, lambda x: print(type(x)))
client.exception_events(session_data.id, lambda x: print(x))
client.node_events(session_data.id, lambda x: print(x))
client.session_events(session_data.id, lambda x: print(x))
client.link_events(session_data.id, lambda x: print(x))
client.file_events(session_data.id, lambda x: print(x))
client.config_events(session_data.id, lambda x: print(x))
print("created session: {}".format(session_data))
print("default services: {}".format(client.get_service_defaults(session_data.id)))
print("emane models: {}".format(client.get_emane_models(session_data.id)))

View file

@ -211,9 +211,9 @@ def send_objects(session):
logging.debug("informed GUI about %d nodes and %d links", len(nodes_data), len(links_data))
class CoreApiServer(core_pb2_grpc.CoreApiServicer):
class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
def __init__(self, coreemu):
super(CoreApiServer, self).__init__()
super(CoreGrpcServer, self).__init__()
self.coreemu = coreemu
self.running = True
atexit.register(self._exit_handler)
@ -228,6 +228,19 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
def _cancel_stream(self, context):
context.abort(grpc.StatusCode.CANCELLED, "server stopping")
def listen(self, address="[::]:50051"):
logging.info("starting grpc api: %s", address)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
core_pb2_grpc.add_CoreApiServicer_to_server(self, server)
server.add_insecure_port(address)
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
def get_session(self, _id, context):
session = self.coreemu.sessions.get(_id)
if not session:
@ -1142,17 +1155,3 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
self.coreemu.delete_session(session.session_id)
return response
def listen(coreemu, address="[::]:50051"):
logging.info("starting grpc api: %s", address)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
core_pb2_grpc.add_CoreApiServicer_to_server(CoreApiServer(coreemu), server)
server.add_insecure_port(address)
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)

View file

@ -17,7 +17,7 @@ from core import constants
from core import enumerations
from core.corehandlers import CoreHandler
from core.coreserver import CoreServer
from core.grpc.server import listen
from core.grpc.server import CoreGrpcServer
from core.misc.utils import close_onexec
load_logging_config()
@ -56,7 +56,8 @@ def cored(cfg):
# initialize grpc api
if cfg["grpc"] == "True":
grpc_thread = threading.Thread(target=listen, args=(server.coreemu,))
api_server = CoreGrpcServer(server.coreemu)
grpc_thread = threading.Thread(target=api_server.listen)
grpc_thread.daemon = True
grpc_thread.start()