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 "event.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 "php_grpc.h" |
| 44 | |
| 45 | #include <stdbool.h> |
| 46 | |
| 47 | #include "grpc/grpc.h" |
| 48 | |
| 49 | #include "byte_buffer.h" |
| 50 | #include "call.h" |
| 51 | #include "timeval.h" |
| 52 | |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 53 | /* Create a new PHP object containing the event data in the event struct. |
| 54 | event must not be used after this function is called */ |
| 55 | zval *grpc_php_convert_event(grpc_event *event) { |
| 56 | zval *data_object; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 57 | char *detail_string; |
| 58 | size_t detail_len; |
| 59 | char *method_string; |
| 60 | size_t method_len; |
| 61 | char *host_string; |
| 62 | size_t host_len; |
| 63 | char *read_string; |
| 64 | size_t read_len; |
| 65 | |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 66 | zval *event_object; |
| 67 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 68 | if (event == NULL) { |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | MAKE_STD_ZVAL(event_object); |
| 73 | object_init(event_object); |
| 74 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 75 | add_property_zval( |
| 76 | event_object, "call", |
| 77 | grpc_php_wrap_call(event->call, event->type == GRPC_SERVER_RPC_NEW)); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 78 | add_property_long(event_object, "type", event->type); |
| 79 | add_property_long(event_object, "tag", (long)event->tag); |
| 80 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 81 | switch (event->type) { |
| 82 | case GRPC_QUEUE_SHUTDOWN: |
| 83 | add_property_null(event_object, "data"); |
| 84 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 85 | case GRPC_READ: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 86 | if (event->data.read == NULL) { |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 87 | add_property_null(event_object, "data"); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 88 | } else { |
| 89 | byte_buffer_to_string(event->data.read, &read_string, &read_len); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 90 | add_property_stringl(event_object, "data", read_string, read_len, true); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 91 | } |
| 92 | break; |
| 93 | case GRPC_INVOKE_ACCEPTED: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 94 | add_property_long(event_object, "data", |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 95 | (long)event->data.invoke_accepted); |
| 96 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 97 | case GRPC_WRITE_ACCEPTED: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 98 | add_property_long(event_object, "data", (long)event->data.write_accepted); |
| 99 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 100 | case GRPC_FINISH_ACCEPTED: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 101 | add_property_long(event_object, "data", |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 102 | (long)event->data.finish_accepted); |
| 103 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 104 | case GRPC_CLIENT_METADATA_READ: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 105 | data_object = grpc_call_create_metadata_array( |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 106 | event->data.client_metadata_read.count, |
| 107 | event->data.client_metadata_read.elements); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 108 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 109 | break; |
| 110 | case GRPC_FINISHED: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 111 | MAKE_STD_ZVAL(data_object); |
| 112 | object_init(data_object); |
| 113 | add_property_long(data_object, "code", event->data.finished.status); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 114 | if (event->data.finished.details == NULL) { |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 115 | add_property_null(data_object, "details"); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 116 | } else { |
| 117 | detail_len = strlen(event->data.finished.details); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 118 | detail_string = ecalloc(detail_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 119 | memcpy(detail_string, event->data.finished.details, detail_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 120 | add_property_string(data_object, "details", detail_string, true); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 121 | } |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 122 | add_property_zval(data_object, "metadata", |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 123 | grpc_call_create_metadata_array( |
| 124 | event->data.finished.metadata_count, |
| 125 | event->data.finished.metadata_elements)); |
| 126 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 127 | break; |
| 128 | case GRPC_SERVER_RPC_NEW: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 129 | MAKE_STD_ZVAL(data_object); |
| 130 | object_init(data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 131 | method_len = strlen(event->data.server_rpc_new.method); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 132 | method_string = ecalloc(method_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 133 | memcpy(method_string, event->data.server_rpc_new.method, method_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 134 | add_property_string(data_object, "method", method_string, false); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 135 | host_len = strlen(event->data.server_rpc_new.host); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 136 | host_string = ecalloc(host_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 137 | memcpy(host_string, event->data.server_rpc_new.host, host_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 138 | add_property_string(data_object, "host", host_string, false); |
| 139 | add_property_zval( |
| 140 | data_object, "absolute_timeout", |
| 141 | grpc_php_wrap_timeval(event->data.server_rpc_new.deadline)); |
| 142 | add_property_zval(data_object, "metadata", |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 143 | grpc_call_create_metadata_array( |
| 144 | event->data.server_rpc_new.metadata_count, |
| 145 | event->data.server_rpc_new.metadata_elements)); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 146 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 147 | break; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 148 | default: |
| 149 | add_property_null(event_object, "data"); |
| 150 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 151 | } |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 152 | grpc_event_finish(event); |
| 153 | return event_object; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 154 | } |