Wiselib
wiselib.testing/radio/reliable/testreliable.h
Go to the documentation of this file.
00001 /*
00002  * File:   testreliable.h
00003  * Author: amaxilatis
00004  *
00005  * Created on July 27, 2010
00006  */
00007 
00008 #ifndef _TESTRELIABLE_H
00009 #define  _TESTRELIABLE_H
00010 
00011 #include "util/delegates/delegate.hpp"
00012 //#include <iostream>
00013 
00014 #define COUNTER_MAX 5000
00015 
00016 namespace wiselib {
00017 
00018     template<typename OsModel_P,
00019     typename Radio_P,
00020     typename Timer_P,
00021     typename Debug_P>
00022     class testreliable {
00023     public:
00024 
00025         typedef int cluster_id_t;
00026         typedef int cluster_level_t; //quite useless within current scheme, supported for compatibility issues
00027         typedef OsModel_P OsModel;
00028         typedef Radio_P Radio;
00029         //typedef typename OsModel::Os Os;
00030         typedef Debug_P Debug;
00031         typedef Timer_P Timer;
00032         
00033         typedef typename Timer::millis_t millis_t;
00034 
00035         typedef testreliable<OsModel, Radio, Timer, Debug> self_type;
00036 
00037         typedef typename Radio::node_id_t node_id_t;
00038         typedef typename Radio::size_t size_t;
00039         typedef typename Radio::block_data_t block_data_t;
00040 
00041         typedef delegate1<void, int> cluster_delegate_t;
00042 
00043         //quite useless within current scheme, supported for compatibility issues
00044 
00045         enum EventIds {
00046             CLUSTER_HEAD_CHANGED = 0,
00047             NODE_JOINED = 1,
00048             NODE_LEFT = 2
00049         };
00050 
00051         //quite useless within current scheme, supported for compatibility issues
00052 
00053         enum ClusterIds {
00054             UNKNOWN_CLUSTER_HEAD = 0
00055         };
00056 
00057         void enable(void);
00058         void disable(void);
00059 
00060         void set_loss_rate(double theta) {
00061             radio().set_max_retries(30*theta+1);
00062             //Radio->set_max_retries(5);
00063         };
00064 
00065         //same as original get_hdl()
00066 
00067         cluster_id_t cluster_id(cluster_level_t) {
00068             return 0;
00069         }
00070 
00071         //quite useless within current scheme, supported for compatibility issues
00072 
00073         cluster_level_t cluster_level() {
00074             return 0;
00075         }
00076 
00077         template<class T, void (T::*TMethod)(int) >
00078         inline int reg_changed_callback(T *obj_pnt) {
00079             state_changed_callback_ = cluster_delegate_t::from_method<T, TMethod > (obj_pnt);
00080             return 0;
00081         }
00082 
00083         void unreg_changed_callback(int idx) {
00084             state_changed_callback_ = cluster_delegate_t();
00085         }
00086 
00087         void receive(node_id_t receiver, size_t len, block_data_t *data);
00088 
00089 
00090         int get_sum(){return sum_;};
00091         int get_duplicates(){return duplicates_;};
00092 
00093         void timer_elapsed(void * node) {
00094             if (COUNTER < COUNTER_MAX) {
00095                 block_data_t m_sid[2];
00096                 m_sid[0] = COUNTER % 256;
00097                 m_sid[1] = COUNTER / 256;
00098 
00099                 debug().debug( "Application::send message=%d\n", COUNTER);
00100                 radio().send( (node_id_t)node , 2, m_sid);
00101                 COUNTER++;
00102 
00103                 
00104                 
00105                 timer().template set_timer<self_type, &self_type::timer_elapsed > (
00106                         TIME_DELAY, this, node );
00107             }
00108         }
00109 
00112 
00113         testreliable() :
00114         theta_(30),
00115         COUNTER(0)
00116 
00117         {
00118         }
00119 
00120         ~testreliable() {
00121         }
00123 
00124 
00125         void init (Radio& radio , Timer& timer , Debug& debug){
00126             radio_ = &radio;
00127             timer_ = &timer;
00128             debug_ = &debug;
00129         };
00130 
00131     private:
00132         int callback_id_;
00133         cluster_delegate_t state_changed_callback_;
00134         int theta_;
00135         int COUNTER;
00136 
00137         int sum_;
00138         int duplicates_;
00139         
00140         millis_t TIME_DELAY;
00141 
00142         Radio * radio_;
00143         Timer * timer_;
00144         Debug * debug_;
00145 
00146 
00147         Radio& radio() {
00148             return *radio_;
00149         }
00150 
00151         Timer& timer() {
00152             return *timer_;
00153         }
00154 
00155         Debug& debug() {
00156             return *debug_;
00157         }
00158         
00159         int messages[COUNTER_MAX];
00160 
00161 
00162     };
00163 
00164     template<typename OsModel_P,
00165     typename Radio_P,
00166     typename Timer_P,
00167     typename Debug_P>
00168     void
00169     testreliable<OsModel_P, Radio_P, Timer_P, Debug_P>::
00170     enable(void) {
00171       TIME_DELAY = 1000;
00172 
00173         debug().debug( "Booting up");
00174         //radio().enable();
00175         radio().enable_radio();
00176         debug().debug( ".");
00177         // Sent messages counter
00178         sum_=0;
00179         COUNTER = 0;
00180         duplicates_=0;
00181    
00182       for(int i=0;i<COUNTER_MAX;i++){
00183             messages[i]=0;
00184       }
00185 
00186 
00187         radio().template reg_recv_callback<self_type, &self_type::receive> ( this);
00188 
00189         debug().debug( ".");
00190         if (radio().id() == 2) {
00191             
00192             timer().template set_timer<self_type, &self_type::timer_elapsed > (
00193                     TIME_DELAY, this, (void *)8);
00194             
00195             //timer().template set_timer<self_type, &self_type::timer_elapsed> (
00196             //        TIME_DELAY, this, (void*) 9);
00197             
00198             debug().debug( ".");
00199 
00200             debug().debug( ".");
00201         } else {
00202 
00203         }
00204 
00205         debug().debug( "OK!\n");
00206     }
00207 
00208     template<typename OsModel_P,
00209     typename Radio_P,
00210     typename Timer_P,
00211     typename Debug_P>
00212     void
00213     testreliable<OsModel_P, Radio_P, Timer_P, Debug_P>::
00214     disable(void) {
00215         radio().unreg_recv_callback( callback_id_);
00216         
00217     }
00218 
00219     template<typename OsModel_P,
00220     typename Radio_P,
00221     typename Timer_P,
00222     typename Debug_P>
00223     void
00224     testreliable<OsModel_P, Radio_P, Timer_P, Debug_P>::
00225     receive(node_id_t from, size_t len, block_data_t* data) {
00226 
00227         if (from == radio().id()) return;
00228         else {
00229          int mesg_num = data[0]+data[1]*256;
00230             debug().debug( "Application::receive [ data= %d ]\n", mesg_num);
00231             {
00232             messages[mesg_num]=1;
00233          }
00234          
00235          int sum=0;
00236          for (int i=0;i<COUNTER_MAX;i++){
00237             sum+=messages[i];
00238          }
00239             sum_=sum;
00240             
00241         }
00242 
00243     }
00244 
00245 
00246 }
00247 
00248 
00249 #endif   /* _TESTRELIABLE_H */
00250 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines