@@ -35,13 +35,24 @@ static zhandle_t *zh;
3535static clientid_t myid ;
3636static const char * clientIdFile = 0 ;
3737struct timeval startTime ;
38+ static char cmd [1024 ];
39+ static int batchMode = 0 ;
3840
3941static int to_send = 0 ;
4042static int sent = 0 ;
4143static int recvd = 0 ;
4244
4345static int shutdownThisThing = 0 ;
4446
47+ static __attribute__ ((unused )) void
48+ printProfileInfo (struct timeval start , struct timeval end ,int thres ,const char * msg )
49+ {
50+ int delay = (end .tv_sec * 1000 + end .tv_usec /1000 )-
51+ (start .tv_sec * 1000 + start .tv_usec /1000 );
52+ if (delay > thres )
53+ fprintf (stderr ,"%s: execution time=%dms\n" ,msg ,delay );
54+ }
55+
4556void watcher (zhandle_t * zzh , int type , int state , const char * path ) {
4657 fprintf (stderr ,"Watcher %d state = %d for %s\n" , type , state , (path ? path : "null" ));
4758 if (type == SESSION_EVENT ) {
@@ -101,6 +112,8 @@ void my_string_completion(int rc, const char *name, const void *data) {
101112 if (!rc ) {
102113 fprintf (stderr , "\tname = %s\n" , name );
103114 }
115+ if (batchMode )
116+ shutdownThisThing = 1 ;
104117}
105118
106119void my_data_completion (int rc , const char * value , int value_len ,
@@ -120,25 +133,19 @@ void my_data_completion(int rc, const char *value, int value_len,
120133 fprintf (stderr , "\nStat:\n" );
121134 dumpStat (stat );
122135 free ((void * )data );
136+ if (batchMode )
137+ shutdownThisThing = 1 ;
123138}
124139
125140void my_silent_data_completion (int rc , const char * value , int value_len ,
126141 const struct Stat * stat , const void * data ) {
127- // char buf[value_len+1];
128- // if(value){
129- // strncpy(buf,value,value_len);buf[value_len]=0;
130- // }
131- // fprintf(stderr, "Data completion: %s=\n[%s] rc = %d\n",(char*)data,
132- // value?buf:"null", rc);
133142 recvd ++ ;
134143 fprintf (stderr , "Data completion %s rc = %d\n" ,(char * )data ,rc );
135144 free ((void * )data );
136- // if(recvd==100){
137- // fprintf(stderr, "Sleeping for a few moments\n");
138- // sleep(2);
139- // }
140145 if (recvd == to_send ) {
141146 fprintf (stderr ,"Recvd %d responses for %d requests sent\n" ,recvd ,to_send );
147+ if (batchMode )
148+ shutdownThisThing = 1 ;
142149 }
143150}
144151
@@ -163,17 +170,23 @@ void my_strings_completion(int rc, const struct String_vector *strings,
163170 sec = tv .tv_sec - startTime .tv_sec ;
164171 usec = tv .tv_usec - startTime .tv_usec ;
165172 fprintf (stderr , "time = %d msec\n" , sec * 1000 + usec /1000 );
173+ if (batchMode )
174+ shutdownThisThing = 1 ;
166175}
167176
168177void my_void_completion (int rc , const void * data ) {
169178 fprintf (stderr , "%s: rc = %d\n" , (char * )data , rc );
170179 free ((void * )data );
180+ if (batchMode )
181+ shutdownThisThing = 1 ;
171182}
172183
173184void my_stat_completion (int rc , const struct Stat * stat , const void * data ) {
174185 fprintf (stderr , "%s: rc = %d Stat:\n" , (char * )data , rc );
175186 dumpStat (stat );
176187 free ((void * )data );
188+ if (batchMode )
189+ shutdownThisThing = 1 ;
177190}
178191
179192void my_silent_stat_completion (int rc , const struct Stat * stat ,
@@ -355,6 +368,7 @@ void processline(char *line) {
355368int main (int argc , char * * argv ) {
356369#ifndef THREADED
357370 fd_set rfds , wfds , efds ;
371+ int processed = 0 ;
358372#endif
359373 char buffer [4096 ];
360374 char p [2048 ];
@@ -366,16 +380,24 @@ int main(int argc, char **argv) {
366380 FILE * fh ;
367381
368382 if (argc < 2 ) {
369- fprintf (stderr , "USAGE %s zookeeper_host_list [clientid_file]\n" , argv [0 ]);
383+ fprintf (stderr ,
384+ "USAGE %s zookeeper_host_list [clientid_file|cmd:(ls|create|od|...)]\n" ,
385+ argv [0 ]);
370386 return 2 ;
371387 }
372388 if (argc > 2 ) {
389+ if (strncmp ("cmd:" ,argv [2 ],4 )== 0 ){
390+ strcpy (cmd ,argv [2 ]+ 4 );
391+ batchMode = 1 ;
392+ fprintf (stderr ,"Batch mode: %s\n" ,cmd );
393+ }else {
373394 clientIdFile = argv [2 ];
374395 fh = fopen (clientIdFile , "r" );
375396 if (fh ) {
376397 fread (& myid , sizeof (myid ), 1 , fh );
377398 fclose (fh );
378399 }
400+ }
379401 }
380402#ifdef YCA
381403 strcpy (appId ,"yahoo.example.yca_test" );
@@ -463,6 +485,11 @@ int main(int argc, char **argv) {
463485 if (FD_ISSET (fd , & wfds )) {
464486 events |= ZOOKEEPER_WRITE ;
465487 }
488+ if (batchMode && processed == 0 ){
489+ //batch mode
490+ processline (cmd );
491+ processed = 1 ;
492+ }
466493 if (FD_ISSET (0 , & rfds )) {
467494 int rc ;
468495 int len = sizeof (buffer ) - bufoff - 1 ;
0 commit comments