TSP: The Transport Sample Protocol



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

simple_client.c

Go to the documentation of this file.
00001 
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 /* All what you need for creating a TSP consumer */
00041 #include "tsp_consumer.h"
00042 #include "tsp_time.h"
00043 
00044 
00045 /* Just for fast exit */
00046 void perror_and_exit(char *txt)
00047 {
00048   perror (txt);
00049   exit (-1);
00050 }
00051 
00052 
00053 /* Everthing must begin somewhere */
00054 int main(int argc, char *argv[])
00055 {
00056   const TSP_consumer_information_t*  information;
00057   TSP_consumer_symbol_requested_list_t symbols;
00058   int i, count_frame, nb_providers, wanted_sym=10, t = -1;
00059   TSP_provider_t* providers;
00060   char* name;
00061 
00062   printf ("#=========================================================#\n");
00063   printf ("# Launching tutorial client\n");
00064   printf ("#=========================================================#\n");
00065 
00066   /* Initialisation for TSP library. */
00067   if(!TSP_consumer_init(&argc, &argv))
00068       perror_and_exit("TSP init failed");
00069  
00070   if (argc==2)
00071     name = argv[1];
00072   else
00073     {
00074       printf("USAGE %s : server_name \n", argv[0]);
00075       return -1;
00076     }
00077 
00078   /*-------------------------------------------------------------------------------------------------------*/   
00079   /* Connects to all found providers on the given host. */
00080   TSP_consumer_connect_all(name, &providers, &nb_providers);
00081   if(!nb_providers)
00082       perror_and_exit("TSP_consumer_connect_all found no provider on host");
00083   
00084   /* Ask the provider for a new consumer session.*/
00085   if(!TSP_consumer_request_open(providers[0], 0, 0))
00086     perror_and_exit("TSP_request_provider_open failed");
00087 
00088   /* Ask the provider informations about several parameters, including
00089    * the available symbol list that can be asked. */
00090   if(!TSP_consumer_request_information(providers[0]))
00091     perror_and_exit("TSP_request_provider_information failed");
00092 
00093   /* Get the provider information asked by TSP_consumer_request_information */
00094   information = TSP_consumer_get_information(providers[0]);
00095   if (wanted_sym > information->symbols.len)
00096         wanted_sym = information->symbols.len;
00097 
00098   /* Will use only the "wanted_sym" first symbols of provider */
00099   symbols.val = (TSP_consumer_symbol_requested_t*)calloc(wanted_sym, sizeof(TSP_consumer_symbol_requested_t));
00100 
00101   symbols.len = wanted_sym;
00102   for(i = 0 ; i < wanted_sym ; i++)
00103     {
00104       symbols.val[i].name = information->symbols.val[i].name;
00105       symbols.val[i].period = 1;        /* at max frequency */
00106       symbols.val[i].phase = 0;         /* with no offset */
00107       printf ("Asking for symbol = %s\n", symbols.val[i].name);
00108     }
00109 
00110   /*-------------------------------------------------------------------------------------------------------*/ 
00111   /* Ask the provider for sampling this  list of symbols. Should check if all symbols are OK*/
00112   if(!TSP_consumer_request_sample(providers[0], &symbols))
00113     perror_and_exit("TSP_request_provider_sample failed");
00114 
00115   /* Start the sampling sequence. */
00116   if(!TSP_consumer_request_sample_init(providers[0], 0, 0))
00117     perror_and_exit("TSP_request_provider_sample_init failed");
00118   
00119   /* Loop on data read */
00120   for (count_frame = 0; count_frame<100; )
00121     {
00122       int new_sample=FALSE;
00123       TSP_sample_t sample;
00124       
00125       /* Read a sample symbol.*/
00126       if (TSP_consumer_read_sample(providers[0], &sample, &new_sample))
00127         {
00128           if(new_sample)
00129             {
00130               if (t != sample.time)
00131                 {
00132                   count_frame++;
00133                   t = sample.time;
00134                   printf ("======== Frame[%d] ======== Time : %d ========\n", count_frame, t);
00135                 }                 
00136               i = sample.provider_global_index;
00137               printf ("# Sample nb[%d] \t%s \tval=%f\n", i, symbols.val[i].name, sample.user_value);
00138             }
00139           else
00140             {
00141               /* Used to give time to other thread for filling fifo of received samples */
00142               tsp_usleep(100*1000); /* in µS, so about 100msec */
00143             }
00144         }
00145       else
00146         {
00147           perror_and_exit ("Function TSP_consumer_read_sample failed !!\n");
00148         }
00149     }
00150 
00151   free (symbols.val);
00152 
00153   /*-------------------------------------------------------------------------------------------------------*/ 
00154   /* Stop and destroy the sampling sequence*/
00155   if(!TSP_consumer_request_sample_destroy(providers[0]))
00156     perror_and_exit("Function TSP_consumer_request_sample_destroy failed" );     
00157 
00158   /* Close the session.*/
00159   if(!TSP_consumer_request_close(providers[0]))
00160     perror_and_exit("Function TSP_consumer_request_close failed" );                         
00161 
00162   /* Disconnected all found providers.*/
00163   TSP_consumer_disconnect_all(providers);
00164   
00165   /* call this function when you are done with the librairy.*/
00166   TSP_consumer_end();                   
00167 
00168   return 0;
00169 }
Framework Home Page.

Beware !! TSP wave is coming...