The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | # Protocol Buffers - Google's data interchange format |
| 2 | # Copyright 2008 Google Inc. All rights reserved. |
| 3 | # http://code.google.com/p/protobuf/ |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Declares the RPC service interfaces. |
| 32 | |
| 33 | This module declares the abstract interfaces underlying proto2 RPC |
| 34 | services. These are intented to be independent of any particular RPC |
| 35 | implementation, so that proto2 services can be used on top of a variety |
| 36 | of implementations. |
| 37 | """ |
| 38 | |
| 39 | __author__ = 'petar@google.com (Petar Petrov)' |
| 40 | |
| 41 | |
| 42 | class Service(object): |
| 43 | |
| 44 | """Abstract base interface for protocol-buffer-based RPC services. |
| 45 | |
| 46 | Services themselves are abstract classes (implemented either by servers or as |
| 47 | stubs), but they subclass this base interface. The methods of this |
| 48 | interface can be used to call the methods of the service without knowing |
| 49 | its exact type at compile time (analogous to the Message interface). |
| 50 | """ |
| 51 | |
| 52 | def GetDescriptor(self): |
| 53 | """Retrieves this service's descriptor.""" |
| 54 | raise NotImplementedError |
| 55 | |
| 56 | def CallMethod(self, method_descriptor, rpc_controller, |
| 57 | request, done): |
| 58 | """Calls a method of the service specified by method_descriptor. |
| 59 | |
| 60 | Preconditions: |
| 61 | * method_descriptor.service == GetDescriptor |
| 62 | * request is of the exact same classes as returned by |
| 63 | GetRequestClass(method). |
| 64 | * After the call has started, the request must not be modified. |
| 65 | * "rpc_controller" is of the correct type for the RPC implementation being |
| 66 | used by this Service. For stubs, the "correct type" depends on the |
| 67 | RpcChannel which the stub is using. |
| 68 | |
| 69 | Postconditions: |
| 70 | * "done" will be called when the method is complete. This may be |
| 71 | before CallMethod() returns or it may be at some point in the future. |
| 72 | """ |
| 73 | raise NotImplementedError |
| 74 | |
| 75 | def GetRequestClass(self, method_descriptor): |
| 76 | """Returns the class of the request message for the specified method. |
| 77 | |
| 78 | CallMethod() requires that the request is of a particular subclass of |
| 79 | Message. GetRequestClass() gets the default instance of this required |
| 80 | type. |
| 81 | |
| 82 | Example: |
| 83 | method = service.GetDescriptor().FindMethodByName("Foo") |
| 84 | request = stub.GetRequestClass(method)() |
| 85 | request.ParseFromString(input) |
| 86 | service.CallMethod(method, request, callback) |
| 87 | """ |
| 88 | raise NotImplementedError |
| 89 | |
| 90 | def GetResponseClass(self, method_descriptor): |
| 91 | """Returns the class of the response message for the specified method. |
| 92 | |
| 93 | This method isn't really needed, as the RpcChannel's CallMethod constructs |
| 94 | the response protocol message. It's provided anyway in case it is useful |
| 95 | for the caller to know the response type in advance. |
| 96 | """ |
| 97 | raise NotImplementedError |
| 98 | |
| 99 | |
| 100 | class RpcController(object): |
| 101 | |
| 102 | """An RpcController mediates a single method call. |
| 103 | |
| 104 | The primary purpose of the controller is to provide a way to manipulate |
| 105 | settings specific to the RPC implementation and to find out about RPC-level |
| 106 | errors. The methods provided by the RpcController interface are intended |
| 107 | to be a "least common denominator" set of features which we expect all |
| 108 | implementations to support. Specific implementations may provide more |
| 109 | advanced features (e.g. deadline propagation). |
| 110 | """ |
| 111 | |
| 112 | # Client-side methods below |
| 113 | |
| 114 | def Reset(self): |
| 115 | """Resets the RpcController to its initial state. |
| 116 | |
| 117 | After the RpcController has been reset, it may be reused in |
| 118 | a new call. Must not be called while an RPC is in progress. |
| 119 | """ |
| 120 | raise NotImplementedError |
| 121 | |
| 122 | def Failed(self): |
| 123 | """Returns true if the call failed. |
| 124 | |
| 125 | After a call has finished, returns true if the call failed. The possible |
| 126 | reasons for failure depend on the RPC implementation. Failed() must not |
| 127 | be called before a call has finished. If Failed() returns true, the |
| 128 | contents of the response message are undefined. |
| 129 | """ |
| 130 | raise NotImplementedError |
| 131 | |
| 132 | def ErrorText(self): |
| 133 | """If Failed is true, returns a human-readable description of the error.""" |
| 134 | raise NotImplementedError |
| 135 | |
| 136 | def StartCancel(self): |
| 137 | """Initiate cancellation. |
| 138 | |
| 139 | Advises the RPC system that the caller desires that the RPC call be |
| 140 | canceled. The RPC system may cancel it immediately, may wait awhile and |
| 141 | then cancel it, or may not even cancel the call at all. If the call is |
| 142 | canceled, the "done" callback will still be called and the RpcController |
| 143 | will indicate that the call failed at that time. |
| 144 | """ |
| 145 | raise NotImplementedError |
| 146 | |
| 147 | # Server-side methods below |
| 148 | |
| 149 | def SetFailed(self, reason): |
| 150 | """Sets a failure reason. |
| 151 | |
| 152 | Causes Failed() to return true on the client side. "reason" will be |
| 153 | incorporated into the message returned by ErrorText(). If you find |
| 154 | you need to return machine-readable information about failures, you |
| 155 | should incorporate it into your response protocol buffer and should |
| 156 | NOT call SetFailed(). |
| 157 | """ |
| 158 | raise NotImplementedError |
| 159 | |
| 160 | def IsCanceled(self): |
| 161 | """Checks if the client cancelled the RPC. |
| 162 | |
| 163 | If true, indicates that the client canceled the RPC, so the server may |
| 164 | as well give up on replying to it. The server should still call the |
| 165 | final "done" callback. |
| 166 | """ |
| 167 | raise NotImplementedError |
| 168 | |
| 169 | def NotifyOnCancel(self, callback): |
| 170 | """Sets a callback to invoke on cancel. |
| 171 | |
| 172 | Asks that the given callback be called when the RPC is canceled. The |
| 173 | callback will always be called exactly once. If the RPC completes without |
| 174 | being canceled, the callback will be called after completion. If the RPC |
| 175 | has already been canceled when NotifyOnCancel() is called, the callback |
| 176 | will be called immediately. |
| 177 | |
| 178 | NotifyOnCancel() must be called no more than once per request. |
| 179 | """ |
| 180 | raise NotImplementedError |
| 181 | |
| 182 | |
| 183 | class RpcChannel(object): |
| 184 | |
| 185 | """Abstract interface for an RPC channel. |
| 186 | |
| 187 | An RpcChannel represents a communication line to a service which can be used |
| 188 | to call that service's methods. The service may be running on another |
| 189 | machine. Normally, you should not use an RpcChannel directly, but instead |
| 190 | construct a stub {@link Service} wrapping it. Example: |
| 191 | |
| 192 | Example: |
| 193 | RpcChannel channel = rpcImpl.Channel("remotehost.example.com:1234") |
| 194 | RpcController controller = rpcImpl.Controller() |
| 195 | MyService service = MyService_Stub(channel) |
| 196 | service.MyMethod(controller, request, callback) |
| 197 | """ |
| 198 | |
| 199 | def CallMethod(self, method_descriptor, rpc_controller, |
| 200 | request, response_class, done): |
| 201 | """Calls the method identified by the descriptor. |
| 202 | |
| 203 | Call the given method of the remote service. The signature of this |
| 204 | procedure looks the same as Service.CallMethod(), but the requirements |
| 205 | are less strict in one important way: the request object doesn't have to |
| 206 | be of any specific class as long as its descriptor is method.input_type. |
| 207 | """ |
| 208 | raise NotImplementedError |