service.py 7.1 KB

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