TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

tsp_ascii_writer_main.c

Go to the documentation of this file.
00001 
00037 #include <stdio.h>
00038 #include <unistd.h>
00039 #include <signal.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 
00043 #include "tsp_ascii_writer.h"
00044 #include "tsp_consumer.h"
00045 
00046 static void 
00047 my_sighandler(int signum) {
00048   fprintf(stderr,"tsp_ascii_writer::Captured signal<%d>\n",signum);
00049   fflush(stderr);
00050   tsp_ascii_writer_stop();    
00051 }
00052                                                                                                  
00053 int
00054 main (int argc, char* argv[]) {
00055   
00056   struct sigaction my_action;
00057   struct sigaction old_action;
00058   int32_t                               retcode=0;
00059   int32_t                               nb_symbols;
00060   TSP_consumer_symbol_requested_list_t  symbol_list;
00061   TSP_consumer_symbol_requested_t*      mysymbols;
00062   char*   input_filename  = NULL;
00063   char*   output_filename = NULL;
00064   FILE*   output_stream   = NULL;
00065   int32_t output_limit    = 0;
00066   char*   provider_url   = "rpc://localhost/";
00067 
00068   /* Main options handling */
00069   char*         errorString;
00070   int           opt_ok;
00071   char          c_opt;
00072 
00073   opt_ok            = 1;
00074     
00075   if (argc < 2) {
00076     opt_ok  = 0;
00077     retcode = -1;
00078     fprintf(stderr,"%s: Insufficient number of options\n",argv[0]);
00079   }
00080 
00081   /* Analyse command line parameters */
00082   while (opt_ok && (EOF != (c_opt = getopt(argc,argv,"x:u:o:l:h")))) {    
00083     switch (c_opt) {
00084     case 'x':
00085       input_filename = strdup(optarg);
00086       fprintf(stdout,"%s: sample config file is <%s>\n",argv[0],input_filename);
00087       break;
00088     case 'o':
00089       output_filename = strdup(optarg);
00090       fprintf(stdout,"%s: output sample file is <%s>\n",argv[0],output_filename);
00091       break;
00092     case 'l':
00093       errorString = NULL;
00094       output_limit = strtol(optarg,&errorString,10);
00095       if ('\0' != *errorString) {
00096         fprintf(stderr,"%s: incorrect output limit format : <%s> (error begin at <%s>)\n",argv[0],optarg, errorString);
00097         opt_ok = 0;
00098       } else {
00099         fprintf(stderr,"%s: TSP sample output file limited to <%d> sample(s).\n",argv[0],output_limit);
00100       }
00101       break;
00102     case 'u':
00103       provider_url = strdup(optarg);
00104       fprintf(stdout,"%s: TSP provider URL is <%s>\n",argv[0],provider_url);
00105       break;
00106     case '?':
00107       fprintf(stderr,"Invalid command line option(s), correct it and rerun\n");
00108       opt_ok = 0;
00109       break;
00110     default:
00111       opt_ok = 0;
00112       break;
00113     } /* end of switch */    
00114   }
00115 
00116   if (!opt_ok) {
00117     printf("Usage: %s -x=<sample_config_file> [-o=<output_filename>] [-l=<nb sample>] [-u=<TSP provider URL>]\n", argv[0]);
00118     printf("   -x   the file specifying the list of symbols to be sampled\n");
00119     printf("   -o   the name of the output file\n");
00120     printf("   -l   (optional) the maximum number of sample to be stored in file\n");
00121     printf("   -u   (optionel) the  TSP provider URL <PROTOCOL://HOST/SERVER:PORT> \n");  
00122     exit(retcode);
00123   }
00124 
00125   tsp_ascii_writer_initialise(&argc,&argv);
00126     
00127   /* install SIGINT handler (POSIX way) */
00128   my_action.sa_handler = &my_sighandler;  
00129   sigfillset(&my_action.sa_mask);
00130   my_action.sa_flags = SA_RESTART;
00131   sigaction(SIGINT,&my_action,&old_action);    
00132 
00133   fprintf(stdout,"%s: Load config file...\n",argv[0]);
00134   retcode = tsp_ascii_writer_load_config(input_filename,&mysymbols,&nb_symbols);
00135 
00136   if (0!=retcode) {
00137     fprintf(stderr,"<%s>: Invalid configuration file (%d parse error(s)).",
00138             input_filename, tsp_ascii_writer_parse_error);
00139     tsp_ascii_writer_stop();
00140   }
00141 
00142   if (0==retcode) {
00143     fprintf(stdout,"%s: Validate symbols against provider info...\n",argv[0]);
00144     fflush(stdout);
00145     retcode = tsp_ascii_writer_validate_symbols(mysymbols,nb_symbols,
00146                                                 provider_url,&symbol_list);
00147   }
00148 
00149   if (0==retcode) {
00150     fprintf(stdout,"%s: Ascii writer running...\n",argv[0]);
00151     fflush(stdout);  
00152     if (NULL == output_filename) {
00153       output_stream = stdout;
00154       output_limit  = 0;
00155     } else {
00156       output_stream = fopen(output_filename,"w");
00157       if ((FILE*)NULL == output_stream) {
00158         fprintf(stderr,"Cannot open output file <%s> for writing\n",output_filename);
00159         retcode = -1;
00160       }    
00161     }
00162   }
00163   
00164   if (0 == retcode) {
00165     retcode = tsp_ascii_writer_start(output_stream,output_limit);
00166   }
00167 
00168   fprintf(stdout,"%s: Ascii writer stopped...\n",argv[0]);
00169   fflush(stdout);  
00170   tsp_ascii_writer_finalise();
00171 
00172   return (retcode);
00173 }
Framework Home Page.

Beware !! TSP wave is coming...