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