asr_servicer.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. __all__= ['ASRServicer','init']
  2. import grpc,threading,traceback
  3. import logging as Log
  4. import mllp_grpc.asr_pb2_grpc as asr_pb2_grpc
  5. import mllp_grpc.asr_pb2 as asr_pb2
  6. import mllp_grpc.asr_common_pb2 as asr_common_pb2
  7. import google.protobuf.empty_pb2 as empty_pb2
  8. import server_lib.decoder_multi as decoder_multi
  9. from server_lib.decoder_mng import DecoderException
  10. def ii_(func,txt,context):
  11. Log.info('[%s] (%s) %s'%(context.peer(),func,txt))
  12. class ASRServicer(asr_pb2_grpc.ASRServicer):
  13. def __init__(self,mng):
  14. self._mng= mng
  15. def AddSystem(self,request,context):
  16. def ii(txt): ii_('AddSystem',txt,context)
  17. try:
  18. self._mng.add(request.host,request.port,ii)
  19. ii('added %s:%d'%(request.host,request.port))
  20. code= asr_pb2.AddSystemResponse.Code.OK
  21. return asr_pb2.AddSystemResponse(code=code)
  22. except Exception as e:
  23. msg= 'error adding system %s:%d: %s'%(request.host,
  24. request.port,
  25. str(e))
  26. ii(msg)
  27. code= asr_pb2.AddSystemResponse.Code.OK
  28. return asr_pb2.AddSystemResponse(code=code,
  29. details=msg)
  30. def GetSystemInfoById(self,request,context):
  31. def ii(txt): ii_('GetSystemInfoById',txt,context)
  32. ii("retrieving ASR system information for %s"%request.system_id)
  33. ok,info= self._mng.get_system_info(request.system_id,ii)
  34. if ok:
  35. return asr_pb2.GetSystemInfoByIdResponse(
  36. code=asr_pb2.GetSystemInfoByIdResponse.Code.OK,
  37. info=info.info,
  38. num_decoders=info.num_decoders,
  39. num_decoders_available=info.num_decoders_available,
  40. id=info.id)
  41. else:
  42. return asr_pb2.GetSystemInfoByIdResponse(
  43. code=asr_pb2.GetSystemInfoByIdResponse.Code.ERR)
  44. def GetSystemsInfo(self,request,context):
  45. def ii(txt): ii_('GetSystemsInfo',txt,context)
  46. ii('retrieving ASR systems information')
  47. for v in self._mng.get_systems_info(ii):
  48. yield asr_pb2.GetSystemsInfoResponse(
  49. info=v.info,
  50. num_decoders=v.num_decoders,
  51. num_decoders_available=v.num_decoders_available,
  52. id=v.id)
  53. def GetHostsInfo(self,request,context):
  54. def ii(txt): ii_('GetHostsInfo',txt,context)
  55. ii('retrieving hosts information')
  56. for v in self._mng.get_hosts_info(ii):
  57. yield asr_pb2.GetHostsInfoResponse(
  58. info=v.info,
  59. num_decoders=v.num_decoders,
  60. num_decoders_available=v.num_decoders_available,
  61. host=v.host,
  62. port=v.port)
  63. def Decode(self,request_iterator,context):
  64. def ii(txt): ii_('Decode',txt,context)
  65. try:
  66. for o in self._mng.decode(request_iterator,ii):
  67. yield o
  68. except DecoderException as e:
  69. ii(e.msg)
  70. status= asr_common_pb2.DecodeResponse.Status(code=e.code,
  71. details=e.msg)
  72. yield asr_common_pb2.DecodeResponse(status=status)
  73. except BaseException as e:
  74. traceback.print_exc()
  75. ii(str(e))
  76. code= asr_common_pb2.DecodeResponse.Status.Code.ERR_RECO
  77. status= asr_common_pb2.DecodeResponse.Status(code=code,
  78. details=str(e))
  79. yield asr_common_pb2.DecodeResponse(status=status)
  80. ii('Done!')
  81. def DecodeMulti(self,request_iterator,context):
  82. def ii(txt): ii_('DecodeMulti',txt,context)
  83. try:
  84. for o in decoder_multi.decode(self._mng,request_iterator,ii):
  85. yield o
  86. except DecoderException as e:
  87. ii(e.msg)
  88. status= asr_common_pb2.DecodeResponse.Status(code=e.code,
  89. details=e.msg)
  90. res= asr_common_pb2.DecodeResponse(status=status)
  91. yield asr_pb2.DecodeMultiResponse(res=res)
  92. except BaseException as e:
  93. traceback.print_exc()
  94. ii(str(e))
  95. code= asr_common_pb2.DecodeResponse.Status.Code.ERR_RECO
  96. status= asr_common_pb2.DecodeResponse.Status(code=code,
  97. details=str(e))
  98. res= asr_common_pb2.DecodeResponse(status=status)
  99. yield asr_pb2.DecodeMultiResponse(res=res)
  100. ii('Done!')
  101. def init(server,mng):
  102. asr_pb2_grpc.add_ASRServicer_to_server(
  103. ASRServicer(mng),server)