Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 34 | #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 Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 55 | zend_class_entry *grpc_ce_completion_queue; |
| 56 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 57 | /* Frees and destroys a wrapped instance of grpc_completion_queue */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 58 | void free_wrapped_grpc_completion_queue(void *object TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 59 | wrapped_grpc_completion_queue *queue = NULL; |
| 60 | grpc_event *event; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 61 | queue = (wrapped_grpc_completion_queue *)object; |
| 62 | if (queue->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 63 | grpc_completion_queue_shutdown(queue->wrapped); |
| 64 | event = grpc_completion_queue_next(queue->wrapped, gpr_inf_future); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 65 | while (event != NULL) { |
| 66 | if (event->type == GRPC_QUEUE_SHUTDOWN) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 67 | 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 */ |
| 78 | zend_object_value create_wrapped_grpc_completion_queue( |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 79 | zend_class_entry *class_type TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 80 | zend_object_value retval; |
| 81 | wrapped_grpc_completion_queue *intern; |
| 82 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 83 | intern = (wrapped_grpc_completion_queue *)emalloc( |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 84 | 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 Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 90 | intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 91 | free_wrapped_grpc_completion_queue, NULL TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 92 | retval.handlers = zend_get_std_object_handlers(); |
| 93 | return retval; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Construct an instance of CompletionQueue |
| 98 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 99 | PHP_METHOD(CompletionQueue, __construct) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 100 | wrapped_grpc_completion_queue *queue = |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 101 | (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis() |
| 102 | TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 103 | 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 Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 114 | PHP_METHOD(CompletionQueue, next) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 115 | zval *timeout; |
| 116 | /* "O" == 1 Object */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 117 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &timeout, |
| 118 | grpc_ce_timeval) == FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 119 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 120 | "next needs a Timeval", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | wrapped_grpc_completion_queue *completion_queue = |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 124 | (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis() |
| 125 | TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 126 | wrapped_grpc_timeval *wrapped_timeout = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 127 | (wrapped_grpc_timeval *)zend_object_store_get_object(timeout TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 128 | grpc_event *event = grpc_completion_queue_next(completion_queue->wrapped, |
| 129 | wrapped_timeout->wrapped); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 130 | if (event == NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 131 | RETURN_NULL(); |
| 132 | } |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 133 | zval *wrapped_event = grpc_php_convert_event(event); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 134 | RETURN_DESTROY_ZVAL(wrapped_event); |
| 135 | } |
| 136 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 137 | PHP_METHOD(CompletionQueue, pluck) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 138 | long tag; |
| 139 | zval *timeout; |
| 140 | /* "lO" == 1 long, 1 Object */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 141 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", &tag, &timeout, |
| 142 | grpc_ce_timeval) == FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 143 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 144 | "pluck needs a long and a Timeval", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 145 | } |
| 146 | wrapped_grpc_completion_queue *completion_queue = |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 147 | (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis() |
| 148 | TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 149 | wrapped_grpc_timeval *wrapped_timeout = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 150 | (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) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 154 | RETURN_NULL(); |
| 155 | } |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 156 | zval *wrapped_event = grpc_php_convert_event(event); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 157 | RETURN_DESTROY_ZVAL(wrapped_event); |
| 158 | } |
| 159 | |
| 160 | static zend_function_entry completion_queue_methods[] = { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 161 | PHP_ME(CompletionQueue, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 162 | PHP_ME(CompletionQueue, next, NULL, ZEND_ACC_PUBLIC) |
| 163 | PHP_ME(CompletionQueue, pluck, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 164 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 165 | void grpc_init_completion_queue(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 166 | 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 | } |