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 | |
| 20 | /* Frees and finishes a wrapped instance of grpc_event */ |
| 21 | void free_wrapped_grpc_event(void *object TSRMLS_DC){ |
| 22 | wrapped_grpc_event *event = (wrapped_grpc_event*)object; |
| 23 | if(event->wrapped != NULL){ |
| 24 | grpc_event_finish(event->wrapped); |
| 25 | } |
| 26 | efree(event); |
| 27 | } |
| 28 | |
| 29 | /* Initializes an instance of wrapped_grpc_channel to be associated with an |
| 30 | * object of a class specified by class_type */ |
| 31 | zend_object_value create_wrapped_grpc_event( |
| 32 | zend_class_entry *class_type TSRMLS_DC){ |
| 33 | zend_object_value retval; |
| 34 | wrapped_grpc_event *intern; |
| 35 | intern = (wrapped_grpc_event*)emalloc(sizeof(wrapped_grpc_event)); |
| 36 | memset(intern, 0, sizeof(wrapped_grpc_event)); |
| 37 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 38 | object_properties_init(&intern->std, class_type); |
| 39 | retval.handle = zend_objects_store_put( |
| 40 | intern, |
| 41 | (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 42 | free_wrapped_grpc_event, |
| 43 | NULL TSRMLS_CC); |
| 44 | retval.handlers = zend_get_std_object_handlers(); |
| 45 | return retval; |
| 46 | } |
| 47 | |
| 48 | zval *grpc_php_wrap_event(grpc_event *wrapped){ |
| 49 | zval *event_object; |
| 50 | MAKE_STD_ZVAL(event_object); |
| 51 | |
| 52 | object_init_ex(event_object, grpc_ce_event); |
| 53 | wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object( |
| 54 | event_object TSRMLS_CC); |
| 55 | event->wrapped = wrapped; |
| 56 | return event_object; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the type of the event |
| 61 | * @return long Integer representing the type |
| 62 | */ |
| 63 | PHP_METHOD(Event, get_type){ |
| 64 | wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object( |
| 65 | getThis() TSRMLS_CC); |
| 66 | RETURN_LONG((long)(event->wrapped->type)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the tag of the event |
| 71 | * @return long The event's tag |
| 72 | */ |
| 73 | PHP_METHOD(Event, get_tag){ |
| 74 | wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object( |
| 75 | getThis() TSRMLS_CC); |
| 76 | RETURN_LONG((long)(event->wrapped->tag)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the call associated with the event |
| 81 | * @return Call The call |
| 82 | */ |
| 83 | PHP_METHOD(Event, get_call){ |
| 84 | wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object( |
| 85 | getThis() TSRMLS_CC); |
| 86 | zval *call_obj = grpc_php_wrap_call(event->wrapped->call); |
| 87 | RETURN_DESTROY_ZVAL(call_obj); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the data associated with the event |
| 92 | * @return object The data, with type depending on the type field |
| 93 | */ |
| 94 | PHP_METHOD(Event, get_data){ |
| 95 | zval *retval; |
| 96 | wrapped_grpc_event *wrapped_event = |
| 97 | (wrapped_grpc_event*)zend_object_store_get_object( |
| 98 | getThis() TSRMLS_CC); |
| 99 | grpc_event *event = wrapped_event->wrapped; |
| 100 | char *detail_string; |
| 101 | size_t detail_len; |
| 102 | char *method_string; |
| 103 | size_t method_len; |
| 104 | char *host_string; |
| 105 | size_t host_len; |
| 106 | char *read_string; |
| 107 | size_t read_len; |
| 108 | |
| 109 | switch(event->type){ |
| 110 | case GRPC_QUEUE_SHUTDOWN: RETURN_NULL(); break; |
| 111 | case GRPC_READ: |
| 112 | if(event->data.read == NULL){ |
| 113 | RETURN_NULL(); |
| 114 | } else { |
| 115 | byte_buffer_to_string(event->data.read, &read_string, &read_len); |
| 116 | RETURN_STRINGL(read_string, read_len, true); |
| 117 | } |
| 118 | break; |
| 119 | case GRPC_INVOKE_ACCEPTED: |
| 120 | RETURN_LONG((long)event->data.invoke_accepted); break; |
| 121 | case GRPC_WRITE_ACCEPTED: |
| 122 | RETURN_LONG((long)event->data.write_accepted); break; |
| 123 | case GRPC_FINISH_ACCEPTED: |
| 124 | RETURN_LONG((long)event->data.finish_accepted); break; |
| 125 | case GRPC_CLIENT_METADATA_READ: |
| 126 | retval = grpc_call_create_metadata_array( |
| 127 | event->data.client_metadata_read.count, |
| 128 | event->data.client_metadata_read.elements); |
| 129 | break; |
| 130 | case GRPC_FINISHED: |
| 131 | MAKE_STD_ZVAL(retval); |
| 132 | object_init(retval); |
| 133 | add_property_long(retval, "code", event->data.finished.code); |
| 134 | if(event->data.finished.details == NULL){ |
| 135 | add_property_null(retval, "details"); |
| 136 | } else { |
| 137 | detail_len = strlen(event->data.finished.details); |
| 138 | detail_string = ecalloc(detail_len+1, sizeof(char)); |
| 139 | memcpy(detail_string, event->data.finished.details, detail_len); |
| 140 | add_property_string(retval, |
| 141 | "details", |
| 142 | detail_string, |
| 143 | true); |
| 144 | } |
| 145 | break; |
| 146 | case GRPC_SERVER_RPC_NEW: |
| 147 | MAKE_STD_ZVAL(retval); |
| 148 | object_init(retval); |
| 149 | method_len = strlen(event->data.server_rpc_new.method); |
| 150 | method_string = ecalloc(method_len+1, sizeof(char)); |
| 151 | memcpy(method_string, event->data.server_rpc_new.method, method_len); |
| 152 | add_property_string(retval, |
| 153 | "method", |
| 154 | method_string, |
| 155 | false); |
| 156 | host_len = strlen(event->data.server_rpc_new.host); |
| 157 | host_string = ecalloc(host_len+1, sizeof(char)); |
| 158 | memcpy(host_string, event->data.server_rpc_new.host, host_len); |
| 159 | add_property_string(retval, |
| 160 | "host", |
| 161 | host_string, |
| 162 | false); |
| 163 | add_property_zval(retval, |
| 164 | "absolute_timeout", |
| 165 | grpc_php_wrap_timeval( |
| 166 | event->data.server_rpc_new.deadline)); |
| 167 | add_property_zval(retval, |
| 168 | "metadata", |
| 169 | grpc_call_create_metadata_array( |
| 170 | event->data.server_rpc_new.metadata_count, |
| 171 | event->data.server_rpc_new.metadata_elements)); |
| 172 | break; |
| 173 | default: RETURN_NULL(); break; |
| 174 | } |
| 175 | RETURN_DESTROY_ZVAL(retval); |
| 176 | } |
| 177 | |
| 178 | static zend_function_entry event_methods[] = { |
| 179 | PHP_ME(Event, get_call, NULL, ZEND_ACC_PUBLIC) |
| 180 | PHP_ME(Event, get_data, NULL, ZEND_ACC_PUBLIC) |
| 181 | PHP_ME(Event, get_tag, NULL, ZEND_ACC_PUBLIC) |
| 182 | PHP_ME(Event, get_type, NULL, ZEND_ACC_PUBLIC) |
| 183 | PHP_FE_END |
| 184 | }; |
| 185 | |
| 186 | void grpc_init_event(TSRMLS_D){ |
| 187 | zend_class_entry ce; |
| 188 | INIT_CLASS_ENTRY(ce, "Grpc\\Event", event_methods); |
| 189 | ce.create_object = create_wrapped_grpc_event; |
| 190 | grpc_ce_event = zend_register_internal_class(&ce TSRMLS_CC); |
| 191 | } |