service.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. """DEPRECATED: Declares the RPC service interfaces.
  8. This module declares the abstract interfaces underlying proto2 RPC
  9. services. These are intended to be independent of any particular RPC
  10. implementation, so that proto2 services can be used on top of a variety
  11. of implementations. Starting with version 2.3.0, RPC implementations should
  12. not try to build on these, but should instead provide code generator plugins
  13. which generate code specific to the particular RPC implementation. This way
  14. the generated code can be more appropriate for the implementation in use
  15. and can avoid unnecessary layers of indirection.
  16. """
  17. __author__ = 'petar@google.com (Petar Petrov)'
  18. import warnings
  19. warnings.warn(
  20. 'google.protobuf.service module is deprecated. RPC implementations '
  21. 'should provide code generator plugins which generate code specific to '
  22. 'the RPC implementation. service.py will be removed in Jan 2025',
  23. stacklevel=2,
  24. )
  25. class RpcException(Exception):
  26. """Exception raised on failed blocking RPC method call."""
  27. pass
  28. class Service(object):
  29. """Abstract base interface for protocol-buffer-based RPC services.
  30. Services themselves are abstract classes (implemented either by servers or as
  31. stubs), but they subclass this base interface. The methods of this
  32. interface can be used to call the methods of the service without knowing
  33. its exact type at compile time (analogous to the Message interface).
  34. """
  35. def GetDescriptor():
  36. """Retrieves this service's descriptor."""
  37. raise NotImplementedError
  38. def CallMethod(self, method_descriptor, rpc_controller,
  39. request, done):
  40. """Calls a method of the service specified by method_descriptor.
  41. If "done" is None then the call is blocking and the response
  42. message will be returned directly. Otherwise the call is asynchronous
  43. and "done" will later be called with the response value.
  44. In the blocking case, RpcException will be raised on error.
  45. Preconditions:
  46. * method_descriptor.service == GetDescriptor
  47. * request is of the exact same classes as returned by
  48. GetRequestClass(method).
  49. * After the call has started, the request must not be modified.
  50. * "rpc_controller" is of the correct type for the RPC implementation being
  51. used by this Service. For stubs, the "correct type" depends on the
  52. RpcChannel which the stub is using.
  53. Postconditions:
  54. * "done" will be called when the method is complete. This may be
  55. before CallMethod() returns or it may be at some point in the future.
  56. * If the RPC failed, the response value passed to "done" will be None.
  57. Further details about the failure can be found by querying the
  58. RpcController.
  59. """
  60. raise NotImplementedError
  61. def GetRequestClass(self, method_descriptor):
  62. """Returns the class of the request message for the specified method.
  63. CallMethod() requires that the request is of a particular subclass of
  64. Message. GetRequestClass() gets the default instance of this required
  65. type.
  66. Example:
  67. method = service.GetDescriptor().FindMethodByName("Foo")
  68. request = stub.GetRequestClass(method)()
  69. request.ParseFromString(input)
  70. service.CallMethod(method, request, callback)
  71. """
  72. raise NotImplementedError
  73. def GetResponseClass(self, method_descriptor):
  74. """Returns the class of the response message for the specified method.
  75. This method isn't really needed, as the RpcChannel's CallMethod constructs
  76. the response protocol message. It's provided anyway in case it is useful
  77. for the caller to know the response type in advance.
  78. """
  79. raise NotImplementedError
  80. class RpcController(object):
  81. """An RpcController mediates a single method call.
  82. The primary purpose of the controller is to provide a way to manipulate
  83. settings specific to the RPC implementation and to find out about RPC-level
  84. errors. The methods provided by the RpcController interface are intended
  85. to be a "least common denominator" set of features which we expect all
  86. implementations to support. Specific implementations may provide more
  87. advanced features (e.g. deadline propagation).
  88. """
  89. # Client-side methods below
  90. def Reset(self):
  91. """Resets the RpcController to its initial state.
  92. After the RpcController has been reset, it may be reused in
  93. a new call. Must not be called while an RPC is in progress.
  94. """
  95. raise NotImplementedError
  96. def Failed(self):
  97. """Returns true if the call failed.
  98. After a call has finished, returns true if the call failed. The possible
  99. reasons for failure depend on the RPC implementation. Failed() must not
  100. be called before a call has finished. If Failed() returns true, the
  101. contents of the response message are undefined.
  102. """
  103. raise NotImplementedError
  104. def ErrorText(self):
  105. """If Failed is true, returns a human-readable description of the error."""
  106. raise NotImplementedError
  107. def StartCancel(self):
  108. """Initiate cancellation.
  109. Advises the RPC system that the caller desires that the RPC call be
  110. canceled. The RPC system may cancel it immediately, may wait awhile and
  111. then cancel it, or may not even cancel the call at all. If the call is
  112. canceled, the "done" callback will still be called and the RpcController
  113. will indicate that the call failed at that time.
  114. """
  115. raise NotImplementedError
  116. # Server-side methods below
  117. def SetFailed(self, reason):
  118. """Sets a failure reason.
  119. Causes Failed() to return true on the client side. "reason" will be
  120. incorporated into the message returned by ErrorText(). If you find
  121. you need to return machine-readable information about failures, you
  122. should incorporate it into your response protocol buffer and should
  123. NOT call SetFailed().
  124. """
  125. raise NotImplementedError
  126. def IsCanceled(self):
  127. """Checks if the client cancelled the RPC.
  128. If true, indicates that the client canceled the RPC, so the server may
  129. as well give up on replying to it. The server should still call the
  130. final "done" callback.
  131. """
  132. raise NotImplementedError
  133. def NotifyOnCancel(self, callback):
  134. """Sets a callback to invoke on cancel.
  135. Asks that the given callback be called when the RPC is canceled. The
  136. callback will always be called exactly once. If the RPC completes without
  137. being canceled, the callback will be called after completion. If the RPC
  138. has already been canceled when NotifyOnCancel() is called, the callback
  139. will be called immediately.
  140. NotifyOnCancel() must be called no more than once per request.
  141. """
  142. raise NotImplementedError
  143. class RpcChannel(object):
  144. """Abstract interface for an RPC channel.
  145. An RpcChannel represents a communication line to a service which can be used
  146. to call that service's methods. The service may be running on another
  147. machine. Normally, you should not use an RpcChannel directly, but instead
  148. construct a stub {@link Service} wrapping it. Example:
  149. Example:
  150. RpcChannel channel = rpcImpl.Channel("remotehost.example.com:1234")
  151. RpcController controller = rpcImpl.Controller()
  152. MyService service = MyService_Stub(channel)
  153. service.MyMethod(controller, request, callback)
  154. """
  155. def CallMethod(self, method_descriptor, rpc_controller,
  156. request, response_class, done):
  157. """Calls the method identified by the descriptor.
  158. Call the given method of the remote service. The signature of this
  159. procedure looks the same as Service.CallMethod(), but the requirements
  160. are less strict in one important way: the request object doesn't have to
  161. be of any specific class as long as its descriptor is method.input_type.
  162. """
  163. raise NotImplementedError