31 """DEPRECATED: Declares the RPC service interfaces.
33 This module declares the abstract interfaces underlying proto2 RPC
34 services. These are intended to be independent of any particular RPC
35 implementation, so that proto2 services can be used on top of a variety
36 of implementations. Starting with version 2.3.0, RPC implementations should
37 not try to build on these, but should instead provide code generator plugins
38 which generate code specific to the particular RPC implementation. This way
39 the generated code can be more appropriate for the implementation in use
40 and can avoid unnecessary layers of indirection.
43 __author__ =
'petar@google.com (Petar Petrov)'
46 class RpcException(Exception):
47 """Exception raised on failed blocking RPC method call."""
51 class Service(object):
53 """Abstract base interface for protocol-buffer-based RPC services.
55 Services themselves are abstract classes (implemented either by servers or as
56 stubs), but they subclass this base interface. The methods of this
57 interface can be used to call the methods of the service without knowing
58 its exact type at compile time (analogous to the Message interface).
62 """Retrieves this service's descriptor."""
63 raise NotImplementedError
67 """Calls a method of the service specified by method_descriptor.
69 If "done" is None then the call is blocking and the response
70 message will be returned directly. Otherwise the call is asynchronous
71 and "done" will later be called with the response value.
73 In the blocking case, RpcException will be raised on error.
77 * method_descriptor.service == GetDescriptor
78 * request is of the exact same classes as returned by
79 GetRequestClass(method).
80 * After the call has started, the request must not be modified.
81 * "rpc_controller" is of the correct type for the RPC implementation being
82 used by this Service. For stubs, the "correct type" depends on the
83 RpcChannel which the stub is using.
87 * "done" will be called when the method is complete. This may be
88 before CallMethod() returns or it may be at some point in the future.
89 * If the RPC failed, the response value passed to "done" will be None.
90 Further details about the failure can be found by querying the
93 raise NotImplementedError
96 """Returns the class of the request message for the specified method.
98 CallMethod() requires that the request is of a particular subclass of
99 Message. GetRequestClass() gets the default instance of this required
103 method = service.GetDescriptor().FindMethodByName("Foo")
104 request = stub.GetRequestClass(method)()
105 request.ParseFromString(input)
106 service.CallMethod(method, request, callback)
108 raise NotImplementedError
111 """Returns the class of the response message for the specified method.
113 This method isn't really needed, as the RpcChannel's CallMethod constructs
114 the response protocol message. It's provided anyway in case it is useful
115 for the caller to know the response type in advance.
117 raise NotImplementedError
122 """An RpcController mediates a single method call.
124 The primary purpose of the controller is to provide a way to manipulate
125 settings specific to the RPC implementation and to find out about RPC-level
126 errors. The methods provided by the RpcController interface are intended
127 to be a "least common denominator" set of features which we expect all
128 implementations to support. Specific implementations may provide more
129 advanced features (e.g. deadline propagation).
135 """Resets the RpcController to its initial state.
137 After the RpcController has been reset, it may be reused in
138 a new call. Must not be called while an RPC is in progress.
140 raise NotImplementedError
143 """Returns true if the call failed.
145 After a call has finished, returns true if the call failed. The possible
146 reasons for failure depend on the RPC implementation. Failed() must not
147 be called before a call has finished. If Failed() returns true, the
148 contents of the response message are undefined.
150 raise NotImplementedError
153 """If Failed is true, returns a human-readable description of the error."""
154 raise NotImplementedError
157 """Initiate cancellation.
159 Advises the RPC system that the caller desires that the RPC call be
160 canceled. The RPC system may cancel it immediately, may wait awhile and
161 then cancel it, or may not even cancel the call at all. If the call is
162 canceled, the "done" callback will still be called and the RpcController
163 will indicate that the call failed at that time.
165 raise NotImplementedError
170 """Sets a failure reason.
172 Causes Failed() to return true on the client side. "reason" will be
173 incorporated into the message returned by ErrorText(). If you find
174 you need to return machine-readable information about failures, you
175 should incorporate it into your response protocol buffer and should
176 NOT call SetFailed().
178 raise NotImplementedError
181 """Checks if the client cancelled the RPC.
183 If true, indicates that the client canceled the RPC, so the server may
184 as well give up on replying to it. The server should still call the
185 final "done" callback.
187 raise NotImplementedError
190 """Sets a callback to invoke on cancel.
192 Asks that the given callback be called when the RPC is canceled. The
193 callback will always be called exactly once. If the RPC completes without
194 being canceled, the callback will be called after completion. If the RPC
195 has already been canceled when NotifyOnCancel() is called, the callback
196 will be called immediately.
198 NotifyOnCancel() must be called no more than once per request.
200 raise NotImplementedError
205 """Abstract interface for an RPC channel.
207 An RpcChannel represents a communication line to a service which can be used
208 to call that service's methods. The service may be running on another
209 machine. Normally, you should not use an RpcChannel directly, but instead
210 construct a stub {@link Service} wrapping it. Example:
213 RpcChannel channel = rpcImpl.Channel("remotehost.example.com:1234")
214 RpcController controller = rpcImpl.Controller()
215 MyService service = MyService_Stub(channel)
216 service.MyMethod(controller, request, callback)
220 request, response_class, done):
221 """Calls the method identified by the descriptor.
223 Call the given method of the remote service. The signature of this
224 procedure looks the same as Service.CallMethod(), but the requirements
225 are less strict in one important way: the request object doesn't have to
226 be of any specific class as long as its descriptor is method.input_type.
228 raise NotImplementedError