blob: a944a83576c5dfe393124af74df9573e74ba5295 [file] [log] [blame]
Masood Malekghassemi743c10c2015-06-16 18:05:27 -07001# 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 Malekghassemiaed42a82015-12-07 16:35:40 -080030cimport cpython
31
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070032from grpc._cython._cygrpc cimport call
33from grpc._cython._cygrpc cimport completion_queue
34from grpc._cython._cygrpc cimport credentials
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070035from grpc._cython._cygrpc cimport grpc
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070036from grpc._cython._cygrpc cimport records
37
38
39cdef class Channel:
40
41 def __cinit__(self, target, records.ChannelArgs arguments=None,
Masood Malekghassemi36297ac2015-11-04 15:17:31 -080042 credentials.ChannelCredentials channel_credentials=None):
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070043 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 Malekghassemi36297ac2015-11-04 15:17:31 -080054 if channel_credentials is None:
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070055 self.c_channel = grpc.grpc_insecure_channel_create(target, c_arguments,
56 NULL)
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070057 else:
58 self.c_channel = grpc.grpc_secure_channel_create(
Masood Malekghassemi36297ac2015-11-04 15:17:31 -080059 channel_credentials.c_credentials, target, c_arguments, NULL)
60 self.references.append(channel_credentials)
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070061 self.references.append(target)
62 self.references.append(arguments)
63
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070064 def create_call(self, call.Call parent, int flags,
65 completion_queue.CompletionQueue queue not None,
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070066 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 Malekghassemiaed42a82015-12-07 16:35:40 -080075 cdef char *host_c_string = NULL
76 if host is None:
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070077 pass
Masood Malekghassemiaed42a82015-12-07 16:35:40 -080078 elif isinstance(host, bytes):
79 host_c_string = host
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070080 elif isinstance(host, basestring):
81 host = host.encode()
Masood Malekghassemiaed42a82015-12-07 16:35:40 -080082 host_c_string = host
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070083 else:
Masood Malekghassemiaed42a82015-12-07 16:35:40 -080084 raise TypeError("expected host to be str, bytes, or None")
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070085 cdef call.Call operation_call = call.Call()
86 operation_call.references = [self, method, host, queue]
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070087 cdef grpc.grpc_call *parent_call = NULL
88 if parent is not None:
89 parent_call = parent.c_call
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070090 operation_call.c_call = grpc.grpc_channel_create_call(
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070091 self.c_channel, parent_call, flags,
Masood Malekghassemiaed42a82015-12-07 16:35:40 -080092 queue.c_completion_queue, method, host_c_string, deadline.c_time,
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070093 NULL)
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070094 return operation_call
95
Masood Malekghassemiaed42a82015-12-07 16:35:40 -080096 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 Malekghassemi743c10c2015-06-16 18:05:27 -0700115 def __dealloc__(self):
116 if self.c_channel != NULL:
117 grpc.grpc_channel_destroy(self.c_channel)