d1cdf01a5dff914147c0ac9f6b0d2ceb77b5716a
[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, "srfsh") ) 
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                         if(!strcmp(variable,"raw_print")) {
335                                 if(!strcmp(val,"true")) {
336                                         raw_print = 1;
337                                         printf("raw_print = true\n");
338                                         return 1;
339                                 } 
340                                 if(!strcmp(val,"false")) {
341                                         raw_print = 0;
342                                         printf("raw_print = false\n");
343                                         return 1;
344                                 } 
345                         }
346
347                 }
348         }
349
350         return 0;
351 }
352
353
354 int handle_print( char* words[]) {
355
356         char* variable;
357         if( (variable=words[1]) ) {
358                 if(!strcmp(variable,"pretty_print")) {
359                         if(pretty_print) {
360                                 printf("pretty_print = true\n");
361                                 return 1;
362                         } else {
363                                 printf("pretty_print = false\n");
364                                 return 1;
365                         }
366                 }
367
368                 if(!strcmp(variable,"login")) {
369                         printf("login session = %s\n", login_session );
370                         return 1;
371                 }
372
373         }
374         return 0;
375 }
376
377 int handle_router( char* words[] ) {
378
379         if(!client)
380                 return 1;
381
382         int i;
383
384         if( words[1] ) { 
385                 if( !strcmp(words[1],"query") ) {
386                         
387                         if( words[2] && !strcmp(words[2],"servers") ) {
388                                 for(i=3; i < COMMAND_BUFSIZE - 3 && words[i]; i++ ) {   
389                                         router_query_servers( words[i] );
390                                 }
391                                 return 1;
392                         }
393                         return 0;
394                 }
395                 return 0;
396         }
397         return 0;
398 }
399
400
401 /* if new shell, spawn a new child and subshell to do the work,
402         otherwise pipe the request to the currently open (piped) shell */
403 int handle_exec(char* words[], int new_shell) {
404
405         if(!words[0]) return 0;
406
407         if( words[0] && words[0][0] == '!') {
408                 int len = strlen(words[0]);
409                 char command[len];
410                 memset(command,0,len);
411         
412                 int i; /* chop out the ! */
413                 for( i=1; i!= len; i++) {
414                         command[i-1] = words[0][i];
415                 }
416         
417                 free(words[0]);
418                 words[0] = strdup(command);
419         }
420
421         if(new_shell) {
422                 signal(SIGCHLD, sig_child_handler);
423
424                 if(fork()) {
425         
426                         waitpid(-1, 0, 0);
427                         if(child_dead) {
428                                 signal(SIGCHLD,sig_child_handler);
429                                 child_dead = 0;
430                         }
431         
432                 } else {
433                         execvp( words[0], words );
434                         exit(0);
435                 }
436
437         } else {
438
439
440                 growing_buffer* b = buffer_init(64);
441                 int i = 0;
442                 while(words[i]) 
443                         buffer_fadd( b, "%s ", words[i++] );
444         
445                 buffer_add( b, "\n");
446         
447                 //int reader;
448                 //int reader = dup2(STDOUT_FILENO, reader);
449                 //int reader = dup(STDOUT_FILENO);
450                 //close(STDOUT_FILENO);
451
452                 fprintf( shell_writer, b->buf );
453                 buffer_free(b);
454         
455                 fflush(shell_writer);
456                 usleep(1000);
457
458                 /*
459                 char c[4096];
460                 bzero(c, 4096);
461                 read( reader, c, 4095 );
462                 fprintf(stderr, "read %s", c);
463                 dup2(reader, STDOUT_FILENO);
464                 */
465
466         }
467
468         
469         return 1;
470 }
471
472
473 int handle_request( char* words[], int relay ) {
474
475         if(!client)
476                 return 1;
477
478         if(words[1]) {
479                 char* server = words[1];
480                 char* method = words[2];
481                 int i;
482                 growing_buffer* buffer = NULL;
483                 if(!relay) {
484                         buffer = buffer_init(128);
485                         buffer_add(buffer, "[");
486                         for(i = 3; words[i] != NULL; i++ ) {
487                                 /* removes trailing semicolon if user accidentally enters it */
488                                 if( words[i][strlen(words[i])-1] == ';' )
489                                         words[i][strlen(words[i])-1] = '\0';
490                                 buffer_add( buffer, words[i] );
491                                 buffer_add(buffer, " ");
492                         }
493                         buffer_add(buffer, "]");
494                 }
495
496                 return send_request( server, method, buffer, relay );
497         } 
498
499         return 0;
500 }
501
502 int send_request( char* server, 
503                 char* method, growing_buffer* buffer, int relay ) {
504         if( server == NULL || method == NULL )
505                 return 0;
506
507         object* params = NULL;
508         if( !relay ) {
509                 if( buffer != NULL && buffer->n_used > 0 ) 
510                         params = json_parse_string(buffer->buf);
511         } else {
512                 if(!last_result || ! last_result->_result_content) { 
513                         printf("We're not going to call 'relay' with no result params\n");
514                         return 1;
515                 }
516                 else {
517                         object* o = new_object(NULL);
518                         o->push(o, last_result->_result_content );
519                         params = o;
520                 }
521         }
522
523
524         if(buffer->n_used > 0 && params == NULL) {
525                 fprintf(stderr, "JSON error detected, not executing\n");
526                 return 1;
527         }
528
529         osrf_app_session* session = osrf_app_client_session_init(server);
530
531         if(!osrf_app_session_connect(session)) {
532                 warning_handler( "Unable to connect to remote service %s\n", server );
533                 return 1;
534         }
535
536         double start = get_timestamp_millis();
537         //int req_id = osrf_app_session_make_request( session, params, method, 1, NULL );
538         int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
539
540
541         osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 60 );
542
543         if(!omsg) 
544                 printf("\nReceived no data from server\n");
545         
546         
547         signal(SIGPIPE, SIG_IGN);
548
549         FILE* less; 
550         if(!is_from_script) less = popen( "less -EX", "w");
551         else less = stdout;
552
553         if( less == NULL ) { less = stdout; }
554
555         growing_buffer* resp_buffer = buffer_init(4096);
556
557         while(omsg) {
558
559                 if(raw_print) {
560
561                         if(omsg->_result_content) {
562         
563                                 osrf_message_free(last_result);
564                                 last_result = omsg;
565         
566                                 char* content;
567         
568                                 if( pretty_print && omsg->_result_content ) {
569                                         char* j = object_to_json(omsg->_result_content);
570                                         content = json_printer(j); 
571                                         free(j);
572                                 } else
573                                         content = object_get_string(omsg->_result_content);
574         
575                                 printf( "\nReceived Data: %s\n", content ); 
576                                 free(content);
577         
578                         } else {
579
580                                 char code[16];
581                                 memset(code, 0, 16);
582                                 sprintf( code, "%d", omsg->status_code );
583                                 buffer_add( resp_buffer, code );
584
585                                 printf( "\nReceived Exception:\nName: %s\nStatus: %s\nStatus: %s\n", 
586                                                 omsg->status_name, omsg->status_text, code );
587
588                                 fflush(stdout);
589                         }
590
591                 } else {
592
593                         if(omsg->_result_content) {
594         
595                                 osrf_message_free(last_result);
596                                 last_result = omsg;
597         
598                                 char* content;
599         
600                                 if( pretty_print && omsg->_result_content ) {
601                                         char* j = object_to_json(omsg->_result_content);
602                                         content = json_printer(j); 
603                                         free(j);
604                                 } else
605                                         content = object_get_string(omsg->_result_content);
606         
607                                 buffer_add( resp_buffer, "\nReceived Data: " ); 
608                                 buffer_add( resp_buffer, content );
609                                 buffer_add( resp_buffer, "\n" );
610                                 free(content);
611         
612                         } else {
613         
614                                 buffer_add( resp_buffer, "\nReceived Exception:\nName: " );
615                                 buffer_add( resp_buffer, omsg->status_name );
616                                 buffer_add( resp_buffer, "\nStatus: " );
617                                 buffer_add( resp_buffer, omsg->status_text );
618                                 buffer_add( resp_buffer, "\nStatus: " );
619                                 char code[16];
620                                 memset(code, 0, 16);
621                                 sprintf( code, "%d", omsg->status_code );
622                                 buffer_add( resp_buffer, code );
623                         }
624                 }
625
626
627                 omsg = osrf_app_session_request_recv( session, req_id, 5 );
628
629         }
630
631         double end = get_timestamp_millis();
632
633         fprintf( less, resp_buffer->buf );
634         buffer_free( resp_buffer );
635         fprintf( less, "\n------------------------------------\n");
636         if( osrf_app_session_request_complete( session, req_id ))
637                 fprintf(less, "Request Completed Successfully\n");
638
639
640         fprintf(less, "Request Time in seconds: %.3f\n", end - start );
641         fprintf(less, "------------------------------------\n");
642
643         pclose(less); 
644
645         osrf_app_session_request_finish( session, req_id );
646         osrf_app_session_disconnect( session );
647         osrf_app_session_destroy( session );
648
649
650         return 1;
651
652
653 }
654
655 int handle_time( char* words[] ) {
656
657         if( ! words[1] ) {
658
659                 char buf[36];
660                 memset(buf,0,36);
661                 get_timestamp(buf);
662                 printf( "%s\n", buf );
663                 return 1;
664         }
665
666         if( words[1] ) {
667                 time_t epoch = (time_t)atoi( words[1] );
668                 char* localtime = strdup( ctime( &epoch ) );
669                 printf( "%s => %s", words[1], localtime );
670                 free(localtime);
671                 return 1;
672         }
673
674         return 0;
675
676 }
677
678                 
679
680 int router_query_servers( char* router_server ) {
681
682         if( ! router_server || strlen(router_server) == 0 ) 
683                 return 0;
684
685         char rbuf[256];
686         memset(rbuf,0,256);
687         sprintf(rbuf,"router@%s/router", router_server );
688                 
689         transport_message* send = 
690                 message_init( "servers", NULL, NULL, rbuf, NULL );
691         message_set_router_info( send, NULL, NULL, NULL, "query", 0 );
692
693         client_send_message( client, send );
694         message_free( send );
695
696         transport_message* recv = client_recv( client, -1 );
697         if( recv == NULL ) {
698                 fprintf(stderr, "NULL message received from router\n");
699                 return 1;
700         }
701         
702         printf( 
703                         "---------------------------------------------------------------------------------\n"
704                         "Received from 'server' query on %s\n"
705                         "---------------------------------------------------------------------------------\n"
706                         "original reg time | latest reg time | last used time | class | server\n"
707                         "---------------------------------------------------------------------------------\n"
708                         "%s"
709                         "---------------------------------------------------------------------------------\n"
710                         , router_server, recv->body );
711
712         message_free( recv );
713         
714         return 1;
715 }
716                 
717 int print_help() {
718
719         printf(
720                         "---------------------------------------------------------------------------------\n"
721                         "Commands:\n"
722                         "---------------------------------------------------------------------------------\n"
723                         "help                   - Display this message\n"
724                         "!<command> [args] - Forks and runs the given command in the shell\n"
725                         "time                   - Prints the current time\n"                                    
726                         "time <timestamp>       - Formats seconds since epoch into readable format\n"   
727                         "set <variable> <value> - set a srfsh variable (e.g. set pretty_print true )\n"
728                         "print <variable>               - Displays the value of a srfsh variable\n"
729                         "---------------------------------------------------------------------------------\n"
730
731                         "router query servers <server1 [, server2, ...]>\n"
732                         "       - Returns stats on connected services\n"
733                         "\n"
734                         "\n"
735                         "request <service> <method> [ <json formatted string of params> ]\n"
736                         "       - Anything passed in will be wrapped in a json array,\n"
737                         "               so add commas if there is more than one param\n"
738                         "\n"
739                         "\n"
740                         "relay <service> <method>\n"
741                         "       - Performs the requested query using the last received result as the param\n"
742                         "\n"
743                         "\n"
744                         "math_bench <num_batches> [0|1|2]\n"
745                         "       - 0 means don't reconnect, 1 means reconnect after each batch of 4, and\n"
746                         "                2 means reconnect after every request\n"
747                         "\n"
748                         "introspect <service>\n"
749                         "       - prints the API for the service\n"
750                         "\n"
751                         "\n"
752                         "---------------------------------------------------------------------------------\n"
753                         " Commands for Open-ILS\n"
754                         "---------------------------------------------------------------------------------\n"
755                         "login <username> <password>\n"
756                         "       -       Logs into the 'server' and displays the session id\n"
757                         "       - To view the session id later, enter: print login\n"
758                         "---------------------------------------------------------------------------------\n"
759                         "\n"
760                         "\n"
761                         "Note: long output is piped through 'less'.  To search in 'less', type: /<search>\n"
762                         "---------------------------------------------------------------------------------\n"
763                         "\n"
764                         );
765
766         return 1;
767 }
768
769
770
771 char* tabs(int count) {
772         growing_buffer* buf = buffer_init(24);
773         int i;
774         for(i=0;i!=count;i++)
775                 buffer_add(buf, "   ");
776
777         char* final = buffer_data( buf );
778         buffer_free( buf );
779         return final;
780 }
781
782 char* json_printer( char* string ) {
783
784         growing_buffer* buf = buffer_init(64);
785         int i;
786         int tab_var = 0;
787         for(i=0; i!= strlen(string); i++) {
788
789                 if( string[i] == '{' ) {
790
791                         buffer_add(buf, "\n");
792                         char* tab = tabs(tab_var);
793                         buffer_add(buf, tab);
794                         free(tab);
795                         buffer_add( buf, "{");
796                         tab_var++;
797                         buffer_add( buf, "\n" );        
798                         tab = tabs(tab_var);
799                         buffer_add( buf, tab ); 
800                         free(tab);
801
802                 } else if( string[i] == '[' ) {
803
804                         buffer_add(buf, "\n");
805                         char* tab = tabs(tab_var);
806                         buffer_add(buf, tab);
807                         free(tab);
808                         buffer_add( buf, "[");
809                         tab_var++;
810                         buffer_add( buf, "\n" );        
811                         tab = tabs(tab_var);
812                         buffer_add( buf, tab ); 
813                         free(tab);
814
815                 } else if( string[i] == '}' ) {
816
817                         tab_var--;
818                         buffer_add(buf, "\n");
819                         char* tab = tabs(tab_var);
820                         buffer_add(buf, tab);
821                         free(tab);
822                         buffer_add( buf, "}");
823                         buffer_add( buf, "\n" );        
824                         tab = tabs(tab_var);
825                         buffer_add( buf, tab ); 
826                         free(tab);
827
828                 } else if( string[i] == ']' ) {
829
830                         tab_var--;
831                         buffer_add(buf, "\n");
832                         char* tab = tabs(tab_var);
833                         buffer_add(buf, tab);
834                         free(tab);
835                         buffer_add( buf, "]");
836                         buffer_add( buf, "\n" );        
837                         tab = tabs(tab_var);
838                         buffer_add( buf, tab ); 
839                         free(tab);
840
841                 } else if( string[i] == ',' ) {
842
843                         buffer_add( buf, ",");
844                         buffer_add( buf, "\n" );        
845                         char* tab = tabs(tab_var);
846                         buffer_add(buf, tab);
847                         free(tab);
848
849                 } else {
850
851                         char b[2];
852                         b[0] = string[i];
853                         b[1] = '\0';
854                         buffer_add( buf, b ); 
855                 }
856
857         }
858
859         char* result = buffer_data(buf);
860         buffer_free(buf);
861         return result;
862
863 }
864
865 int handle_math( char* words[] ) {
866         if( words[1] && words[2] ) 
867                 return do_math( atoi(words[1]), atoi(words[2]) );
868         return 0;
869 }
870
871
872 int do_math( int count, int style ) {
873
874         osrf_app_session* session = osrf_app_client_session_init(  "opensrf.math" );
875
876         object* params = json_parse_string("[]");
877         params->push(params,new_object("1"));
878         params->push(params,new_object("2"));
879
880         char* methods[] = { "add", "sub", "mult", "div" };
881         char* answers[] = { "3", "-1", "2", "0.500000" };
882
883         float times[ count * 4 ];
884         memset(times,0,count*4);
885
886         int k;
887         for(k=0;k!=100;k++) {
888                 if(!(k%10)) 
889                         fprintf(stderr,"|");
890                 else
891                         fprintf(stderr,".");
892         }
893
894         fprintf(stderr,"\n\n");
895
896         int running = 0;
897         int i;
898         for(i=0; i!= count; i++) {
899
900                 int j;
901                 for(j=0; j != 4; j++) {
902
903                         ++running;
904                         struct timeb t1;
905                         struct timeb t2;
906
907                         ftime(&t1);
908                         //int req_id = osrf_app_session_make_request( session, params, methods[j], 1, NULL );
909                         int req_id = osrf_app_session_make_req( session, params, methods[j], 1, NULL );
910
911                         osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 5 );
912
913
914                         ftime(&t2);
915
916                         double start    = ( (int)t1.time        + ( ((float)t1.millitm) / 1000 ) );
917                         double end              = ( (int)t2.time        + ( ((float)t2.millitm) / 1000 ) );
918
919                         times[(4*i) + j] = end - start;
920
921                         if(omsg) {
922         
923                                 if(omsg->_result_content) {
924                                         char* jsn = object_to_json(omsg->_result_content);
925                                         if(!strcmp(jsn, answers[j]))
926                                                 fprintf(stderr, "+");
927                                         else
928                                                 fprintf(stderr, "\n![%s] - should be %s\n", jsn, answers[j] );
929                                         free(jsn);
930                                 }
931
932
933                                 osrf_message_free(omsg);
934                 
935                         } else { fprintf( stderr, "\nempty message for tt: %d\n", req_id ); }
936
937                         osrf_app_session_request_finish( session, req_id );
938
939                         if(style == 2)
940                                 osrf_app_session_disconnect( session );
941
942                         if(!(running%100))
943                                 fprintf(stderr,"\n");
944                 }
945
946                 if(style==1)
947                         osrf_app_session_disconnect( session );
948         }
949
950         osrf_app_session_destroy( session );
951         free_object(params);
952
953         int c;
954         float total = 0;
955         for(c=0; c!= count*4; c++) 
956                 total += times[c];
957
958         float avg = total / (count*4); 
959         fprintf(stderr, "\n      Average round trip time: %f\n", avg );
960
961         return 1;
962 }