TSP: The Transport Sample Protocol



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

client_res.c

Go to the documentation of this file.
00001 
00038 #include "tsp_sys_headers.h"
00039 #include <signal.h>
00040 #include <unistd.h>
00041 #include "tsp_prjcfg.h" 
00042 #include "tsp_time.h" 
00043 #include "tsp_consumer.h"
00044 #include "libUTIL.h"
00045 
00046 
00047 /*µS*/
00048 #define TSP_NANOSLEEP_PERIOD_US (200*1000)
00049 
00050 
00051 /* libUTIL */
00052 extern int _use_dbl;
00053 
00054 typedef void Sigfunc(int);
00055 
00056 static int stop = FALSE;
00057 static int stop_end = FALSE;
00058 
00059 static Sigfunc* _signal(int signo, Sigfunc* func)
00060 {
00061   struct sigaction act, oact;
00062   
00063   act.sa_handler = func;
00064   sigemptyset(&act.sa_mask);
00065   act.sa_flags = 0;
00066   if(signo == SIGALRM )
00067     {
00068 #ifdef SA_INTERRUPT
00069       act.sa_flags |= SA_INTERRUPT; /*SunOS 4;x */
00070 #endif
00071     }
00072   else
00073     {
00074 #ifdef SA_RESTART
00075       act.sa_flags |= SA_RESTART; /*SVR4, 4.4BSD*/
00076 #endif
00077     }
00078   if(sigaction(signo, &act, &oact) < 0)
00079     return(SIG_ERR);
00080   return (oact.sa_handler);
00081 } 
00082 
00083 void catch_ctrl_c(int i)
00084 {
00085   stop = TRUE;
00086 
00087   STRACE_TEST(("Waiting eol and saving file..."));
00088 }
00089 
00090 void usage (char *txt)
00091 {
00092   STRACE_ERROR(("USAGE : %s -f: [-u:] [-tmdh]", txt));
00093   printf("\t -f filename   : output RES format filename\n");
00094   printf("\t[-u serverURL] : TSP Universal Resource Locator\n\n");
00095   printf(TSP_URL_FORMAT_USAGE);
00096   printf("\n\t[-t period]    : expressed in provider's cycles (1-N), default 1\n");
00097   printf("\t[-m mode]      : recording mode (1-3), default 1\n");
00098   printf("\t                 1 = All variables, retry connection forever\n");
00099   printf("\t                 2 = 3 variables (first, middle, last)\n");
00100   printf("\t                 3 = 10 first variables\n");
00101   printf("\t[-d]           : use IEEE-754 double format for RES file\n");
00102   printf("\t[-h]           : this help\n");
00103   printf("\t[--tsp-stream-init-start file[.res] --tsp-stream-init-stop]\n");
00104   printf("\t               : stream sent to TSP provider\n");
00105   
00106   printf("Note : CTRL+C cleanly save RES file and quit\n");
00107   exit(-1);
00108 }
00109 
00110 int main(int argc, char *argv[]){
00111 
00112   const TSP_consumer_information_t*  information;
00113   TSP_consumer_symbol_requested_list_t symbols[TSP_MAX_SERVER_NUMBER];
00114 
00115   int i, count=0;
00116   int nb_providers = 0;
00117   void* res_values;
00118   int new_sample;
00119   int provider;
00120   int base_frequency = 1e6;
00121   TSP_sample_t sample;
00122   int res_value_i, res_values_nb;
00123   TSP_provider_t providers[TSP_MAX_SERVER_NUMBER];
00124   int buffersBeforeStop = 0;
00125 
00126   char myopt; /* Options */
00127   char* out_file_res = NULL;
00128   int period=1;
00129   int test_mode = 1;
00130 
00131   extern char* optarg;
00132 
00133   _use_dbl = 0;
00134 
00135 
00136   /* catch ctrl-c */
00137   _signal(SIGINT, catch_ctrl_c);
00138 
00139 
00140   STRACE_INFO(("Autodetect CPU : %d bits", sizeof(long)*8));
00141 
00142   /* TSP Init */
00143   if(!TSP_consumer_init(&argc, &argv))
00144     {
00145       STRACE_ERROR(("TSP init failed"));
00146       return -1;
00147     }
00148     
00149   
00150   while ((myopt = getopt(argc, argv, "u:f:t:m:dh")) != -1)
00151     {
00152       switch(myopt)
00153         {
00154         case 'u':
00155           /*-------------------------------------*/ 
00156           /* Connection to providers, URL based */
00157           /*-------------------------------------*/ 
00158           if(nb_providers < TSP_MAX_SERVER_NUMBER &&
00159              (providers[nb_providers] = TSP_consumer_connect_url(optarg)))
00160             nb_providers++;
00161           else
00162             {
00163               STRACE_ERROR(("Cannot connect to %s", optarg));
00164             }
00165           break;
00166         case 'f':   out_file_res = optarg;      break;
00167         case 't':   period = atoi(optarg);      break;
00168         case 'm':   test_mode = atoi(optarg);   break;
00169         case 'd':   _use_dbl = 1;               break;
00170         case 'h':   /* no break please, do as default */
00171         default :   usage(argv[0]);             break;
00172         }
00173     }
00174 
00175   if(!out_file_res)
00176     usage(argv[0]);
00177 
00178   if(nb_providers == 0)
00179     {
00180       providers[nb_providers++] = TSP_consumer_connect_url(NULL);
00181       if(!providers[0])
00182         {
00183           STRACE_ERROR(("Cannot connect to a TSP provider on local host"));
00184           usage(argv[0]);
00185          }
00186     }
00187 
00188   for(provider=0; provider<nb_providers; provider++)
00189     {
00190       /*-------------------------*/ 
00191       /* Open requested provider */
00192       /*-------------------------*/ 
00193       if(!TSP_consumer_request_open(providers[provider], 0, 0 ))
00194         {
00195           STRACE_ERROR(("TSP_request_provider_open failed"));
00196           return -1;
00197         }
00198  
00199 
00200       /*-----------------------------*/ 
00201       /* TEST : STAGE 002 | STEP 003 */
00202       /*-----------------------------*/
00203       do
00204         {
00205           if(!TSP_consumer_request_information(providers[provider]))
00206             {
00207               STRACE_ERROR(("TSP_request_provider_information failed"));
00208               return -1;
00209             }
00210           
00211           information = TSP_consumer_get_information(providers[provider]);
00212           symbols[provider].len = information->symbols.len;
00213           if(!symbols[provider].len)
00214             tsp_usleep(TSP_NANOSLEEP_PERIOD_US);
00215         }
00216       while(!symbols[provider].len);
00217       
00218       symbols[provider].val = (TSP_consumer_symbol_requested_t*)calloc(symbols[provider].len, sizeof(TSP_consumer_symbol_requested_t));
00219       TSP_CHECK_ALLOC(symbols[provider].val, -1);
00220       
00221       for( i = 0 ; i< symbols[provider].len ; i++)
00222         {
00223           STRACE_INFO(("Id=%d Sym='%s'",i, information->symbols.val[i].name));
00224           symbols[provider].val[i].name = information->symbols.val[i].name;
00225           symbols[provider].val[i].period = period;
00226           symbols[provider].val[i].phase = 0;
00227         }
00228       
00229       /* chose smallest frequency */
00230       if(information->base_frequency < base_frequency)
00231         base_frequency = information->base_frequency;
00232 
00233       
00234       /* take only the first, midle and last variable in 'first_last' mode*/
00235       switch(test_mode)
00236         {
00237         case 1 : 
00238           /* all variables */
00239           break;
00240         case 2 :
00241           symbols[provider].val[1] = symbols[provider].val[ symbols[provider].len/2];
00242           symbols[provider].val[2] = symbols[provider].val[ symbols[provider].len-1];
00243           symbols[provider].len = 3;
00244           break;
00245         case 3 :     
00246           symbols[provider].len = 10;
00247           break;
00248         default:
00249           STRACE_ERROR(("Unknown test number"));
00250           return -1;
00251         }
00252 
00253      
00254       /*-----------------------------------------------*/ 
00255       /* Adjust period according to smallest frequency */
00256       /*-----------------------------------------------*/ 
00257       /* TODO */
00258        
00259   
00260       /*---------------------*/ 
00261       /* Ask for sample list */
00262       /*---------------------*/ 
00263       if(!TSP_consumer_request_sample(providers[provider],&symbols[provider]))
00264         {
00265           STRACE_ERROR(("TSP_request_provider_sample failed"));
00266           return -1;
00267         }
00268     }
00269 
00270   /* in case of stop request, flush N buffers (if any) before stopping */
00271   buffersBeforeStop = base_frequency * 1 /* seconds */;
00272 
00273 
00274   for(provider=0; provider<nb_providers; provider++)
00275     {
00276       /*----------------*/ 
00277       /* Start sampling */
00278       /*----------------*/ 
00279       if(!TSP_consumer_request_sample_init(providers[provider],0,0))
00280         {
00281           STRACE_ERROR(("TSP_request_provider_sample_init failed"));
00282           return -1;
00283         }
00284     }
00285 
00286   
00287   /*-------------------*/ 
00288   /* Loop on data read */
00289   /*-------------------*/ 
00290   
00291   STRACE_INFO(("file=%s", out_file_res));
00292   d_wopen(out_file_res);
00293   d_wcom(""); /* No comment */
00294 
00295   res_values_nb = 0;
00296   for(provider=0; provider<nb_providers; provider++)
00297     {
00298       for (i = 0; i < symbols[provider].len; i++)
00299         {
00300           d_wnam(symbols[provider].val[i].name, "?"); /* write header with no unit */
00301         }
00302       res_values_nb += symbols[provider].len;
00303     }
00304   
00305   res_values = _use_dbl ? 
00306     calloc(( res_values_nb+1),sizeof(double)) :
00307       calloc(( res_values_nb+1),sizeof(float)) ;
00308   assert(res_values);
00309   
00310   res_value_i = 0;
00311   while(!stop_end)
00312     {
00313       for(provider=0; provider<nb_providers; provider++)
00314         {
00315           if(!TSP_consumer_read_sample(providers[provider], &sample, &new_sample))
00316             {
00317               stop_end = TRUE;
00318               break;
00319             }
00320           if(new_sample)
00321             {
00322               if(_use_dbl)
00323                 {
00324                   double* d_res_values = (double*)res_values;
00325                   d_res_values[res_value_i++] = sample.user_value;
00326                 }
00327               else
00328                 {
00329                   float* f_res_values = (float*)res_values;
00330                   f_res_values[res_value_i++] = sample.user_value;            
00331                 }
00332 
00333               /* Received complete buffer, need to write */
00334               if( res_value_i == res_values_nb )
00335                 {
00336                   count++;
00337                   d_writ(res_values);
00338                   res_value_i = 0;
00339 
00340                   /* wait a little before stopping to flush buffers */
00341                   if(stop) stop++;
00342                   if(stop > buffersBeforeStop) stop_end = TRUE;
00343                 }
00344             }
00345           else
00346             {
00347               tsp_usleep(TSP_NANOSLEEP_PERIOD_US);
00348               /* no more buffers, stop immediately */
00349               if(stop) stop_end = TRUE;
00350             }
00351         }
00352     }
00353 
00354   d_clos();
00355 
00356   TSP_consumer_end();
00357 
00358   return 0;
00359 }
Framework Home Page.

Beware !! TSP wave is coming...