blob: 8d94c59683e939a29bb6be038f1b6df0a2bd9785 [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller1a61b172015-02-16 11:53:47 -08004 * 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>
murgatroid998242ba72015-04-01 15:29:44 -070051#include <grpc/grpc_security.h>
mlumishb892a272014-12-09 16:28:23 -080052
Stanley Cheunga63fdd02015-08-11 15:11:11 -070053#include "completion_queue.h"
Stanley Cheung9c0b35e2015-10-21 17:07:56 -070054#include "channel_credentials.h"
Stanley Cheunga63fdd02015-08-11 15:11:11 -070055#include "server.h"
56#include "timeval.h"
mlumishb892a272014-12-09 16:28:23 -080057
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080058zend_class_entry *grpc_ce_channel;
59
mlumishb892a272014-12-09 16:28:23 -080060/* Frees and destroys an instance of wrapped_grpc_channel */
Craig Tillerb5dcec52015-01-13 11:13:42 -080061void free_wrapped_grpc_channel(void *object TSRMLS_DC) {
62 wrapped_grpc_channel *channel = (wrapped_grpc_channel *)object;
63 if (channel->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -080064 grpc_channel_destroy(channel->wrapped);
65 }
66 efree(channel);
67}
68
69/* Initializes an instance of wrapped_grpc_channel to be associated with an
70 * object of a class specified by class_type */
Craig Tillerb5dcec52015-01-13 11:13:42 -080071zend_object_value create_wrapped_grpc_channel(zend_class_entry *class_type
72 TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080073 zend_object_value retval;
74 wrapped_grpc_channel *intern;
Craig Tillerb5dcec52015-01-13 11:13:42 -080075 intern = (wrapped_grpc_channel *)emalloc(sizeof(wrapped_grpc_channel));
mlumishb892a272014-12-09 16:28:23 -080076 memset(intern, 0, sizeof(wrapped_grpc_channel));
77 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
78 object_properties_init(&intern->std, class_type);
79 retval.handle = zend_objects_store_put(
Craig Tillerb5dcec52015-01-13 11:13:42 -080080 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
81 free_wrapped_grpc_channel, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080082 retval.handlers = zend_get_std_object_handlers();
83 return retval;
84}
85
Michael Bausor4f8e40b2016-05-16 11:41:25 -070086void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080087 HashTable *array_hash;
88 HashPosition array_pointer;
89 int args_index;
90 zval **data;
91 char *key;
92 uint key_len;
93 ulong index;
94 array_hash = Z_ARRVAL_P(args_array);
95 args->num_args = zend_hash_num_elements(array_hash);
96 args->args = ecalloc(args->num_args, sizeof(grpc_arg));
97 args_index = 0;
Craig Tillerb5dcec52015-01-13 11:13:42 -080098 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
99 zend_hash_get_current_data_ex(array_hash, (void **)&data,
100 &array_pointer) == SUCCESS;
101 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
102 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
103 &array_pointer) != HASH_KEY_IS_STRING) {
mlumishb892a272014-12-09 16:28:23 -0800104 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800105 "args keys must be strings", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800106 return;
107 }
108 args->args[args_index].key = key;
Craig Tillerb5dcec52015-01-13 11:13:42 -0800109 switch (Z_TYPE_P(*data)) {
mlumishb892a272014-12-09 16:28:23 -0800110 case IS_LONG:
111 args->args[args_index].value.integer = (int)Z_LVAL_P(*data);
Stanley Cheung7895da22016-03-17 16:49:57 -0700112 args->args[args_index].type = GRPC_ARG_INTEGER;
mlumishb892a272014-12-09 16:28:23 -0800113 break;
114 case IS_STRING:
115 args->args[args_index].value.string = Z_STRVAL_P(*data);
Stanley Cheung7895da22016-03-17 16:49:57 -0700116 args->args[args_index].type = GRPC_ARG_STRING;
mlumishb892a272014-12-09 16:28:23 -0800117 break;
118 default:
119 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800120 "args values must be int or string", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800121 return;
122 }
123 args_index++;
124 }
125}
126
127/**
128 * Construct an instance of the Channel class. If the $args array contains a
Stanley Cheung9c0b35e2015-10-21 17:07:56 -0700129 * "credentials" key mapping to a ChannelCredentials object, a secure channel
130 * will be created with those credentials.
mlumishb892a272014-12-09 16:28:23 -0800131 * @param string $target The hostname to associate with this channel
132 * @param array $args The arguments to pass to the Channel (optional)
133 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800134PHP_METHOD(Channel, __construct) {
mlumishb892a272014-12-09 16:28:23 -0800135 wrapped_grpc_channel *channel =
Stanley Cheung9c0b35e2015-10-21 17:07:56 -0700136 (wrapped_grpc_channel *)zend_object_store_get_object(
137 getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800138 char *target;
139 int target_length;
140 zval *args_array = NULL;
141 grpc_channel_args args;
142 HashTable *array_hash;
143 zval **creds_obj = NULL;
Stanley Cheungaeea1022015-10-21 17:00:49 -0700144 wrapped_grpc_channel_credentials *creds = NULL;
Stanley Cheungf77a4ad2016-02-16 09:45:51 -0800145 /* "sa" == 1 string, 1 array */
146 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800147 &target_length, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800148 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800149 "Channel expects a string and an array", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800150 return;
151 }
Stanley Cheungcccf9292016-02-12 16:37:19 -0800152 array_hash = Z_ARRVAL_P(args_array);
153 if (zend_hash_find(array_hash, "credentials", sizeof("credentials"),
154 (void **)&creds_obj) == SUCCESS) {
155 if (Z_TYPE_P(*creds_obj) == IS_NULL) {
156 creds = NULL;
157 zend_hash_del(array_hash, "credentials", 12);
158 } else if (zend_get_class_entry(*creds_obj TSRMLS_CC) !=
159 grpc_ce_channel_credentials) {
160 zend_throw_exception(spl_ce_InvalidArgumentException,
161 "credentials must be a ChannelCredentials object",
162 1 TSRMLS_CC);
163 return;
mlumishb892a272014-12-09 16:28:23 -0800164 } else {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800165 creds = (wrapped_grpc_channel_credentials *)zend_object_store_get_object(
166 *creds_obj TSRMLS_CC);
167 zend_hash_del(array_hash, "credentials", 12);
mlumishb892a272014-12-09 16:28:23 -0800168 }
mlumishb892a272014-12-09 16:28:23 -0800169 }
Michael Bausor4f8e40b2016-05-16 11:41:25 -0700170 php_grpc_read_args_array(args_array, &args TSRMLS_CC);
Stanley Cheungcccf9292016-02-12 16:37:19 -0800171 if (creds == NULL) {
172 channel->wrapped = grpc_insecure_channel_create(target, &args, NULL);
173 } else {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800174 channel->wrapped =
175 grpc_secure_channel_create(creds->wrapped, target, &args, NULL);
176 }
177 efree(args.args);
mlumishb892a272014-12-09 16:28:23 -0800178}
179
180/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700181 * Get the endpoint this call/stream is connected to
182 * @return string The URI of the endpoint
183 */
184PHP_METHOD(Channel, getTarget) {
185 wrapped_grpc_channel *channel =
186 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
187 RETURN_STRING(grpc_channel_get_target(channel->wrapped), 1);
188}
189
190/**
Stanley Cheunge63354a2015-08-10 15:46:42 -0700191 * Get the connectivity state of the channel
192 * @param bool (optional) try to connect on the channel
193 * @return long The grpc connectivity state
194 */
195PHP_METHOD(Channel, getConnectivityState) {
196 wrapped_grpc_channel *channel =
197 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
198 bool try_to_connect;
199 /* "|b" == 1 optional bool */
200 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) ==
201 FAILURE) {
202 zend_throw_exception(spl_ce_InvalidArgumentException,
203 "getConnectivityState expects a bool", 1 TSRMLS_CC);
204 return;
205 }
206 RETURN_LONG(grpc_channel_check_connectivity_state(channel->wrapped,
207 (int)try_to_connect));
208}
209
210/**
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700211 * Watch the connectivity state of the channel until it changed
212 * @param long The previous connectivity state of the channel
213 * @param Timeval The deadline this function should wait until
Stanley Cheung4c5c7b82015-08-12 16:28:58 -0700214 * @return bool If the connectivity state changes from last_state
215 * before deadline
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700216 */
217PHP_METHOD(Channel, watchConnectivityState) {
218 wrapped_grpc_channel *channel =
219 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
220 long last_state;
221 zval *deadline_obj;
222 /* "lO" == 1 long 1 object */
223 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
224 &last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) {
225 zend_throw_exception(spl_ce_InvalidArgumentException,
226 "watchConnectivityState expects 1 long 1 timeval",
227 1 TSRMLS_CC);
228 return;
229 }
230
231 wrapped_grpc_timeval *deadline =
232 (wrapped_grpc_timeval *)zend_object_store_get_object(
233 deadline_obj TSRMLS_CC);
234 grpc_channel_watch_connectivity_state(
235 channel->wrapped, (grpc_connectivity_state)last_state,
236 deadline->wrapped, completion_queue, NULL);
237 grpc_event event = grpc_completion_queue_pluck(
238 completion_queue, NULL,
Nicolas "Pixel" Noblef1234432015-08-13 23:39:43 +0200239 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700240 RETURN_BOOL(event.success);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700241}
242
243/**
mlumishb892a272014-12-09 16:28:23 -0800244 * Close the channel
245 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800246PHP_METHOD(Channel, close) {
mlumishb892a272014-12-09 16:28:23 -0800247 wrapped_grpc_channel *channel =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800248 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
249 if (channel->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -0800250 grpc_channel_destroy(channel->wrapped);
251 channel->wrapped = NULL;
252 }
253}
254
255static zend_function_entry channel_methods[] = {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800256 PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
Stanley Cheungdb98e082015-07-27 10:19:45 -0700257 PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC)
Stanley Cheunge63354a2015-08-10 15:46:42 -0700258 PHP_ME(Channel, getConnectivityState, NULL, ZEND_ACC_PUBLIC)
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700259 PHP_ME(Channel, watchConnectivityState, NULL, ZEND_ACC_PUBLIC)
Stanley Cheungdb98e082015-07-27 10:19:45 -0700260 PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC)
261 PHP_FE_END};
mlumishb892a272014-12-09 16:28:23 -0800262
Craig Tillerb5dcec52015-01-13 11:13:42 -0800263void grpc_init_channel(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800264 zend_class_entry ce;
265 INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
266 ce.create_object = create_wrapped_grpc_channel;
267 grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
268}