summaryrefslogtreecommitdiff
path: root/ext/rpc/rpc.c
blob: f46dde5b8a0a52cc393d241fe2d938d1a052f607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"

#include "php_rpc.h"
#include "rpc.h"
#include "layer.h"

ZEND_DECLARE_MODULE_GLOBALS(rpc)

static int rpc_global_startup(void);
static int rpc_global_shutdown(void);
static void rpc_globals_ctor(zend_rpc_globals *rpc_globals TSRMLS_DC);
static void rpc_instance_dtor(void *pDest);
static void rpc_export_functions(char *name, zend_class_entry *ce, function_entry functions[] TSRMLS_DC);
static zend_object_value rpc_create_object(zend_class_entry *class_type TSRMLS_DC);

/* object handler */
static void rpc_add_ref(zval *object);
static void rpc_del_ref(zval *object);
static void rpc_delete(zval *object);
static zend_object_value rpc_clone(zval *object);
static zval* rpc_read(zval *object, zval *member, int type TSRMLS_DC);
static void rpc_write(zval *object, zval *member, zval *value TSRMLS_DC);
static zval** rpc_get_property(zval *object, zval *member TSRMLS_DC);
static zval** rpc_get_property_zval(zval *object, zval *member TSRMLS_DC);
static zval* rpc_get(zval *property TSRMLS_DC);
static void rpc_set(zval **property, zval *value TSRMLS_DC);
static int rpc_has_property(zval *object, zval *member, int check_empty TSRMLS_DC);
static void rpc_unset_property(zval *object, zval *member TSRMLS_DC);
static HashTable* rpc_get_properties(zval *object TSRMLS_DC);
static union _zend_function* rpc_get_method(zval *object, char *method, int method_len TSRMLS_DC);
static int rpc_call(char *method, INTERNAL_FUNCTION_PARAMETERS);
static union _zend_function* rpc_get_constructor(zval *object TSRMLS_DC);
static int rpc_get_classname(zval *object, char **class_name, zend_uint *class_name_len, int parent TSRMLS_DC);
static int rpc_compare(zval *object1, zval *object2 TSRMLS_DC);
/**/

static zend_object_handlers rpc_handlers = {
	rpc_add_ref,
	rpc_del_ref,
	rpc_delete,
	rpc_clone,
	rpc_read,
	rpc_write,
	rpc_get_property,
	rpc_get_property_zval,
	rpc_get,
	rpc_set,
	rpc_has_property,
	rpc_unset_property,
	rpc_get_properties,
	rpc_get_method,
	rpc_call,
	rpc_get_constructor,
	rpc_get_classname,
	rpc_compare
};

/* {{{ rpc_functions[]
 */
function_entry rpc_functions[] = {
	{NULL, NULL, NULL}
};
/* }}} */

/* {{{ rpc_module_entry
 */
zend_module_entry rpc_module_entry = {
	STANDARD_MODULE_HEADER,
	"rpc",
	rpc_functions,
	PHP_MINIT(rpc),
	PHP_MSHUTDOWN(rpc),
	NULL,
	NULL,
	PHP_MINFO(rpc),
	"0.1a",
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

static HashTable *instance;
static HashTable *handlers;

static MUTEX_T mx_instance;
static unsigned long thread_count = 0;

#ifdef COMPILE_DL_RPC
ZEND_GET_MODULE(rpc);
#endif

/* {{{ PHP_INI
 */
PHP_INI_BEGIN()
/* TODO: add module specific ini settings here */
PHP_INI_END()
/* }}} */

/* still not sure if MINIT is really only once per server and not once per thread
 * so i keep the init stuff here here
 */
static int rpc_global_startup(void)
{
	mx_instance = tsrm_mutex_alloc();

	handlers = (HashTable *) pemalloc(sizeof(HashTable), TRUE);
	instance = (HashTable *) pemalloc(sizeof(HashTable), TRUE);

	zend_hash_init(handlers, 0, NULL, NULL, TRUE);	
	zend_hash_init(instance, 0, NULL, rpc_instance_dtor, TRUE);

	FOREACH_HANDLER {
		HANDLER.rpc_handler_init();
		
		/* create a class entry for every rpc handler */
		INIT_OVERLOADED_CLASS_ENTRY((*(HANDLER.ce)),
									HANDLER.name,
									NULL,
									NULL,
									NULL,
									NULL);

		HANDLER.ce->create_object = rpc_create_object;

		/* load all available rpc handler into a hash */
		zend_hash_add(handlers, HANDLER.name, strlen(HANDLER.name) + 1, &(HANDLER.handlers), sizeof(rpc_object_handlers *), NULL);
	}

	return SUCCESS;
}

/* same as above for shutdown */
static int rpc_global_shutdown(void)
{
	tsrm_mutex_free(mx_instance);
	
	zend_hash_destroy(handlers);
	zend_hash_destroy(instance);

	return SUCCESS;
}

static void rpc_globals_ctor(zend_rpc_globals *rpc_globals TSRMLS_DC)
{
}

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(rpc)
{
	/* GINIT */
	if (thread_count++ == 0) {
		rpc_global_startup();
	}
	
	FOREACH_HANDLER {
		/* register classes and functions */
		zend_register_internal_class(HANDLER.ce TSRMLS_CC);
		zend_register_functions(HANDLER.functions, NULL, MODULE_PERSISTENT TSRMLS_CC);
	}

	ZEND_INIT_MODULE_GLOBALS(rpc, rpc_globals_ctor, NULL);
	REGISTER_INI_ENTRIES();

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(rpc)
{
	/* GSHUTDOWN */
	if (--thread_count == 0) {
		rpc_global_shutdown();
	}

	UNREGISTER_INI_ENTRIES();
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(rpc)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "rpc support", "enabled");
	php_info_print_table_end();

	DISPLAY_INI_ENTRIES();
}
/* }}} */

static void rpc_instance_dtor(void *pDest)
{
	rpc_internal **intern;
	
	intern = (rpc_internal **) pDest;

	/* TODO: destruct intern */
}

static zend_object_value rpc_create_object(zend_class_entry *class_type TSRMLS_DC)
{
	zend_object_value *zov;
	rpc_internal *intern;
	
	/* set up the object value struct */
	zov = (zend_object_value*) pemalloc(sizeof(zend_object_value), TRUE);
	zov->handlers = &rpc_handlers;

	/* set up the internal representation of our rpc instance */
	intern = (rpc_internal *) pemalloc(sizeof(rpc_internal), TRUE);
	intern->ce = class_type;
	intern->refcount = 0;
	intern->data = NULL;
	if (zend_hash_find(handlers, class_type->name, class_type->name_length + 1, (void **) &(intern->handlers)) == FAILURE) {
		/* TODO: exception */
	}

	/* store the instance in a hash and set the key as handle, thus
	 * we can find it later easily
	 */
	tsrm_mutex_lock(mx_instance);
	{
		zov->handle = instance->nNextFreeElement;
		zend_hash_next_index_insert(instance, &intern, sizeof(rpc_internal *), NULL);
	}
	tsrm_mutex_unlock(mx_instance);

	return *zov;
}

/* object handler */

static void rpc_add_ref(zval *object)
{
	GET_INTERNAL(intern);
	RPC_ADDREF(intern);
}

static void rpc_del_ref(zval *object)
{
	GET_INTERNAL(intern);

	if (RPC_REFCOUNT(intern) > 0) {
		RPC_DELREF(intern);
	}

	if (RPC_REFCOUNT(intern) == 0) {
		rpc_instance_dtor(intern);
	}
}

static void rpc_delete(zval *object)
{
	GET_INTERNAL(intern);
	rpc_instance_dtor(intern);
}

static zend_object_value rpc_clone(zval *object)
{
//	TSRMLS_FETCH();
//	GET_INTERNAL(intern);

	/* FIXME */
	return object->value.obj;
}

static zval* rpc_read(zval *object, zval *member, int type TSRMLS_DC)
{
//	GET_INTERNAL(intern);

	/* FIXME */
	return NULL;
}

static void rpc_write(zval *object, zval *member, zval *value TSRMLS_DC)
{
//	GET_INTERNAL(intern);
	/* FIXME */
}

static zval** rpc_get_property(zval *object, zval *member TSRMLS_DC)
{
//	GET_INTERNAL(intern);

	/* FIXME */
	return NULL;
}

static zval** rpc_get_property_zval(zval *object, zval *member TSRMLS_DC)
{
//	GET_INTERNAL(intern);

	/* FIXME */
	return NULL;
}

static zval* rpc_get(zval *property TSRMLS_DC)
{
//	GET_INTERNAL(intern);

	/* FIXME */
	return NULL;
}

static void rpc_set(zval **property, zval *value TSRMLS_DC)
{
//	GET_INTERNAL(intern);
	/* FIXME */
}

static int rpc_has_property(zval *object, zval *member, int check_empty TSRMLS_DC)
{
//	GET_INTERNAL(intern);
	
	/* FIXME */
	return FAILURE;
}

static void rpc_unset_property(zval *object, zval *member TSRMLS_DC)
{
//	GET_INTERNAL(intern);
	/* FIXME */
}

static HashTable* rpc_get_properties(zval *object TSRMLS_DC)
{
//	GET_INTERNAL(intern);

	/* FIXME */
	return NULL;
}

static union _zend_function* rpc_get_method(zval *object, char *method, int method_len TSRMLS_DC)
{
	zend_function *function;

	function = (zend_function *) emalloc(sizeof(zend_function));
	function->type = ZEND_OVERLOADED_FUNCTION;
	function->common.arg_types = NULL;
	function->common.function_name = method;
	function->common.scope = NULL;

	//	GET_INTERNAL(intern);

	/* FIXME */
	return function;
}

static int rpc_call(char *method, INTERNAL_FUNCTION_PARAMETERS)
{
//	zval *object = getThis();
//	GET_INTERNAL(intern);

	return SUCCESS;
}

static union _zend_function* rpc_get_constructor(zval *object TSRMLS_DC)
{
	zend_internal_function *rpc_ctor;
	GET_INTERNAL(intern);
	
	rpc_ctor = (zend_internal_function *) emalloc(sizeof(zend_internal_function));

	rpc_ctor->type = ZEND_INTERNAL_FUNCTION;
	rpc_ctor->scope = (*intern)->ce;
	rpc_ctor->arg_types = NULL;
	rpc_ctor->handler = ZEND_FN(rpc_load);

	return (zend_function *) rpc_ctor;
}

static int rpc_get_classname(zval *object, char **class_name, zend_uint *class_name_len, int parent TSRMLS_DC)
{
//	GET_INTERNAL(intern);

	/* FIXME */
	return FAILURE;
}

static int rpc_compare(zval *object1, zval *object2 TSRMLS_DC)
{
	/* FIXME */
	return FAILURE;
}

/**/

/* constructor */
ZEND_FUNCTION(rpc_load)
{
	zval *object = getThis();
	rpc_internal **intern;

	/* check if we were called as a constructor or as a function */
	if (!object) {
		/* we were called as a function so we have to figure out which rpc layer was requested
		 * and then we have to set up a zval containing the object
		 */

		char *key;
		int key_len;

		/* the name of the rpc layer is prepended to '_load' so lets strip everything after
		 * the first '_' away from the function name
		 */
		zend_class_entry *ce;
		key = estrdup(get_active_function_name(TSRMLS_C));
		key_len = strchr(key, '_') - key;
		key[key_len] = '\0';

		/* get the class entry for the requested rpc layer */
		if (zend_hash_find(CG(class_table), key, key_len + 1, (void **) &ce) == FAILURE) {
			/* TODO: exception here */
			RETURN_FALSE;
		}

		/* set up a new zval container */
		ALLOC_ZVAL(object);
		INIT_PZVAL(object);

		Z_TYPE_P(object) = IS_OBJECT;
		ZVAL_REFCOUNT(object) = 1;
		PZVAL_IS_REF(object) = 1;

		/* create a new object */
		object->value.obj = rpc_create_object(ce TSRMLS_CC);

		return_value = object;

		/* now everything is set up the same way as if we were called as a constructor */
	}

	GET_INTERNAL_EX(intern, object);

	/* FIXME */
}

ZEND_FUNCTION(rpc_call)
{
	/* FIXME */
}

ZEND_FUNCTION(rpc_set)
{
	/* FIXME */
}

ZEND_FUNCTION(rpc_get)
{
	/* FIXME */
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */