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, _VTable vtable not None): 34 self._user_tag = user_tag 35 self.call = None 36 self.call_details = None 37 self._vtable = vtable 38 39 cdef void prepare(self) except *: 40 self.call = Call(self._vtable) 41 self.call_details = CallDetails() 42 grpc_metadata_array_init(&self.c_invocation_metadata) 43 44 cdef RequestCallEvent event(self, grpc_event c_event): 45 cdef tuple invocation_metadata = _metadata(&self.c_invocation_metadata) 46 grpc_metadata_array_destroy(&self.c_invocation_metadata) 47 return RequestCallEvent( 48 c_event.type, c_event.success, self._user_tag, self.call, 49 self.call_details, invocation_metadata) 50 51 52cdef class _BatchOperationTag: 53 54 def __cinit__(self, user_tag, operations, call): 55 self._user_tag = user_tag 56 self._operations = operations 57 self._retained_call = call 58 59 cdef void prepare(self) except *: 60 cdef Operation operation 61 self.c_nops = 0 if self._operations is None else len(self._operations) 62 if 0 < self.c_nops: 63 self.c_ops = <grpc_op *>gpr_malloc(sizeof(grpc_op) * self.c_nops) 64 for index, operation in enumerate(self._operations): 65 operation.c() 66 self.c_ops[index] = operation.c_op 67 68 cdef BatchOperationEvent event(self, grpc_event c_event): 69 cdef Operation operation 70 if 0 < self.c_nops: 71 for operation in self._operations: 72 operation.un_c() 73 gpr_free(self.c_ops) 74 return BatchOperationEvent( 75 c_event.type, c_event.success, self._user_tag, self._operations) 76 else: 77 return BatchOperationEvent( 78 c_event.type, c_event.success, self._user_tag, ()) 79 80 81cdef class _ServerShutdownTag(_Tag): 82 83 def __cinit__(self, user_tag, shutting_down_server): 84 self._user_tag = user_tag 85 self._shutting_down_server = shutting_down_server 86 87 cdef ServerShutdownEvent event(self, grpc_event c_event): 88 self._shutting_down_server.notify_shutdown_complete() 89 return ServerShutdownEvent(c_event.type, c_event.success, self._user_tag) 90