|
| 1 | +import sys |
| 2 | +import logging |
| 3 | + |
| 4 | +import grpc |
| 5 | +import concurrent.futures as futures |
| 6 | + |
| 7 | +import service.common |
| 8 | + |
| 9 | +# Importing the generated codes from buildproto.sh |
| 10 | +import service.service_spec.example_service_pb2_grpc as grpc_bt_grpc |
| 11 | +from service.service_spec.example_service_pb2 import Result |
| 12 | + |
| 13 | +logging.basicConfig(level=10, format="%(asctime)s - [%(levelname)8s] - %(name)s - %(message)s") |
| 14 | +log = logging.getLogger("example_service") |
| 15 | + |
| 16 | + |
| 17 | +""" |
| 18 | +Simple arithmetic service to test the Snet Daemon (gRPC), dApp and/or Snet-CLI. |
| 19 | +The user must provide the method (arithmetic operation) and |
| 20 | +two numeric inputs: "a" and "b". |
| 21 | +
|
| 22 | +e.g: |
| 23 | +With dApp: 'method': mul |
| 24 | + 'params': {"a": 12.0, "b": 77.0} |
| 25 | +Resulting: response: |
| 26 | + value: 924.0 |
| 27 | +
|
| 28 | +
|
| 29 | +Full snet-cli cmd: |
| 30 | +$ snet client call mul '{"a":12.0, "b":77.0}' |
| 31 | +
|
| 32 | +Result: |
| 33 | +(Transaction info) |
| 34 | +Signing job... |
| 35 | +
|
| 36 | +Read call params from cmdline... |
| 37 | +
|
| 38 | +Calling service... |
| 39 | +
|
| 40 | + response: |
| 41 | + value: 924.0 |
| 42 | +""" |
| 43 | + |
| 44 | + |
| 45 | +# Create a class to be added to the gRPC server |
| 46 | +# derived from the protobuf codes. |
| 47 | +class CalculatorServicer(grpc_bt_grpc.CalculatorServicer): |
| 48 | + def __init__(self): |
| 49 | + self.a = 0 |
| 50 | + self.b = 0 |
| 51 | + self.result = 0 |
| 52 | + # Just for debugging purpose. |
| 53 | + log.debug("CalculatorServicer created") |
| 54 | + |
| 55 | + # The method that will be exposed to the snet-cli call command. |
| 56 | + # request: incoming data |
| 57 | + # context: object that provides RPC-specific information (timeout, etc). |
| 58 | + def add(self, request, context): |
| 59 | + # In our case, request is a Numbers() object (from .proto file) |
| 60 | + self.a = request.a |
| 61 | + self.b = request.b |
| 62 | + |
| 63 | + # To respond we need to create a Result() object (from .proto file) |
| 64 | + self.result = Result() |
| 65 | + |
| 66 | + self.result.value = self.a + self.b |
| 67 | + log.debug("add({},{})={}".format(self.a, self.b, self.result.value)) |
| 68 | + return self.result |
| 69 | + |
| 70 | + def sub(self, request, context): |
| 71 | + self.a = request.a |
| 72 | + self.b = request.b |
| 73 | + |
| 74 | + self.result = Result() |
| 75 | + self.result.value = self.a - self.b |
| 76 | + log.debug("sub({},{})={}".format(self.a, self.b, self.result.value)) |
| 77 | + return self.result |
| 78 | + |
| 79 | + def mul(self, request, context): |
| 80 | + self.a = request.a |
| 81 | + self.b = request.b |
| 82 | + |
| 83 | + self.result = Result() |
| 84 | + self.result.value = self.a * self.b |
| 85 | + log.debug("mul({},{})={}".format(self.a, self.b, self.result.value)) |
| 86 | + return self.result |
| 87 | + |
| 88 | + def div(self, request, context): |
| 89 | + self.a = request.a |
| 90 | + self.b = request.b |
| 91 | + |
| 92 | + self.result = Result() |
| 93 | + self.result.value = self.a / self.b |
| 94 | + log.debug("div({},{})={}".format(self.a, self.b, self.result.value)) |
| 95 | + return self.result |
| 96 | + |
| 97 | + |
| 98 | +# The gRPC serve function. |
| 99 | +# |
| 100 | +# Params: |
| 101 | +# max_workers: pool of threads to execute calls asynchronously |
| 102 | +# port: gRPC server port |
| 103 | +# |
| 104 | +# Add all your classes to the server here. |
| 105 | +# (from generated .py files by protobuf compiler) |
| 106 | +def serve(max_workers=10, port=7777): |
| 107 | + server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers)) |
| 108 | + grpc_bt_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server) |
| 109 | + server.add_insecure_port("[::]:{}".format(port)) |
| 110 | + return server |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + """ |
| 115 | + Runs the gRPC server to communicate with the Snet Daemon. |
| 116 | + """ |
| 117 | + parser = service.common.common_parser(__file__) |
| 118 | + args = parser.parse_args(sys.argv[1:]) |
| 119 | + service.common.main_loop(serve, args) |
0 commit comments