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