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