Wiselib
wiselib.testing/algorithms/cluster/modules/chd/leach_chd.h
Go to the documentation of this file.
00001 /*
00002  * File:   leach_chd.h
00003  * Author: Amaxilatis
00004  */
00005 
00006 #ifndef _PROBABILISTIC_CHD_H
00007 #define  _PROBABILISTIC_CHD_H
00008 
00009 namespace wiselib {
00010 
00016     template< typename OsModel_P>
00017     class LeachClusterHeadDecision {
00018     public:
00019 
00020         //TYPEDEFS
00021         typedef OsModel_P OsModel;
00022         typedef typename OsModel::Radio Radio;
00023         typedef typename OsModel::Debug Debug;
00024         typedef typename OsModel::Rand Rand;
00025 
00026         // data types
00027         typedef int cluster_id_t;
00028         typedef int cluster_level_t; //quite useless within current scheme, supported for compatibility issues
00029         typedef typename Radio::node_id_t node_id_t;
00030         typedef typename Radio::size_t size_t;
00031         typedef typename Radio::block_data_t block_data_t;
00032 
00033         // delegate
00034         typedef delegate1<int, int*> chd_delegate_t;
00035 
00036         /*
00037          * Constructor
00038          * */
00039         LeachClusterHeadDecision() :
00040         cluster_head_(false),
00041         probability_(10),
00042         id_(-1),
00043         clustering_round_(0),
00044         rounds_since_head_(-1) {
00045         };
00046 
00047         /*
00048          * Destructor
00049          * */
00050         ~LeachClusterHeadDecision() {
00051         };
00052 
00053         /*
00054          * INIT
00055          * initializes the values of radio and debug
00056          */
00057         void init(Radio& radio, Debug& debug) {
00058             radio_ = &radio;
00059             debug_ = &debug;
00060         };
00061 
00062         /* SET functions */
00063 
00064         // Set my id
00065 
00066         void set_id(node_id_t id) {
00067             id_ = id;
00068         };
00069 
00070         // get an integer [0-100]
00071         // this the probability in % to
00072         // become a cluster_head
00073 
00074         void set_probability(int prob) {
00075             if (prob > 100) {
00076                 probability_ = 100;
00077             } else if (prob < 0) {
00078                 probability_ = 0;
00079             } else {
00080                 probability_ = prob;
00081             }
00082 
00083         }
00084 
00085         /* GET functions */
00086 
00087         // Returns if Cluster Head
00088 
00089         bool is_cluster_head(void) {
00090             return cluster_head_;
00091         };
00092 
00093         /*
00094          * ENABLE
00095          * enables the module
00096          * initializes values
00097          * */
00098         void enable() {
00099             cluster_head_ = false;
00100         };
00101 
00102         /*
00103          * Disable
00104          * disables the bfsclustering module
00105          * unregisters callbacks
00106          * */
00107         void disable() {
00108         };
00109 
00110         /*
00111          * CALCULATE_HEAD
00112          * defines if the node is a cluster head or not
00113          * if a cluster head return true
00114          * */
00115         bool calculate_head() {
00116             int random_num = (Random() % 100);
00117 
00118             int rmod = 100 / probability_;
00119 
00120             int election_probability = (probability_ * 100) / (100 - probability_ * (clustering_round_ % rmod));
00121 
00122             if (rounds_since_head_ != -1) {
00123 
00124                 if (rounds_since_head_ < (100 / probability_)) {
00125                     election_probability = 0;
00126 
00127                 }
00128                 debug().debug("Rounds since head %d, %d %d%%\n", radio().id(), rounds_since_head_, election_probability);
00129             }
00130 
00131 
00132             // check condition to be a cluster head
00133             if (random_num < election_probability) {
00134                 cluster_head_ = true;
00135                 rounds_since_head_ = 0;
00136             } else {
00137                 cluster_head_ = false;
00138             }
00139             if (rounds_since_head_ != -1) {
00140                 rounds_since_head_++;
00141             }
00142             clustering_round_++;
00143             return cluster_head_;
00144         };
00145     private:
00146 
00147         bool cluster_head_; // if a cluster head
00148         int probability_; // clustering parameter
00149         node_id_t id_; // contains the nodes id
00150         int clustering_round_; //counts the times clusterheads were elected
00151         int rounds_since_head_;
00152         // delegate for callbacks
00153         chd_delegate_t winner_callback_;
00154         chd_delegate_t sender_callback_;
00155 
00156         Radio * radio_;
00157         Debug * debug_;
00158         Rand * rand_;
00159 
00160         Radio& radio() {
00161             return *radio_;
00162         }
00163 
00164         Debug& debug() {
00165             return *debug_;
00166         }
00167 
00168         Rand& rand() {
00169             return *rand_;
00170         }
00171 
00172     };
00173 
00174 }
00175 
00176 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines