TSP: The Transport Sample Protocol



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

client_stdout.c

Go to the documentation of this file.
00001 
00038 #include "tsp_sys_headers.h"
00039 #include "tsp_prjcfg.h"
00040 #include "tsp_consumer.h"
00041 
00042 /*#include "fortify.h"*/
00043 
00044 
00045 /* Number of samples  that will be counted before the data check test pass */
00046 #define TSP_TEST_COUNT_SAMPLES 100  
00047 #define TSP_NANOSLEEP_PERIOD_US (100*1000) /* 10Hz */
00048 
00049 int main(int argc, char *argv[]){
00050 
00051   const TSP_consumer_information_t*  information;
00052   TSP_consumer_symbol_requested_list_t symbols;
00053 
00054   int i, j;
00055   int period=0;
00056   char* name;
00057   int count_samples = 0;
00058   int nb_samples = -1;
00059   char symbol_buf[50];
00060   int test_ok = TRUE;
00061   int test_mode = 0;
00062   int all_data_ok = TRUE;
00063   TSP_provider_t provider;
00064 
00065 
00066   /*Fortify_EnterScope();*/
00067   printf ("#=========================================================#\n");
00068   printf ("# Launching <stdout_client> for printing symbols received #\n");
00069   printf ("#=========================================================#\n");
00070  
00071   if(!TSP_consumer_init(&argc, &argv))
00072     {
00073       STRACE_ERROR(("TSP init failed"));
00074       return -1;
00075     }
00076 
00077   switch (argc-1) { /* No Breaks please */
00078   case 5: 
00079     nb_samples = atoi(argv[5]);  /* nb-sample */
00080   case 4:
00081     test_mode = atoi(argv[4]);  /* 0 = no, 1=infinite loop */
00082   case 3:       /* Anything after name and perdio --> test mode */
00083   case 2:
00084     period = atoi (argv[2]);
00085   case 1:      
00086     name = argv[1];
00087     break;
00088   default : 
00089       STRACE_ERROR(("USAGE %s : serverURL period <test mode nb_sample> \n", argv[0]));
00090       printf(TSP_URL_FORMAT_USAGE);
00091       return -1;
00092   }
00093 
00094  once_again:
00095   
00096   /*-------------------------------------------------------------------------------------------------------*/ 
00097   /* TEST : STAGE 001 | STEP 001 */
00098   /*-------------------------------------------------------------------------------------------------------*/ 
00099   provider = TSP_consumer_connect_url(name);
00100   if(provider)
00101     {
00102       const char* info = TSP_consumer_get_connected_name(provider) ;
00103       STRACE_INFO(("Server %s, info = '%s'", name, info));
00104           
00105       /* Check name */
00106       if(!strstr(info, "StubbedServer"))
00107         {
00108           STRACE_ERROR(("Serveur name corrupted"));
00109           STRACE_TEST(("STAGE 001 | STEP 001 : FAILED"));
00110           return -1;
00111         }
00112           
00113     }
00114   else
00115     {
00116       STRACE_ERROR(("Unable to find any provider for host"));
00117       return -1;
00118     }
00119 
00120   STRACE_TEST(("STAGE 001 | STEP 001 : PASSED"));
00121 
00122 
00123   /*-------------------------------------------------------------------------------------------------------*/ 
00124   /* TEST : STAGE 001 | STEP 002 */
00125   /*-------------------------------------------------------------------------------------------------------*/ 
00126   /* Le 1er provider existe puisqu'il y en a au moins 1 */
00127 
00128   if(!TSP_consumer_request_open(provider, 0, 0))
00129     {
00130       STRACE_ERROR(("TSP_request_provider_open failed"));
00131       STRACE_TEST(("STAGE 001 | STEP 002 : FAILED"));
00132       return -1;
00133     }
00134   
00135   STRACE_TEST(("STAGE 001 | STEP 002 : PASSED"));
00136 
00137   /*-------------------------------------------------------------------------------------------------------*/ 
00138   /* TEST : STAGE 001 | STEP 003 */
00139   /*-------------------------------------------------------------------------------------------------------*/ 
00140   if(!TSP_consumer_request_information(provider))
00141     {
00142       STRACE_ERROR(("TSP_request_provider_information failed"));
00143       STRACE_TEST(("STAGE 001 | STEP 003 : FAILED"));
00144       return -1;
00145     }
00146   
00147   /* be wild, ask twice to try to trigger mem leak  */
00148   if(!TSP_consumer_request_information(provider))
00149     {
00150       STRACE_ERROR(("TSP_request_provider_information failed"));
00151       STRACE_TEST(("STAGE 001 | STEP 003 : FAILED"));
00152       return -1;
00153     }
00154 
00155 
00156   information = TSP_consumer_get_information(provider);
00157 
00158   /* Check total symbol number */
00159   if(information->symbols.len < 1 || information->symbols.len > 999999)
00160     {
00161       STRACE_ERROR(("The total number of symbols should be fair, and not %d",information->symbols.len));
00162       STRACE_TEST(("STAGE 001 | STEP 003 : FAILED"));
00163       return -1;      
00164     }
00165 
00166   /* Compare symbols names */
00167   for( i = 1 ; i<  information->symbols.len ; i++)
00168     {
00169       sprintf(symbol_buf, "Symbol%d",i);
00170       if(strcmp(symbol_buf,  information->symbols.val[i].name))
00171         {
00172 printf("%s != %s\n", symbol_buf,  information->symbols.val[i].name);
00173           STRACE_ERROR(("Symbol name corrupted"));
00174           STRACE_TEST(("STAGE 001 | STEP 003 : FAILED"));
00175           return -1;
00176         }
00177       
00178     }
00179       
00180   STRACE_TEST(("STAGE 001 | STEP 003 : PASSED"));
00181 
00182   if (nb_samples>0)
00183     symbols.len = nb_samples;
00184   else
00185     symbols.len = information->symbols.len;
00186   symbols.val = (TSP_consumer_symbol_requested_t*)calloc(symbols.len, sizeof(TSP_consumer_symbol_requested_t));
00187   TSP_CHECK_ALLOC(symbols.val, -1);
00188 
00189   /* Change period of sampling for each client */
00190   for(i = 0 ; i < symbols.len ; i++)
00191     {
00192       symbols.val[i].name = information->symbols.val[i].name;
00193       symbols.val[i].period = period;
00194       symbols.val[i].phase = 0;
00195     }
00196 
00197   STRACE_INFO(("Asking for %d symboles", symbols.len));
00198   /*-------------------------------------------------------------------------------------------------------*/ 
00199   /* TEST : STAGE 001 | STEP 004 */
00200   /*-------------------------------------------------------------------------------------------------------*/ 
00201   if(!TSP_consumer_request_sample(provider, &symbols))
00202     {
00203       STRACE_ERROR(("TSP_request_provider_sample failed"));
00204       STRACE_TEST(("STAGE 001 | STEP 004 : FAILED"));
00205       return -1;
00206     }
00207   /* be wild, ask twice to try to trigger mem leak  */
00208   if(!TSP_consumer_request_sample(provider, &symbols))
00209     {
00210       STRACE_ERROR(("TSP_request_provider_sample failed"));
00211       STRACE_TEST(("STAGE 001 | STEP 004 : FAILED"));
00212       return -1;
00213     }
00214 
00215   free(symbols.val);
00216 
00217   STRACE_TEST(("STAGE 001 | STEP 004 : PASSED"));
00218   /*-------------------------------------------------------------------------------------------------------*/ 
00219   /* TEST : STAGE 001 | STEP 005 */
00220   /*-------------------------------------------------------------------------------------------------------*/ 
00221   if(!TSP_consumer_request_sample_init(provider, 0, 0))
00222     {
00223       STRACE_ERROR(("TSP_request_provider_sample_init failed"));
00224       STRACE_TEST(("STAGE 001 | STEP 005 : FAILED"));
00225       return -1;
00226     }
00227   STRACE_TEST(("STAGE 001 | STEP 005 : PASSED"));
00228 
00229   /*-------------------------------------------------------------------------------------------------------*/ 
00230   /* TEST : STAGE 001 | STEP 006 */
00231   /*-------------------------------------------------------------------------------------------------------*/ 
00232   /* Loop on data read */
00233   count_samples = 0;
00234   while(1)
00235     {
00236       int new_sample;
00237       TSP_sample_t sample;
00238       int i;int j;
00239       int t = -1000;
00240       
00241       new_sample = FALSE;
00242       do{
00243         if(TSP_consumer_read_sample(provider,&sample, &new_sample))
00244           {
00245             if(new_sample)
00246               {
00247                     
00248                 double calc;
00249                     
00250                 i = sample.provider_global_index;
00251                 if(t == -1000) 
00252                   {
00253                     t = sample.time - 1; 
00254                   }
00255 
00256                 if(i == 1 )
00257                   {
00258                     count_samples++;
00259                     printf ("TSP : Sample nb[%d] time=%d val=%f\n", count_samples, sample.time, sample.user_value);
00260                   }
00261 
00262                 calc = calc_func(i,sample.time);
00263                 
00264                 /* i = 0 is t */
00265                 if(i != 0)
00266                   {
00267                     if( (ABS(sample.user_value - calc) > 1e-7) && (t == (sample.time - 1)) )
00268                       {
00269                         STRACE_ERROR(("!!!!ERROR : T=%u, I=%d, V1=%f, V2=%f", sample.time,i,sample.user_value,calc ));                  
00270                         all_data_ok = FALSE;
00271                       }
00272                   }
00273 
00274                 t = sample.time;
00275 
00276                 /* Test */
00277                 if(count_samples >= TSP_TEST_COUNT_SAMPLES && test_mode!=1)
00278                   {
00279                     if(all_data_ok)
00280                       {
00281 
00282                         STRACE_TEST(("STAGE 001 | STEP 006 : PASSED" ));                        
00283                         
00284                         /* --------------- */
00285                         /* Close providers */
00286                         /* --------------- */
00287                         if(!TSP_consumer_request_sample_destroy(provider))
00288                           {
00289                             STRACE_ERROR(("Function TSP_consumer_request_sample_destroy failed" ));      
00290                           }
00291                         
00292                         if(!TSP_consumer_request_close(provider))
00293                           {
00294                             STRACE_ERROR(("Function TSP_consumer_request_close failed" ));                          
00295                           }
00296                         
00297                         TSP_consumer_disconnect_one(provider);
00298                         
00299 
00300                         /* Fortify calls */
00301                         /*Fortify_LeaveScope();
00302                           Fortify_OutputStatistics();*/
00303                         
00304                         /* goto once_again; */
00305                         TSP_consumer_end();
00306 
00307                         printf ("#=========================================================#\n");
00308                         printf ("# End of Test OK \n");
00309                         printf ("#=========================================================#\n");
00310                         return 1;
00311                       }
00312                     else
00313                       {
00314                         printf ("#=========================================================#\n");
00315                         printf ("# End of Test KO \n");
00316                         printf ("#=========================================================#\n");
00317                         return -1;
00318                       }
00319                   }
00320               }
00321           }
00322         else
00323           {
00324             STRACE_ERROR(("TSP_read_sample failed"));
00325           }
00326       } while(new_sample);
00327 
00328       /* Used to give time to other thread for filling fifo of received samples */
00329       tsp_usleep(TSP_NANOSLEEP_PERIOD_US); 
00330 
00331     }
00332 
00333   return 0;
00334 }
Framework Home Page.

Beware !! TSP wave is coming...