blob: 93abf5df36bff2b7da199fdc9b964ff5fff27daf [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 "completion_queue.h"
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40#include "php.h"
41#include "php_ini.h"
42#include "ext/standard/info.h"
43#include "ext/spl/spl_exceptions.h"
44#include "php_grpc.h"
45
46#include "zend_exceptions.h"
47
48#include <stdbool.h>
49
50#include "grpc/grpc.h"
51
52#include "event.h"
53#include "timeval.h"
54
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080055zend_class_entry *grpc_ce_completion_queue;
56
mlumishb892a272014-12-09 16:28:23 -080057/* Frees and destroys a wrapped instance of grpc_completion_queue */
Craig Tillerb5dcec52015-01-13 11:13:42 -080058void free_wrapped_grpc_completion_queue(void *object TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080059 wrapped_grpc_completion_queue *queue = NULL;
60 grpc_event *event;
Craig Tillerb5dcec52015-01-13 11:13:42 -080061 queue = (wrapped_grpc_completion_queue *)object;
62 if (queue->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -080063 grpc_completion_queue_shutdown(queue->wrapped);
64 event = grpc_completion_queue_next(queue->wrapped, gpr_inf_future);
Craig Tillerb5dcec52015-01-13 11:13:42 -080065 while (event != NULL) {
66 if (event->type == GRPC_QUEUE_SHUTDOWN) {
mlumishb892a272014-12-09 16:28:23 -080067 break;
68 }
69 event = grpc_completion_queue_next(queue->wrapped, gpr_inf_future);
70 }
71 grpc_completion_queue_destroy(queue->wrapped);
72 }
73 efree(queue);
74}
75
76/* Initializes an instance of wrapped_grpc_channel to be associated with an
77 * object of a class specified by class_type */
78zend_object_value create_wrapped_grpc_completion_queue(
Craig Tillerb5dcec52015-01-13 11:13:42 -080079 zend_class_entry *class_type TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080080 zend_object_value retval;
81 wrapped_grpc_completion_queue *intern;
82
Craig Tillerb5dcec52015-01-13 11:13:42 -080083 intern = (wrapped_grpc_completion_queue *)emalloc(
mlumishb892a272014-12-09 16:28:23 -080084 sizeof(wrapped_grpc_completion_queue));
85 memset(intern, 0, sizeof(wrapped_grpc_completion_queue));
86
87 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
88 object_properties_init(&intern->std, class_type);
89 retval.handle = zend_objects_store_put(
Craig Tillerb5dcec52015-01-13 11:13:42 -080090 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
91 free_wrapped_grpc_completion_queue, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080092 retval.handlers = zend_get_std_object_handlers();
93 return retval;
94}
95
96/**
97 * Construct an instance of CompletionQueue
98 */
Craig Tillerb5dcec52015-01-13 11:13:42 -080099PHP_METHOD(CompletionQueue, __construct) {
mlumishb892a272014-12-09 16:28:23 -0800100 wrapped_grpc_completion_queue *queue =
Craig Tillerecd49342015-01-18 14:36:47 -0800101 (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis()
102 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800103 queue->wrapped = grpc_completion_queue_create();
104}
105
106/**
107 * Blocks until an event is available, the completion queue is being shutdown,
108 * or timeout is reached. Returns NULL on timeout, otherwise the event that
109 * occurred. Callers should call event.finish once they have processed the
110 * event.
111 * @param Timeval $timeout The timeout for the event
112 * @return Event The event that occurred
113 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800114PHP_METHOD(CompletionQueue, next) {
mlumishb892a272014-12-09 16:28:23 -0800115 zval *timeout;
116 /* "O" == 1 Object */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800117 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &timeout,
118 grpc_ce_timeval) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800119 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800120 "next needs a Timeval", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800121 return;
122 }
123 wrapped_grpc_completion_queue *completion_queue =
Craig Tillerecd49342015-01-18 14:36:47 -0800124 (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis()
125 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800126 wrapped_grpc_timeval *wrapped_timeout =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800127 (wrapped_grpc_timeval *)zend_object_store_get_object(timeout TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800128 grpc_event *event = grpc_completion_queue_next(completion_queue->wrapped,
129 wrapped_timeout->wrapped);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800130 if (event == NULL) {
mlumishb892a272014-12-09 16:28:23 -0800131 RETURN_NULL();
132 }
mlumish34cd1f02015-01-02 13:32:41 -0800133 zval *wrapped_event = grpc_php_convert_event(event);
mlumishb892a272014-12-09 16:28:23 -0800134 RETURN_DESTROY_ZVAL(wrapped_event);
135}
136
Craig Tillerb5dcec52015-01-13 11:13:42 -0800137PHP_METHOD(CompletionQueue, pluck) {
mlumishb892a272014-12-09 16:28:23 -0800138 long tag;
139 zval *timeout;
140 /* "lO" == 1 long, 1 Object */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800141 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", &tag, &timeout,
142 grpc_ce_timeval) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800143 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800144 "pluck needs a long and a Timeval", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800145 }
146 wrapped_grpc_completion_queue *completion_queue =
Craig Tillerecd49342015-01-18 14:36:47 -0800147 (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis()
148 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800149 wrapped_grpc_timeval *wrapped_timeout =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800150 (wrapped_grpc_timeval *)zend_object_store_get_object(timeout TSRMLS_CC);
151 grpc_event *event = grpc_completion_queue_pluck(
152 completion_queue->wrapped, (void *)tag, wrapped_timeout->wrapped);
153 if (event == NULL) {
mlumishb892a272014-12-09 16:28:23 -0800154 RETURN_NULL();
155 }
mlumish34cd1f02015-01-02 13:32:41 -0800156 zval *wrapped_event = grpc_php_convert_event(event);
mlumishb892a272014-12-09 16:28:23 -0800157 RETURN_DESTROY_ZVAL(wrapped_event);
158}
159
160static zend_function_entry completion_queue_methods[] = {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800161 PHP_ME(CompletionQueue, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
Craig Tillerecd49342015-01-18 14:36:47 -0800162 PHP_ME(CompletionQueue, next, NULL, ZEND_ACC_PUBLIC)
163 PHP_ME(CompletionQueue, pluck, NULL, ZEND_ACC_PUBLIC) PHP_FE_END};
mlumishb892a272014-12-09 16:28:23 -0800164
Craig Tillerb5dcec52015-01-13 11:13:42 -0800165void grpc_init_completion_queue(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800166 zend_class_entry ce;
167 INIT_CLASS_ENTRY(ce, "Grpc\\CompletionQueue", completion_queue_methods);
168 ce.create_object = create_wrapped_grpc_completion_queue;
169 grpc_ce_completion_queue = zend_register_internal_class(&ce TSRMLS_CC);
170}