Wiselib
wiselib.testing/algorithms/cluster/modules/jd/bfs_jd.h
Go to the documentation of this file.
00001 #ifndef __BFS_JOIN_DECISION_H_
00002 #define __BFS_JOIN_DECISION_H_
00003 
00004 namespace wiselib {
00005 
00011 template<typename OsModel_P, typename Radio_P>
00012 class BfsJoinDecision {
00013 public:
00014    //TYPEDEFS
00015    typedef OsModel_P OsModel;
00016    // os modules
00017    typedef Radio_P Radio;
00018    typedef typename OsModel::Debug Debug;
00019    // data types
00020    typedef typename Radio::node_id_t node_id_t;
00021    typedef typename Radio::block_data_t block_data_t;
00022    typedef node_id_t cluster_id_t;
00023 
00024    /*
00025     * Constructor
00026     * */
00027    BfsJoinDecision() :
00028       cluster_id_(-1), hops_(0) {
00029    }
00030 
00031    /*
00032     * Destructor
00033     * */
00034    ~BfsJoinDecision() {
00035    }
00036 
00037    /*
00038     * INIT
00039     * initializes the values of radio and debug
00040     */
00041    void init(Radio& radio, Debug& debug) {
00042       radio_ = &radio;
00043       debug_ = &debug;
00044    }
00045 
00046    /* SET functions */
00047 
00048    //set the cluster id
00049    inline void set_cluster_id(cluster_id_t cluster_id) {
00050       cluster_id_ = cluster_id;
00051    }
00052 
00053    inline void set_maxhops(int hops) {
00054       maxhops_ = hops;
00055    }
00056 
00057    //set the hops from head
00058    inline void set_hops(int hops) {
00059       hops_ = hops;
00060    }
00061 
00062    inline int hops() {
00063       return hops_;
00064    }
00065 
00066    /* GET functions */
00067 
00068    //get the cluster id
00069    inline cluster_id_t cluster_id(void) {
00070       return cluster_id_;
00071    }
00072 
00073    //get the join request payload
00074    JoinClusterMsg<OsModel, Radio> get_join_request_payload() {
00075       JoinClusterMsg<OsModel, Radio> msg;
00076       msg.set_cluster_id(cluster_id_);
00077       msg.set_hops(hops_);
00078 
00079 #ifdef DEBUG_PAYLOAD
00080       debug().debug("[%d|%x|%d]\n",JOIN,cluster_id_,hops_);
00081 #endif
00082       return msg;
00083    }
00084    JoinAccClusterMsg<OsModel, Radio> get_join_accept_payload() {
00085       JoinAccClusterMsg<OsModel, Radio> msg;
00086       msg.set_node_id(radio_->id());
00087 
00088 #ifdef DEBUG_PAYLOAD
00089       debug().debug("[%d|%x]\n",JOIN_ACCEPT,radio_->id());
00090 #endif
00091       return msg;
00092    }
00093 
00094    /*
00095     * JOIN
00096     * respond to an JOIN message received
00097     * either join to a cluster or not
00098     * */
00099    bool join(uint8_t *payload, uint8_t length) {
00100       JoinClusterMsg<OsModel, Radio> msg;
00101       memcpy(&msg, payload, length);
00102 #ifdef SHAWN
00103                 if (msg.cluster_id()==-1) return false;
00104 #endif
00105                 int mess_hops = msg.hops();
00106    
00107       if (msg.cluster_id()>radio_->id()) return false;
00108 
00109       //if in no cluster yet
00110       if (cluster_id_ == Radio::NULL_NODE_ID) {
00111 #ifdef DEBUG_CLUSTERING_EXTRA
00112                     debug().debug("JOIN::%x::%d<%d::%d\n",radio_->id(),mess_hops,maxhops_,msg.cluster_id());
00113 #endif
00114          if (mess_hops < maxhops_) {
00115 #ifdef DEBUG_CLUSTERING_EXTRA
00116                         debug().debug("Joined::%x::",radio_->id());
00117 #endif
00118             //join the cluster
00119             cluster_id_ = msg.cluster_id();
00120             //set the hops from head
00121             hops_ = mess_hops + 1;
00122             //return true
00123             return true;
00124          } else {
00125             return false;
00126          }
00127       }//if already in a cluster
00128             else {
00129                 cluster_id_t mess_cluster_id = msg.cluster_id();
00130                 uint8_t mess_hops = msg.hops();
00131 #ifdef DEBUG_CLUSTERING_EXTRA
00132                 debug().debug("JOIN2::%x::%d<%d::%x<%x::", radio_->id(), mess_hops, maxhops_, mess_cluster_id, cluster_id_);
00133 #endif
00134                 if (mess_hops + 1 <= hops_) {
00135                     if (mess_cluster_id < cluster_id_) {
00136 #ifdef DEBUG_CLUSTERING_EXTRA
00137                         debug().debug("Joined::%x::", radio_->id());
00138 #endif
00139                         //join the cluster
00140                         cluster_id_ = mess_cluster_id;
00141                         //set the hops from head
00142                         hops_ = mess_hops + 1;
00143                         //return true
00144                         return true;
00145                     } else {
00146                         return false;
00147                     }
00148                 } else {
00149                     if ((hops_ == 0) && (msg.hops() < maxhops_) && (msg.cluster_id() < cluster_id_)) {
00150 #ifdef DEBUG_CLUSTERING_EXTRA
00151                         debug().debug("Joined::%x::", radio_->id());
00152 #endif
00153                         //join the cluster
00154                         cluster_id_ = mess_cluster_id;
00155                         //set the hops from head
00156                         hops_ = mess_hops + 1;
00157                         //return true
00158                         return true;
00159 
00160                     }
00161                     //return false
00162                     return false;
00163                 }
00164             }
00165         }
00166 
00167         /*
00168          * RESET
00169          * resets the module
00170          * initializes values
00171          * */
00172         inline void reset() {
00173             cluster_id_ = Radio::NULL_NODE_ID;
00174             hops_ = 0;
00175         }
00176 
00177     private:
00178 
00179         cluster_id_t cluster_id_; //the cluste's id
00180         int hops_; //hops from cluster head
00181         int maxhops_;
00182 
00183         Radio * radio_; //radio module
00184         Debug * debug_; //debug module
00185     };
00186 }
00187 
00188 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines