initial files to support grpc server/client
This commit is contained in:
parent
9b1141a135
commit
0a445f2706
5 changed files with 97 additions and 0 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -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
|
||||
|
||||
|
|
0
daemon/core/grpc/__init__.py
Normal file
0
daemon/core/grpc/__init__.py
Normal file
38
daemon/core/grpc/client.py
Normal file
38
daemon/core/grpc/client.py
Normal file
|
@ -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()
|
37
daemon/core/grpc/server.py
Normal file
37
daemon/core/grpc/server.py
Normal file
|
@ -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()
|
18
daemon/proto/core.proto
Normal file
18
daemon/proto/core.proto
Normal file
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue