1# Copyright 2017 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16cdef class _Tag:
17
18  cdef object event(self, grpc_event c_event):
19    raise NotImplementedError()
20
21
22cdef class _ConnectivityTag(_Tag):
23
24  def __cinit__(self, user_tag):
25    self._user_tag = user_tag
26
27  cdef ConnectivityEvent event(self, grpc_event c_event):
28    return ConnectivityEvent(c_event.type, c_event.success, self._user_tag)
29
30
31cdef class _RequestCallTag(_Tag):
32
33  def __cinit__(self, user_tag):
34    self._user_tag = user_tag
35    self.call = None
36    self.call_details = None
37
38  cdef void prepare(self) except *:
39    self.call = Call()
40    self.call_details = CallDetails()
41    grpc_metadata_array_init(&self.c_invocation_metadata)
42
43  cdef RequestCallEvent event(self, grpc_event c_event):
44    cdef tuple invocation_metadata = _metadata(&self.c_invocation_metadata)
45    grpc_metadata_array_destroy(&self.c_invocation_metadata)
46    return RequestCallEvent(
47        c_event.type, c_event.success, self._user_tag, self.call,
48        self.call_details, invocation_metadata)
49
50
51cdef class _BatchOperationTag:
52
53  def __cinit__(self, user_tag, operations, call):
54    self._user_tag = user_tag
55    self._operations = operations
56    self._retained_call = call
57
58  cdef void prepare(self) except *:
59    cdef Operation operation
60    self.c_nops = 0 if self._operations is None else len(self._operations)
61    if 0 < self.c_nops:
62      self.c_ops = <grpc_op *>gpr_malloc(sizeof(grpc_op) * self.c_nops)
63      for index, operation in enumerate(self._operations):
64        operation.c()
65        self.c_ops[index] = operation.c_op
66
67  cdef BatchOperationEvent event(self, grpc_event c_event):
68    cdef Operation operation
69    if 0 < self.c_nops:
70      for operation in self._operations:
71        operation.un_c()
72      gpr_free(self.c_ops)
73      return BatchOperationEvent(
74          c_event.type, c_event.success, self._user_tag, self._operations)
75    else:
76      return BatchOperationEvent(
77          c_event.type, c_event.success, self._user_tag, ())
78
79
80cdef class _ServerShutdownTag(_Tag):
81
82  def __cinit__(self, user_tag, shutting_down_server):
83    self._user_tag = user_tag
84    self._shutting_down_server = shutting_down_server
85
86  cdef ServerShutdownEvent event(self, grpc_event c_event):
87    self._shutting_down_server.notify_shutdown_complete()
88    return ServerShutdownEvent(c_event.type, c_event.success, self._user_tag)
89