forked from enmand/zkpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zookeeper.c
1666 lines (1501 loc) · 48.8 KB
/
zookeeper.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
/**
* 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 <Python.h>
#include <zookeeper.h>
#include <assert.h>
//////////////////////////////////////////////
// EXCEPTIONS
PyObject *ZooKeeperException = NULL;
PyObject *SystemErrorException;
PyObject *RuntimeInconsistencyException;
PyObject *DataInconsistencyException;
PyObject *ConnectionLossException;
PyObject *MarshallingErrorException;
PyObject *UnimplementedException;
PyObject *OperationTimeoutException;
PyObject *BadArgumentsException;
PyObject *InvalidStateException;
PyObject *ApiErrorException;
PyObject *NoNodeException;
PyObject *NoAuthException;
PyObject *NodeExistsException;
PyObject *BadVersionException;
PyObject *NoChildrenForEphemeralsException;
PyObject *NotEmptyException;
PyObject *SessionExpiredException;
PyObject *SessionMovedException;
PyObject *InvalidCallbackException;
PyObject *InvalidACLException;
PyObject *AuthFailedException;
PyObject *ClosingException;
PyObject *NothingException;
PyObject *err_to_exception(int errcode) {
switch (errcode) {
case ZSYSTEMERROR:
return SystemErrorException;
case ZRUNTIMEINCONSISTENCY:
return RuntimeInconsistencyException;
case ZDATAINCONSISTENCY:
return DataInconsistencyException;
case ZCONNECTIONLOSS:
return ConnectionLossException;
case ZMARSHALLINGERROR:
return MarshallingErrorException;
case ZUNIMPLEMENTED:
return UnimplementedException;
case ZOPERATIONTIMEOUT:
return OperationTimeoutException;
case ZBADARGUMENTS:
return BadArgumentsException;
case ZINVALIDSTATE:
return InvalidStateException;
case ZAPIERROR:
return ApiErrorException;
case ZNONODE:
return NoNodeException;
case ZNOAUTH:
return NoAuthException;
case ZBADVERSION:
return BadVersionException;
case ZNOCHILDRENFOREPHEMERALS:
return NoChildrenForEphemeralsException;
case ZNODEEXISTS:
return NodeExistsException;
case ZINVALIDACL:
return InvalidACLException;
case ZAUTHFAILED:
return AuthFailedException;
case ZNOTEMPTY:
return NotEmptyException;
case ZSESSIONEXPIRED:
return SessionExpiredException;
case ZINVALIDCALLBACK:
return InvalidCallbackException;
case ZSESSIONMOVED:
return SessionMovedException;
case ZOK:
default:
return NULL;
}
}
#define CHECK_ZHANDLE(z) if ( (z) < 0 || (z) >= num_zhandles) { \
PyErr_SetString( ZooKeeperException, "zhandle out of range" ); \
return NULL; \
} else if ( zhandles[(z)] == NULL ) { \
PyErr_SetString(ZooKeeperException, "zhandle already freed"); \
return NULL; \
}
/* Contains all the state required for a watcher callback - these are
passed to the *dispatch functions as void*, cast to pywatcher_t and
then their callback member is invoked if not NULL */
typedef struct {
int zhandle;
PyObject *callback;
int permanent;
}pywatcher_t;
/* This array exists because we need to ref. count the global watchers
for each connection - but they're inaccessible without pulling in
zk_adaptor.h, which I'm trying to avoid. */
static pywatcher_t **watchers;
/* We keep an array of zhandles available for use. When a zhandle is
correctly closed, the C client frees the memory so we set the
zhandles[i] entry to NULL. This entry can then be re-used. */
static zhandle_t** zhandles = NULL;
static int num_zhandles = 0;
static int max_zhandles = 0;
#define REAL_MAX_ZHANDLES 32768
/* -------------------------------------------------------------------------- */
/* zhandles - unique connection ids - tracking */
/* -------------------------------------------------------------------------- */
/* Allocates an initial zhandle and watcher array */
int init_zhandles(int num) {
zhandles = malloc(sizeof(zhandle_t*)*num);
watchers = malloc(sizeof(pywatcher_t*)*num);
if (zhandles == NULL || watchers == NULL) {
return 0;
}
max_zhandles = num;
num_zhandles = 0;
memset(zhandles, 0, sizeof(zhandle_t*)*max_zhandles);
return 1;
}
/* Note that the following zhandle functions are not thread-safe. The
C-Python runtime does not seem to pre-empt a thread that is in a C
module, so there's no need for synchronisation. */
/* Doubles the size of the zhandle / watcher array Returns 0 if the
new array would be >= REAL_MAX_ZHANDLES in size. Called when zhandles
is full. Returns 0 if allocation failed or if max num zhandles
exceeded. */
int resize_zhandles(void) {
zhandle_t **tmp = zhandles;
pywatcher_t ** wtmp = watchers;
if (max_zhandles >= REAL_MAX_ZHANDLES >> 1) {
return 0;
}
max_zhandles *= 2;
zhandles = malloc(sizeof(zhandle_t*)*max_zhandles);
if (zhandles == NULL) {
PyErr_SetString(PyExc_MemoryError, "malloc for new zhandles failed");
return 0;
}
memset(zhandles, 0, sizeof(zhandle_t*)*max_zhandles);
memcpy(zhandles, tmp, sizeof(zhandle_t*)*max_zhandles/2);
watchers = malloc(sizeof(pywatcher_t*)*max_zhandles);
if (watchers == NULL) {
PyErr_SetString(PyExc_MemoryError, "malloc for new watchers failed");
return 0;
}
memset(watchers, 0, sizeof(pywatcher_t*)*max_zhandles);
memcpy(watchers, wtmp, sizeof(pywatcher_t*)*max_zhandles/2);
free(wtmp);
free(tmp);
return 1;
}
/* Find a free zhandle - this iterates through the list of open
zhandles, but we expect it to be infrequently called. There are
optimisations that can be made if this turns out to be problematic.
Returns -1 if no free handle is found - resize_handles() can be
called in that case. */
unsigned int next_zhandle(void) {
int i = 0;
for (i=0;i<max_zhandles;++i) {
if (zhandles[i] == NULL) {
num_zhandles++;
return i;
}
}
return -1;
}
/* -------------------------------------------------------------------------- */
/* Utility functions to construct and deallocate data structures */
/* -------------------------------------------------------------------------- */
/* Creates a new pywatcher_t to hold connection state, a callback
object and a flag to say if the watcher is permanent. Takes a new
reference to the callback object. */
pywatcher_t *create_pywatcher(int zh, PyObject* cb, int permanent)
{
pywatcher_t *ret = (pywatcher_t*)calloc(sizeof(pywatcher_t),1);
if (ret == NULL) {
PyErr_SetString(PyExc_MemoryError, "calloc failed in create_pywatcher");
return NULL;
}
Py_INCREF(cb);
ret->zhandle = zh; ret->callback = cb; ret->permanent = permanent;
return ret;
}
/* Releases the reference taken in create_pywatcher to the callback,
then frees the allocated pywatcher_t* */
void free_pywatcher(pywatcher_t *pw)
{
if (pw == NULL) {
return;
}
Py_DECREF(pw->callback);
free(pw);
}
/* Constructs a new stat object. Returns Py_None if stat == NULL or a
dictionary containing all the stat information otherwise. In either
case, takes a reference to the returned object. */
PyObject *build_stat( const struct Stat *stat )
{
if (stat == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
return Py_BuildValue( "{s:K, s:K, s:K, s:K,"
"s:i, s:i, s:i, s:K,"
"s:i, s:i, s:K}",
"czxid", stat->czxid,
"mzxid", stat->mzxid,
"ctime", stat->ctime,
"mtime", stat->mtime,
"version", stat->version,
"cversion", stat->cversion,
"aversion", stat->aversion,
"ephemeralOwner", stat->ephemeralOwner,
"dataLength", stat->dataLength,
"numChildren", stat->numChildren,
"pzxid", stat->pzxid );
}
/* Creates a new list of strings from a String_vector. Returns the
empty list if the String_vector is NULL. Takes a reference to the
returned PyObject and gives that reference to the caller. */
PyObject *build_string_vector(const struct String_vector *sv)
{
PyObject *ret;
if (!sv) {
return PyList_New(0);
}
ret = PyList_New(sv->count);
if (ret) {
int i;
for (i=0;i<sv->count;++i) {
#if PY_MAJOR_VERSION >= 3
PyObject *s = PyUnicode_FromString(sv->data[i]);
#else
PyObject *s = PyString_FromString(sv->data[i]);
#endif
if (!s) {
if (ret != Py_None) {
Py_DECREF(ret);
}
ret = NULL;
break;
}
PyList_SetItem(ret, i, s);
}
}
return ret;
}
/* Returns 1 if the PyObject is a valid representation of an ACL, and
0 otherwise. */
int check_is_acl(PyObject *o) {
int i;
PyObject *entry;
if (o == NULL) {
return 0;
}
if (!PyList_Check(o)) {
return 0;
}
for (i=0;i<PyList_Size(o);++i) {
PyObject *element = PyList_GetItem(o,i);
if (!PyDict_Check(element)) {
return 0;
}
entry = PyDict_GetItemString( element, "perms" );
if (entry == NULL) {
return 0;
}
entry = PyDict_GetItemString( element, "scheme" );
if (entry == NULL) {
return 0;
}
entry = PyDict_GetItemString( element, "id" );
if (entry == NULL) {
return 0;
}
}
return 1;
}
/* Macro form to throw exception if o is not an ACL */
#define CHECK_ACLS(o) if (check_is_acl(o) == 0) { \
PyErr_SetString(err_to_exception(ZINVALIDACL), zerror(ZINVALIDACL)); \
return NULL; \
}
/* Creates a new list of ACL dictionaries from an ACL_vector. Returns
the empty list if the ACL_vector is NULL. Takes a reference to the
returned PyObject and gives that reference to the caller. */
PyObject *build_acls( const struct ACL_vector *acls )
{
if (acls == NULL) {
return PyList_New(0);
}
PyObject *ret = PyList_New(acls->count);
int i;
for (i=0;i<acls->count;++i) {
PyObject *acl = Py_BuildValue( "{s:i, s:s, s:s}",
"perms", acls->data[i].perms,
"scheme", acls->data[i].id.scheme,
"id", acls->data[i].id.id );
PyList_SetItem(ret, i, acl);
}
return ret;
}
/* Parse the Python representation of an ACL list into an ACL_vector
(which needs subsequent freeing) */
int parse_acls(struct ACL_vector *acls, PyObject *pyacls)
{
PyObject *a;
int i;
if (acls == NULL || pyacls == NULL) {
PyErr_SetString(PyExc_ValueError, "acls or pyacls NULL in parse_acls");
return 0;
}
acls->count = PyList_Size( pyacls );
// Is this a list? If not, we can't do anything
if (PyList_Check(pyacls) == 0) {
PyErr_SetString(InvalidACLException, "List of ACLs required in parse_acls");
return 0;
}
acls->data = (struct ACL *)calloc(acls->count, sizeof(struct ACL));
if (acls->data == NULL) {
PyErr_SetString(PyExc_MemoryError, "calloc failed in parse_acls");
return 0;
}
for (i=0;i<acls->count;++i) {
a = PyList_GetItem(pyacls, i);
// a is now a dictionary
PyObject *perms = PyDict_GetItemString( a, "perms" );
#if PY_MAJOR_VERSION >= 3
acls->data[i].perms = (int32_t)(PyLong_AsLong(perms));
acls->data[i].id.id = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "id" ) ) );
acls->data[i].id.scheme = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "scheme" ) ) );
#else
acls->data[i].perms = (int32_t)(PyInt_AsLong(perms));
acls->data[i].id.id = strdup( PyString_AsString( PyDict_GetItemString( a, "id" ) ) );
acls->data[i].id.scheme = strdup( PyString_AsString( PyDict_GetItemString( a, "scheme" ) ) );
#endif
}
return 1;
}
/* Deallocates the memory allocated inside an ACL_vector, but not the
ACL_vector itself */
void free_acls( struct ACL_vector *acls )
{
if (acls == NULL) {
return;
}
int i;
for (i=0;i<acls->count;++i) {
free(acls->data[i].id.id);
free(acls->data[i].id.scheme);
}
free(acls->data);
}
/* -------------------------------------------------------------------------- */
/* Watcher and callback implementation */
/* -------------------------------------------------------------------------- */
/* Every watcher invocation goes through this dispatch point, which
a) acquires the global interpreter lock
b) unpacks the PyObject to call from the passed context pointer,
which handily includes the index of the relevant zookeeper handle
to pass back to Python.
c) Makes the call into Python, checking for error conditions which
we are responsible for detecting and doing something about (we just
print the error and plough right on)
d) releases the lock after freeing up the context object, which is
only used for one watch invocation (watches are one-shot, unless
'permanent' != 0)
*/
void watcher_dispatch(zhandle_t *zzh, int type, int state,
const char *path, void *context)
{
PyGILState_STATE gstate;
pywatcher_t *pyw = (pywatcher_t*)context;
PyObject *callback = pyw->callback;
if (callback == NULL) {
// This is unexpected
char msg[256];
sprintf(msg, "pywatcher: %d %p %d", pyw->zhandle, pyw->callback, pyw->permanent);
PyErr_SetString(PyExc_ValueError, msg);
return;
}
gstate = PyGILState_Ensure();
PyObject *arglist = Py_BuildValue("(i,i,i,s)", pyw->zhandle,type, state, path);
if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) {
PyErr_Print();
}
Py_DECREF(arglist);
if (pyw->permanent == 0 && (type != ZOO_SESSION_EVENT || state < 0)) {
free_pywatcher(pyw);
}
PyGILState_Release(gstate);
}
/* The completion callbacks (from asynchronous calls) are implemented similarly */
/* Called when an asynchronous call that returns void completes and
dispatches user provided callback */
void void_completion_dispatch(int rc, const void *data)
{
PyGILState_STATE gstate;
pywatcher_t *pyw = (pywatcher_t*)data;
if (pyw == NULL)
return;
PyObject *callback = pyw->callback;
gstate = PyGILState_Ensure();
PyObject *arglist = Py_BuildValue("(i,i)", pyw->zhandle, rc);
if (PyObject_CallObject((PyObject*)callback, arglist) == NULL)
PyErr_Print();
Py_DECREF(arglist);
free_pywatcher(pyw);
PyGILState_Release(gstate);
}
/* Called when an asynchronous call that returns a stat structure
completes and dispatches user provided callback */
void stat_completion_dispatch(int rc, const struct Stat *stat, const void *data)
{
PyGILState_STATE gstate;
pywatcher_t *pyw = (pywatcher_t*)data;
if (pyw == NULL)
return;
PyObject *callback = pyw->callback;
gstate = PyGILState_Ensure();
PyObject *pystat = build_stat(stat);
PyObject *arglist = Py_BuildValue("(i,i,O)", pyw->zhandle,rc, pystat);
Py_DECREF(pystat);
if (PyObject_CallObject((PyObject*)callback, arglist) == NULL)
PyErr_Print();
Py_DECREF(arglist);
free_pywatcher(pyw);
PyGILState_Release(gstate);
}
/* Called when an asynchronous call that returns a stat structure and
some untyped data completes and dispatches user provided
callback (used by aget) */
void data_completion_dispatch(int rc, const char *value, int value_len, const struct Stat *stat, const void *data)
{
PyGILState_STATE gstate;
pywatcher_t *pyw = (pywatcher_t*)data;
if (pyw == NULL)
return;
PyObject *callback = pyw->callback;
gstate = PyGILState_Ensure();
PyObject *pystat = build_stat(stat);
PyObject *arglist = Py_BuildValue("(i,i,s#,O)", pyw->zhandle,rc, value,value_len, pystat);
Py_DECREF(pystat);
if (PyObject_CallObject((PyObject*)callback, arglist) == NULL)
PyErr_Print();
Py_DECREF(arglist);
free_pywatcher(pyw);
PyGILState_Release(gstate);
}
/* Called when an asynchronous call that returns a list of strings
completes and dispatches user provided callback */
void strings_completion_dispatch(int rc, const struct String_vector *strings, const void *data)
{
PyGILState_STATE gstate;
pywatcher_t *pyw = (pywatcher_t*)data;
if (pyw == NULL)
return;
PyObject *callback = pyw->callback;
gstate = PyGILState_Ensure();
PyObject *pystrings = build_string_vector(strings);
if (pystrings)
{
PyObject *arglist = Py_BuildValue("(i,i,O)", pyw->zhandle, rc, pystrings);
if (arglist == NULL || PyObject_CallObject((PyObject*)callback, arglist) == NULL)
PyErr_Print();
Py_DECREF(arglist);
}
else
PyErr_Print();
Py_DECREF(pystrings);
free_pywatcher(pyw);
PyGILState_Release(gstate);
}
/* Called when an asynchronous call that returns a single string
completes and dispatches user provided callback */
void string_completion_dispatch(int rc, const char *value, const void *data)
{
PyGILState_STATE gstate;
pywatcher_t *pyw = (pywatcher_t*)data;
if (pyw == NULL) {
return;
}
PyObject *callback = pyw->callback;
gstate = PyGILState_Ensure();
PyObject *arglist = Py_BuildValue("(i,i,s)", pyw->zhandle,rc, value);
if (PyObject_CallObject((PyObject*)callback, arglist) == NULL)
PyErr_Print();
Py_DECREF(arglist);
free_pywatcher(pyw);
PyGILState_Release(gstate);
}
/* Called when an asynchronous call that returns a list of ACLs
completes and dispatches user provided callback */
void acl_completion_dispatch(int rc, struct ACL_vector *acl, struct Stat *stat, const void *data)
{
PyGILState_STATE gstate;
pywatcher_t *pyw = (pywatcher_t*)data;
if (pyw == NULL) {
return;
}
PyObject *callback = pyw->callback;
gstate = PyGILState_Ensure();
PyObject *pystat = build_stat(stat);
PyObject *pyacls = build_acls(acl);
PyObject *arglist = Py_BuildValue("(i,i,O,O)", pyw->zhandle,rc, pyacls, pystat);
Py_DECREF(pystat);
Py_DECREF(pyacls);
if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) {
PyErr_Print();
}
Py_DECREF(arglist);
free_pywatcher(pyw);
PyGILState_Release(gstate);
}
/* -------------------------------------------------------------------------- */
/* ZOOKEEPER API IMPLEMENTATION */
/* -------------------------------------------------------------------------- */
static PyObject *pyzookeeper_init(PyObject *self, PyObject *args)
{
const char *host;
PyObject *watcherfn = Py_None;
int recv_timeout = 10000;
// int clientid = -1;
clientid_t cid;
cid.client_id = -1;
const char *passwd;
int handle = next_zhandle();
if (handle == -1) {
if (resize_zhandles() == 0) {
return NULL;
}
handle = next_zhandle();
}
if (handle == -1) {
PyErr_SetString(ZooKeeperException,"Couldn't find a free zhandle, something is very wrong");
return NULL;
}
if (!PyArg_ParseTuple(args, "s|Oi(Ls)", &host, &watcherfn, &recv_timeout, &cid.client_id, &passwd))
return NULL;
if (cid.client_id != -1) {
strncpy(cid.passwd, passwd, 16*sizeof(char));
}
pywatcher_t *pyw = NULL;
if (watcherfn != Py_None) {
pyw = create_pywatcher(handle, watcherfn,1);
if (pyw == NULL) {
return NULL;
}
}
watchers[handle] = pyw;
zhandle_t *zh = zookeeper_init( host, watcherfn != Py_None ? watcher_dispatch : NULL,
recv_timeout, cid.client_id == -1 ? 0 : &cid,
pyw,
0 );
if (zh == NULL)
{
PyErr_SetString( ZooKeeperException, "Could not internally obtain zookeeper handle" );
return NULL;
}
zhandles[handle] = zh;
return Py_BuildValue( "i", handle);
}
/* -------------------------------------------------------------------------- */
/* Asynchronous API implementation */
/* -------------------------------------------------------------------------- */
/* Asynchronous node creation, returns integer error code */
PyObject *pyzoo_acreate(PyObject *self, PyObject *args)
{
int zkhid; char *path; char *value; int valuelen;
struct ACL_vector acl; int flags = 0;
PyObject *completion_callback = Py_None;
PyObject *pyacls = Py_None;
if (!PyArg_ParseTuple(args, "iss#O|iO", &zkhid, &path,
&value, &valuelen, &pyacls, &flags,
&completion_callback)) {
return NULL;
}
CHECK_ZHANDLE(zkhid);
CHECK_ACLS(pyacls);
if (parse_acls(&acl, pyacls) == 0) {
return NULL;
}
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_acreate( zhandles[zkhid],
path,
value,
valuelen,
pyacls == Py_None ? NULL : &acl,
flags,
string_completion_dispatch,
pyw);
free_acls(&acl);
if (err != ZOK)
{
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);
}
/* Asynchronous node deletion, returns integer error code */
PyObject *pyzoo_adelete(PyObject *self, PyObject *args)
{
int zkhid; char *path; int version = -1;
PyObject *completion_callback = Py_None;
if (!PyArg_ParseTuple(args, "is|iO", &zkhid, &path, &version, &completion_callback))
return NULL;
CHECK_ZHANDLE(zkhid);
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_adelete( zhandles[zkhid],
path,
version,
void_completion_dispatch,
pyw);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);
}
/* Asynchronous node existance check, returns integer error code */
PyObject *pyzoo_aexists(PyObject *self, PyObject *args)
{
int zkhid; char *path;
PyObject *completion_callback = Py_None;
PyObject *exists_watch = Py_None;
if (!PyArg_ParseTuple(args, "is|OO", &zkhid, &path,
&exists_watch, &completion_callback))
return NULL;
CHECK_ZHANDLE(zkhid);
void *comp_pyw = NULL;
if (completion_callback != Py_None) {
comp_pyw = create_pywatcher(zkhid, completion_callback, 0);
if (comp_pyw == NULL) {
return NULL;
}
}
void *exist_pyw = NULL;
if (exists_watch != Py_None) {
exist_pyw = create_pywatcher(zkhid, exists_watch, 0);
if (exist_pyw == NULL) {
return NULL;
}
}
int err = zoo_awexists( zhandles[zkhid],
path,
exists_watch != Py_None ? watcher_dispatch : NULL,
exist_pyw,
stat_completion_dispatch,
comp_pyw);
if (err != ZOK)
{
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);;
}
/* Asynchronous node data retrieval, returns integer error code */
PyObject *pyzoo_aget(PyObject *self, PyObject *args)
{
int zkhid; char *path;
PyObject *completion_callback = Py_None;
PyObject *get_watch = Py_None;
void *comp_pw = NULL;
void *watch_pw = NULL;
if (!PyArg_ParseTuple(args, "is|OO", &zkhid, &path,
&get_watch, &completion_callback)) {
return NULL;
}
CHECK_ZHANDLE(zkhid);
if (get_watch != Py_None) {
if ((watch_pw = create_pywatcher(zkhid, get_watch, 0)) == NULL) {
return NULL;
}
}
if (completion_callback != Py_None) {
if ((comp_pw = create_pywatcher(zkhid, completion_callback, 0)) == NULL) {
return NULL;
}
}
int err = zoo_awget( zhandles[zkhid],
path,
get_watch != Py_None ? watcher_dispatch : NULL,
watch_pw,
data_completion_dispatch,
comp_pw);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);
}
/* Asynchronous node contents update, returns integer error code */
PyObject *pyzoo_aset(PyObject *self, PyObject *args)
{
int zkhid; char *path; char *buffer; int buflen; int version=-1;
PyObject *completion_callback = Py_None;
if (!PyArg_ParseTuple(args, "iss#|iO", &zkhid, &path, &buffer, &buflen, &version, &completion_callback))
return NULL;
CHECK_ZHANDLE(zkhid);
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_aset( zhandles[zkhid],
path,
buffer,
buflen,
version,
stat_completion_dispatch,
pyw);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);
}
/* Asynchronous node child retrieval, returns integer error code */
PyObject *pyzoo_aget_children(PyObject *self, PyObject *args)
{
int zkhid; char *path;
PyObject *completion_callback = Py_None;
PyObject *get_watch;
if (!PyArg_ParseTuple(args, "is|OO", &zkhid, &path,
&get_watch, &completion_callback))
return NULL;
CHECK_ZHANDLE(zkhid);
void *get_pyw = NULL;
if (get_watch != Py_None) {
get_pyw = create_pywatcher(zkhid, get_watch, 0);
if (get_pyw == NULL) {
return NULL;
}
}
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_awget_children( zhandles[zkhid],
path,
get_watch != Py_None ? watcher_dispatch : NULL,
get_pyw,
strings_completion_dispatch,
pyw);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);;
}
/* Asynchronous sync, returns integer error code */
PyObject *pyzoo_async(PyObject *self, PyObject *args)
{
int zkhid; char *path;
PyObject *completion_callback = Py_None;
if (!PyArg_ParseTuple(args, "is|O", &zkhid, &path,
&completion_callback)) {
return NULL;
}
CHECK_ZHANDLE(zkhid);
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_async( zhandles[zkhid],
path,
string_completion_dispatch,
pyw);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);;
}
/* Asynchronous node ACL retrieval, returns integer error code */
PyObject *pyzoo_aget_acl(PyObject *self, PyObject *args)
{
int zkhid; char *path;
PyObject *completion_callback = Py_None;
if (!PyArg_ParseTuple(args, "is|O", &zkhid, &path,
&completion_callback)) {
return NULL;
}
CHECK_ZHANDLE(zkhid);
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_aget_acl( zhandles[zkhid],
path,
acl_completion_dispatch,
pyw);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);;
}
/* Asynchronous node ACL update, returns integer error code */
PyObject *pyzoo_aset_acl(PyObject *self, PyObject *args)
{
int zkhid; char *path; int version;
PyObject *completion_callback = Py_None, *pyacl;
struct ACL_vector aclv;
if (!PyArg_ParseTuple(args, "isiO|O", &zkhid, &path, &version,
&pyacl, &completion_callback)) {
return NULL;
}
CHECK_ZHANDLE(zkhid);
CHECK_ACLS(pyacl);
if (parse_acls(&aclv, pyacl) == 0) {
return NULL;
}
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_aset_acl( zhandles[zkhid],
path,
version,
&aclv,
void_completion_dispatch,
pyw);
free_acls(&aclv);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));
return NULL;
}
return Py_BuildValue("i", err);;
}
/* Asynchronous authorization addition, returns integer error code */
PyObject *pyzoo_add_auth(PyObject *self, PyObject *args)
{
int zkhid;
char *scheme, *cert;
int certLen;
PyObject *completion_callback;
if (!PyArg_ParseTuple(args, "iss#O", &zkhid, &scheme, &cert, &certLen,
&completion_callback)) {
return NULL;
}
CHECK_ZHANDLE(zkhid);
void *pyw = NULL;
if (completion_callback != Py_None) {
pyw = create_pywatcher(zkhid, completion_callback, 0);
if (pyw == NULL) {
return NULL;
}
}
int err = zoo_add_auth( zhandles[zkhid],
scheme,
cert,
certLen,
void_completion_dispatch,
pyw);
if (err != ZOK) {
PyErr_SetString(err_to_exception(err), zerror(err));