TSP: The Transport Sample Protocol



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

client_callback_stdout.c

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

Beware !! TSP wave is coming...