]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/srfsh/srfsh.c
Added additional param to all the osrfLog* calls which mark
[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                 int orgloci = 0;
248                 if(orgloc) orgloci = atoi(orgloc);
249                 if(!type) type = "opac";
250
251                 char buf[256];
252                 memset(buf,0,256);
253
254                 char buf2[256];
255                 memset(buf2,0,256);
256
257                 sprintf( buf, 
258                                 "request open-ils.auth open-ils.auth.authenticate.init \"%s\"", username );
259                 parse_request(buf); 
260
261                 char* hash;
262                 if(last_result && last_result->_result_content) {
263                         jsonObject* r = last_result->_result_content;
264                         hash = jsonObjectGetString(r);
265                 } else return 0;
266
267
268                 char* pass_buf = md5sum(password);
269
270                 char both_buf[256];
271                 memset(both_buf,0,256);
272                 sprintf(both_buf,"%s%s",hash, pass_buf);
273
274                 char* mess_buf = md5sum(both_buf);
275
276                 sprintf( buf2, "request open-ils.auth open-ils.auth.authenticate.complete "
277                                 "\"%s\", \"%s\", \"%s\", %d", username, mess_buf, type, orgloci );
278
279                 free(pass_buf);
280                 free(mess_buf);
281
282                 parse_request( buf2 );
283
284                 jsonObject* x = last_result->_result_content;
285                 double authtime = 0;
286                 if(x) {
287                         char* authtoken = jsonObjectGetString(
288                                         jsonObjectGetKey(jsonObjectGetKey(x,"payload"), "authtoken"));
289                         authtime  = jsonObjectGetNumber(
290                                         jsonObjectGetKey(jsonObjectGetKey(x,"payload"), "authtime"));
291                         if(authtoken) login_session = strdup(authtoken);
292                         else login_session = NULL;
293                 }
294                 else login_session = NULL;
295
296                 printf("Login Session: %s.  Session timeout: %lf\n", login_session, authtime );
297                 
298                 return 1;
299
300         }
301
302         return 0;
303 }
304
305 int handle_set( char* words[]) {
306
307         char* variable;
308         if( (variable=words[1]) ) {
309
310                 char* val;
311                 if( (val=words[2]) ) {
312
313                         if(!strcmp(variable,"pretty_print")) {
314                                 if(!strcmp(val,"true")) {
315                                         pretty_print = 1;
316                                         printf("pretty_print = true\n");
317                                         return 1;
318                                 } 
319                                 if(!strcmp(val,"false")) {
320                                         pretty_print = 0;
321                                         printf("pretty_print = false\n");
322                                         return 1;
323                                 } 
324                         }
325
326                         if(!strcmp(variable,"raw_print")) {
327                                 if(!strcmp(val,"true")) {
328                                         raw_print = 1;
329                                         printf("raw_print = true\n");
330                                         return 1;
331                                 } 
332                                 if(!strcmp(val,"false")) {
333                                         raw_print = 0;
334                                         printf("raw_print = false\n");
335                                         return 1;
336                                 } 
337                         }
338
339                 }
340         }
341
342         return 0;
343 }
344
345
346 int handle_print( char* words[]) {
347
348         char* variable;
349         if( (variable=words[1]) ) {
350                 if(!strcmp(variable,"pretty_print")) {
351                         if(pretty_print) {
352                                 printf("pretty_print = true\n");
353                                 return 1;
354                         } else {
355                                 printf("pretty_print = false\n");
356                                 return 1;
357                         }
358                 }
359
360                 if(!strcmp(variable,"login")) {
361                         printf("login session = %s\n", login_session );
362                         return 1;
363                 }
364
365         }
366         return 0;
367 }
368
369 int handle_router( char* words[] ) {
370
371         if(!client)
372                 return 1;
373
374         int i;
375
376         if( words[1] ) { 
377                 if( !strcmp(words[1],"query") ) {
378                         
379                         if( words[2] && !strcmp(words[2],"servers") ) {
380                                 for(i=3; i < COMMAND_BUFSIZE - 3 && words[i]; i++ ) {   
381                                         router_query_servers( words[i] );
382                                 }
383                                 return 1;
384                         }
385                         return 0;
386                 }
387                 return 0;
388         }
389         return 0;
390 }
391
392
393 /* if new shell, spawn a new child and subshell to do the work,
394         otherwise pipe the request to the currently open (piped) shell */
395 int handle_exec(char* words[], int new_shell) {
396
397         if(!words[0]) return 0;
398
399         if( words[0] && words[0][0] == '!') {
400                 int len = strlen(words[0]);
401                 char command[len];
402                 memset(command,0,len);
403         
404                 int i; /* chop out the ! */
405                 for( i=1; i!= len; i++) {
406                         command[i-1] = words[0][i];
407                 }
408         
409                 free(words[0]);
410                 words[0] = strdup(command);
411         }
412
413         if(new_shell) {
414                 signal(SIGCHLD, sig_child_handler);
415
416                 if(fork()) {
417         
418                         waitpid(-1, 0, 0);
419                         if(child_dead) {
420                                 signal(SIGCHLD,sig_child_handler);
421                                 child_dead = 0;
422                         }
423         
424                 } else {
425                         execvp( words[0], words );
426                         exit(0);
427                 }
428
429         } else {
430
431
432                 growing_buffer* b = buffer_init(64);
433                 int i = 0;
434                 while(words[i]) 
435                         buffer_fadd( b, "%s ", words[i++] );
436         
437                 buffer_add( b, "\n");
438         
439                 //int reader;
440                 //int reader = dup2(STDOUT_FILENO, reader);
441                 //int reader = dup(STDOUT_FILENO);
442                 //close(STDOUT_FILENO);
443
444                 fprintf( shell_writer, b->buf );
445                 buffer_free(b);
446         
447                 fflush(shell_writer);
448                 usleep(1000);
449
450                 /*
451                 char c[4096];
452                 bzero(c, 4096);
453                 read( reader, c, 4095 );
454                 fprintf(stderr, "read %s", c);
455                 dup2(reader, STDOUT_FILENO);
456                 */
457
458         }
459
460         
461         return 1;
462 }
463
464
465 int handle_request( char* words[], int relay ) {
466
467         if(!client)
468                 return 1;
469
470         if(words[1]) {
471                 char* server = words[1];
472                 char* method = words[2];
473                 int i;
474                 growing_buffer* buffer = NULL;
475                 if(!relay) {
476                         buffer = buffer_init(128);
477                         buffer_add(buffer, "[");
478                         for(i = 3; words[i] != NULL; i++ ) {
479                                 /* removes trailing semicolon if user accidentally enters it */
480                                 if( words[i][strlen(words[i])-1] == ';' )
481                                         words[i][strlen(words[i])-1] = '\0';
482                                 buffer_add( buffer, words[i] );
483                                 buffer_add(buffer, " ");
484                         }
485                         buffer_add(buffer, "]");
486                 }
487
488                 return send_request( server, method, buffer, relay );
489         } 
490
491         return 0;
492 }
493
494 int send_request( char* server, 
495                 char* method, growing_buffer* buffer, int relay ) {
496         if( server == NULL || method == NULL )
497                 return 0;
498
499         jsonObject* params = NULL;
500         if( !relay ) {
501                 if( buffer != NULL && buffer->n_used > 0 ) 
502                         params = json_parse_string(buffer->buf);
503         } else {
504                 if(!last_result || ! last_result->_result_content) { 
505                         printf("We're not going to call 'relay' with no result params\n");
506                         return 1;
507                 }
508                 else {
509                         jsonObject* o = jsonNewObject(NULL);
510                         jsonObjectPush(o, last_result->_result_content );
511                         params = o;
512                 }
513         }
514
515
516         if(buffer->n_used > 0 && params == NULL) {
517                 fprintf(stderr, "JSON error detected, not executing\n");
518                 return 1;
519         }
520
521         osrf_app_session* session = osrf_app_client_session_init(server);
522
523         if(!osrf_app_session_connect(session)) {
524                 osrfLogWarning( OSRF_LOG_MARK,  "Unable to connect to remote service %s\n", server );
525                 return 1;
526         }
527
528         double start = get_timestamp_millis();
529         //int req_id = osrf_app_session_make_request( session, params, method, 1, NULL );
530         int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
531
532
533         osrf_message* omsg = osrf_app_session_request_recv( session, req_id, recv_timeout );
534
535         if(!omsg) 
536                 printf("\nReceived no data from server\n");
537         
538         
539         signal(SIGPIPE, SIG_IGN);
540
541         FILE* less; 
542         if(!is_from_script) less = popen( "less -EX", "w");
543         else less = stdout;
544
545         if( less == NULL ) { less = stdout; }
546
547         growing_buffer* resp_buffer = buffer_init(4096);
548
549         while(omsg) {
550
551                 if(raw_print) {
552
553                         if(omsg->_result_content) {
554         
555                                 osrf_message_free(last_result);
556                                 last_result = omsg;
557         
558                                 char* content;
559         
560                                 if( pretty_print && omsg->_result_content ) {
561                                         char* j = jsonObjectToJSON(omsg->_result_content);
562                                         //content = json_printer(j); 
563                                         content = jsonFormatString(j);
564                                         free(j);
565                                 } else
566                                         content = jsonObjectGetString(omsg->_result_content);
567         
568                                 printf( "\nReceived Data: %s\n", content ); 
569                                 free(content);
570         
571                         } else {
572
573                                 char code[16];
574                                 memset(code, 0, 16);
575                                 sprintf( code, "%d", omsg->status_code );
576                                 buffer_add( resp_buffer, code );
577
578                                 printf( "\nReceived Exception:\nName: %s\nStatus: %s\nStatus: %s\n", 
579                                                 omsg->status_name, omsg->status_text, code );
580
581                                 fflush(stdout);
582                         }
583
584                 } else {
585
586                         if(omsg->_result_content) {
587         
588                                 osrf_message_free(last_result);
589                                 last_result = omsg;
590         
591                                 char* content;
592         
593                                 if( pretty_print && omsg->_result_content ) {
594                                         char* j = jsonObjectToJSON(omsg->_result_content);
595                                         //content = json_printer(j); 
596                                         content = jsonFormatString(j);
597                                         free(j);
598                                 } else
599                                         content = jsonObjectGetString(omsg->_result_content);
600         
601                                 buffer_add( resp_buffer, "\nReceived Data: " ); 
602                                 buffer_add( resp_buffer, content );
603                                 buffer_add( resp_buffer, "\n" );
604                                 free(content);
605         
606                         } else {
607         
608                                 buffer_add( resp_buffer, "\nReceived Exception:\nName: " );
609                                 buffer_add( resp_buffer, omsg->status_name );
610                                 buffer_add( resp_buffer, "\nStatus: " );
611                                 buffer_add( resp_buffer, omsg->status_text );
612                                 buffer_add( resp_buffer, "\nStatus: " );
613                                 char code[16];
614                                 memset(code, 0, 16);
615                                 sprintf( code, "%d", omsg->status_code );
616                                 buffer_add( resp_buffer, code );
617                         }
618                 }
619
620
621                 omsg = osrf_app_session_request_recv( session, req_id, recv_timeout );
622
623         }
624
625         double end = get_timestamp_millis();
626
627         fprintf( less, resp_buffer->buf );
628         buffer_free( resp_buffer );
629         fprintf( less, "\n------------------------------------\n");
630         if( osrf_app_session_request_complete( session, req_id ))
631                 fprintf(less, "Request Completed Successfully\n");
632
633
634         fprintf(less, "Request Time in seconds: %.6f\n", end - start );
635         fprintf(less, "------------------------------------\n");
636
637         pclose(less); 
638
639         osrf_app_session_request_finish( session, req_id );
640         osrf_app_session_disconnect( session );
641         osrf_app_session_destroy( session );
642
643
644         return 1;
645
646
647 }
648
649 /*
650 int handle_time( char* words[] ) {
651
652         if( ! words[1] ) {
653
654                 char buf[36];
655                 memset(buf,0,36);
656                 get_timestamp(buf);
657                 printf( "%s\n", buf );
658                 return 1;
659         }
660
661         if( words[1] ) {
662                 time_t epoch = (time_t)atoi( words[1] );
663                 char* localtime = strdup( ctime( &epoch ) );
664                 printf( "%s => %s", words[1], localtime );
665                 free(localtime);
666                 return 1;
667         }
668
669         return 0;
670
671 }
672 */
673
674                 
675
676 int router_query_servers( char* router_server ) {
677
678         if( ! router_server || strlen(router_server) == 0 ) 
679                 return 0;
680
681         char rbuf[256];
682         memset(rbuf,0,256);
683         sprintf(rbuf,"router@%s/router", router_server );
684                 
685         transport_message* send = 
686                 message_init( "servers", NULL, NULL, rbuf, NULL );
687         message_set_router_info( send, NULL, NULL, NULL, "query", 0 );
688
689         client_send_message( client, send );
690         message_free( send );
691
692         transport_message* recv = client_recv( client, -1 );
693         if( recv == NULL ) {
694                 fprintf(stderr, "NULL message received from router\n");
695                 return 1;
696         }
697         
698         printf( 
699                         "---------------------------------------------------------------------------------\n"
700                         "Received from 'server' query on %s\n"
701                         "---------------------------------------------------------------------------------\n"
702                         "original reg time | latest reg time | last used time | class | server\n"
703                         "---------------------------------------------------------------------------------\n"
704                         "%s"
705                         "---------------------------------------------------------------------------------\n"
706                         , router_server, recv->body );
707
708         message_free( recv );
709         
710         return 1;
711 }
712                 
713 int print_help() {
714
715         printf(
716                         "---------------------------------------------------------------------------------\n"
717                         "Commands:\n"
718                         "---------------------------------------------------------------------------------\n"
719                         "help                   - Display this message\n"
720                         "!<command> [args] - Forks and runs the given command in the shell\n"
721                         "time                   - Prints the current time\n"                                    
722                         "time <timestamp>       - Formats seconds since epoch into readable format\n"   
723                         "set <variable> <value> - set a srfsh variable (e.g. set pretty_print true )\n"
724                         "print <variable>               - Displays the value of a srfsh variable\n"
725                         "---------------------------------------------------------------------------------\n"
726
727                         "router query servers <server1 [, server2, ...]>\n"
728                         "       - Returns stats on connected services\n"
729                         "\n"
730                         "\n"
731                         "request <service> <method> [ <json formatted string of params> ]\n"
732                         "       - Anything passed in will be wrapped in a json array,\n"
733                         "               so add commas if there is more than one param\n"
734                         "\n"
735                         "\n"
736                         "relay <service> <method>\n"
737                         "       - Performs the requested query using the last received result as the param\n"
738                         "\n"
739                         "\n"
740                         "math_bench <num_batches> [0|1|2]\n"
741                         "       - 0 means don't reconnect, 1 means reconnect after each batch of 4, and\n"
742                         "                2 means reconnect after every request\n"
743                         "\n"
744                         "introspect <service>\n"
745                         "       - prints the API for the service\n"
746                         "\n"
747                         "\n"
748                         "---------------------------------------------------------------------------------\n"
749                         " Commands for Open-ILS\n"
750                         "---------------------------------------------------------------------------------\n"
751                         "login <username> <password>\n"
752                         "       -       Logs into the 'server' and displays the session id\n"
753                         "       - To view the session id later, enter: print login\n"
754                         "---------------------------------------------------------------------------------\n"
755                         "\n"
756                         "\n"
757                         "Note: long output is piped through 'less'.  To search in 'less', type: /<search>\n"
758                         "---------------------------------------------------------------------------------\n"
759                         "\n"
760                         );
761
762         return 1;
763 }
764
765
766
767 char* tabs(int count) {
768         growing_buffer* buf = buffer_init(24);
769         int i;
770         for(i=0;i!=count;i++)
771                 buffer_add(buf, "  ");
772
773         char* final = buffer_data( buf );
774         buffer_free( buf );
775         return final;
776 }
777
778 int handle_math( char* words[] ) {
779         if( words[1] )
780                 return do_math( atoi(words[1]), 0 );
781         return 0;
782 }
783
784
785 int do_math( int count, int style ) {
786
787         osrf_app_session* session = osrf_app_client_session_init(  "opensrf.math" );
788
789         jsonObject* params = json_parse_string("[]");
790         jsonObjectPush(params,jsonNewObject("1"));
791         jsonObjectPush(params,jsonNewObject("2"));
792
793         char* methods[] = { "add", "sub", "mult", "div" };
794         char* answers[] = { "3", "-1", "2", "0.500000" };
795
796         float times[ count * 4 ];
797         memset(times,0,count*4);
798
799         int k;
800         for(k=0;k!=100;k++) {
801                 if(!(k%10)) 
802                         fprintf(stderr,"|");
803                 else
804                         fprintf(stderr,".");
805         }
806
807         fprintf(stderr,"\n\n");
808
809         int running = 0;
810         int i;
811         for(i=0; i!= count; i++) {
812
813                 int j;
814                 for(j=0; j != 4; j++) {
815
816                         ++running;
817
818                         double start = get_timestamp_millis();
819                         int req_id = osrf_app_session_make_req( session, params, methods[j], 1, NULL );
820                         osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 5 );
821                         double end = get_timestamp_millis();
822
823                         times[(4*i) + j] = end - start;
824
825                         if(omsg) {
826         
827                                 if(omsg->_result_content) {
828                                         char* jsn = jsonObjectToJSON(omsg->_result_content);
829                                         if(!strcmp(jsn, answers[j]))
830                                                 fprintf(stderr, "+");
831                                         else
832                                                 fprintf(stderr, "\n![%s] - should be %s\n", jsn, answers[j] );
833                                         free(jsn);
834                                 }
835
836
837                                 osrf_message_free(omsg);
838                 
839                         } else { fprintf( stderr, "\nempty message for tt: %d\n", req_id ); }
840
841                         osrf_app_session_request_finish( session, req_id );
842
843                         if(style == 2)
844                                 osrf_app_session_disconnect( session );
845
846                         if(!(running%100))
847                                 fprintf(stderr,"\n");
848                 }
849
850                 if(style==1)
851                         osrf_app_session_disconnect( session );
852         }
853
854         osrf_app_session_destroy( session );
855         jsonObjectFree(params);
856
857         int c;
858         float total = 0;
859         for(c=0; c!= count*4; c++) 
860                 total += times[c];
861
862         float avg = total / (count*4); 
863         fprintf(stderr, "\n      Average round trip time: %f\n", avg );
864
865         return 1;
866 }