summaryrefslogtreecommitdiff
path: root/ext/rpc/rpc.c
blob: d0fc90a49cc99a136e502aa6a81aa46b01aa2c3f (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
#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 void rpc_globals_ctor(zend_rpc_globals * TSRMLS_DC);
static void rpc_instance_dtor(void *);
static void rpc_proxy_dtor(void *pDest);
static void rpc_class_dtor(void *);
static void rpc_string_dtor(void *);
static void rpc_export_functions(char *, zend_class_entry *, function_entry[] TSRMLS_DC);
static zend_object_value rpc_create_object(zend_class_entry * TSRMLS_DC);

/* object handler */
static void rpc_add_ref(zval * TSRMLS_DC);
static void rpc_del_ref(zval * TSRMLS_DC);
static void rpc_delete(zval * TSRMLS_DC);
static zend_object_value rpc_clone(zval * TSRMLS_DC);
static zval* rpc_read(zval *, zval *, int  TSRMLS_DC);
static void rpc_write(zval *, zval *, zval * TSRMLS_DC);
static zval** rpc_get_property(zval *, zval * TSRMLS_DC);
static zval* rpc_get(zval * TSRMLS_DC);
static void rpc_set(zval **, zval * TSRMLS_DC);
static int rpc_has_property(zval *, zval *, int  TSRMLS_DC);
static void rpc_unset_property(zval *, zval * TSRMLS_DC);
static HashTable* rpc_get_properties(zval * TSRMLS_DC);
static union _zend_function* rpc_get_method(zval *, char *, int TSRMLS_DC);
static union _zend_function* rpc_get_constructor(zval * TSRMLS_DC);
static zend_class_entry** rpc_get_class_entry(zval *object TSRMLS_DC);
static int rpc_get_classname(zval *, char **, zend_uint *, int  TSRMLS_DC);
static int rpc_compare(zval *, zval * TSRMLS_DC);
/**/

/* pseudo handler */
static void rpc_internal_get(rpc_internal *, char *, zend_uint, zval *);
static void rpc_internal_set(rpc_internal *, char *, zend_uint, zval *);
/**/

extern zend_object_handlers rpc_proxy_handlers;

static zend_object_handlers rpc_handlers = {
	rpc_add_ref,
	rpc_del_ref,
	rpc_delete,
	rpc_clone,
	rpc_read,
	rpc_write,
	rpc_get_property,
	NULL,
	rpc_get,
	rpc_set,
	rpc_has_property,
	rpc_unset_property,
	rpc_get_properties,
	rpc_get_method,
	NULL,
	rpc_get_constructor,
	rpc_get_class_entry,
	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,
	ZEND_MINIT(rpc),
	ZEND_MSHUTDOWN(rpc),
	NULL,
	NULL,
	ZEND_MINFO(rpc),
	"0.1a",
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

static HashTable *handlers;
static TsHashTable *instance;
static TsHashTable *proxy;
static TsHashTable *classes;
static zend_llist *classes_list;

#ifdef COMPILE_DL_RPC
ZEND_GET_MODULE(rpc);
#endif

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

static void rpc_globals_ctor(zend_rpc_globals *rpc_globals TSRMLS_DC)
{
}

/* {{{ ZEND_MINIT_FUNCTION
 */
ZEND_MINIT_FUNCTION(rpc)
{
	handlers = (HashTable *) pemalloc(sizeof(HashTable), TRUE);
	instance = (TsHashTable *) pemalloc(sizeof(TsHashTable), TRUE);
	proxy = (TsHashTable *) pemalloc(sizeof(TsHashTable), TRUE);
	classes = (TsHashTable *) pemalloc(sizeof(TsHashTable), TRUE);
	classes_list = (zend_llist *) pemalloc(sizeof(zend_llist), TRUE);

	zend_hash_init(handlers, 0, NULL, NULL, TRUE);	
	zend_ts_hash_init(instance, 0, NULL, rpc_instance_dtor, TRUE);
	zend_ts_hash_init(proxy, 0, NULL, rpc_proxy_dtor, TRUE);
	zend_ts_hash_init(classes, 0, NULL, NULL, TRUE);
	zend_llist_init(classes_list, sizeof(rpc_class_hash **), rpc_class_dtor, TRUE);

	FOREACH_HANDLER {
		HANDLER.rpc_handler_init();
		
		/* create a class entry for every rpc handler */
		INIT_OVERLOADED_CLASS_ENTRY((*(HANDLER.ce)),
									HANDLER.name,
									HANDLER.methods,
									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);

		/* 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;
}
/* }}} */

/* {{{ ZEND_MSHUTDOWN_FUNCTION
 */
ZEND_MSHUTDOWN_FUNCTION(rpc)
{
	/* destroy instances first */
	zend_ts_hash_destroy(instance);
	zend_ts_hash_destroy(proxy);
	zend_ts_hash_destroy(classes);

	zend_llist_destroy(classes_list);
	zend_hash_destroy(handlers);

	pefree(handlers, TRUE);
	pefree(instance, TRUE);
	pefree(proxy, TRUE);
	pefree(classes, TRUE);

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

/* {{{ ZEND_MINFO_FUNCTION
 */
ZEND_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;
	
	(*(*intern)->handlers)->rpc_dtor(&((*intern)->data));

	tsrm_mutex_free((*intern)->function_table.mx_reader);
	tsrm_mutex_free((*intern)->function_table.mx_writer);
	tsrm_mutex_free((*intern)->mx_handler);

	pefree(*intern, TRUE);
}

static void rpc_proxy_dtor(void *pDest)
{
	rpc_proxy **proxy_intern;
	
	proxy_intern = (rpc_proxy **) pDest;
	
	pefree(*proxy_intern, TRUE);
}

static void rpc_class_dtor(void *pDest)
{
	rpc_class_hash **hash;

	hash = (rpc_class_hash **) pDest;

	zend_ts_hash_destroy(&((*hash)->methods));
	zend_ts_hash_destroy(&((*hash)->properties));

	free((*hash)->name.str);
	pefree((*hash), TRUE);
}

static void rpc_string_dtor(void *pDest)
{
	rpc_string **string;

	string = (rpc_string **) pDest;

	free((*string)->str);
	pefree(*string, TRUE);
}

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->class_name = NULL;
	intern->class_name_len = 0;
	intern->ce = class_type;
	intern->refcount = 1;
	intern->clonecount = 1;
	intern->data = NULL;
	intern->pool_instances = 0;
	intern->function_table.hash = intern->ce->function_table;
	intern->function_table.reader = 0;
	intern->function_table.mx_reader = tsrm_mutex_alloc();
	intern->function_table.mx_writer = tsrm_mutex_alloc();
	intern->mx_handler = tsrm_mutex_alloc();

	if (zend_hash_find(handlers, class_type->name, class_type->name_length + 1, (void **) &(intern->handlers)) != SUCCESS) {
		/* TODO: exception */
	}

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

	return *zov;
}

static zend_object_value rpc_create_proxy(TSRMLS_D)
{
	zend_object_value *zov;
	rpc_proxy *proxy_intern;

	/* set up the object value struct */
	zov = (zend_object_value*) pemalloc(sizeof(zend_object_value), TRUE);
	zov->handlers = &rpc_proxy_handlers;

	/* set up the internal representation of the proxy */
	proxy_intern = (rpc_proxy *) pemalloc(sizeof(rpc_proxy), TRUE);

	/* store the instance in a hash and set the key as handle, thus
	 * we can find it later easily
	 */
	tsrm_mutex_lock(proxy->mx_writer);
	{
		zov->handle = zend_hash_next_free_element(&(proxy->hash));
		zend_ts_hash_next_index_insert(proxy, &proxy_intern, sizeof(rpc_proxy *), NULL);
	}
	tsrm_mutex_unlock(proxy->mx_writer);

	return *zov;
}

/* object handler */

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

static void rpc_del_ref(zval *object TSRMLS_DC)
{
	rpc_internal **intern;

	if (GET_INTERNAL_EX(intern, object) == SUCCESS) {
		if (RPC_REFCOUNT(intern) > 0) {
			RPC_DELREF(intern);
		}

		if (RPC_REFCOUNT(intern) == 0) {
			zend_ts_hash_index_del(instance, Z_OBJ_HANDLE(*object));
		}
	}
}

static void rpc_delete(zval *object TSRMLS_DC)
{
	rpc_internal **intern;
	
	if (GET_INTERNAL_EX(intern, object) == SUCCESS) {
		if (RPC_CLONECOUNT(intern) > 0) {
			RPC_DELCLONE(intern);
		}

		if (RPC_CLONECOUNT(intern) == 0) {
			zend_ts_hash_index_del(instance, Z_OBJ_HANDLE_P(object));
		}
	}
}

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

	/* cloning the underlaying resource is neither possible nor would it
	 * make sense, therfore we return the old zend_object_value and increase
	 * the clone count to not loose the clone when the original object gets
	 * deleted.
	 */
	RPC_ADDCLONE(intern);
	
	/* also increase the refcounter as a clone is just another reference */
	RPC_ADDREF(intern);

	return object->value.obj;
}

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

	/* seting up the return value and decrease the refcounter as we don't
	 * keep a reference to this zval.
	 */
	MAKE_STD_ZVAL(return_value);
	ZVAL_DELREF(return_value);
	ZVAL_NULL(return_value);

	if ((*intern)->hash && Z_TYPE_P(member) == IS_LONG) {
		rpc_internal_get(*intern, NULL, Z_LVAL_P(member), return_value);
	} else if (Z_TYPE_P(member) == IS_STRING) {
		rpc_internal_get(*intern, Z_STRVAL_P(member), Z_STRLEN_P(member), return_value);
	} else {
		/* TODO: exception here */
	}

	return return_value;
}

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

	if ((*intern)->hash && Z_TYPE_P(member) == IS_LONG) {
		rpc_internal_set(*intern, NULL, Z_LVAL_P(member), value);
	} else if (Z_TYPE_P(member) == IS_STRING) {
		rpc_internal_set(*intern, Z_STRVAL_P(member), Z_STRLEN_P(member), value);
	} else {
		/* TODO: exception here */
	}
}

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

	return_value = emalloc(sizeof(zval *));
	MAKE_STD_ZVAL(*return_value);
//	ZVAL_DELREF(*return_value);
	Z_TYPE_P(object) = IS_OBJECT;
	(*return_value)->value.obj = rpc_create_proxy(TSRMLS_C);

	return return_value;
}

static zval* rpc_get(zval *property TSRMLS_DC)
{
	/* not yet implemented */
	return NULL;
}

static void rpc_set(zval **property, zval *value TSRMLS_DC)
{
	/* not yet implemented */
}

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;
	GET_INTERNAL(intern);

	if (zend_ts_hash_find(&((*intern)->function_table), method, method_len + 1, &function) != SUCCESS) {
		zend_internal_function *zif;

		zif = (zend_internal_function *) emalloc(sizeof(zend_internal_function));
		zif->arg_types = NULL;
		zif->function_name = method;
		zif->handler = ZEND_FN(rpc_call);
		zif->scope = NULL;
		zif->type = ZEND_INTERNAL_FUNCTION;

		/* add new method to the method table */
		zend_ts_hash_add(&((*intern)->function_table), method, method_len + 1, zif, sizeof(zend_function), &function);
		efree(zif);
	}

	return function;
}

static union _zend_function* rpc_get_constructor(zval *object TSRMLS_DC)
{
	zend_function *rpc_ctor;
	GET_INTERNAL(intern);
	
	if (zend_ts_hash_find(&((*intern)->function_table), (*intern)->ce->name, (*intern)->ce->name_length + 1, &rpc_ctor) != SUCCESS) {
		zend_internal_function *zif;

		zif = (zend_internal_function *) emalloc(sizeof(zend_internal_function));

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

		/* add new constructor to the method table */
		zend_ts_hash_add(&((*intern)->function_table), (*intern)->ce->name, (*intern)->ce->name_length + 1, zif, sizeof(zend_function), &rpc_ctor);
		efree(zif);
	}

	return rpc_ctor;
}

static zend_class_entry** rpc_get_class_entry(zval *object TSRMLS_DC)
{
	GET_INTERNAL(intern);

	return &((*intern)->ce);
}

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();
	zval ***args, ***args_free;
	zend_uint num_args = ZEND_NUM_ARGS(); 
	rpc_class_hash *class_hash;
	rpc_class_hash **class_hash_find = NULL;
	rpc_internal **intern;
	rpc_string hash_val;
	int retval, append = 0;
	char *arg_types;

	/* 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
		 */

		/* get class entry */
		GET_CLASS(ce);

		/* set up a new zval container */
		object = return_value;

		Z_TYPE_P(object) = IS_OBJECT;

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

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

	if (GET_INTERNAL_EX(intern, object) != SUCCESS) {
		/* TODO: exception */
	}

	/* fetch further parameters */
	GET_ARGS_EX(num_args, args, args_free, 1);

	/* if classname != integer */
	if ((zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 1 TSRMLS_CC, "l", &((*intern)->class_name_len)) != SUCCESS) ||
		/* or we have no hash function */
		!((*(*intern)->handlers)->rpc_hash) ||
		/* or integer hashing is not allowed */
		!((*(*intern)->handlers)->hash_type & HASH_AS_INT)) {

		/* else check for string - classname */
		if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 1 TSRMLS_CC, "s", &((*intern)->class_name), &((*intern)->class_name_len)) != SUCCESS) {
			/* none of the two possibilities */
			/* TODO: exception */
			php_error(E_WARNING, "wrong arguments for %s()", get_active_function_name(TSRMLS_C));
		} else {
			/* hash classname if hashing function exists */
			if ((*(*intern)->handlers)->rpc_hash) {

				GET_CTOR_SIGNATURE(intern, hash_val, num_args, arg_types);

				/* check if already hashed */	
				if (zend_ts_hash_find(classes, hash_val.str, hash_val.len + 1, (void **) &class_hash_find) != SUCCESS) {
					class_hash = pemalloc(sizeof(rpc_class_hash), TRUE);

					/* set up the cache */
					zend_ts_hash_init(&(class_hash->methods), 0, NULL, rpc_string_dtor, TRUE);
					zend_ts_hash_init(&(class_hash->properties), 0, NULL, rpc_string_dtor, TRUE);


					/* do hashing */
					if ((*(*intern)->handlers)->rpc_hash((*intern)->class_name, (*intern)->class_name_len, &(class_hash->name.str),
														 &(class_hash->name.len), num_args, arg_types, CLASS) != SUCCESS) {
						/* TODO: exception */
					}

					/* register with non-hashed key 
					 * also track all instaces in a llist for destruction later on, because there might be duplicate entries in
					 * the hashtable and we can't determine if a pointer references to an already freed element
					 */
					zend_ts_hash_add(classes, hash_val.str, hash_val.len + 1, &class_hash, sizeof(rpc_class_hash *), (void **) &class_hash_find);
					tsrm_mutex_lock(classes->mx_writer);
					zend_llist_add_element(classes_list, class_hash_find);
					tsrm_mutex_unlock(classes->mx_writer);

					if (class_hash->name.str) {
						/* register string hashcode */
						zend_ts_hash_add(classes, class_hash->name.str, class_hash->name.len + 1, &class_hash, sizeof(rpc_class_hash *), NULL);
					} else if (!class_hash->name.str && ((*(*intern)->handlers)->hash_type & HASH_AS_INT)) {
						/* register int hashcode */
						zend_ts_hash_index_update(classes, class_hash->name.len, &class_hash, sizeof(rpc_class_hash *), NULL);
					}
				} else {
					class_hash = *class_hash_find;
				}

				FREE_SIGNATURE(hash_val, arg_types);
			}
		}
	} else {
		/* integer classname (hashcode) */
		if (zend_ts_hash_index_find(classes, (*intern)->class_name_len, (void**) &class_hash_find) != SUCCESS) {
			class_hash = pemalloc(sizeof(rpc_class_hash), TRUE);

			/* set up the cache */
			class_hash->name.str = NULL;
			class_hash->name.len = (*intern)->class_name_len;

			zend_ts_hash_init(&(class_hash->methods), 0, NULL, rpc_string_dtor, TRUE);
			zend_ts_hash_init(&(class_hash->properties), 0, NULL, rpc_string_dtor, TRUE);

			/* register int hashcode, we don't know more */
			zend_ts_hash_index_update(classes, class_hash->name.len, &class_hash, sizeof(rpc_class_hash *), NULL);
		} else {
			class_hash = *class_hash_find;
		}
	}
	
	/* if hash function available */
	if ((*(*intern)->handlers)->rpc_hash) {
		/* assign cache structure */
		(*intern)->hash = class_hash;
		
		/* call the rpc ctor */
		retval = (*(*intern)->handlers)->rpc_ctor(class_hash->name.str, class_hash->name.len, &((*intern)->data), num_args, args);
	} else {
		/* disable caching from now on */
		(*intern)->hash = NULL;

		/* call the rpc ctor */
		retval = (*(*intern)->handlers)->rpc_ctor((*intern)->class_name, (*intern)->class_name_len, &((*intern)->data), num_args, args);
	}

	efree(args_free);

	if (retval != SUCCESS) {
		/* TODO: exception */
	}
}

ZEND_FUNCTION(rpc_call)
{
	zval *object = getThis();
	zval ***args, ***args_free;
	zend_uint num_args = ZEND_NUM_ARGS(); 
	char *hash = NULL, *arg_types;
	int hash_len, retval, strip = 0;

	/* check if we were called as a method or as a function */
	if (!object) {
		/* we were called as a function so we have to figure out which rpc layer was requested */

		/* get class entry */
		GET_CLASS(ce);

		if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 2 TSRMLS_CC, "Ol", &object, *ce, &hash_len) != SUCCESS) {
			if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 2 TSRMLS_CC, "Os", &object, *ce, &hash, &hash_len) != SUCCESS) {
				/* none of the two possibilities */
				/* TODO: exception */
				php_error(E_WARNING, "wrong arguments for %s()", get_active_function_name(TSRMLS_C));
			}
		}

		strip = 2;
	} else {
		hash = get_active_function_name(TSRMLS_C);
		hash_len = strlen(hash);
	}

	GET_ARGS_EX(num_args, args, args_free, strip);
	
	/* scope for internal data */
	{
		rpc_string hash_val, *method_hash, **method_hash_find;
		GET_INTERNAL(intern);

		method_hash = (rpc_string *) pemalloc(sizeof(rpc_string), TRUE);
		method_hash->str = hash;
		method_hash->len = hash_len;
		
		if ((*intern)->hash) {
			/* cache method table lookups */
		
			if (!hash && !((*(*intern)->handlers)->hash_type & HASH_AS_INT)) {
				/* TODO: exception */
			} else if(hash) {
				/* string passed */
				GET_METHOD_SIGNATURE(intern, method_hash, hash_val, num_args, arg_types);

				/* check if already hashed */	
				if (zend_ts_hash_find(&((*intern)->hash->methods), hash_val.str, hash_val.len + 1, (void **) &method_hash_find) != SUCCESS) {
					if ((*(*intern)->handlers)->rpc_hash(hash, hash_len, &(method_hash->str), &(method_hash->len), num_args, arg_types, METHOD) != SUCCESS) {
						/* TODO: exception */
					}

					/* register with non-hashed key */
					zend_ts_hash_add(&((*intern)->hash->methods), hash_val.str, hash_val.len + 1, &method_hash, sizeof(rpc_string *), NULL);
				} else {
					pefree(method_hash, TRUE);
					method_hash = *method_hash_find;
				}

				FREE_SIGNATURE(hash_val, arg_types);
			}
		}

		/* actually this should not be neccesary, but who knows :)
		 * considering possible thread implementations in future php versions
		 * and srm it is better to do concurrency checks
		 */
		tsrm_mutex_lock((*intern)->mx_handler);
		retval = (*(*intern)->handlers)->rpc_call(method_hash->str, method_hash->len, &((*intern)->data), &return_value, num_args, args);
		tsrm_mutex_unlock((*intern)->mx_handler);
	}

	efree(args_free);

	if (retval != SUCCESS) {
		/* TODO: exception here */
	}
}

ZEND_FUNCTION(rpc_set)
{
	zval *object, *value;
	char *property = NULL;
	int property_len;
	rpc_internal **intern;
	/* get class entry */
	GET_CLASS(ce);

	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 3 TSRMLS_CC, "Olz", &object, *ce, &property_len, &value) != SUCCESS) {
		if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 3 TSRMLS_CC, "Osz", &object, *ce, &property, &property_len, &value) != SUCCESS) {
			/* none of the two possibilities */
			/* TODO: exception */
			php_error(E_WARNING, "wrong arguments for %s()", get_active_function_name(TSRMLS_C));
		}
	}

	GET_INTERNAL_EX(intern, object);
	if (!property && !(*intern)->hash) {
		/* TODO: exception here */
	} else {
		rpc_internal_set(*intern, property, property_len, value);
	}
}

ZEND_FUNCTION(rpc_get)
{
	zval *object;
	char *property = NULL;
	int property_len;
	rpc_internal **intern;
	/* get class entry */
	GET_CLASS(ce);

	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 2 TSRMLS_CC, "Ol", &object, *ce, &property_len) != SUCCESS) {
		if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 2 TSRMLS_CC, "Os", &object, *ce, &property, &property_len) != SUCCESS) {
			/* none of the two possibilities */
			/* TODO: exception */
			php_error(E_WARNING, "wrong arguments for %s()", get_active_function_name(TSRMLS_C));
		}
	}

	GET_INTERNAL_EX(intern, object);
	if (!property && !(*intern)->hash) {
		/* TODO: exception here */
	} else {
		rpc_internal_get(*intern, property, property_len, return_value);
	}
}

static void rpc_internal_get(rpc_internal *intern, char *property, zend_uint property_len, zval *return_value)
{
	int retval;
	rpc_string *property_hash, **property_hash_find;

	Z_TYPE_P(return_value) = IS_NULL;

	property_hash = (rpc_string *) pemalloc(sizeof(rpc_string), TRUE);
	property_hash->str = property;
	property_hash->len = property_len;
	
	if (intern->hash) {
		/* cache method table lookups */
	
		if (!property && !((*intern->handlers)->hash_type & HASH_AS_INT)) {
			/* TODO: exception */
		} else if(property) {
			/* check if already hashed */	
			if (zend_ts_hash_find(&(intern->hash->properties), property, property_len + 1, (void **) &property_hash_find) != SUCCESS) {
				if ((*intern->handlers)->rpc_hash(property, property_len, &(property_hash->str), &(property_hash->len), 0, NULL, PROPERTY) != SUCCESS) {
					/* TODO: exception */
				}

				/* register with non-hashed key */
				zend_ts_hash_add(&(intern->hash->properties), property, property_len + 1, &property_hash, sizeof(rpc_string *), NULL);
			} else {
				pefree(property_hash, TRUE);
				property_hash = *property_hash_find;
			}
		}
	}


	tsrm_mutex_lock(intern->mx_handler);
	retval = (*intern->handlers)->rpc_get(property_hash->str, property_hash->len, return_value, intern->data);
	tsrm_mutex_unlock(intern->mx_handler);

	if (retval != SUCCESS) {
		/* TODO: exception here */
	}
}

static void rpc_internal_set(rpc_internal *intern, char *property, zend_uint property_len, zval *value)
{
	int retval;

	tsrm_mutex_lock(intern->mx_handler);
	retval = (*intern->handlers)->rpc_set(property, property_len, value, intern->data);
	tsrm_mutex_unlock(intern->mx_handler);

	if (retval != SUCCESS) {
		/* TODO: exception here */
	}
}

/*
 * 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
 */