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