mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | #include "event.h" |
| 2 | |
| 3 | #ifdef HAVE_CONFIG_H |
| 4 | #include "config.h" |
| 5 | #endif |
| 6 | |
| 7 | #include "php.h" |
| 8 | #include "php_ini.h" |
| 9 | #include "ext/standard/info.h" |
| 10 | #include "php_grpc.h" |
| 11 | |
| 12 | #include <stdbool.h> |
| 13 | |
| 14 | #include "grpc/grpc.h" |
| 15 | |
| 16 | #include "byte_buffer.h" |
| 17 | #include "call.h" |
| 18 | #include "timeval.h" |
| 19 | |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 20 | /* Create a new PHP object containing the event data in the event struct. |
| 21 | event must not be used after this function is called */ |
| 22 | zval *grpc_php_convert_event(grpc_event *event) { |
| 23 | zval *data_object; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 24 | char *detail_string; |
| 25 | size_t detail_len; |
| 26 | char *method_string; |
| 27 | size_t method_len; |
| 28 | char *host_string; |
| 29 | size_t host_len; |
| 30 | char *read_string; |
| 31 | size_t read_len; |
| 32 | |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 33 | zval *event_object; |
| 34 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 35 | if (event == NULL) { |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | MAKE_STD_ZVAL(event_object); |
| 40 | object_init(event_object); |
| 41 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 42 | add_property_zval( |
| 43 | event_object, "call", |
| 44 | grpc_php_wrap_call(event->call, event->type == GRPC_SERVER_RPC_NEW)); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 45 | add_property_long(event_object, "type", event->type); |
| 46 | add_property_long(event_object, "tag", (long)event->tag); |
| 47 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 48 | switch (event->type) { |
| 49 | case GRPC_QUEUE_SHUTDOWN: |
| 50 | add_property_null(event_object, "data"); |
| 51 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 52 | case GRPC_READ: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 53 | if (event->data.read == NULL) { |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 54 | add_property_null(event_object, "data"); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 55 | } else { |
| 56 | byte_buffer_to_string(event->data.read, &read_string, &read_len); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 57 | add_property_stringl(event_object, "data", read_string, read_len, true); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 58 | } |
| 59 | break; |
| 60 | case GRPC_INVOKE_ACCEPTED: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 61 | add_property_long(event_object, "data", |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 62 | (long)event->data.invoke_accepted); |
| 63 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 64 | case GRPC_WRITE_ACCEPTED: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 65 | add_property_long(event_object, "data", (long)event->data.write_accepted); |
| 66 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 67 | case GRPC_FINISH_ACCEPTED: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 68 | add_property_long(event_object, "data", |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 69 | (long)event->data.finish_accepted); |
| 70 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 71 | case GRPC_CLIENT_METADATA_READ: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 72 | data_object = grpc_call_create_metadata_array( |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 73 | event->data.client_metadata_read.count, |
| 74 | event->data.client_metadata_read.elements); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 75 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 76 | break; |
| 77 | case GRPC_FINISHED: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 78 | MAKE_STD_ZVAL(data_object); |
| 79 | object_init(data_object); |
| 80 | add_property_long(data_object, "code", event->data.finished.status); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 81 | if (event->data.finished.details == NULL) { |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 82 | add_property_null(data_object, "details"); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 83 | } else { |
| 84 | detail_len = strlen(event->data.finished.details); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 85 | detail_string = ecalloc(detail_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 86 | memcpy(detail_string, event->data.finished.details, detail_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 87 | add_property_string(data_object, "details", detail_string, true); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 88 | } |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 89 | add_property_zval(data_object, "metadata", |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 90 | grpc_call_create_metadata_array( |
| 91 | event->data.finished.metadata_count, |
| 92 | event->data.finished.metadata_elements)); |
| 93 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 94 | break; |
| 95 | case GRPC_SERVER_RPC_NEW: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 96 | MAKE_STD_ZVAL(data_object); |
| 97 | object_init(data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 98 | method_len = strlen(event->data.server_rpc_new.method); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 99 | method_string = ecalloc(method_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 100 | memcpy(method_string, event->data.server_rpc_new.method, method_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 101 | add_property_string(data_object, "method", method_string, false); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 102 | host_len = strlen(event->data.server_rpc_new.host); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 103 | host_string = ecalloc(host_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 104 | memcpy(host_string, event->data.server_rpc_new.host, host_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 105 | add_property_string(data_object, "host", host_string, false); |
| 106 | add_property_zval( |
| 107 | data_object, "absolute_timeout", |
| 108 | grpc_php_wrap_timeval(event->data.server_rpc_new.deadline)); |
| 109 | add_property_zval(data_object, "metadata", |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 110 | grpc_call_create_metadata_array( |
| 111 | event->data.server_rpc_new.metadata_count, |
| 112 | event->data.server_rpc_new.metadata_elements)); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 113 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 114 | break; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame^] | 115 | default: |
| 116 | add_property_null(event_object, "data"); |
| 117 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 118 | } |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 119 | grpc_event_finish(event); |
| 120 | return event_object; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 121 | } |