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