Wiselib
wiselib.testing/algorithms/cluster/modules/it/normal_it.h
Go to the documentation of this file.
00001 /*
00002  * File:   normal_it.h
00003  * Author: Amaxilatis
00004  */
00005 
00006 #ifndef __BFS_ITERATOR_H_
00007 #define __BFS_ITERATOR_H_
00008 
00009 
00010 #include "util/pstl/vector_static.h"
00011 #include "util/delegates/delegate.hpp"
00012 
00013 namespace wiselib {
00014 
00020     template<typename OsModel_P>
00021     class NormalIterator {
00022     public:
00023         //TYPEDEFS
00024         typedef OsModel_P OsModel;
00025         // os modules
00026         typedef typename OsModel::Radio Radio;
00027         typedef typename OsModel::Timer Timer;
00028         typedef typename OsModel::Debug Debug;
00029         typedef NormalIterator<OsModel_P> self_t;
00030 
00031         // data types
00032         typedef int cluster_id_t;
00033         typedef typename Radio::node_id_t node_id_t;
00034         typedef typename Radio::block_data_t block_data_t;
00035         typedef wiselib::vector_static<OsModel, node_id_t, 100 > vector_t;
00036 
00037         /*
00038          * Constructor
00039          * */
00040         NormalIterator() :
00041         //flag_(false),
00042         parent_(-1),
00043         cluster_id_(-1),
00044         node_type_(UNCLUSTERED),
00045         any_joined_(false) {
00046             neighbors_.clear();
00047             cluster_neighbors_.clear();
00048             non_cluster_neighbors_.clear();
00049         };
00050 
00051         /*
00052          * Destructor
00053          * */
00054         ~NormalIterator() {
00055         };
00056 
00057         /*
00058          * INIT
00059          * initializes the values of radio timer and debug
00060          */
00061         void init(Radio& radio, Timer& timer, Debug& debug) {
00062             radio_ = &radio;
00063             timer_ = &timer;
00064             debug_ = &debug;
00065         };
00066 
00067 
00068         /* SET functions */
00069 
00070         //set the parent value
00071 
00072         void set_parent(node_id_t parent) {
00073             parent_ = parent;
00074         };
00075 
00076         //set the cluster id
00077 
00078         void set_cluster_id(cluster_id_t cluster_id) {
00079             cluster_id_ = cluster_id;
00080 
00081             if (cluster_id == id_) {
00082                 set_node_type(HEAD);
00083 
00084             } else {
00085                 set_node_type(SIMPLE);
00086             }
00087         };
00088 
00089         //set the hops from head
00090 
00091         void set_hops(int hops) {
00092             hops_ = hops;
00093         };
00094 
00095         //set the node type
00096 
00097         void set_node_type(int node_type) {
00098             if (node_type_ != HEAD) {
00099                 node_type_ = node_type;
00100             }
00101         };
00102 
00103         //set the node's id
00104 
00105         void set_id(node_id_t id) {
00106             id_ = id;
00107         };
00108 
00109         //set none joined the cluster
00110 
00111         void clear_any_joined() {
00112             any_joined_ = false;
00113         };
00114 
00115         //clear neighbors list
00116 
00117         void clear_neighbors() {
00118             neighbors_.clear();
00119         }
00120 
00121         //set someone joined the cluster
00122 
00123         void did_joined() {
00124             any_joined_ = true;
00125         }
00126 
00127         /* GET functions */
00128 
00129         //get the cluster id
00130 
00131         cluster_id_t cluster_id(void) {
00132             return cluster_id_;
00133         };
00134 
00135         //get the parent
00136 
00137         node_id_t parent(void) {
00138             return parent_;
00139         };
00140 
00141         //get the node type
00142 
00143         int node_type() {
00144             return node_type_;
00145         };
00146 
00147         //get if anyone joined the cluster
00148 
00149         bool any_joined() {
00150             return any_joined_;
00151         };
00152 
00153         void enable(void) {
00154             parent_ = id_;
00155             cluster_id_ = -1;
00156             node_type_ = UNCLUSTERED;
00157             any_joined_ = false;
00158             cluster_neighbors_.clear();
00159             non_cluster_neighbors_.clear();
00160             neighbors_.clear();
00161 
00162         };
00163 
00164         void disable(void) {
00165         };
00166 
00167         //add node to cluster neighbors
00168 
00169         void node_joined(node_id_t node) {
00170             for (size_t i = 0; i < cluster_neighbors_.size(); i++) {
00171                 if (node == cluster_neighbors_.at(i)) {
00172                     return;
00173                 }
00174             }
00175             cluster_neighbors_.push_back(node);
00176         };
00177 
00178         //add node to non cluster neighbors
00179 
00180         void node_not_joined(node_id_t node) {
00181             for (size_t i = 0; i < non_cluster_neighbors_.size(); i++) {
00182                 if (node == non_cluster_neighbors_.at(i)) {
00183                     return;
00184                 }
00185             }
00186             non_cluster_neighbors_.push_back(node);
00187         };
00188 
00189         // if we lost contact with one node remove him from lists
00190         void drop_node(node_id_t node) {
00191             
00192         }
00193 
00194         //return the number of nodes known
00195 
00196         int node_count(int type) {
00197             if (type == 1)
00198                 //inside cluster
00199                 return cluster_neighbors_.size();
00200             else if (type == 0)
00201                 //outside cluster
00202                 return non_cluster_neighbors_.size();
00203         }
00204 
00205         //get the cluster neighbors in a list
00206 
00207         int get_intra_nodes(node_id_t* position) {
00208             node_id_t nodes[cluster_neighbors_.size()];
00209             for (int i = 0; i < cluster_neighbors_.size(); i++) {
00210                 nodes[i] = cluster_neighbors_.at(i);
00211             }
00212             memcpy(position, nodes, cluster_neighbors_.size() * sizeof (node_id_t));
00213             return cluster_neighbors_.size();
00214         }
00215 
00216         //get the non cluster neighbors in a list
00217 
00218         int get_outer_nodes(node_id_t* position) {
00219             node_id_t nodes[non_cluster_neighbors_.size()];
00220             for (int i = 0; i < non_cluster_neighbors_.size(); i++) {
00221                 nodes[i] = non_cluster_neighbors_.at(i);
00222             }
00223             memcpy(position, nodes, non_cluster_neighbors_.size() * sizeof (node_id_t));
00224             return non_cluster_neighbors_.size();
00225         }
00226 
00227         /* controls vector neighbor with all nearby nodes */
00228         void add_neighbor(node_id_t node_id) {
00229             for (size_t i = 0; i < neighbors_.size(); i++) {
00230                 if (neighbors_.at(i) == node_id) return;
00231             }
00232             neighbors_.push_back(node_id);
00233         }
00234 
00235         node_id_t next_neighbor() {
00236             if (neighbors_.size() == 0) {
00237                 return Radio::NULL_NODE_ID;
00238             } else {
00239                 node_id_t ret_val = neighbors_.back();
00240                 neighbors_.pop_back();
00241 
00242                 return ret_val;
00243             }
00244         }
00245 
00246         void get_resume_payload(block_data_t * mess) {
00247             uint8_t type = RESUME; // type of message
00248             memcpy(mess, &type, 1);
00249             memcpy(mess + 1, &id_, sizeof (node_id_t));
00250 
00251 #ifdef DEBUG
00252             debug().debug("[%d|%x]\n", type, id_);
00253 #endif
00254 
00255         }
00256 
00257         size_t get_payload_length(int type) {
00258             if (type == RESUME)
00259                 return 1 + sizeof (node_id_t);
00260             else
00261                 return 0;
00262         };
00263 
00264         /* SHOW all the known nodes */
00265         void present_neighbors() {
00266 #ifdef DEBUG
00267             debug().debug("Present Node %x Neighbors:", radio().id());
00268             debug().debug("Cluster: ");
00269             for (size_t i = 0; i < cluster_neighbors_.size(); i++) {
00270                 debug().debug("%x ", cluster_neighbors_.at(i));
00271             }
00272             debug().debug("Non-Cluster: ");
00273             for (size_t i = 0; i < non_cluster_neighbors_.size(); i++) {
00274                 debug().debug("%x ", non_cluster_neighbors_.at(i));
00275             }
00276 #endif
00277         };
00278 
00279     private:
00280         //bool flag_;
00281         vector_t cluster_neighbors_;
00282         vector_t non_cluster_neighbors_;
00283         vector_t neighbors_;
00284         node_id_t parent_;
00285         node_id_t id_;
00286 
00287 
00288 
00289         cluster_id_t cluster_id_;
00290         static const int time_slice_ = 1000;
00291         int node_type_;
00292         int hops_;
00293         bool any_joined_;
00294 
00295 
00296         Radio * radio_;
00297 
00298         Radio& radio() {
00299             return *radio_;
00300         }
00301         Timer * timer_;
00302 
00303         Timer& timer() {
00304             return *timer_;
00305         }
00306         Debug * debug_;
00307 
00308         Debug& debug() {
00309             return *debug_;
00310         }
00311 
00312     };
00313 
00314 }
00315 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines