]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/srfsh/srfsh.c
Open-ILS/src/c-apps/oils_auth.c
[OpenSRF.git] / src / srfsh / srfsh.c
1 #include "srfsh.h"
2
3 int recv_timeout = 120;
4 int is_from_script = 0;
5 FILE* shell_writer = NULL;
6 FILE* shell_reader = NULL;
7
8 int main( int argc, char* argv[] ) {
9
10         /* --------------------------------------------- */
11         /* see if they have a .srfsh.xml in their home directory */
12         char* home = getenv("HOME");
13         int l = strlen(home) + 36;
14         char fbuf[l];
15         memset(fbuf, 0, l);
16         sprintf(fbuf,"%s/.srfsh.xml",home);
17         
18         if(!access(fbuf, R_OK)) {
19                 if( ! osrf_system_bootstrap_client(fbuf, "srfsh") ) {
20                         fprintf(stderr,"Unable to bootstrap client for requests\n");
21                         osrfLogError( OSRF_LOG_MARK,  "Unable to bootstrap client for requests");
22                         return -1;
23                 }
24
25         } else {
26                 fprintf(stderr,"No Config file found at %s\n", fbuf ); 
27                 return -1;
28         }
29
30         if(argc > 1) {
31                 /* for now.. the first arg is used as a script file for processing */
32                 int f;
33                 if( (f = open(argv[1], O_RDONLY)) == -1 ) {
34                         osrfLogError( OSRF_LOG_MARK, "Unable to open file %s for reading, exiting...", argv[1]);
35                         return -1;
36                 }
37
38                 if(dup2(f, STDIN_FILENO) == -1) {
39                         osrfLogError( OSRF_LOG_MARK, "Unable to duplicate STDIN, exiting...");
40                         return -1;
41                 }
42
43                 close(f);
44                 is_from_script = 1;
45         }
46                 
47         /* --------------------------------------------- */
48         load_history();
49
50
51         client = osrf_system_get_transport_client();
52
53         /* open the shell handle */
54         shell_writer = popen( "bash", "w");
55         //shell_reader = popen( "bash", "r");
56
57         /* main process loop */
58         char* request;
59         while((request=readline(prompt))) {
60
61                 if( !strcasecmp(request, "exit") || !strcasecmp(request,"quit")) 
62                         break; 
63
64                 char* req_copy = strdup(request);
65
66                 parse_request( req_copy ); 
67                 if( request && strlen(request) > 1 ) {
68                         add_history(request);
69                 }
70
71                 free(request);
72                 free(req_copy);
73
74                 fflush(shell_writer);
75                 fflush(stderr);
76                 fflush(stdout);
77         }
78
79         if(history_file != NULL )
80                 write_history(history_file);
81
82         free(request);
83
84         osrf_system_shutdown();
85         return 0;
86 }
87
88 void sig_child_handler( int s ) {
89         child_dead = 1;
90 }
91
92 /*
93 void sig_int_handler( int s ) {
94         printf("\n");
95         caught_sigint = 1;
96         signal(SIGINT,sig_int_handler);
97 }
98 */
99
100 int load_history() {
101
102         char* home = getenv("HOME");
103         int l = strlen(home) + 24;
104         char fbuf[l];
105
106         memset(fbuf, 0, l);
107         sprintf(fbuf,"%s/.srfsh_history",home);
108         history_file = strdup(fbuf);
109
110         if(!access(history_file, W_OK | R_OK )) {
111                 history_length = 999;
112                 read_history(history_file);
113         }
114         return 1;
115 }
116
117
118 int parse_error( char* words[] ) {
119
120         if( ! words )
121                 return 0;
122
123
124         int i = 0;
125         char* current;
126         char buffer[256];
127         memset(buffer, 0, 256);
128         while( (current=words[i++]) ) {
129                 strcat(buffer, current);
130                 strcat(buffer, " ");
131         }
132         if( ! buffer || strlen(buffer) < 1 ) 
133                 printf("\n");
134
135         fprintf( stderr, "???: %s\n", buffer );
136         return 0;
137
138 }
139
140
141 int parse_request( char* request ) {
142
143         if( request == NULL )
144                 return 0;
145
146         int ret_val = 0;
147         int i = 0;
148         char* words[COMMAND_BUFSIZE]; 
149         memset(words,0,COMMAND_BUFSIZE);
150         char* req = request;
151
152         char* cur_tok = strtok( req, " " );
153
154         if( cur_tok == NULL )
155                 return 0;
156
157         while(cur_tok != NULL) {
158                 words[i++] = cur_tok;
159                 cur_tok = strtok( NULL, " " );
160         }
161
162
163         // not sure why (strtok?), but this is necessary
164         memset( words + i, 0, COMMAND_BUFSIZE - i );
165
166         /* pass off to the top level command */
167         if( !strcmp(words[0],"router") ) 
168                 ret_val = handle_router( words );
169
170         /*
171         else if( !strcmp(words[0],"time") ) 
172                 ret_val = handle_time( words );
173                 */
174
175         else if (!strcmp(words[0],"request"))
176                 ret_val = handle_request( words, 0 );
177
178         else if (!strcmp(words[0],"relay"))
179                 ret_val = handle_request( words, 1 );
180
181         else if (!strcmp(words[0],"help"))
182                 ret_val = print_help();
183
184         else if (!strcmp(words[0],"set"))
185                 ret_val = handle_set(words);
186
187         else if (!strcmp(words[0],"print"))
188                 ret_val = handle_print(words);
189
190         else if (!strcmp(words[0],"math_bench"))
191                 ret_val = handle_math(words);
192
193         else if (!strcmp(words[0],"introspect"))
194                 ret_val = handle_introspect(words);
195
196         else if (!strcmp(words[0],"login"))
197                 ret_val = handle_login(words);
198
199         else if (words[0][0] == '!')
200                 ret_val = handle_exec( words, 1 );
201
202         if(!ret_val) {
203                 #ifdef EXEC_DEFAULT
204                         return handle_exec( words, 0 );
205                 #else
206                         return parse_error( words );
207                 #endif
208         }
209
210         return 1;
211
212 }
213
214
215 int handle_introspect(char* words[]) {
216
217         if(words[1] && words[2]) {
218                 fprintf(stderr, "--> %s\n", words[1]);
219                 char buf[256];
220                 memset(buf,0,256);
221                 sprintf( buf, "request %s opensrf.system.method %s", words[1], words[2] );
222                 return parse_request( buf );
223
224         } else {
225         
226                 if(words[1]) {
227                         fprintf(stderr, "--> %s\n", words[1]);
228                         char buf[256];
229                         memset(buf,0,256);
230                         sprintf( buf, "request %s opensrf.system.method.all", words[1] );
231                         return parse_request( buf );
232                 }
233         }
234
235         return 0;
236 }
237
238
239 int handle_login( char* words[]) {
240
241         if( words[1] && words[2]) {
242
243                 char* username = words[1];
244                 char* password = words[2];
245                 char* type              = words[3];
246                 char* orgloc    = words[4];
247                 char* wsid              = words[5];
248                 int orgloci = (orgloc) ? atoi(orgloc) : 0;
249                 int wsidi = (wsid) ? atoi(wsid) : 0;
250                 if(!type) type = "opac";
251
252                 char buf[256];
253                 memset(buf,0,256);
254
255                 char buf2[256];
256                 memset(buf2,0,256);
257
258                 sprintf( buf, 
259                                 "request open-ils.auth open-ils.auth.authenticate.init \"%s\"", username );
260                 parse_request(buf); 
261
262                 char* hash;
263                 if(last_result && last_result->_result_content) {
264                         jsonObject* r = last_result->_result_content;
265                         hash = jsonObjectGetString(r);
266                 } else return 0;
267
268
269                 char* pass_buf = md5sum(password);
270
271                 char both_buf[256];
272                 memset(both_buf,0,256);
273                 sprintf(both_buf,"%s%s",hash, pass_buf);
274
275                 char* mess_buf = md5sum(both_buf);
276
277                 sprintf( buf2, "request open-ils.auth open-ils.auth.authenticate.complete "
278                                 "\"%s\", \"%s\", \"%s\", %d, %d", username, mess_buf, type, orgloci, wsidi );
279
280                 free(pass_buf);
281                 free(mess_buf);
282
283                 parse_request( buf2 );
284
285                 jsonObject* x = last_result->_result_content;
286                 double authtime = 0;
287                 if(x) {
288                         char* authtoken = jsonObjectGetString(
289                                         jsonObjectGetKey(jsonObjectGetKey(x,"payload"), "authtoken"));
290                         authtime  = jsonObjectGetNumber(
291                                         jsonObjectGetKey(jsonObjectGetKey(x,"payload"), "authtime"));
292                         if(authtoken) login_session = strdup(authtoken);
293                         else login_session = NULL;
294                 }
295                 else login_session = NULL;
296
297                 printf("Login Session: %s.  Session timeout: %lf\n", login_session, authtime );
298                 
299                 return 1;
300
301         }
302
303         return 0;
304 }
305
306 int handle_set( char* words[]) {
307
308         char* variable;
309         if( (variable=words[1]) ) {
310
311                 char* val;
312                 if( (val=words[2]) ) {
313
314                         if(!strcmp(variable,"pretty_print")) {
315                                 if(!strcmp(val,"true")) {
316                                         pretty_print = 1;
317                                         printf("pretty_print = true\n");
318                                         return 1;
319                                 } 
320                                 if(!strcmp(val,"false")) {
321                                         pretty_print = 0;
322                                         printf("pretty_print = false\n");
323                                         return 1;
324                                 } 
325                         }
326
327                         if(!strcmp(variable,"raw_print")) {
328                                 if(!strcmp(val,"true")) {
329                                         raw_print = 1;
330                                         printf("raw_print = true\n");
331                                         return 1;
332                                 } 
333                                 if(!strcmp(val,"false")) {
334                                         raw_print = 0;
335                                         printf("raw_print = false\n");
336                                         return 1;
337                                 } 
338                         }
339
340                 }
341         }
342
343         return 0;
344 }
345
346
347 int handle_print( char* words[]) {
348
349         char* variable;
350         if( (variable=words[1]) ) {
351                 if(!strcmp(variable,"pretty_print")) {
352                         if(pretty_print) {
353                                 printf("pretty_print = true\n");
354                                 return 1;
355                         } else {
356                                 printf("pretty_print = false\n");
357                                 return 1;
358                         }
359                 }
360
361                 if(!strcmp(variable,"login")) {
362                         printf("login session = %s\n", login_session );
363                         return 1;
364                 }
365
366         }
367         return 0;
368 }
369
370 int handle_router( char* words[] ) {
371
372         if(!client)
373                 return 1;
374
375         int i;
376
377         if( words[1] ) { 
378                 if( !strcmp(words[1],"query") ) {
379                         
380                         if( words[2] && !strcmp(words[2],"servers") ) {
381                                 for(i=3; i < COMMAND_BUFSIZE - 3 && words[i]; i++ ) {   
382                                         router_query_servers( words[i] );
383                                 }
384                                 return 1;
385                         }
386                         return 0;
387                 }
388                 return 0;
389         }
390         return 0;
391 }
392
393
394 /* if new shell, spawn a new child and subshell to do the work,
395         otherwise pipe the request to the currently open (piped) shell */
396 int handle_exec(char* words[], int new_shell) {
397
398         if(!words[0]) return 0;
399
400         if( words[0] && words[0][0] == '!') {
401                 int len = strlen(words[0]);
402                 char command[len];
403                 memset(command,0,len);
404         
405                 int i; /* chop out the ! */
406                 for( i=1; i!= len; i++) {
407                         command[i-1] = words[0][i];
408                 }
409         
410                 free(words[0]);
411                 words[0] = strdup(command);
412         }
413
414         if(new_shell) {
415                 signal(SIGCHLD, sig_child_handler);
416
417                 if(fork()) {
418         
419                         waitpid(-1, 0, 0);
420                         if(child_dead) {
421                                 signal(SIGCHLD,sig_child_handler);
422                                 child_dead = 0;
423                         }
424         
425                 } else {
426                         execvp( words[0], words );
427                         exit(0);
428                 }
429
430         } else {
431
432
433                 growing_buffer* b = buffer_init(64);
434                 int i = 0;
435                 while(words[i]) 
436                         buffer_fadd( b, "%s ", words[i++] );
437         
438                 buffer_add( b, "\n");
439         
440                 //int reader;
441                 //int reader = dup2(STDOUT_FILENO, reader);
442                 //int reader = dup(STDOUT_FILENO);
443                 //close(STDOUT_FILENO);
444
445                 fprintf( shell_writer, b->buf );
446                 buffer_free(b);
447         
448                 fflush(shell_writer);
449                 usleep(1000);
450
451                 /*
452                 char c[4096];
453                 bzero(c, 4096);
454                 read( reader, c, 4095 );
455                 fprintf(stderr, "read %s", c);
456                 dup2(reader, STDOUT_FILENO);
457                 */
458
459         }
460
461         
462         return 1;
463 }
464
465
466 int handle_request( char* words[], int relay ) {
467
468         if(!client)
469                 return 1;
470
471         if(words[1]) {
472                 char* server = words[1];
473                 char* method = words[2];
474                 int i;
475                 growing_buffer* buffer = NULL;
476                 if(!relay) {
477                         buffer = buffer_init(128);
478                         buffer_add(buffer, "[");
479                         for(i = 3; words[i] != NULL; i++ ) {
480                                 /* removes trailing semicolon if user accidentally enters it */
481                                 if( words[i][strlen(words[i])-1] == ';' )
482                                         words[i][strlen(words[i])-1] = '\0';
483                                 buffer_add( buffer, words[i] );
484                                 buffer_add(buffer, " ");
485                         }
486                         buffer_add(buffer, "]");
487                 }
488
489                 return send_request( server, method, buffer, relay );
490         } 
491
492         return 0;
493 }
494
495 int send_request( char* server, 
496                 char* method, growing_buffer* buffer, int relay ) {
497         if( server == NULL || method == NULL )
498                 return 0;
499
500         jsonObject* params = NULL;
501         if( !relay ) {
502                 if( buffer != NULL && buffer->n_used > 0 ) 
503                         params = json_parse_string(buffer->buf);
504         } else {
505                 if(!last_result || ! last_result->_result_content) { 
506                         printf("We're not going to call 'relay' with no result params\n");
507                         return 1;
508                 }
509                 else {
510                         jsonObject* o = jsonNewObject(NULL);
511                         jsonObjectPush(o, last_result->_result_content );
512                         params = o;
513                 }
514         }
515
516
517         if(buffer->n_used > 0 && params == NULL) {
518                 fprintf(stderr, "JSON error detected, not executing\n");
519                 return 1;
520         }
521
522         osrf_app_session* session = osrf_app_client_session_init(server);
523
524         if(!osrf_app_session_connect(session)) {
525                 osrfLogWarning( OSRF_LOG_MARK,  "Unable to connect to remote service %s\n", server );
526                 return 1;
527         }
528
529         double start = get_timestamp_millis();
530         //int req_id = osrf_app_session_make_request( session, params, method, 1, NULL );
531         int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
532
533
534         osrf_message* omsg = osrf_app_session_request_recv( session, req_id, recv_timeout );
535
536         if(!omsg) 
537                 printf("\nReceived no data from server\n");
538         
539         
540         signal(SIGPIPE, SIG_IGN);
541
542         FILE* less; 
543         if(!is_from_script) less = popen( "less -EX", "w");
544         else less = stdout;
545
546         if( less == NULL ) { less = stdout; }
547
548         growing_buffer* resp_buffer = buffer_init(4096);
549
550         while(omsg) {
551
552                 if(raw_print) {
553
554                         if(omsg->_result_content) {
555         
556                                 osrf_message_free(last_result);
557                                 last_result = omsg;
558         
559                                 char* content;
560         
561                                 if( pretty_print && omsg->_result_content ) {
562                                         char* j = jsonObjectToJSON(omsg->_result_content);
563                                         //content = json_printer(j); 
564                                         content = jsonFormatString(j);
565                                         free(j);
566                                 } else
567                                         content = jsonObjectGetString(omsg->_result_content);
568         
569                                 printf( "\nReceived Data: %s\n", content ); 
570                                 free(content);
571         
572                         } else {
573
574                                 char code[16];
575                                 memset(code, 0, 16);
576                                 sprintf( code, "%d", omsg->status_code );
577                                 buffer_add( resp_buffer, code );
578
579                                 printf( "\nReceived Exception:\nName: %s\nStatus: %s\nStatus: %s\n", 
580                                                 omsg->status_name, omsg->status_text, code );
581
582                                 fflush(stdout);
583                         }
584
585                 } else {
586
587                         if(omsg->_result_content) {
588         
589                                 osrf_message_free(last_result);
590                                 last_result = omsg;
591         
592                                 char* content;
593         
594                                 if( pretty_print && omsg->_result_content ) {
595                                         char* j = jsonObjectToJSON(omsg->_result_content);
596                                         //content = json_printer(j); 
597                                         content = jsonFormatString(j);
598                                         free(j);
599                                 } else
600                                         content = jsonObjectGetString(omsg->_result_content);
601         
602                                 buffer_add( resp_buffer, "\nReceived Data: " ); 
603                                 buffer_add( resp_buffer, content );
604                                 buffer_add( resp_buffer, "\n" );
605                                 free(content);
606         
607                         } else {
608         
609                                 buffer_add( resp_buffer, "\nReceived Exception:\nName: " );
610                                 buffer_add( resp_buffer, omsg->status_name );
611                                 buffer_add( resp_buffer, "\nStatus: " );
612                                 buffer_add( resp_buffer, omsg->status_text );
613                                 buffer_add( resp_buffer, "\nStatus: " );
614                                 char code[16];
615                                 memset(code, 0, 16);
616                                 sprintf( code, "%d", omsg->status_code );
617                                 buffer_add( resp_buffer, code );
618                         }
619                 }
620
621
622                 omsg = osrf_app_session_request_recv( session, req_id, recv_timeout );
623
624         }
625
626         double end = get_timestamp_millis();
627
628         fprintf( less, resp_buffer->buf );
629         buffer_free( resp_buffer );
630         fprintf( less, "\n------------------------------------\n");
631         if( osrf_app_session_request_complete( session, req_id ))
632                 fprintf(less, "Request Completed Successfully\n");
633
634
635         fprintf(less, "Request Time in seconds: %.6f\n", end - start );
636         fprintf(less, "------------------------------------\n");
637
638         pclose(less); 
639
640         osrf_app_session_request_finish( session, req_id );
641         osrf_app_session_disconnect( session );
642         osrf_app_session_destroy( session );
643
644
645         return 1;
646
647
648 }
649
650 /*
651 int handle_time( char* words[] ) {
652
653         if( ! words[1] ) {
654
655                 char buf[36];
656                 memset(buf,0,36);
657                 get_timestamp(buf);
658                 printf( "%s\n", buf );
659                 return 1;
660         }
661
662         if( words[1] ) {
663                 time_t epoch = (time_t)atoi( words[1] );
664                 char* localtime = strdup( ctime( &epoch ) );
665                 printf( "%s => %s", words[1], localtime );
666                 free(localtime);
667                 return 1;
668         }
669
670         return 0;
671
672 }
673 */
674
675                 
676
677 int router_query_servers( char* router_server ) {
678
679         if( ! router_server || strlen(router_server) == 0 ) 
680                 return 0;
681
682         char rbuf[256];
683         memset(rbuf,0,256);
684         sprintf(rbuf,"router@%s/router", router_server );
685                 
686         transport_message* send = 
687                 message_init( "servers", NULL, NULL, rbuf, NULL );
688         message_set_router_info( send, NULL, NULL, NULL, "query", 0 );
689
690         client_send_message( client, send );
691         message_free( send );
692
693         transport_message* recv = client_recv( client, -1 );
694         if( recv == NULL ) {
695                 fprintf(stderr, "NULL message received from router\n");
696                 return 1;
697         }
698         
699         printf( 
700                         "---------------------------------------------------------------------------------\n"
701                         "Received from 'server' query on %s\n"
702                         "---------------------------------------------------------------------------------\n"
703                         "original reg time | latest reg time | last used time | class | server\n"
704                         "---------------------------------------------------------------------------------\n"
705                         "%s"
706                         "---------------------------------------------------------------------------------\n"
707                         , router_server, recv->body );
708
709         message_free( recv );
710         
711         return 1;
712 }
713                 
714 int print_help() {
715
716         printf(
717                         "---------------------------------------------------------------------------------\n"
718                         "Commands:\n"
719                         "---------------------------------------------------------------------------------\n"
720                         "help                   - Display this message\n"
721                         "!<command> [args] - Forks and runs the given command in the shell\n"
722                         "time                   - Prints the current time\n"                                    
723                         "time <timestamp>       - Formats seconds since epoch into readable format\n"   
724                         "set <variable> <value> - set a srfsh variable (e.g. set pretty_print true )\n"
725                         "print <variable>               - Displays the value of a srfsh variable\n"
726                         "---------------------------------------------------------------------------------\n"
727
728                         "router query servers <server1 [, server2, ...]>\n"
729                         "       - Returns stats on connected services\n"
730                         "\n"
731                         "\n"
732                         "request <service> <method> [ <json formatted string of params> ]\n"
733                         "       - Anything passed in will be wrapped in a json array,\n"
734                         "               so add commas if there is more than one param\n"
735                         "\n"
736                         "\n"
737                         "relay <service> <method>\n"
738                         "       - Performs the requested query using the last received result as the param\n"
739                         "\n"
740                         "\n"
741                         "math_bench <num_batches> [0|1|2]\n"
742                         "       - 0 means don't reconnect, 1 means reconnect after each batch of 4, and\n"
743                         "                2 means reconnect after every request\n"
744                         "\n"
745                         "introspect <service>\n"
746                         "       - prints the API for the service\n"
747                         "\n"
748                         "\n"
749                         "---------------------------------------------------------------------------------\n"
750                         " Commands for Open-ILS\n"
751                         "---------------------------------------------------------------------------------\n"
752                         "login <username> <password>\n"
753                         "       -       Logs into the 'server' and displays the session id\n"
754                         "       - To view the session id later, enter: print login\n"
755                         "---------------------------------------------------------------------------------\n"
756                         "\n"
757                         "\n"
758                         "Note: long output is piped through 'less'.  To search in 'less', type: /<search>\n"
759                         "---------------------------------------------------------------------------------\n"
760                         "\n"
761                         );
762
763         return 1;
764 }
765
766
767
768 char* tabs(int count) {
769         growing_buffer* buf = buffer_init(24);
770         int i;
771         for(i=0;i!=count;i++)
772                 buffer_add(buf, "  ");
773
774         char* final = buffer_data( buf );
775         buffer_free( buf );
776         return final;
777 }
778
779 int handle_math( char* words[] ) {
780         if( words[1] )
781                 return do_math( atoi(words[1]), 0 );
782         return 0;
783 }
784
785
786 int do_math( int count, int style ) {
787
788         osrf_app_session* session = osrf_app_client_session_init(  "opensrf.math" );
789
790         jsonObject* params = json_parse_string("[]");
791         jsonObjectPush(params,jsonNewObject("1"));
792         jsonObjectPush(params,jsonNewObject("2"));
793
794         char* methods[] = { "add", "sub", "mult", "div" };
795         char* answers[] = { "3", "-1", "2", "0.500000" };
796
797         float times[ count * 4 ];
798         memset(times,0,count*4);
799
800         int k;
801         for(k=0;k!=100;k++) {
802                 if(!(k%10)) 
803                         fprintf(stderr,"|");
804                 else
805                         fprintf(stderr,".");
806         }
807
808         fprintf(stderr,"\n\n");
809
810         int running = 0;
811         int i;
812         for(i=0; i!= count; i++) {
813
814                 int j;
815                 for(j=0; j != 4; j++) {
816
817                         ++running;
818
819                         double start = get_timestamp_millis();
820                         int req_id = osrf_app_session_make_req( session, params, methods[j], 1, NULL );
821                         osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 5 );
822                         double end = get_timestamp_millis();
823
824                         times[(4*i) + j] = end - start;
825
826                         if(omsg) {
827         
828                                 if(omsg->_result_content) {
829                                         char* jsn = jsonObjectToJSON(omsg->_result_content);
830                                         if(!strcmp(jsn, answers[j]))
831                                                 fprintf(stderr, "+");
832                                         else
833                                                 fprintf(stderr, "\n![%s] - should be %s\n", jsn, answers[j] );
834                                         free(jsn);
835                                 }
836
837
838                                 osrf_message_free(omsg);
839                 
840                         } else { fprintf( stderr, "\nempty message for tt: %d\n", req_id ); }
841
842                         osrf_app_session_request_finish( session, req_id );
843
844                         if(style == 2)
845                                 osrf_app_session_disconnect( session );
846
847                         if(!(running%100))
848                                 fprintf(stderr,"\n");
849                 }
850
851                 if(style==1)
852                         osrf_app_session_disconnect( session );
853         }
854
855         osrf_app_session_destroy( session );
856         jsonObjectFree(params);
857
858         int c;
859         float total = 0;
860         for(c=0; c!= count*4; c++) 
861                 total += times[c];
862
863         float avg = total / (count*4); 
864         fprintf(stderr, "\n      Average round trip time: %f\n", avg );
865
866         return 1;
867 }