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