xref: /aosp_15_r20/external/flatbuffers/grpc/examples/python/greeter/server.py (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Workerfrom concurrent import futures
2*890232f2SAndroid Build Coastguard Workerimport sys
3*890232f2SAndroid Build Coastguard Workerimport argparse
4*890232f2SAndroid Build Coastguard Workerimport grpc
5*890232f2SAndroid Build Coastguard Worker
6*890232f2SAndroid Build Coastguard Workersys.path.insert(0, '../../../../../flatbuffers/python')
7*890232f2SAndroid Build Coastguard Worker
8*890232f2SAndroid Build Coastguard Workerimport flatbuffers
9*890232f2SAndroid Build Coastguard Workerfrom models import HelloReply, HelloRequest, greeter_grpc_fb
10*890232f2SAndroid Build Coastguard Worker
11*890232f2SAndroid Build Coastguard Workerparser = argparse.ArgumentParser()
12*890232f2SAndroid Build Coastguard Workerparser.add_argument("port", help="server on port", default=3000)
13*890232f2SAndroid Build Coastguard Worker
14*890232f2SAndroid Build Coastguard Workerdef build_reply(message):
15*890232f2SAndroid Build Coastguard Worker    builder = flatbuffers.Builder()
16*890232f2SAndroid Build Coastguard Worker    ind = builder.CreateString(message)
17*890232f2SAndroid Build Coastguard Worker    HelloReply.HelloReplyStart(builder)
18*890232f2SAndroid Build Coastguard Worker    HelloReply.HelloReplyAddMessage(builder, ind)
19*890232f2SAndroid Build Coastguard Worker    root = HelloReply.HelloReplyEnd(builder)
20*890232f2SAndroid Build Coastguard Worker    builder.Finish(root)
21*890232f2SAndroid Build Coastguard Worker    return bytes(builder.Output())
22*890232f2SAndroid Build Coastguard Worker
23*890232f2SAndroid Build Coastguard Workerclass GreeterServicer(greeter_grpc_fb.GreeterServicer):
24*890232f2SAndroid Build Coastguard Worker
25*890232f2SAndroid Build Coastguard Worker    def __init__(self):
26*890232f2SAndroid Build Coastguard Worker        self.greetings = ["Hi", "Hallo", "Ciao"]
27*890232f2SAndroid Build Coastguard Worker
28*890232f2SAndroid Build Coastguard Worker    def SayHello(self, request, context):
29*890232f2SAndroid Build Coastguard Worker        r = HelloRequest.HelloRequest().GetRootAs(request, 0)
30*890232f2SAndroid Build Coastguard Worker        reply = "Unknown"
31*890232f2SAndroid Build Coastguard Worker        if r.Name():
32*890232f2SAndroid Build Coastguard Worker            reply = r.Name()
33*890232f2SAndroid Build Coastguard Worker        return build_reply("welcome " + reply.decode('UTF-8'))
34*890232f2SAndroid Build Coastguard Worker
35*890232f2SAndroid Build Coastguard Worker    def SayManyHellos(self, request, context):
36*890232f2SAndroid Build Coastguard Worker        r = HelloRequest.HelloRequest().GetRootAs(request, 0)
37*890232f2SAndroid Build Coastguard Worker        reply = "Unknown"
38*890232f2SAndroid Build Coastguard Worker        if r.Name():
39*890232f2SAndroid Build Coastguard Worker            reply = r.Name()
40*890232f2SAndroid Build Coastguard Worker
41*890232f2SAndroid Build Coastguard Worker        for greeting in self.greetings:
42*890232f2SAndroid Build Coastguard Worker            print(type(reply))
43*890232f2SAndroid Build Coastguard Worker            yield build_reply(greeting + " " + reply.decode('UTF-8'))
44*890232f2SAndroid Build Coastguard Worker
45*890232f2SAndroid Build Coastguard Worker
46*890232f2SAndroid Build Coastguard Workerdef serve():
47*890232f2SAndroid Build Coastguard Worker    args = parser.parse_args()
48*890232f2SAndroid Build Coastguard Worker    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
49*890232f2SAndroid Build Coastguard Worker    greeter_grpc_fb.add_GreeterServicer_to_server(
50*890232f2SAndroid Build Coastguard Worker        GreeterServicer(), server
51*890232f2SAndroid Build Coastguard Worker    )
52*890232f2SAndroid Build Coastguard Worker    server.add_insecure_port('[::]:' + args.port)
53*890232f2SAndroid Build Coastguard Worker    server.start()
54*890232f2SAndroid Build Coastguard Worker    server.wait_for_termination()
55*890232f2SAndroid Build Coastguard Worker
56*890232f2SAndroid Build Coastguard Workerif __name__ == '__main__':
57*890232f2SAndroid Build Coastguard Worker    serve()