Wiselib
wiselib.testing/algorithms/cluster/modules/chd/prob_chd.h
Go to the documentation of this file.
00001 /*
00002  * File:   prob_chd.h
00003  * Author: Amaxilatis
00004  */
00005 
00006 #ifndef _ALGORITHMS_CLUSTER_MODULES_CHD_PROB_CHD_H
00007 #define  _ALGORITHMS_CLUSTER_MODULES_CHD_PROB_CHD_H
00008 
00009 namespace wiselib {
00010 
00016 template<typename OsModel_P, typename Radio_P>
00017 class ProbabilisticClusterHeadDecision {
00018 public:
00019 
00020         //TYPEDEFS
00021         typedef OsModel_P OsModel;
00022         typedef Radio_P Radio;
00023         typedef typename OsModel::Debug Debug;
00024         typedef typename OsModel::Rand Rand;
00025 
00026         /*
00027          * Constructor
00028          * */
00029         ProbabilisticClusterHeadDecision() :
00030         cluster_head_(false), probability_(30) {
00031         }
00032 
00033         /*
00034          * Destructor
00035          * */
00036         ~ProbabilisticClusterHeadDecision() {
00037         }
00038 
00039         /*
00040          * INIT
00041          * initializes the values of radio and debug
00042          */
00043         void init(Radio& radio, Debug& debug, Rand& rand) {
00044             radio_ = &radio;
00045             debug_ = &debug;
00046             rand_ = &rand;
00047         }
00048 
00049         /* 
00050          * SET probability
00051          * get an integer [0-100]
00052          * this the probability in % to
00053          * become a cluster_head
00054          */
00055         void set_probability(int prob) {
00056             if (prob > 100) {
00057                 probability_ = 100;
00058             } else if (prob < 0) {
00059                 probability_ = 0;
00060             } else {
00061                 probability_ = prob;
00062             }
00063         }
00064 
00065         /*
00066          * GET is_cluster_head
00067          * Returns if Cluster Head
00068          */
00069         bool is_cluster_head(void) {
00070             return cluster_head_;
00071         }
00072 
00073         /*
00074          * RESET
00075          * resets the module
00076          * initializes values
00077          * */
00078         void reset() {
00079             cluster_head_ = false;
00080         }
00081 
00082         /*
00083          * CALCULATE_HEAD
00084          * defines if the node is a cluster head or not
00085          * if a cluster head return true
00086          * */
00087 
00088         bool calculate_head() {
00089             int random_num = rand()(100);
00090 
00091             // check condition to be a cluster head
00092             if (random_num < probability_) {
00093                 cluster_head_ = true;
00094             } else {
00095                 cluster_head_ = false;
00096             }
00097             return cluster_head_;
00098         }
00099     private:
00100 
00101         bool cluster_head_; // if a cluster head
00102         int probability_; // clustering parameter
00103 
00104         Radio * radio_;
00105         Debug * debug_;
00106         Rand * rand_;
00107 
00108         Radio& radio() {
00109             return *radio_;
00110         }
00111 
00112         Debug& debug() {
00113             return *debug_;
00114         }
00115 
00116         Rand& rand() {
00117             return *rand_;
00118         }
00119     };
00120 }
00121 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines