OLD | NEW |
(Empty) | |
| 1 # Copyright 2015, Google Inc. |
| 2 # All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 """Entry points into the Crust layer of RPC Framework.""" |
| 31 |
| 32 from grpc.framework.common import cardinality |
| 33 from grpc.framework.common import style |
| 34 from grpc.framework.crust import _calls |
| 35 from grpc.framework.crust import _service |
| 36 from grpc.framework.interfaces.base import base |
| 37 from grpc.framework.interfaces.face import face |
| 38 |
| 39 |
| 40 class _BaseServicer(base.Servicer): |
| 41 |
| 42 def __init__(self, adapted_methods, adapted_multi_method): |
| 43 self._adapted_methods = adapted_methods |
| 44 self._adapted_multi_method = adapted_multi_method |
| 45 |
| 46 def service(self, group, method, context, output_operator): |
| 47 adapted_method = self._adapted_methods.get((group, method), None) |
| 48 if adapted_method is not None: |
| 49 return adapted_method(output_operator, context) |
| 50 elif self._adapted_multi_method is not None: |
| 51 try: |
| 52 return self._adapted_multi_method( |
| 53 group, method, output_operator, context) |
| 54 except face.NoSuchMethodError: |
| 55 raise base.NoSuchMethodError(None, None) |
| 56 else: |
| 57 raise base.NoSuchMethodError(None, None) |
| 58 |
| 59 |
| 60 class _UnaryUnaryMultiCallable(face.UnaryUnaryMultiCallable): |
| 61 |
| 62 def __init__(self, end, group, method, pool): |
| 63 self._end = end |
| 64 self._group = group |
| 65 self._method = method |
| 66 self._pool = pool |
| 67 |
| 68 def __call__( |
| 69 self, request, timeout, metadata=None, with_call=False, |
| 70 protocol_options=None): |
| 71 return _calls.blocking_unary_unary( |
| 72 self._end, self._group, self._method, timeout, with_call, |
| 73 protocol_options, metadata, request) |
| 74 |
| 75 def future(self, request, timeout, metadata=None, protocol_options=None): |
| 76 return _calls.future_unary_unary( |
| 77 self._end, self._group, self._method, timeout, protocol_options, |
| 78 metadata, request) |
| 79 |
| 80 def event( |
| 81 self, request, receiver, abortion_callback, timeout, |
| 82 metadata=None, protocol_options=None): |
| 83 return _calls.event_unary_unary( |
| 84 self._end, self._group, self._method, timeout, protocol_options, |
| 85 metadata, request, receiver, abortion_callback, self._pool) |
| 86 |
| 87 |
| 88 class _UnaryStreamMultiCallable(face.UnaryStreamMultiCallable): |
| 89 |
| 90 def __init__(self, end, group, method, pool): |
| 91 self._end = end |
| 92 self._group = group |
| 93 self._method = method |
| 94 self._pool = pool |
| 95 |
| 96 def __call__(self, request, timeout, metadata=None, protocol_options=None): |
| 97 return _calls.inline_unary_stream( |
| 98 self._end, self._group, self._method, timeout, protocol_options, |
| 99 metadata, request) |
| 100 |
| 101 def event( |
| 102 self, request, receiver, abortion_callback, timeout, |
| 103 metadata=None, protocol_options=None): |
| 104 return _calls.event_unary_stream( |
| 105 self._end, self._group, self._method, timeout, protocol_options, |
| 106 metadata, request, receiver, abortion_callback, self._pool) |
| 107 |
| 108 |
| 109 class _StreamUnaryMultiCallable(face.StreamUnaryMultiCallable): |
| 110 |
| 111 def __init__(self, end, group, method, pool): |
| 112 self._end = end |
| 113 self._group = group |
| 114 self._method = method |
| 115 self._pool = pool |
| 116 |
| 117 def __call__( |
| 118 self, request_iterator, timeout, metadata=None, |
| 119 with_call=False, protocol_options=None): |
| 120 return _calls.blocking_stream_unary( |
| 121 self._end, self._group, self._method, timeout, with_call, |
| 122 protocol_options, metadata, request_iterator, self._pool) |
| 123 |
| 124 def future( |
| 125 self, request_iterator, timeout, metadata=None, protocol_options=None): |
| 126 return _calls.future_stream_unary( |
| 127 self._end, self._group, self._method, timeout, protocol_options, |
| 128 metadata, request_iterator, self._pool) |
| 129 |
| 130 def event( |
| 131 self, receiver, abortion_callback, timeout, metadata=None, |
| 132 protocol_options=None): |
| 133 return _calls.event_stream_unary( |
| 134 self._end, self._group, self._method, timeout, protocol_options, |
| 135 metadata, receiver, abortion_callback, self._pool) |
| 136 |
| 137 |
| 138 class _StreamStreamMultiCallable(face.StreamStreamMultiCallable): |
| 139 |
| 140 def __init__(self, end, group, method, pool): |
| 141 self._end = end |
| 142 self._group = group |
| 143 self._method = method |
| 144 self._pool = pool |
| 145 |
| 146 def __call__( |
| 147 self, request_iterator, timeout, metadata=None, protocol_options=None): |
| 148 return _calls.inline_stream_stream( |
| 149 self._end, self._group, self._method, timeout, protocol_options, |
| 150 metadata, request_iterator, self._pool) |
| 151 |
| 152 def event( |
| 153 self, receiver, abortion_callback, timeout, metadata=None, |
| 154 protocol_options=None): |
| 155 return _calls.event_stream_stream( |
| 156 self._end, self._group, self._method, timeout, protocol_options, |
| 157 metadata, receiver, abortion_callback, self._pool) |
| 158 |
| 159 |
| 160 class _GenericStub(face.GenericStub): |
| 161 """An face.GenericStub implementation.""" |
| 162 |
| 163 def __init__(self, end, pool): |
| 164 self._end = end |
| 165 self._pool = pool |
| 166 |
| 167 def blocking_unary_unary( |
| 168 self, group, method, request, timeout, metadata=None, |
| 169 with_call=None, protocol_options=None): |
| 170 return _calls.blocking_unary_unary( |
| 171 self._end, group, method, timeout, with_call, protocol_options, |
| 172 metadata, request) |
| 173 |
| 174 def future_unary_unary( |
| 175 self, group, method, request, timeout, metadata=None, |
| 176 protocol_options=None): |
| 177 return _calls.future_unary_unary( |
| 178 self._end, group, method, timeout, protocol_options, metadata, request) |
| 179 |
| 180 def inline_unary_stream( |
| 181 self, group, method, request, timeout, metadata=None, |
| 182 protocol_options=None): |
| 183 return _calls.inline_unary_stream( |
| 184 self._end, group, method, timeout, protocol_options, metadata, request) |
| 185 |
| 186 def blocking_stream_unary( |
| 187 self, group, method, request_iterator, timeout, metadata=None, |
| 188 with_call=None, protocol_options=None): |
| 189 return _calls.blocking_stream_unary( |
| 190 self._end, group, method, timeout, with_call, protocol_options, |
| 191 metadata, request_iterator, self._pool) |
| 192 |
| 193 def future_stream_unary( |
| 194 self, group, method, request_iterator, timeout, metadata=None, |
| 195 protocol_options=None): |
| 196 return _calls.future_stream_unary( |
| 197 self._end, group, method, timeout, protocol_options, metadata, |
| 198 request_iterator, self._pool) |
| 199 |
| 200 def inline_stream_stream( |
| 201 self, group, method, request_iterator, timeout, metadata=None, |
| 202 protocol_options=None): |
| 203 return _calls.inline_stream_stream( |
| 204 self._end, group, method, timeout, protocol_options, metadata, |
| 205 request_iterator, self._pool) |
| 206 |
| 207 def event_unary_unary( |
| 208 self, group, method, request, receiver, abortion_callback, timeout, |
| 209 metadata=None, protocol_options=None): |
| 210 return _calls.event_unary_unary( |
| 211 self._end, group, method, timeout, protocol_options, metadata, request, |
| 212 receiver, abortion_callback, self._pool) |
| 213 |
| 214 def event_unary_stream( |
| 215 self, group, method, request, receiver, abortion_callback, timeout, |
| 216 metadata=None, protocol_options=None): |
| 217 return _calls.event_unary_stream( |
| 218 self._end, group, method, timeout, protocol_options, metadata, request, |
| 219 receiver, abortion_callback, self._pool) |
| 220 |
| 221 def event_stream_unary( |
| 222 self, group, method, receiver, abortion_callback, timeout, |
| 223 metadata=None, protocol_options=None): |
| 224 return _calls.event_stream_unary( |
| 225 self._end, group, method, timeout, protocol_options, metadata, receiver, |
| 226 abortion_callback, self._pool) |
| 227 |
| 228 def event_stream_stream( |
| 229 self, group, method, receiver, abortion_callback, timeout, |
| 230 metadata=None, protocol_options=None): |
| 231 return _calls.event_stream_stream( |
| 232 self._end, group, method, timeout, protocol_options, metadata, receiver, |
| 233 abortion_callback, self._pool) |
| 234 |
| 235 def unary_unary(self, group, method): |
| 236 return _UnaryUnaryMultiCallable(self._end, group, method, self._pool) |
| 237 |
| 238 def unary_stream(self, group, method): |
| 239 return _UnaryStreamMultiCallable(self._end, group, method, self._pool) |
| 240 |
| 241 def stream_unary(self, group, method): |
| 242 return _StreamUnaryMultiCallable(self._end, group, method, self._pool) |
| 243 |
| 244 def stream_stream(self, group, method): |
| 245 return _StreamStreamMultiCallable(self._end, group, method, self._pool) |
| 246 |
| 247 |
| 248 class _DynamicStub(face.DynamicStub): |
| 249 """An face.DynamicStub implementation.""" |
| 250 |
| 251 def __init__(self, end, group, cardinalities, pool): |
| 252 self._end = end |
| 253 self._group = group |
| 254 self._cardinalities = cardinalities |
| 255 self._pool = pool |
| 256 |
| 257 def __getattr__(self, attr): |
| 258 method_cardinality = self._cardinalities.get(attr) |
| 259 if method_cardinality is cardinality.Cardinality.UNARY_UNARY: |
| 260 return _UnaryUnaryMultiCallable(self._end, self._group, attr, self._pool) |
| 261 elif method_cardinality is cardinality.Cardinality.UNARY_STREAM: |
| 262 return _UnaryStreamMultiCallable(self._end, self._group, attr, self._pool) |
| 263 elif method_cardinality is cardinality.Cardinality.STREAM_UNARY: |
| 264 return _StreamUnaryMultiCallable(self._end, self._group, attr, self._pool) |
| 265 elif method_cardinality is cardinality.Cardinality.STREAM_STREAM: |
| 266 return _StreamStreamMultiCallable( |
| 267 self._end, self._group, attr, self._pool) |
| 268 else: |
| 269 raise AttributeError('_DynamicStub object has no attribute "%s"!' % attr) |
| 270 |
| 271 |
| 272 def _adapt_method_implementations(method_implementations, pool): |
| 273 adapted_implementations = {} |
| 274 for name, method_implementation in method_implementations.iteritems(): |
| 275 if method_implementation.style is style.Service.INLINE: |
| 276 if method_implementation.cardinality is cardinality.Cardinality.UNARY_UNAR
Y: |
| 277 adapted_implementations[name] = _service.adapt_inline_unary_unary( |
| 278 method_implementation.unary_unary_inline, pool) |
| 279 elif method_implementation.cardinality is cardinality.Cardinality.UNARY_ST
REAM: |
| 280 adapted_implementations[name] = _service.adapt_inline_unary_stream( |
| 281 method_implementation.unary_stream_inline, pool) |
| 282 elif method_implementation.cardinality is cardinality.Cardinality.STREAM_U
NARY: |
| 283 adapted_implementations[name] = _service.adapt_inline_stream_unary( |
| 284 method_implementation.stream_unary_inline, pool) |
| 285 elif method_implementation.cardinality is cardinality.Cardinality.STREAM_S
TREAM: |
| 286 adapted_implementations[name] = _service.adapt_inline_stream_stream( |
| 287 method_implementation.stream_stream_inline, pool) |
| 288 elif method_implementation.style is style.Service.EVENT: |
| 289 if method_implementation.cardinality is cardinality.Cardinality.UNARY_UNAR
Y: |
| 290 adapted_implementations[name] = _service.adapt_event_unary_unary( |
| 291 method_implementation.unary_unary_event, pool) |
| 292 elif method_implementation.cardinality is cardinality.Cardinality.UNARY_ST
REAM: |
| 293 adapted_implementations[name] = _service.adapt_event_unary_stream( |
| 294 method_implementation.unary_stream_event, pool) |
| 295 elif method_implementation.cardinality is cardinality.Cardinality.STREAM_U
NARY: |
| 296 adapted_implementations[name] = _service.adapt_event_stream_unary( |
| 297 method_implementation.stream_unary_event, pool) |
| 298 elif method_implementation.cardinality is cardinality.Cardinality.STREAM_S
TREAM: |
| 299 adapted_implementations[name] = _service.adapt_event_stream_stream( |
| 300 method_implementation.stream_stream_event, pool) |
| 301 return adapted_implementations |
| 302 |
| 303 |
| 304 def servicer(method_implementations, multi_method_implementation, pool): |
| 305 """Creates a base.Servicer. |
| 306 |
| 307 It is guaranteed that any passed face.MultiMethodImplementation will |
| 308 only be called to service an RPC if there is no |
| 309 face.MethodImplementation for the RPC method in the passed |
| 310 method_implementations dictionary. |
| 311 |
| 312 Args: |
| 313 method_implementations: A dictionary from RPC method name to |
| 314 face.MethodImplementation object to be used to service the named |
| 315 RPC method. |
| 316 multi_method_implementation: An face.MultiMethodImplementation to be |
| 317 used to service any RPCs not serviced by the |
| 318 face.MethodImplementations given in the method_implementations |
| 319 dictionary, or None. |
| 320 pool: A thread pool. |
| 321 |
| 322 Returns: |
| 323 A base.Servicer that services RPCs via the given implementations. |
| 324 """ |
| 325 adapted_implementations = _adapt_method_implementations( |
| 326 method_implementations, pool) |
| 327 if multi_method_implementation is None: |
| 328 adapted_multi_method_implementation = None |
| 329 else: |
| 330 adapted_multi_method_implementation = _service.adapt_multi_method( |
| 331 multi_method_implementation, pool) |
| 332 return _BaseServicer( |
| 333 adapted_implementations, adapted_multi_method_implementation) |
| 334 |
| 335 |
| 336 def generic_stub(end, pool): |
| 337 """Creates an face.GenericStub. |
| 338 |
| 339 Args: |
| 340 end: A base.End. |
| 341 pool: A futures.ThreadPoolExecutor. |
| 342 |
| 343 Returns: |
| 344 A face.GenericStub that performs RPCs via the given base.End. |
| 345 """ |
| 346 return _GenericStub(end, pool) |
| 347 |
| 348 |
| 349 def dynamic_stub(end, group, cardinalities, pool): |
| 350 """Creates an face.DynamicStub. |
| 351 |
| 352 Args: |
| 353 end: A base.End. |
| 354 group: The group identifier for all RPCs to be made with the created |
| 355 face.DynamicStub. |
| 356 cardinalities: A dict from method identifier to cardinality.Cardinality |
| 357 value identifying the cardinality of every RPC method to be supported by |
| 358 the created face.DynamicStub. |
| 359 pool: A futures.ThreadPoolExecutor. |
| 360 |
| 361 Returns: |
| 362 A face.DynamicStub that performs RPCs via the given base.End. |
| 363 """ |
| 364 return _DynamicStub(end, group, cardinalities, pool) |
OLD | NEW |