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 | |
| 35 | if(event == NULL) { |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | MAKE_STD_ZVAL(event_object); |
| 40 | object_init(event_object); |
| 41 | |
| 42 | add_property_zval(event_object, |
| 43 | "call", |
| 44 | grpc_php_wrap_call(event->call, |
| 45 | event->type==GRPC_SERVER_RPC_NEW)); |
| 46 | add_property_long(event_object, "type", event->type); |
| 47 | add_property_long(event_object, "tag", (long)event->tag); |
| 48 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 49 | switch(event->type){ |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 50 | case GRPC_QUEUE_SHUTDOWN: add_property_null(event_object, "data"); break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 51 | case GRPC_READ: |
| 52 | if(event->data.read == NULL){ |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 53 | add_property_null(event_object, "data"); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 54 | } else { |
| 55 | byte_buffer_to_string(event->data.read, &read_string, &read_len); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 56 | add_property_stringl(event_object, "data", read_string, read_len, true); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 57 | } |
| 58 | break; |
| 59 | case GRPC_INVOKE_ACCEPTED: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 60 | add_property_long(event_object, |
| 61 | "data", |
| 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: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 68 | add_property_long(event_object, |
| 69 | "data", |
| 70 | (long)event->data.finish_accepted); |
| 71 | break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 72 | case GRPC_CLIENT_METADATA_READ: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 73 | data_object = grpc_call_create_metadata_array( |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 74 | event->data.client_metadata_read.count, |
| 75 | event->data.client_metadata_read.elements); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 76 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 77 | break; |
| 78 | case GRPC_FINISHED: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 79 | MAKE_STD_ZVAL(data_object); |
| 80 | object_init(data_object); |
| 81 | add_property_long(data_object, "code", event->data.finished.status); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 82 | if(event->data.finished.details == NULL){ |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 83 | add_property_null(data_object, "details"); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 84 | } else { |
| 85 | detail_len = strlen(event->data.finished.details); |
| 86 | detail_string = ecalloc(detail_len+1, sizeof(char)); |
| 87 | memcpy(detail_string, event->data.finished.details, detail_len); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 88 | add_property_string(data_object, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 89 | "details", |
| 90 | detail_string, |
| 91 | true); |
| 92 | } |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 93 | add_property_zval(data_object, |
| 94 | "metadata", |
| 95 | grpc_call_create_metadata_array( |
| 96 | event->data.finished.metadata_count, |
| 97 | event->data.finished.metadata_elements)); |
| 98 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 99 | break; |
| 100 | case GRPC_SERVER_RPC_NEW: |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 101 | MAKE_STD_ZVAL(data_object); |
| 102 | object_init(data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 103 | method_len = strlen(event->data.server_rpc_new.method); |
| 104 | method_string = ecalloc(method_len+1, sizeof(char)); |
| 105 | memcpy(method_string, event->data.server_rpc_new.method, method_len); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 106 | add_property_string(data_object, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 107 | "method", |
| 108 | method_string, |
| 109 | false); |
| 110 | host_len = strlen(event->data.server_rpc_new.host); |
| 111 | host_string = ecalloc(host_len+1, sizeof(char)); |
| 112 | memcpy(host_string, event->data.server_rpc_new.host, host_len); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 113 | add_property_string(data_object, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 114 | "host", |
| 115 | host_string, |
| 116 | false); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 117 | add_property_zval(data_object, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 118 | "absolute_timeout", |
| 119 | grpc_php_wrap_timeval( |
| 120 | event->data.server_rpc_new.deadline)); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 121 | add_property_zval(data_object, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 122 | "metadata", |
| 123 | grpc_call_create_metadata_array( |
| 124 | event->data.server_rpc_new.metadata_count, |
| 125 | event->data.server_rpc_new.metadata_elements)); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 126 | add_property_zval(event_object, "data", data_object); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 127 | break; |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 128 | default: add_property_null(event_object, "data"); break; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 129 | } |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame^] | 130 | grpc_event_finish(event); |
| 131 | return event_object; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 132 | } |