blob: e25db3e2a442095aec7c367afcc6b262e6a0474d [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
30from grpc._cython._cygrpc cimport call
31from grpc._cython._cygrpc cimport completion_queue
32from grpc._cython._cygrpc cimport credentials
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070033from grpc._cython._cygrpc cimport grpc
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070034from grpc._cython._cygrpc cimport records
35
36
37cdef class Channel:
38
39 def __cinit__(self, target, records.ChannelArgs arguments=None,
Masood Malekghassemi36297ac2015-11-04 15:17:31 -080040 credentials.ChannelCredentials channel_credentials=None):
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070041 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 Malekghassemi36297ac2015-11-04 15:17:31 -080052 if channel_credentials is None:
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070053 self.c_channel = grpc.grpc_insecure_channel_create(target, c_arguments,
54 NULL)
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070055 else:
56 self.c_channel = grpc.grpc_secure_channel_create(
Masood Malekghassemi36297ac2015-11-04 15:17:31 -080057 channel_credentials.c_credentials, target, c_arguments, NULL)
58 self.references.append(channel_credentials)
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070059 self.references.append(target)
60 self.references.append(arguments)
61
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070062 def create_call(self, call.Call parent, int flags,
63 completion_queue.CompletionQueue queue not None,
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070064 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 Malekghassemi5a65bcd2015-09-25 11:27:10 -070081 cdef grpc.grpc_call *parent_call = NULL
82 if parent is not None:
83 parent_call = parent.c_call
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070084 operation_call.c_call = grpc.grpc_channel_create_call(
Masood Malekghassemi5a65bcd2015-09-25 11:27:10 -070085 self.c_channel, parent_call, flags,
86 queue.c_completion_queue, method, host, deadline.c_time,
87 NULL)
Masood Malekghassemi743c10c2015-06-16 18:05:27 -070088 return operation_call
89
90 def __dealloc__(self):
91 if self.c_channel != NULL:
92 grpc.grpc_channel_destroy(self.c_channel)