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