Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 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 | |
Masood Malekghassemi | aed42a8 | 2015-12-07 16:35:40 -0800 | [diff] [blame^] | 30 | cimport cpython |
| 31 | |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 32 | from grpc._cython._cygrpc cimport call |
| 33 | from grpc._cython._cygrpc cimport completion_queue |
| 34 | from grpc._cython._cygrpc cimport credentials |
Masood Malekghassemi | 5a65bcd | 2015-09-25 11:27:10 -0700 | [diff] [blame] | 35 | from grpc._cython._cygrpc cimport grpc |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 36 | from grpc._cython._cygrpc cimport records |
| 37 | |
| 38 | |
| 39 | cdef class Channel: |
| 40 | |
| 41 | def __cinit__(self, target, records.ChannelArgs arguments=None, |
Masood Malekghassemi | 36297ac | 2015-11-04 15:17:31 -0800 | [diff] [blame] | 42 | credentials.ChannelCredentials channel_credentials=None): |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 43 | cdef grpc.grpc_channel_args *c_arguments = NULL |
| 44 | self.c_channel = NULL |
| 45 | self.references = [] |
| 46 | if arguments is not None: |
| 47 | c_arguments = &arguments.c_args |
| 48 | if isinstance(target, bytes): |
| 49 | pass |
| 50 | elif isinstance(target, basestring): |
| 51 | target = target.encode() |
| 52 | else: |
| 53 | raise TypeError("expected target to be str or bytes") |
Masood Malekghassemi | 36297ac | 2015-11-04 15:17:31 -0800 | [diff] [blame] | 54 | if channel_credentials is None: |
Masood Malekghassemi | 5a65bcd | 2015-09-25 11:27:10 -0700 | [diff] [blame] | 55 | self.c_channel = grpc.grpc_insecure_channel_create(target, c_arguments, |
| 56 | NULL) |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 57 | else: |
| 58 | self.c_channel = grpc.grpc_secure_channel_create( |
Masood Malekghassemi | 36297ac | 2015-11-04 15:17:31 -0800 | [diff] [blame] | 59 | channel_credentials.c_credentials, target, c_arguments, NULL) |
| 60 | self.references.append(channel_credentials) |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 61 | self.references.append(target) |
| 62 | self.references.append(arguments) |
| 63 | |
Masood Malekghassemi | 5a65bcd | 2015-09-25 11:27:10 -0700 | [diff] [blame] | 64 | def create_call(self, call.Call parent, int flags, |
| 65 | completion_queue.CompletionQueue queue not None, |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 66 | method, host, records.Timespec deadline not None): |
| 67 | if queue.is_shutting_down: |
| 68 | raise ValueError("queue must not be shutting down or shutdown") |
| 69 | if isinstance(method, bytes): |
| 70 | pass |
| 71 | elif isinstance(method, basestring): |
| 72 | method = method.encode() |
| 73 | else: |
| 74 | raise TypeError("expected method to be str or bytes") |
Masood Malekghassemi | aed42a8 | 2015-12-07 16:35:40 -0800 | [diff] [blame^] | 75 | cdef char *host_c_string = NULL |
| 76 | if host is None: |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 77 | pass |
Masood Malekghassemi | aed42a8 | 2015-12-07 16:35:40 -0800 | [diff] [blame^] | 78 | elif isinstance(host, bytes): |
| 79 | host_c_string = host |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 80 | elif isinstance(host, basestring): |
| 81 | host = host.encode() |
Masood Malekghassemi | aed42a8 | 2015-12-07 16:35:40 -0800 | [diff] [blame^] | 82 | host_c_string = host |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 83 | else: |
Masood Malekghassemi | aed42a8 | 2015-12-07 16:35:40 -0800 | [diff] [blame^] | 84 | raise TypeError("expected host to be str, bytes, or None") |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 85 | cdef call.Call operation_call = call.Call() |
| 86 | operation_call.references = [self, method, host, queue] |
Masood Malekghassemi | 5a65bcd | 2015-09-25 11:27:10 -0700 | [diff] [blame] | 87 | cdef grpc.grpc_call *parent_call = NULL |
| 88 | if parent is not None: |
| 89 | parent_call = parent.c_call |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 90 | operation_call.c_call = grpc.grpc_channel_create_call( |
Masood Malekghassemi | 5a65bcd | 2015-09-25 11:27:10 -0700 | [diff] [blame] | 91 | self.c_channel, parent_call, flags, |
Masood Malekghassemi | aed42a8 | 2015-12-07 16:35:40 -0800 | [diff] [blame^] | 92 | queue.c_completion_queue, method, host_c_string, deadline.c_time, |
Masood Malekghassemi | 5a65bcd | 2015-09-25 11:27:10 -0700 | [diff] [blame] | 93 | NULL) |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 94 | return operation_call |
| 95 | |
Masood Malekghassemi | aed42a8 | 2015-12-07 16:35:40 -0800 | [diff] [blame^] | 96 | def check_connectivity_state(self, bint try_to_connect): |
| 97 | return grpc.grpc_channel_check_connectivity_state(self.c_channel, |
| 98 | try_to_connect) |
| 99 | |
| 100 | def watch_connectivity_state( |
| 101 | self, last_observed_state, records.Timespec deadline not None, |
| 102 | completion_queue.CompletionQueue queue not None, tag): |
| 103 | cdef records.OperationTag operation_tag = records.OperationTag(tag) |
| 104 | cpython.Py_INCREF(operation_tag) |
| 105 | grpc.grpc_channel_watch_connectivity_state( |
| 106 | self.c_channel, last_observed_state, deadline.c_time, |
| 107 | queue.c_completion_queue, <cpython.PyObject *>operation_tag) |
| 108 | |
| 109 | def target(self): |
| 110 | cdef char * target = grpc.grpc_channel_get_target(self.c_channel) |
| 111 | result = <bytes>target |
| 112 | grpc.gpr_free(target) |
| 113 | return result |
| 114 | |
Masood Malekghassemi | 743c10c | 2015-06-16 18:05:27 -0700 | [diff] [blame] | 115 | def __dealloc__(self): |
| 116 | if self.c_channel != NULL: |
| 117 | grpc.grpc_channel_destroy(self.c_channel) |