github.com/form3tech-oss/cilium@v1.6.3/examples/kubernetes-grpc/cc_door_server.py (about)

     1  # Copyright 2015 gRPC authors.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  """The Python implementation of the GRPC cloudcity.DoorManager server."""
    16  
    17  from concurrent import futures
    18  import time
    19  
    20  import grpc
    21  
    22  import cloudcity_pb2
    23  import cloudcity_pb2_grpc
    24  
    25  _ONE_DAY_IN_SECONDS = 60 * 60 * 24
    26  
    27  
    28  class DoorManager(cloudcity_pb2_grpc.DoorManagerServicer):
    29  
    30    def GetName(self, request, context):
    31      return cloudcity_pb2.DoorNameReply(name='Spaceport Door #%s' % request.door_id)
    32  
    33    def GetLocation(self, request, context): 
    34      return cloudcity_pb2.DoorLocationReply(lat=10.2222, long=68.8788)
    35  
    36    def GetStatus(self, request, context): 
    37      return cloudcity_pb2.DoorStatusReply(state=cloudcity_pb2.CLOSED)
    38  
    39    def RequestMaintenance(self, request, context): 
    40      return cloudcity_pb2.DoorActionReply(success=True) 
    41  
    42    def SetAccessCode(self, request, context): 
    43      return cloudcity_pb2.DoorActionReply(success=True) 
    44  
    45  
    46  def serve():
    47    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    48    cloudcity_pb2_grpc.add_DoorManagerServicer_to_server(DoorManager(), server)
    49    server.add_insecure_port('[::]:50051')
    50    server.start()
    51    try:
    52      while True:
    53        time.sleep(_ONE_DAY_IN_SECONDS)
    54    except KeyboardInterrupt:
    55      server.stop(0)
    56  
    57  if __name__ == '__main__':
    58    serve()