1 /*
2 +----------------------------------------------------------------------+
3 | Yar - Light, concurrent RPC framework |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2012-2013 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Xinchen Hui <laruence@php.net> |
16 | Zhenyu Zhang <zhangzhenyu@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 /* $Id$ */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "php.h"
27 #include "php_yar.h"
28 #include "yar_transport.h"
29
30 /* True globals, no need for thread safety */
31 int le_calldata;
32 int le_plink;
33
34 struct _yar_transports_list {
35 unsigned int size;
36 unsigned int num;
37 const yar_transport_t **transports;
38 } yar_transports_list;
39
40 extern const yar_transport_t yar_transport_curl;
41 extern const yar_transport_t yar_transport_socket;
42
php_yar_plink_dtor(zend_resource * rsrc)43 static void php_yar_plink_dtor(zend_resource *rsrc) /* {{{ */ {
44 yar_persistent_le_t *le = (yar_persistent_le_t *)rsrc->ptr;
45 le->dtor(le->ptr);
46 efree(le);
47 }
48 /* }}} */
49
php_yar_calldata_dtor(zend_resource * rsrc)50 static void php_yar_calldata_dtor(zend_resource *rsrc) /* {{{ */ {
51 yar_call_data_t *entry = (yar_call_data_t *)rsrc->ptr;
52
53 if (entry->uri) {
54 zend_string_release(entry->uri);
55 }
56
57 if (entry->method) {
58 zend_string_release(entry->method);
59 }
60
61 zval_ptr_dtor(&entry->callback);
62 zval_ptr_dtor(&entry->ecallback);
63 zval_ptr_dtor(&entry->parameters);
64 zval_ptr_dtor(&entry->options);
65
66 efree(entry);
67 }
68 /* }}} */
69
php_yar_transport_get(char * name,int nlen)70 PHP_YAR_API const yar_transport_t * php_yar_transport_get(char *name, int nlen) /* {{{ */ {
71 int i = 0;
72 for (;i<yar_transports_list.num;i++) {
73 if (strncmp(yar_transports_list.transports[i]->name, name, nlen) == 0) {
74 return yar_transports_list.transports[i];
75 }
76 }
77
78 return NULL;
79 } /* }}} */
80
php_yar_transport_register(const yar_transport_t * transport)81 PHP_YAR_API int php_yar_transport_register(const yar_transport_t *transport) /* {{{ */ {
82
83 if (!yar_transports_list.size) {
84 yar_transports_list.size = 5;
85 yar_transports_list.transports = (const yar_transport_t **)malloc(sizeof(yar_transport_t *) * yar_transports_list.size);
86 } else if (yar_transports_list.num == yar_transports_list.size) {
87 yar_transports_list.size += 5;
88 yar_transports_list.transports = (const yar_transport_t **)realloc(yar_transports_list.transports, sizeof(yar_transport_t *) * yar_transports_list.size);
89 }
90 yar_transports_list.transports[yar_transports_list.num] = transport;
91
92 return yar_transports_list.num++;
93 } /* }}} */
94
YAR_STARTUP_FUNCTION(transport)95 YAR_STARTUP_FUNCTION(transport) /* {{{ */ {
96 php_yar_transport_register(&yar_transport_curl);
97 php_yar_transport_register(&yar_transport_socket);
98 le_calldata = zend_register_list_destructors_ex(php_yar_calldata_dtor, NULL, "Yar Call Data", module_number);
99 le_plink = zend_register_list_destructors_ex(php_yar_plink_dtor, NULL, "Yar Persistent Link", module_number);
100
101 return SUCCESS;
102 } /* }}} */
103
YAR_ACTIVATE_FUNCTION(transport)104 YAR_ACTIVATE_FUNCTION(transport) /* {{{ */ {
105
106 YAR_G(transport) = &yar_transport_curl;
107
108 return SUCCESS;
109 } /* }}} */
110
YAR_SHUTDOWN_FUNCTION(transport)111 YAR_SHUTDOWN_FUNCTION(transport) /* {{{ */ {
112 if (yar_transports_list.size) {
113 free(yar_transports_list.transports);
114 }
115 return SUCCESS;
116 } /* }}} */
117
118 /*
119 * Local variables:
120 * tab-width: 4
121 * c-basic-offset: 4
122 * End:
123 * vim600: noet sw=4 ts=4 fdm=marker
124 * vim<600: noet sw=4 ts=4
125 */
126