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