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