Wiselib
wiselib.testing/algorithms/cluster/modules/it/fronts_it.h
Go to the documentation of this file.
00001 /*
00002  * File:   fronts_it.h
00003  * Author: Amaxilatis
00004  */
00005 
00006 #ifndef __BFS_ITERATOR_H_
00007 #define __BFS_ITERATOR_H_
00008 
00009 #include "util/pstl/vector_static.h"
00010 #include "util/delegates/delegate.hpp"
00011 #include "util/pstl/iterator.h"
00012 #include "util/pstl/pair.h"
00013 
00014 namespace wiselib {
00015 
00021     template<typename OsModel_P, typename Radio_P>
00022     class FrontsIterator {
00023     public:
00024         //TYPEDEFS
00025         typedef OsModel_P OsModel;
00026         // os modules
00027         typedef Radio_P Radio;
00028         //typedef typename OsModel::Radio Radio;
00029         typedef typename OsModel::Timer Timer;
00030         typedef typename OsModel::Debug Debug;
00031 
00032         typedef FrontsIterator<OsModel_P, Radio_P> self_t;
00033 
00034         // data types
00035         typedef int cluster_id_t;
00036         typedef typename Radio::node_id_t node_id_t;
00037         typedef typename Radio::block_data_t block_data_t;
00038         typedef wiselib::vector_static<OsModel, node_id_t, 10 > vector_t;
00039         typedef wiselib::pair<node_id_t, cluster_id_t> gateway_vector_entry_t;
00040         typedef wiselib::vector_static<OsModel, gateway_vector_entry_t, 10 > gateway_vector_t;
00041 
00042         typedef wiselib::vector_static<OsModel, wiselib::pair<node_id_t, node_id_t>, 20 > tree_childs_t;
00043         /*
00044          * Constructor
00045          */
00046         FrontsIterator() :
00047         //flag_(false),
00048         parent_(-1),
00049         cluster_id_(-1),
00050         node_type_(UNCLUSTERED),
00051         hops_(0),
00052         any_joined_(false),
00053         is_gateway_(false){
00054             cluster_neighbors_.clear();
00055             non_cluster_neighbors_.clear();
00056             tree_childs.clear();
00057         }
00058 
00059         /*
00060          * Destructor
00061          * */
00062         ~FrontsIterator() {
00063         }
00064 
00065         /*
00066          * INIT
00067          * initializes the values of radio timer and debug
00068          */
00069         void init(Radio& radio, Timer& timer, Debug& debug) {
00070             radio_ = &radio;
00071             timer_ = &timer;
00072             debug_ = &debug;
00073             is_gateway_=false;
00074         }
00075 
00076         /*
00077          * SET functions : parent_
00078          */
00079         inline void set_parent(node_id_t parent) {
00080             parent_ = parent;
00081         }
00082 
00083         /*
00084          * SET functions : cluster_id_
00085          */
00086         void set_cluster_id(cluster_id_t cluster_id) {
00087             cluster_id_ = cluster_id;
00088             //also set the node type
00089             if (cluster_id == radio().id()) {
00090                 set_node_type(HEAD);
00091             } else {
00092                 set_node_type(SIMPLE);
00093             }
00094         }
00095 
00096         /*
00097          * SET functions : hops_
00098          */
00099         inline void set_hops(int hops) {
00100             hops_ = hops;
00101         }
00102 
00103         /*
00104          * SET functions : node_type_
00105          */
00106         inline void set_node_type(int node_type) {
00107             node_type_ = node_type;
00108         }
00109 
00110         /*
00111          * SET functions : any_joined_
00112          */
00113         inline void clear_any_joined() {
00114             any_joined_ = false;
00115         }
00116 
00117         inline void did_joined() {
00118             any_joined_ = true;
00119         }
00120 
00121         /*
00122          * GET functions : cluster_id_
00123          */
00124         inline cluster_id_t cluster_id(void) {
00125             return cluster_id_;
00126         }
00127 
00128         /*
00129          * GET functions : parent_
00130          */
00131         inline node_id_t parent(void) {
00132             return parent_;
00133         }
00134 
00135         inline int hops() {
00136             return hops_;
00137         }
00138 
00139         /*
00140          * GET functions : node_type_
00141          */
00142         inline int node_type() {
00143             return node_type_;
00144         }
00145 
00146         /*
00147          * GET functions : any_joined_
00148          */
00149         inline bool any_joined() {
00150             return any_joined_;
00151         }
00152 
00153         /*
00154          * Enable
00155          */
00156         void reset(void) {
00157             parent_ = radio().id();
00158             cluster_id_ = -1;
00159             node_type_ = UNCLUSTERED;
00160             any_joined_ = false;
00161             cluster_neighbors_.clear();
00162             non_cluster_neighbors_.clear();
00163             hops_ = 0;
00164             is_gateway_ = false;
00165         }
00166 
00167         /*
00168          * Add node to cluster
00169          * neighbors list
00170          */
00171         inline void node_joined(node_id_t node) {
00172             //remove node_id from both structs if exists
00173             drop_node(node);
00174             //add to cluster_neighbors list
00175             cluster_neighbors_.push_back(node);
00176         }
00177 
00178         /*
00179          * Add node to cluster
00180          * neighbors list
00181          */
00182         void add_resume(node_id_t from, node_id_t originator) {
00183             tree_childs.push_back(wiselib::pair<node_id_t, node_id_t>(from, originator));
00184         }
00185 
00186         /*
00187          * Add node to cluster
00188          * neighbors list
00189          */
00190         node_id_t get_child(node_id_t target) {
00191 
00192             for (typename tree_childs_t::iterator
00193                     it = tree_childs.begin();
00194                     it != tree_childs.end();
00195                     it++) {
00196                 if (it->second == target) {
00197                     return it->first;
00198                 }
00199             }
00200             return radio().NULL_NODE_ID;
00201         }
00202 
00203 
00204         /*
00205          * Add node to non_cluster
00206          * neighbors list
00207          */
00208         inline void node_not_joined(node_id_t node, cluster_id_t cluster) {
00209             //remove node_id from both structs if exists
00210             drop_node(node);
00211             gateway_vector_entry_t new_entry;
00212             new_entry.first = node;
00213             new_entry.second = cluster;
00214             //add to non_cluster_neighbors list
00215             non_cluster_neighbors_.push_back(new_entry);
00216             is_gateway_ = true;
00217         }
00218 
00219         bool is_gateway() {
00220             return is_gateway_;
00221         }
00222 
00223         /*
00224          * Drops the node_id from
00225          * both lists of cluster
00226          * and non_cluster neighbors
00227          */
00228         inline void drop_node(node_id_t node) {
00229             for (typename vector_t::iterator it = cluster_neighbors_.begin(); it
00230                     != cluster_neighbors_.end(); ++it) {
00231                 if (*it == node) {
00232                     cluster_neighbors_.erase(it);
00233                     break;
00234                 }
00235             }
00236             for (typename gateway_vector_t::iterator it = non_cluster_neighbors_.begin(); it
00237                     != non_cluster_neighbors_.end(); ++it) {
00238                 if ((*it).first == node) {
00239                     non_cluster_neighbors_.erase(it);
00240                     if (non_cluster_neighbors_.size() == 0) {
00241                         is_gateway_ = false;
00242                     }
00243                     break;
00244                 }
00245             }
00246         }
00247 
00248         //return the number of nodes known
00249         /*
00250         inline size_t node_count(int type) {
00251             if (type == 1) {
00252                 //inside cluster
00253                 return cluster_neighbors_.size();
00254             } else if (type == 0) {
00255                 //outside cluster
00256                 return non_cluster_neighbors_.size();
00257             }
00258             return 0;
00259         }
00260          */
00261 
00262 
00263         //get the cluster neighbors in a list
00264 
00265         inline int childs_count() {
00266             return cluster_neighbors_.size();
00267         }
00268 
00269         void childs(node_id_t *list) {
00270             for (size_t i = 0; i < cluster_neighbors_.size(); i++) {
00271                 list[i] = cluster_neighbors_.at(i);
00272             }
00273         }
00274 
00275         //get the non cluster neighbors in a list
00276 
00277         inline int get_outer_nodes(node_id_t* position) {
00278             return non_cluster_neighbors_.size();
00279         }
00280 
00281         ResumeClusterMsg<OsModel, Radio> get_resume_payload() {
00282             ResumeClusterMsg<OsModel, Radio> msg;
00283             msg.set_node_id(radio().id());
00284 #ifdef DEBUG_PAYLOADS
00285             debug().debug("[%d|%x]\n", msg.msg_id(), msg.node_id());
00286 #endif
00287             return msg;
00288         }
00289 
00290         /* SHOW all the known nodes */
00291         void present_neighbors() {
00292 #ifdef SHAWN
00293             if (node_type() == HEAD)
00294                 debug().debug("Clusters::Node %x::HEAD(%d)::%d::%d::\n", radio().id(), node_type(), cluster_neighbors_.size(), non_cluster_neighbors_.size());
00295             else
00296                 debug().debug("Clusters::Node %x::IN::%x::dist %d::Parent %x::\n", radio().id(), cluster_id(), hops_, parent_);
00297             if (is_gateway())
00298                 debug().debug("Clusters::Node %x::GATEWAY\n", radio().id());
00299 
00300 #else
00301             if (node_type() == HEAD)
00302 //                debug().debug("Clusters::%x::%d::%d::%d::", radio().id(), node_type(), cluster_neighbors_.size(), non_cluster_neighbors_.size());
00303                 debug().debug("Clusters::%x::%d::", radio().id(), node_type());
00304             else
00305 //                debug().debug("Clusters::%x::%d::%x::%d::%x::", radio().id(), node_type(), cluster_id(), hops_, parent_);
00306                 debug().debug("Clusters::%x::%d::%x::", radio().id(), node_type(), cluster_id());
00307 #endif
00308         }
00309 
00310         vector_t cluster_neighbors_;
00311         gateway_vector_t non_cluster_neighbors_;
00312         tree_childs_t tree_childs;
00313 
00314     private:
00315         node_id_t parent_;
00316 
00317         cluster_id_t cluster_id_;
00318         static const int time_slice_ = 1000;
00319         int node_type_;
00320         int hops_;
00321         bool any_joined_;
00322         bool is_gateway_;
00323 
00324         Radio * radio_;
00325 
00326         inline Radio& radio() {
00327             return *radio_;
00328         }
00329         Timer * timer_;
00330 
00331         inline Timer& timer() {
00332             return *timer_;
00333         }
00334         Debug * debug_;
00335 
00336         inline Debug& debug() {
00337             return *debug_;
00338         }
00339 
00340     };
00341 }
00342 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines