-
Notifications
You must be signed in to change notification settings - Fork 47
/
zklua.c
1821 lines (1686 loc) · 56.5 KB
/
zklua.c
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Zklua: Lua Binding Of Apache ZooKeeper
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
_
#include "zklua.h"
static FILE *zklua_log_stream = NULL;
/**
* unique static address used for indexing watcher_fn
* in LUA_REGISTRYINDEX.
**/
static char watcher_fn_key = 'k';
/**
* unique static address used for indexing zklua_handle_t zh
* in LUA_REGISTRYINDEX.
**/
static char zklua_handle_key = 'k';
static int _zklua_build_stat(lua_State *L, const struct Stat *stat);
static int _zklua_build_string_vector(lua_State *L, const struct String_vector *sv);
static int _zklua_build_acls(lua_State *L, const struct ACL_vector *acls);
static int _zklua_unref(lua_State *L, int ref);
void watcher_dispatch(zhandle_t *zh, int type, int state,
const char *path, void *watcherctx)
{
zklua_global_watcher_context_t *wrapper = (zklua_global_watcher_context_t *)watcherctx;
lua_State *L = wrapper->L;
void *context = wrapper->context;
/** push lua watcher_fn onto the stack. */
lua_pushlightuserdata(L, (void *)&watcher_fn_key);
lua_gettable(L, LUA_REGISTRYINDEX);
/* push zklua_handle_t onto the stack. */
lua_pushlightuserdata(L, (void *)&zklua_handle_key);
lua_gettable(L, LUA_REGISTRYINDEX);
/** push type onto the stack. */
lua_pushinteger(L, type);
/** push state onto the stack. */
lua_pushinteger(L, state);
/** push path onto the stack. */
lua_pushstring(L, path);
/** push watcher context onto the stack. */
lua_pushstring(L, context);
lua_call(L, 5, 0);
}
void local_watcher_dispatch(zhandle_t *zh, int type, int state,
const char *path, void *watcherctx)
{
zklua_local_watcher_context_t *wrapper = (zklua_local_watcher_context_t *)watcherctx;
lua_State *L = wrapper->L;
void *context = wrapper->context;
int zhref = wrapper->zhref;
int cbref = wrapper->cbref;
/** push lua watcher_fn onto the stack. */
lua_rawgeti(L, LUA_REGISTRYINDEX, cbref);
/* push zklua_handle_t onto the stack. */
lua_rawgeti(L, LUA_REGISTRYINDEX, zhref);
/** push type onto the stack. */
lua_pushinteger(L, type);
/** push state onto the stack. */
lua_pushinteger(L, state);
/** push path onto the stack. */
lua_pushstring(L, path);
/** push watcher context onto the stack. */
lua_pushstring(L, context);
lua_call(L, 5, 0);
_zklua_unref(L, zhref);
_zklua_unref(L, cbref);
}
void void_completion_dispatch(int rc, const void *data)
{
zklua_completion_data_t *wrapper = (zklua_completion_data_t *)data;
lua_State *L = wrapper->L;
const char *real_data = wrapper->data;
lua_pushinteger(L, rc);
lua_pushstring(L, real_data);
lua_call(L, 2, 0);
free(wrapper);
}
void stat_completion_dispatch(int rc, const struct Stat *stat,
const void *data)
{
zklua_completion_data_t *wrapper = (zklua_completion_data_t *)data;
lua_State *L = wrapper->L;
const char *real_data = wrapper->data;
lua_pushinteger(L, rc);
_zklua_build_stat(L, stat);
lua_pushstring(L, real_data);
lua_call(L, 3, 0);
free(wrapper);
}
void data_completion_dispatch(int rc, const char *value, int value_len,
const struct Stat *stat, const void *data)
{
zklua_completion_data_t *wrapper = (zklua_completion_data_t *)data;
lua_State *L = wrapper->L;
const char *real_data = wrapper->data;
lua_pushinteger(L, rc);
lua_pushlstring(L, value, value_len);
_zklua_build_stat(L, stat);
lua_pushstring(L, real_data);
lua_call(L, 4, 0);
free(wrapper);
}
void strings_completion_dispatch(int rc, const struct String_vector *strings,
const void *data)
{
zklua_completion_data_t *wrapper = (zklua_completion_data_t *)data;
lua_State *L = wrapper->L;
const char *real_data = wrapper->data;
lua_pushinteger(L, rc);
_zklua_build_string_vector(L, strings);
lua_pushstring(L, real_data);
lua_call(L, 3, 0);
free(wrapper);
}
void strings_stat_completion_dispatch(int rc, const struct String_vector *strings,
const struct Stat *stat, const void *data)
{
zklua_completion_data_t *wrapper = (zklua_completion_data_t *)data;
lua_State *L = wrapper->L;
const char *real_data = wrapper->data;
lua_pushinteger(L, rc);
_zklua_build_string_vector(L, strings);
_zklua_build_stat(L, stat);
lua_pushstring(L, real_data);
lua_call(L, 4, 0);
free(wrapper);
}
void string_completion_dispatch(int rc, const char *value, const void *data)
{
zklua_completion_data_t *wrapper = (zklua_completion_data_t *)data;
lua_State *L = wrapper->L;
const char *real_data = wrapper->data;
lua_pushinteger(L, rc);
lua_pushstring(L, value);
lua_pushstring(L, real_data);
lua_call(L, 3, 0);
free(wrapper);
}
void acl_completion_dispatch(int rc, struct ACL_vector *acl,
struct Stat *stat, const void *data)
{
zklua_completion_data_t *wrapper = (zklua_completion_data_t *)data;
lua_State *L = wrapper->L;
const char *real_data = wrapper->data;
lua_pushinteger(L, rc);
_zklua_build_acls(L, acl);
_zklua_build_stat(L, stat);
lua_pushstring(L, real_data);
lua_call(L, 3, 0);
free(wrapper);
}
/**
* return 1 if @host@ has the following format:
* "127.0.0.1:2081,127.0.0.1:2082,127.0.0.1:2083".
* TODO: check if the given host is valid.
**/
static int _zklua_check_host(const char *host)
{
return 1;
}
static int _zklua_build_stat(lua_State *L, const struct Stat *stat)
{
if (stat != NULL) {
lua_newtable(L);
lua_pushstring(L, "czxid");
lua_pushnumber(L, stat->czxid);
lua_settable(L, -3);
lua_pushstring(L, "mzxid");
lua_pushnumber(L, stat->mzxid);
lua_settable(L, -3);
lua_pushstring(L, "ctime");
lua_pushnumber(L, stat->ctime);
lua_settable(L, -3);
lua_pushstring(L, "mtime");
lua_pushnumber(L, stat->mtime);
lua_settable(L, -3);
lua_pushstring(L, "version");
lua_pushnumber(L, stat->version);
lua_settable(L, -3);
lua_pushstring(L, "cversion");
lua_pushnumber(L, stat->cversion);
lua_settable(L, -3);
lua_pushstring(L, "aversion");
lua_pushnumber(L, stat->aversion);
lua_settable(L, -3);
lua_pushstring(L, "ephemeralOwner");
lua_pushnumber(L, stat->ephemeralOwner);
lua_settable(L, -3);
lua_pushstring(L, "dataLength");
lua_pushnumber(L, stat->dataLength);
lua_settable(L, -3);
lua_pushstring(L, "numChildren");
lua_pushnumber(L, stat->numChildren);
lua_settable(L, -3);
lua_pushstring(L, "pzxid");
lua_pushnumber(L, stat->pzxid);
lua_settable(L, -3);
} else {
lua_newtable(L);
lua_pushstring(L, "czxid");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "mzxid");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "ctime");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "mtime");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "version");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "cversion");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "aversion");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "ephemeralOwner");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "dataLength");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "numChildren");
lua_pushnumber(L, 0);
lua_settable(L, -3);
lua_pushstring(L, "pzxid");
lua_pushnumber(L, 0);
lua_settable(L, -3);
}
return 1;
}
static int _zklua_build_string_vector(lua_State *L, const struct String_vector *sv)
{
int i;
lua_newtable(L);
if (sv != NULL) {
for (i = 0; i < sv->count; ++i) {
lua_pushstring(L, sv->data[i]);
lua_rawseti(L, -2, i);
}
}
return 0;
}
static int _zklua_build_acls(lua_State *L, const struct ACL_vector *acls)
{
int i;
lua_newtable(L);
if (acls != NULL) {
for (i = 0; i < acls->count; ++i) {
lua_newtable(L);
lua_pushstring(L, "perms");
lua_pushnumber(L, acls->data[i].perms);
lua_settable(L, -3);
lua_pushstring(L, "scheme");
lua_pushstring(L, acls->data[i].id.scheme);
lua_settable(L, -3);
lua_pushstring(L, "id");
lua_pushstring(L, acls->data[i].id.id);
lua_settable(L, -3);
lua_rawseti(L, -2, i+1);
}
}
return 0;
}
/**
* parse an ACL_vector struct from the given acceptable index.
* return 1 if the parsing is successful, otherwise 0 returned.
**/
static int _zklua_parse_acls(lua_State *L, int index, struct ACL_vector *acls)
{
int count = 0, i = 0, j = 0;
luaL_checktype(L, index, LUA_TTABLE);
count = lua_objlen(L, index);
acls->count = count;
acls->data = (struct ACL *)calloc(count, sizeof(struct ACL));
if (acls->data == NULL) {
luaL_error(L, "out of memory when zklua trys to "
"alloc an internal object.");
return 0;
}
for (i = 1; i <= count; i++) {
lua_rawgeti(L, index, i);
lua_pushstring(L, "perms");
lua_rawget(L, -2);
acls->data[i-1].perms = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "scheme");
lua_rawget(L, -2);
acls->data[i-1].id.scheme = strdup(lua_tostring(L, -1));
lua_pop(L, 1);
lua_pushstring(L, "id");
lua_rawget(L, -2);
acls->data[i-1].id.id = strdup(lua_tostring(L, -1));
lua_pop(L, 1);
lua_pop(L, 1);
}
return 1;
}
/**
* frees an ACL_vector struct.
**/
static int _zklua_free_acls(struct ACL_vector *acls)
{
int i;
if (acls == NULL) {
return -1;
}
for (i = 0; i < acls->count; ++i) {
free(acls->data[i].id.id);
free(acls->data[i].id.scheme);
}
free(acls->data);
return 1;
}
static int _zklua_check_handle(lua_State *L, zklua_handle_t *handle)
{
if (handle->zh) {
return 1;
} else {
luaL_error(L, "invalid zookeeper handle.");
return 0;
}
}
static int _zklua_ref(lua_State *L, int index)
{
lua_pushvalue(L, index);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
return ref;
}
static int _zklua_unref(lua_State *L, int ref)
{
luaL_unref(L, LUA_REGISTRYINDEX, ref);
return 0;
}
static zklua_global_watcher_context_t *_zklua_global_watcher_context_init(
lua_State *L, void *data)
{
zklua_global_watcher_context_t *wrapper = (zklua_global_watcher_context_t *)malloc(
sizeof(zklua_global_watcher_context_t));
if (wrapper == NULL) {
luaL_error(L, "out of memory when zklua trys to "
"alloc an internal object.");
}
wrapper->L = L;
wrapper->context = data;
return wrapper;
}
static zklua_local_watcher_context_t *_zklua_local_watcher_context_init(
lua_State *L, void *data, int zhref, int cbref)
{
zklua_local_watcher_context_t *wrapper = (zklua_local_watcher_context_t *)malloc(
sizeof(zklua_local_watcher_context_t));
if (wrapper == NULL) {
luaL_error(L, "out of memory when zklua trys to "
"alloc an internal object.");
}
wrapper->L = L;
wrapper->context = data;
wrapper->zhref = zhref;
wrapper->cbref = cbref;
return wrapper;
}
/**
* save watcher_fn into LUA_REGISTRYINDEX, and @index@ is the index in lua_State
* where the lua watcher_fn resides.
**/
static void _zklua_save_watcherfn(lua_State *L, int index)
{
lua_pushlightuserdata(L, (void *)&watcher_fn_key);
lua_pushvalue(L, index);
lua_settable(L, LUA_REGISTRYINDEX);
}
/**
* save zklua_handle_t *zh into LUA_REGISTRYINDEX, and @index@ is
* the index in lua_State where the lua watcher_fn resides.
**/
static void _zklua_save_zklua_handle(lua_State *L, int index)
{
lua_pushlightuserdata(L, (void *)&zklua_handle_key);
lua_pushvalue(L, index);
lua_settable(L, LUA_REGISTRYINDEX);
}
/**
* remove zklua_handle_t *zh from LUA_REGISTRYINDEX.
**/
static void _zklua_remove_zklua_handle(lua_State *L)
{
lua_pushlightuserdata(L, (void *)&zklua_handle_key);
lua_pushnil(L);
lua_settable(L, LUA_REGISTRYINDEX);
}
/**
* initialize C clientid_t struct from lua table.
**/
static clientid_t *_zklua_clientid_init(
lua_State *L, int index)
{
size_t passwd_len = 0;
const char *clientid_passwd = NULL;
clientid_t *clientid = NULL;
clientid = (clientid_t *)malloc(sizeof(clientid_t));
if (clientid == NULL) {
luaL_error(L, "out of memory when zklua trys to "
"alloc an internal object.");
}
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, "client_id");
clientid->client_id = luaL_checkint(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "passwd");
clientid_passwd = luaL_checklstring(L, -1, &passwd_len);
memset(clientid->passwd, 0, 16);
memcpy(clientid->passwd, clientid_passwd, passwd_len);
lua_pop(L, 1);
return clientid;
}
static void _zklua_clientid_fini(clientid_t **clientid)
{
if (*clientid != NULL) {
free(*clientid);
*clientid = NULL;
}
}
/**
* initialize a zookeeper handle.
**/
static int zklua_init(lua_State *L)
{
int top = lua_gettop(L);
size_t host_len = 0;
const char *host = NULL;
int recv_timeout = 0;
clientid_t *clientid = NULL;
size_t real_context_len = 0;
char *real_watcher_context = NULL;
int flags = 0;
zklua_global_watcher_context_t *wrapper = NULL;
zklua_handle_t *handle = (zklua_handle_t *)lua_newuserdata(L,
sizeof(zklua_handle_t));
luaL_getmetatable(L, ZKLUA_METATABLE_NAME);
lua_setmetatable(L, -2);
_zklua_save_zklua_handle(L, -1);
host = luaL_checklstring(L, 1, &host_len);
if (!_zklua_check_host(host)) {
return luaL_error(L, "invalid arguments:"
"host must be a string with format:\n"
"127.0.0.1:2081,127.0.0.1:2082");
}
luaL_checktype(L, 2, LUA_TFUNCTION);
_zklua_save_watcherfn(L, 2);
recv_timeout = luaL_checkint(L, 3);
switch(top) {
case 3:
wrapper = _zklua_global_watcher_context_init(L, NULL);
handle->zh = zookeeper_init(host, watcher_dispatch,
recv_timeout, 0, wrapper, 0);
break;
case 4:
luaL_checktype(L, 4, LUA_TTABLE);
clientid = _zklua_clientid_init(L, 4);
wrapper = _zklua_global_watcher_context_init(L, NULL);
handle->zh = zookeeper_init(host, watcher_dispatch,
recv_timeout, clientid, wrapper, 0);
_zklua_clientid_fini(&clientid);
break;
case 5:
luaL_checktype(L, 4, LUA_TTABLE);
clientid = _zklua_clientid_init(L, 4);
real_watcher_context = (char *)luaL_checklstring(L, 5, &real_context_len);
wrapper = _zklua_global_watcher_context_init(L, real_watcher_context);
handle->zh = zookeeper_init(host, watcher_dispatch,
recv_timeout, clientid, wrapper, 0);
_zklua_clientid_fini(&clientid);
break;
case 6:
luaL_checktype(L, 4, LUA_TTABLE);
clientid = _zklua_clientid_init(L, 4);
real_watcher_context = (char *)luaL_checklstring(L, 5, &real_context_len);
wrapper = _zklua_global_watcher_context_init(L, real_watcher_context);
flags = luaL_checkint(L, 6);
handle->zh = zookeeper_init(host, watcher_dispatch,
recv_timeout, clientid, wrapper, flags);
_zklua_clientid_fini(&clientid);
break;
default:
break;
}
return 1;
}
static int zklua_close(lua_State *L)
{
int ret = 0;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (handle->zh != NULL) {
/* close zookeeper handle. */
ret = zookeeper_close(handle->zh);
handle->zh = NULL;
/* remove zookeeper handle from LUA_REGISTRYINDEX. */
_zklua_remove_zklua_handle(L);
} else {
return luaL_error(L, "unable to close the zookeeper handle.");
}
/* close log stream. */
if (zklua_log_stream != NULL) fclose(zklua_log_stream);
/* push ret code of zookeeper_close() onto stack. */
lua_pushinteger(L, ret);
return 1;
}
/**
* return clientid_t of the current connection.
**/
static int zklua_client_id(lua_State *L)
{
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
const clientid_t *clientid = zoo_client_id(handle->zh);
lua_newtable(L);
lua_pushstring(L, "client_id");
lua_pushnumber(L, clientid->client_id);
lua_settable(L, -3);
lua_pushstring(L, "passwd");
lua_pushstring(L, clientid->passwd);
lua_settable(L, -3);
return 1;
} else {
return luaL_error(L, "unable to get client id.");
}
}
static int zklua_recv_timeout(lua_State *L)
{
int recv_timeout = 0;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
recv_timeout = zoo_recv_timeout(handle->zh);
lua_pushinteger(L, recv_timeout);
return 1;
} else {
return luaL_error(L, "unable to get recv_timeout.");
}
}
static int zklua_get_context(lua_State *L)
{
const char *context = NULL;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
context = zoo_get_context(handle->zh);
lua_pushstring(L, context);
return 1;
} else {
return luaL_error(L, "unable to get zookeeper handle context.");
}
}
static int zklua_set_context(lua_State *L)
{
size_t context_len = 0;
char *context = NULL;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
context = (char *)luaL_checklstring(L, -1, &context_len);
zoo_set_context(handle->zh, context);
return 0;
} else {
return luaL_error(L, "unable to get zookeeper handle context.");
}
}
static int zklua_set_watcher(lua_State *L)
{
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
lua_pushlightuserdata(L, (void *)&watcher_fn_key);
lua_gettable(L, LUA_REGISTRYINDEX);
luaL_checktype(L, 2, LUA_TFUNCTION);
_zklua_save_watcherfn(L, 2);
return 1;
} else {
return luaL_error(L, "unable to set zookeeper new watcher_fn.");
}
}
static int zklua_get_connected_host(lua_State *L)
{
unsigned short port = 0;
struct sockaddr *paddr = NULL;
struct sockaddr saddr;
socklen_t socklen = 0;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
paddr = zookeeper_get_connected_host(handle->zh, &saddr, &socklen);
if (paddr != NULL) {
if (socklen == sizeof(struct sockaddr_in)) {
char addrstr[INET_ADDRSTRLEN] = {0};
port = ntohs(((struct sockaddr_in *)paddr)->sin_port);
inet_ntop(AF_INET, &(((struct sockaddr_in *)paddr)->sin_addr), addrstr, INET_ADDRSTRLEN);
lua_pushstring(L, addrstr);
lua_pushinteger(L, port);
return 2;
} else if (socklen == sizeof(struct sockaddr_in6)) {
char addr6str[INET6_ADDRSTRLEN] = {0};
port = ntohs(((struct sockaddr_in6 *)paddr)->sin6_port);
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)paddr)->sin6_addr), addr6str, INET6_ADDRSTRLEN);
lua_pushstring(L, addr6str);
lua_pushinteger(L, port);
return 2;
} else {
return luaL_error(L, "unsupported network sockaddr type.");
}
}
} else {
return luaL_error(L, "unable to get connected host.");
}
return 0;
}
/**
* TODO: implement zklua_interest later.
**/
static int zklua_interest(lua_State *L)
{
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
return 0;
}
/**
* TODO: implement zklua_process later.
**/
static int zklua_process(lua_State *L)
{
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
return 0;
}
static int zklua_state(lua_State *L)
{
int ret = 0;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
ret = zoo_state(handle->zh);
lua_pushinteger(L, ret);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
/**
* create a node asynchronously.
**/
static int zklua_acreate(lua_State *L)
{
size_t path_len = 0, value_len=0;
const char *path = NULL;
const char *value = NULL;
const char *data = NULL;
struct ACL_vector acl;
zklua_completion_data_t *cdata = NULL;
int flags = 0;
int ret = -1;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
value = luaL_checklstring(L, 3, &value_len);
if (!_zklua_parse_acls(L, 4, &acl)) return luaL_error(L,
"invalid ACL format.");
flags = luaL_checkint(L, 5);
luaL_checktype(L, 6, LUA_TFUNCTION);
data = luaL_checkstring(L, 7);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 6);
lua_xmove(L, cdata->L, 1);
ret = zoo_acreate(handle->zh, path, value, value_len,
(const struct ACL_vector *)&acl, flags,
string_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.
lua_pushinteger(L, ret);
_zklua_free_acls(&acl);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
/**
* delete a node asynchronously.
**/
static int zklua_adelete(lua_State *L)
{
size_t path_len = 0;
const char *path = NULL;
int version = -1;
const char *data = NULL;
zklua_completion_data_t *cdata = NULL;
int ret = -1;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
version = luaL_checkint(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
data = luaL_checkstring(L, 5);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 4);
lua_xmove(L, cdata->L, 1);
ret = zoo_adelete(handle->zh, path, version, void_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.
// printf("zklua_adelete: %s\n", lua_typename(L, lua_type(L, 1)));
lua_pushnumber(L, ret);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
/**
* checks the existence of a node in zookeeper.
**/
static int zklua_aexists(lua_State *L)
{
size_t path_len = 0;
const char *path = NULL;
int watch = 0;
const char *data = NULL;
zklua_completion_data_t *cdata = NULL;
int ret = -1;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
watch = luaL_checkint(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
data = luaL_checkstring(L, 5);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 4);
lua_xmove(L, cdata->L, 1);
ret = zoo_aexists(handle->zh, path, watch,
stat_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.
lua_pushinteger(L, ret);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
/**
* checks the existence of a node in zookeeper.
**/
static int zklua_awexists(lua_State *L)
{
size_t path_len = 0;
const char *real_local_watcherctx = NULL;
const char *path = NULL;
int watch = 0;
const char *data = NULL;
zklua_local_watcher_context_t *wrapper = NULL;
zklua_completion_data_t *cdata = NULL;
int ret = -1;
int zhref = 0;
int cbref = 0;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
zhref = _zklua_ref(L, 1);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
luaL_checktype(L, 3, LUA_TFUNCTION);
cbref = _zklua_ref(L, 3);
real_local_watcherctx = luaL_checkstring(L, 4);
wrapper = _zklua_local_watcher_context_init(L,
(void *)real_local_watcherctx, zhref, cbref);
luaL_checktype(L, 5, LUA_TFUNCTION);
data = luaL_checkstring(L, 6);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 5);
lua_xmove(L, cdata->L, 1);
ret = zoo_awexists(handle->zh, path, local_watcher_dispatch,
(void *)wrapper, stat_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.
lua_pushinteger(L, ret);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
/**
* gets the data associated with a node.
**/
static int zklua_aget(lua_State *L)
{
size_t path_len = 0;
const char *path = NULL;
int watch = 0;
const char *data = NULL;
zklua_completion_data_t *cdata = NULL;
int ret = -1;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
watch = luaL_checkint(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
data = luaL_checkstring(L, 5);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 4);
lua_xmove(L, cdata->L, 1);
ret = zoo_aget(handle->zh, path, watch,
data_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.
lua_pushinteger(L, ret);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
static int zklua_awget(lua_State *L)
{
size_t path_len = 0;
const char *real_local_watcherctx = NULL;
const char *path = NULL;
int watch = 0;
const char *data = NULL;
zklua_local_watcher_context_t *wrapper = NULL;
zklua_completion_data_t *cdata = NULL;
int ret = -1;
int zhref = 0;
int cbref = 0;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
zhref = _zklua_ref(L, 1);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
luaL_checktype(L, 3, LUA_TFUNCTION);
cbref = _zklua_ref(L, 3);
real_local_watcherctx = luaL_checkstring(L, 4);
wrapper = _zklua_local_watcher_context_init(L,
(void *)real_local_watcherctx, zhref, cbref);
luaL_checktype(L, 5, LUA_TFUNCTION);
data = luaL_checkstring(L, 6);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 5);
lua_xmove(L, cdata->L, 1);
ret = zoo_awget(handle->zh, path, local_watcher_dispatch,
(void *)wrapper, data_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.
lua_pushinteger(L, ret);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
static int zklua_aset(lua_State *L)
{
size_t path_len = 0;
size_t buffer_len = 0;
const char *path = NULL;
const char *buffer = NULL;
const char *data = NULL;
zklua_completion_data_t *cdata = NULL;
int version = 0;
int ret = -1;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
buffer = luaL_checklstring(L, 3, &buffer_len);
version = luaL_checkint(L, 4);
luaL_checktype(L, 5, LUA_TFUNCTION);
data = luaL_checkstring(L, 6);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 5);
lua_xmove(L, cdata->L, 1);
ret = zoo_aset(handle->zh, path, buffer, buffer_len, version,
stat_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.
lua_pushinteger(L, ret);
return 1;
} else {
return luaL_error(L, "invalid zookeeper handle.");
}
}
static int zklua_aget_children(lua_State *L)
{
size_t path_len = 0;
const char *path = NULL;
int watch = 0;
const char *data = NULL;
zklua_completion_data_t *cdata = NULL;
int ret = -1;
zklua_handle_t *handle = luaL_checkudata(L, 1, ZKLUA_METATABLE_NAME);
if (_zklua_check_handle(L, handle)) {
path = luaL_checklstring(L, 2, &path_len);
watch = luaL_checkint(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
data = luaL_checkstring(L, 5);
cdata = (zklua_completion_data_t *)malloc(sizeof(zklua_completion_data_t));
cdata->L = lua_newthread(L);
cdata->data= data;
lua_pushvalue(L, 4);
lua_xmove(L, cdata->L, 1);
ret = zoo_aget_children(handle->zh, path, watch,
strings_completion_dispatch, cdata);
lua_pop(L, 1); // popup the thread.