blob: 30c871b078319d68d9e31f9cb96ebff4d86dc62a [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
55/* Frees and destroys a wrapped instance of grpc_completion_queue */
Craig Tillerb5dcec52015-01-13 11:13:42 -080056void free_wrapped_grpc_completion_queue(void *object TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080057 wrapped_grpc_completion_queue *queue = NULL;
58 grpc_event *event;
Craig Tillerb5dcec52015-01-13 11:13:42 -080059 queue = (wrapped_grpc_completion_queue *)object;
60 if (queue->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -080061 grpc_completion_queue_shutdown(queue->wrapped);
62 event = grpc_completion_queue_next(queue->wrapped, gpr_inf_future);
Craig Tillerb5dcec52015-01-13 11:13:42 -080063 while (event != NULL) {
64 if (event->type == GRPC_QUEUE_SHUTDOWN) {
mlumishb892a272014-12-09 16:28:23 -080065 break;
66 }
67 event = grpc_completion_queue_next(queue->wrapped, gpr_inf_future);
68 }
69 grpc_completion_queue_destroy(queue->wrapped);
70 }
71 efree(queue);
72}
73
74/* Initializes an instance of wrapped_grpc_channel to be associated with an
75 * object of a class specified by class_type */
76zend_object_value create_wrapped_grpc_completion_queue(
Craig Tillerb5dcec52015-01-13 11:13:42 -080077 zend_class_entry *class_type TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080078 zend_object_value retval;
79 wrapped_grpc_completion_queue *intern;
80
Craig Tillerb5dcec52015-01-13 11:13:42 -080081 intern = (wrapped_grpc_completion_queue *)emalloc(
mlumishb892a272014-12-09 16:28:23 -080082 sizeof(wrapped_grpc_completion_queue));
83 memset(intern, 0, sizeof(wrapped_grpc_completion_queue));
84
85 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
86 object_properties_init(&intern->std, class_type);
87 retval.handle = zend_objects_store_put(
Craig Tillerb5dcec52015-01-13 11:13:42 -080088 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
89 free_wrapped_grpc_completion_queue, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080090 retval.handlers = zend_get_std_object_handlers();
91 return retval;
92}
93
94/**
95 * Construct an instance of CompletionQueue
96 */
Craig Tillerb5dcec52015-01-13 11:13:42 -080097PHP_METHOD(CompletionQueue, __construct) {
mlumishb892a272014-12-09 16:28:23 -080098 wrapped_grpc_completion_queue *queue =
Craig Tillerecd49342015-01-18 14:36:47 -080099 (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis()
100 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800101 queue->wrapped = grpc_completion_queue_create();
102}
103
104/**
105 * Blocks until an event is available, the completion queue is being shutdown,
106 * or timeout is reached. Returns NULL on timeout, otherwise the event that
107 * occurred. Callers should call event.finish once they have processed the
108 * event.
109 * @param Timeval $timeout The timeout for the event
110 * @return Event The event that occurred
111 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800112PHP_METHOD(CompletionQueue, next) {
mlumishb892a272014-12-09 16:28:23 -0800113 zval *timeout;
114 /* "O" == 1 Object */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800115 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &timeout,
116 grpc_ce_timeval) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800117 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800118 "next needs a Timeval", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800119 return;
120 }
121 wrapped_grpc_completion_queue *completion_queue =
Craig Tillerecd49342015-01-18 14:36:47 -0800122 (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis()
123 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800124 wrapped_grpc_timeval *wrapped_timeout =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800125 (wrapped_grpc_timeval *)zend_object_store_get_object(timeout TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800126 grpc_event *event = grpc_completion_queue_next(completion_queue->wrapped,
127 wrapped_timeout->wrapped);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800128 if (event == NULL) {
mlumishb892a272014-12-09 16:28:23 -0800129 RETURN_NULL();
130 }
mlumish34cd1f02015-01-02 13:32:41 -0800131 zval *wrapped_event = grpc_php_convert_event(event);
mlumishb892a272014-12-09 16:28:23 -0800132 RETURN_DESTROY_ZVAL(wrapped_event);
133}
134
Craig Tillerb5dcec52015-01-13 11:13:42 -0800135PHP_METHOD(CompletionQueue, pluck) {
mlumishb892a272014-12-09 16:28:23 -0800136 long tag;
137 zval *timeout;
138 /* "lO" == 1 long, 1 Object */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800139 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", &tag, &timeout,
140 grpc_ce_timeval) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800141 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800142 "pluck needs a long and a Timeval", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800143 }
144 wrapped_grpc_completion_queue *completion_queue =
Craig Tillerecd49342015-01-18 14:36:47 -0800145 (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis()
146 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800147 wrapped_grpc_timeval *wrapped_timeout =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800148 (wrapped_grpc_timeval *)zend_object_store_get_object(timeout TSRMLS_CC);
149 grpc_event *event = grpc_completion_queue_pluck(
150 completion_queue->wrapped, (void *)tag, wrapped_timeout->wrapped);
151 if (event == NULL) {
mlumishb892a272014-12-09 16:28:23 -0800152 RETURN_NULL();
153 }
mlumish34cd1f02015-01-02 13:32:41 -0800154 zval *wrapped_event = grpc_php_convert_event(event);
mlumishb892a272014-12-09 16:28:23 -0800155 RETURN_DESTROY_ZVAL(wrapped_event);
156}
157
158static zend_function_entry completion_queue_methods[] = {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800159 PHP_ME(CompletionQueue, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
Craig Tillerecd49342015-01-18 14:36:47 -0800160 PHP_ME(CompletionQueue, next, NULL, ZEND_ACC_PUBLIC)
161 PHP_ME(CompletionQueue, pluck, NULL, ZEND_ACC_PUBLIC) PHP_FE_END};
mlumishb892a272014-12-09 16:28:23 -0800162
Craig Tillerb5dcec52015-01-13 11:13:42 -0800163void grpc_init_completion_queue(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800164 zend_class_entry ce;
165 INIT_CLASS_ENTRY(ce, "Grpc\\CompletionQueue", completion_queue_methods);
166 ce.create_object = create_wrapped_grpc_completion_queue;
167 grpc_ce_completion_queue = zend_register_internal_class(&ce TSRMLS_CC);
168}