blob: 55541934a1a2dc788dfe99b0df4c833d9206891b [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
mlumishb892a272014-12-09 16:28:23 -080034#include "channel.h"
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
murgatroid998242ba72015-04-01 15:29:44 -070040#include <php.h>
41#include <php_ini.h>
42#include <ext/standard/info.h>
43#include <ext/spl/spl_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080044#include "php_grpc.h"
45
murgatroid998242ba72015-04-01 15:29:44 -070046#include <zend_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080047
48#include <stdbool.h>
49
murgatroid998242ba72015-04-01 15:29:44 -070050#include <grpc/grpc.h>
51#include <grpc/support/log.h>
52#include <grpc/grpc_security.h>
mlumishb892a272014-12-09 16:28:23 -080053
Stanley Cheunga63fdd02015-08-11 15:11:11 -070054#include "completion_queue.h"
mlumishb892a272014-12-09 16:28:23 -080055#include "credentials.h"
Stanley Cheunga63fdd02015-08-11 15:11:11 -070056#include "server.h"
57#include "timeval.h"
mlumishb892a272014-12-09 16:28:23 -080058
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080059zend_class_entry *grpc_ce_channel;
60
mlumishb892a272014-12-09 16:28:23 -080061/* Frees and destroys an instance of wrapped_grpc_channel */
Craig Tillerb5dcec52015-01-13 11:13:42 -080062void free_wrapped_grpc_channel(void *object TSRMLS_DC) {
63 wrapped_grpc_channel *channel = (wrapped_grpc_channel *)object;
64 if (channel->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -080065 grpc_channel_destroy(channel->wrapped);
66 }
67 efree(channel);
68}
69
70/* Initializes an instance of wrapped_grpc_channel to be associated with an
71 * object of a class specified by class_type */
Craig Tillerb5dcec52015-01-13 11:13:42 -080072zend_object_value create_wrapped_grpc_channel(zend_class_entry *class_type
73 TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080074 zend_object_value retval;
75 wrapped_grpc_channel *intern;
Craig Tillerb5dcec52015-01-13 11:13:42 -080076 intern = (wrapped_grpc_channel *)emalloc(sizeof(wrapped_grpc_channel));
mlumishb892a272014-12-09 16:28:23 -080077 memset(intern, 0, sizeof(wrapped_grpc_channel));
78 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
79 object_properties_init(&intern->std, class_type);
80 retval.handle = zend_objects_store_put(
Craig Tillerb5dcec52015-01-13 11:13:42 -080081 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
82 free_wrapped_grpc_channel, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080083 retval.handlers = zend_get_std_object_handlers();
84 return retval;
85}
86
Craig Tillerb5dcec52015-01-13 11:13:42 -080087void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) {
mlumishb892a272014-12-09 16:28:23 -080088 HashTable *array_hash;
89 HashPosition array_pointer;
90 int args_index;
91 zval **data;
92 char *key;
93 uint key_len;
94 ulong index;
95 array_hash = Z_ARRVAL_P(args_array);
96 args->num_args = zend_hash_num_elements(array_hash);
97 args->args = ecalloc(args->num_args, sizeof(grpc_arg));
98 args_index = 0;
Craig Tillerb5dcec52015-01-13 11:13:42 -080099 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
100 zend_hash_get_current_data_ex(array_hash, (void **)&data,
101 &array_pointer) == SUCCESS;
102 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
103 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
104 &array_pointer) != HASH_KEY_IS_STRING) {
mlumishb892a272014-12-09 16:28:23 -0800105 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800106 "args keys must be strings", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800107 return;
108 }
109 args->args[args_index].key = key;
Craig Tillerb5dcec52015-01-13 11:13:42 -0800110 switch (Z_TYPE_P(*data)) {
mlumishb892a272014-12-09 16:28:23 -0800111 case IS_LONG:
112 args->args[args_index].value.integer = (int)Z_LVAL_P(*data);
113 break;
114 case IS_STRING:
115 args->args[args_index].value.string = Z_STRVAL_P(*data);
116 break;
117 default:
118 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800119 "args values must be int or string", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800120 return;
121 }
122 args_index++;
123 }
124}
125
126/**
127 * Construct an instance of the Channel class. If the $args array contains a
128 * "credentials" key mapping to a Credentials object, a secure channel will be
129 * created with those credentials.
130 * @param string $target The hostname to associate with this channel
131 * @param array $args The arguments to pass to the Channel (optional)
132 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800133PHP_METHOD(Channel, __construct) {
mlumishb892a272014-12-09 16:28:23 -0800134 wrapped_grpc_channel *channel =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800135 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800136 char *target;
137 int target_length;
138 zval *args_array = NULL;
139 grpc_channel_args args;
140 HashTable *array_hash;
141 zval **creds_obj = NULL;
Stanley Cheungaeea1022015-10-21 17:00:49 -0700142 wrapped_grpc_channel_credentials *creds = NULL;
mlumishb892a272014-12-09 16:28:23 -0800143 /* "s|a" == 1 string, 1 optional array */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800144 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &target,
145 &target_length, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800146 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800147 "Channel expects a string and an array", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800148 return;
149 }
150 if (args_array == NULL) {
Julien Boeuf5cc1e2e2015-08-25 22:07:58 -0700151 channel->wrapped = grpc_insecure_channel_create(target, NULL, NULL);
mlumishb892a272014-12-09 16:28:23 -0800152 } else {
153 array_hash = Z_ARRVAL_P(args_array);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800154 if (zend_hash_find(array_hash, "credentials", sizeof("credentials"),
155 (void **)&creds_obj) == SUCCESS) {
Stanley Cheungaeea1022015-10-21 17:00:49 -0700156 if (zend_get_class_entry(*creds_obj TSRMLS_CC) != grpc_ce_channel_credentials) {
mlumishb892a272014-12-09 16:28:23 -0800157 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheungaeea1022015-10-21 17:00:49 -0700158 "credentials must be a ChannelCredentials object",
mlumishb892a272014-12-09 16:28:23 -0800159 1 TSRMLS_CC);
160 return;
161 }
Stanley Cheungaeea1022015-10-21 17:00:49 -0700162 creds = (wrapped_grpc_channel_credentials *)zend_object_store_get_object(
mlumishb892a272014-12-09 16:28:23 -0800163 *creds_obj TSRMLS_CC);
164 zend_hash_del(array_hash, "credentials", 12);
165 }
166 php_grpc_read_args_array(args_array, &args);
167 if (creds == NULL) {
Nicolas "Pixel" Noble9d72b142015-08-08 01:45:38 +0200168 channel->wrapped = grpc_insecure_channel_create(target, &args, NULL);
mlumishb892a272014-12-09 16:28:23 -0800169 } else {
170 gpr_log(GPR_DEBUG, "Initialized secure channel");
Craig Tillerb5dcec52015-01-13 11:13:42 -0800171 channel->wrapped =
Julien Boeufc17fecd2015-08-25 19:41:20 -0700172 grpc_secure_channel_create(creds->wrapped, target, &args, NULL);
mlumishb892a272014-12-09 16:28:23 -0800173 }
174 efree(args.args);
175 }
mlumishb892a272014-12-09 16:28:23 -0800176}
177
178/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700179 * Get the endpoint this call/stream is connected to
180 * @return string The URI of the endpoint
181 */
182PHP_METHOD(Channel, getTarget) {
183 wrapped_grpc_channel *channel =
184 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
185 RETURN_STRING(grpc_channel_get_target(channel->wrapped), 1);
186}
187
188/**
Stanley Cheunge63354a2015-08-10 15:46:42 -0700189 * Get the connectivity state of the channel
190 * @param bool (optional) try to connect on the channel
191 * @return long The grpc connectivity state
192 */
193PHP_METHOD(Channel, getConnectivityState) {
194 wrapped_grpc_channel *channel =
195 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
196 bool try_to_connect;
197 /* "|b" == 1 optional bool */
198 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) ==
199 FAILURE) {
200 zend_throw_exception(spl_ce_InvalidArgumentException,
201 "getConnectivityState expects a bool", 1 TSRMLS_CC);
202 return;
203 }
204 RETURN_LONG(grpc_channel_check_connectivity_state(channel->wrapped,
205 (int)try_to_connect));
206}
207
208/**
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700209 * Watch the connectivity state of the channel until it changed
210 * @param long The previous connectivity state of the channel
211 * @param Timeval The deadline this function should wait until
Stanley Cheung4c5c7b82015-08-12 16:28:58 -0700212 * @return bool If the connectivity state changes from last_state
213 * before deadline
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700214 */
215PHP_METHOD(Channel, watchConnectivityState) {
216 wrapped_grpc_channel *channel =
217 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
218 long last_state;
219 zval *deadline_obj;
220 /* "lO" == 1 long 1 object */
221 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
222 &last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) {
223 zend_throw_exception(spl_ce_InvalidArgumentException,
224 "watchConnectivityState expects 1 long 1 timeval",
225 1 TSRMLS_CC);
226 return;
227 }
228
229 wrapped_grpc_timeval *deadline =
230 (wrapped_grpc_timeval *)zend_object_store_get_object(
231 deadline_obj TSRMLS_CC);
232 grpc_channel_watch_connectivity_state(
233 channel->wrapped, (grpc_connectivity_state)last_state,
234 deadline->wrapped, completion_queue, NULL);
235 grpc_event event = grpc_completion_queue_pluck(
236 completion_queue, NULL,
Nicolas "Pixel" Noblef1234432015-08-13 23:39:43 +0200237 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700238 RETURN_BOOL(event.success);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700239}
240
241/**
mlumishb892a272014-12-09 16:28:23 -0800242 * Close the channel
243 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800244PHP_METHOD(Channel, close) {
mlumishb892a272014-12-09 16:28:23 -0800245 wrapped_grpc_channel *channel =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800246 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
247 if (channel->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -0800248 grpc_channel_destroy(channel->wrapped);
249 channel->wrapped = NULL;
250 }
251}
252
253static zend_function_entry channel_methods[] = {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800254 PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
Stanley Cheungdb98e082015-07-27 10:19:45 -0700255 PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC)
Stanley Cheunge63354a2015-08-10 15:46:42 -0700256 PHP_ME(Channel, getConnectivityState, NULL, ZEND_ACC_PUBLIC)
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700257 PHP_ME(Channel, watchConnectivityState, NULL, ZEND_ACC_PUBLIC)
Stanley Cheungdb98e082015-07-27 10:19:45 -0700258 PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC)
259 PHP_FE_END};
mlumishb892a272014-12-09 16:28:23 -0800260
Craig Tillerb5dcec52015-01-13 11:13:42 -0800261void grpc_init_channel(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800262 zend_class_entry ce;
263 INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
264 ce.create_object = create_wrapped_grpc_channel;
265 grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
266}