aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/_framework/face/implementations.py
blob: 94362e20071b5d6d4ef7a30e56dc0739ce7c8939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright 2015, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Entry points into the Face layer of RPC Framework."""

from _framework.base import exceptions as _base_exceptions
from _framework.base import interfaces as base_interfaces
from _framework.face import _calls
from _framework.face import _service
from _framework.face import exceptions
from _framework.face import interfaces


class _BaseServicer(base_interfaces.Servicer):

  def __init__(self, methods, multi_method):
    self._methods = methods
    self._multi_method = multi_method

  def service(self, name, context, output_consumer):
    method = self._methods.get(name, None)
    if method is not None:
      return method(output_consumer, context)
    elif self._multi_method is not None:
      try:
        return self._multi_method.service(name, output_consumer, context)
      except exceptions.NoSuchMethodError:
        raise _base_exceptions.NoSuchMethodError()
    else:
      raise _base_exceptions.NoSuchMethodError()


class _Server(interfaces.Server):
  """An interfaces.Server implementation."""


class _Stub(interfaces.Stub):
  """An interfaces.Stub implementation."""

  def __init__(self, front, pool):
    self._front = front
    self._pool = pool

  def blocking_value_in_value_out(self, name, request, timeout):
    return _calls.blocking_value_in_value_out(
        self._front, name, request, timeout, 'unused trace ID')

  def future_value_in_value_out(self, name, request, timeout):
    return _calls.future_value_in_value_out(
        self._front, name, request, timeout, 'unused trace ID')

  def inline_value_in_stream_out(self, name, request, timeout):
    return _calls.inline_value_in_stream_out(
        self._front, name, request, timeout, 'unused trace ID')

  def blocking_stream_in_value_out(self, name, request_iterator, timeout):
    return _calls.blocking_stream_in_value_out(
        self._front, name, request_iterator, timeout, 'unused trace ID')

  def future_stream_in_value_out(self, name, request_iterator, timeout):
    return _calls.future_stream_in_value_out(
        self._front, name, request_iterator, timeout, 'unused trace ID',
        self._pool)

  def inline_stream_in_stream_out(self, name, request_iterator, timeout):
    return _calls.inline_stream_in_stream_out(
        self._front, name, request_iterator, timeout, 'unused trace ID',
        self._pool)

  def event_value_in_value_out(
      self, name, request, response_callback, abortion_callback, timeout):
    return _calls.event_value_in_value_out(
        self._front, name, request, response_callback, abortion_callback,
        timeout, 'unused trace ID')

  def event_value_in_stream_out(
      self, name, request, response_consumer, abortion_callback, timeout):
    return _calls.event_value_in_stream_out(
        self._front, name, request, response_consumer, abortion_callback,
        timeout, 'unused trace ID')

  def event_stream_in_value_out(
      self, name, response_callback, abortion_callback, timeout):
    return _calls.event_stream_in_value_out(
        self._front, name, response_callback, abortion_callback, timeout,
        'unused trace ID')

  def event_stream_in_stream_out(
      self, name, response_consumer, abortion_callback, timeout):
    return _calls.event_stream_in_stream_out(
        self._front, name, response_consumer, abortion_callback, timeout,
        'unused trace ID')


def _aggregate_methods(
    pool,
    inline_value_in_value_out_methods,
    inline_value_in_stream_out_methods,
    inline_stream_in_value_out_methods,
    inline_stream_in_stream_out_methods,
    event_value_in_value_out_methods,
    event_value_in_stream_out_methods,
    event_stream_in_value_out_methods,
    event_stream_in_stream_out_methods):
  """Aggregates methods coded in according to different interfaces."""
  methods = {}

  def adapt_unpooled_methods(adapted_methods, unadapted_methods, adaptation):
    if unadapted_methods is not None:
      for name, unadapted_method in unadapted_methods.iteritems():
        adapted_methods[name] = adaptation(unadapted_method)

  def adapt_pooled_methods(adapted_methods, unadapted_methods, adaptation):
    if unadapted_methods is not None:
      for name, unadapted_method in unadapted_methods.iteritems():
        adapted_methods[name] = adaptation(unadapted_method, pool)

  adapt_unpooled_methods(
      methods, inline_value_in_value_out_methods,
      _service.adapt_inline_value_in_value_out)
  adapt_unpooled_methods(
      methods, inline_value_in_stream_out_methods,
      _service.adapt_inline_value_in_stream_out)
  adapt_pooled_methods(
      methods, inline_stream_in_value_out_methods,
      _service.adapt_inline_stream_in_value_out)
  adapt_pooled_methods(
      methods, inline_stream_in_stream_out_methods,
      _service.adapt_inline_stream_in_stream_out)
  adapt_unpooled_methods(
      methods, event_value_in_value_out_methods,
      _service.adapt_event_value_in_value_out)
  adapt_unpooled_methods(
      methods, event_value_in_stream_out_methods,
      _service.adapt_event_value_in_stream_out)
  adapt_unpooled_methods(
      methods, event_stream_in_value_out_methods,
      _service.adapt_event_stream_in_value_out)
  adapt_unpooled_methods(
      methods, event_stream_in_stream_out_methods,
      _service.adapt_event_stream_in_stream_out)

  return methods


def servicer(
    pool,
    inline_value_in_value_out_methods=None,
    inline_value_in_stream_out_methods=None,
    inline_stream_in_value_out_methods=None,
    inline_stream_in_stream_out_methods=None,
    event_value_in_value_out_methods=None,
    event_value_in_stream_out_methods=None,
    event_stream_in_value_out_methods=None,
    event_stream_in_stream_out_methods=None,
    multi_method=None):
  """Creates a base_interfaces.Servicer.

  The key sets of the passed dictionaries must be disjoint. It is guaranteed
  that any passed MultiMethod implementation will only be called to service an
  RPC if the RPC method name is not present in the key sets of the passed
  dictionaries.

  Args:
    pool: A thread pool.
    inline_value_in_value_out_methods: A dictionary mapping method names to
      interfaces.InlineValueInValueOutMethod implementations.
    inline_value_in_stream_out_methods: A dictionary mapping method names to
      interfaces.InlineValueInStreamOutMethod implementations.
    inline_stream_in_value_out_methods: A dictionary mapping method names to
      interfaces.InlineStreamInValueOutMethod implementations.
    inline_stream_in_stream_out_methods: A dictionary mapping method names to
      interfaces.InlineStreamInStreamOutMethod implementations.
    event_value_in_value_out_methods: A dictionary mapping method names to
      interfaces.EventValueInValueOutMethod implementations.
    event_value_in_stream_out_methods: A dictionary mapping method names to
      interfaces.EventValueInStreamOutMethod implementations.
    event_stream_in_value_out_methods: A dictionary mapping method names to
      interfaces.EventStreamInValueOutMethod implementations.
    event_stream_in_stream_out_methods: A dictionary mapping method names to
      interfaces.EventStreamInStreamOutMethod implementations.
    multi_method: An implementation of interfaces.MultiMethod.

  Returns:
    A base_interfaces.Servicer that services RPCs via the given implementations.
  """
  methods = _aggregate_methods(
      pool,
      inline_value_in_value_out_methods,
      inline_value_in_stream_out_methods,
      inline_stream_in_value_out_methods,
      inline_stream_in_stream_out_methods,
      event_value_in_value_out_methods,
      event_value_in_stream_out_methods,
      event_stream_in_value_out_methods,
      event_stream_in_stream_out_methods)

  return _BaseServicer(methods, multi_method)


def server():
  """Creates an interfaces.Server.

  Returns:
    An interfaces.Server.
  """
  return _Server()


def stub(front, pool):
  """Creates an interfaces.Stub.

  Args:
    front: A base_interfaces.Front.
    pool: A futures.ThreadPoolExecutor.

  Returns:
    An interfaces.Stub that performs RPCs via the given base_interfaces.Front.
  """
  return _Stub(front, pool)