diff --git a/.gitignore b/.gitignore index 7415be63..200bc9b9 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,10 @@ configure debian stamp-h1 +# generated protobuf files +daemon/core/grpc/core_pb2.py +daemon/core/grpc/core_pb2_grpc.py + # python build directory dist diff --git a/daemon/core/grpc/__init__.py b/daemon/core/grpc/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/daemon/core/grpc/client.py b/daemon/core/grpc/client.py new file mode 100644 index 00000000..40c7842b --- /dev/null +++ b/daemon/core/grpc/client.py @@ -0,0 +1,38 @@ +from __future__ import print_function +import logging +from contextlib import contextmanager + +import grpc + +import core_pb2 +import core_pb2_grpc + + +class CoreApiClient(object): + def __init__(self, address="localhost:50051"): + self.address = address + self.stub = None + + def get_sessions(self): + return self.stub.GetSessions(core_pb2.SessionsRequest()) + + @contextmanager + def connect(self): + channel = grpc.insecure_channel(self.address) + try: + self.stub = core_pb2_grpc.CoreApiStub(channel) + yield channel + finally: + channel.close() + + +def main(): + client = CoreApiClient() + with client.connect(): + response = client.get_sessions() + print("core client received: %s" % response) + + +if __name__ == "__main__": + logging.basicConfig() + main() diff --git a/daemon/core/grpc/server.py b/daemon/core/grpc/server.py new file mode 100644 index 00000000..45472915 --- /dev/null +++ b/daemon/core/grpc/server.py @@ -0,0 +1,37 @@ +from concurrent import futures +import time +import logging + +import grpc + +import core_pb2 +import core_pb2_grpc + +_ONE_DAY_IN_SECONDS = 60 * 60 * 24 + + +class CoreApiServer(core_pb2_grpc.CoreApiServicer): + + def GetSessions(self, request, context): + response = core_pb2.SessionsResponse() + session = response.sessions.add() + session.id = 1 + return response + + +def main(): + server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) + core_pb2_grpc.add_CoreApiServicer_to_server(CoreApiServer(), server) + server.add_insecure_port("[::]:50051") + server.start() + + try: + while True: + time.sleep(_ONE_DAY_IN_SECONDS) + except KeyboardInterrupt: + server.stop(0) + + +if __name__ == "__main__": + logging.basicConfig() + main() diff --git a/daemon/proto/core.proto b/daemon/proto/core.proto new file mode 100644 index 00000000..51d11cdc --- /dev/null +++ b/daemon/proto/core.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; + +package core; + +service CoreApi { + rpc GetSessions (SessionsRequest) returns (SessionsResponse) {} +} + +message SessionsRequest { +} + +message SessionsResponse { + repeated Session sessions = 1; +} + +message Session { + int32 id = 1; +}