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):
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):
59    self.c_nops = 0 if self._operations is None else len(self._operations)
60    if 0 < self.c_nops:
61      self.c_ops = <grpc_op *>gpr_malloc(sizeof(grpc_op) * self.c_nops)
62      for index, operation in enumerate(self._operations):
63        (<Operation>operation).c()
64        self.c_ops[index] = (<Operation>operation).c_op
65
66  cdef BatchOperationEvent event(self, grpc_event c_event):
67    if 0 < self.c_nops:
68      for index, operation in enumerate(self._operations):
69        (<Operation>operation).c_op = self.c_ops[index]
70        (<Operation>operation).un_c()
71      gpr_free(self.c_ops)
72      return BatchOperationEvent(
73          c_event.type, c_event.success, self._user_tag, self._operations)
74    else:
75      return BatchOperationEvent(
76          c_event.type, c_event.success, self._user_tag, ())
77
78
79cdef class _ServerShutdownTag(_Tag):
80
81  def __cinit__(self, user_tag, shutting_down_server):
82    self._user_tag = user_tag
83    self._shutting_down_server = shutting_down_server
84
85  cdef ServerShutdownEvent event(self, grpc_event c_event):
86    self._shutting_down_server.notify_shutdown_complete()
87    return ServerShutdownEvent(c_event.type, c_event.success, self._user_tag)