Wiselib
wiselib.testing/algorithms/cluster/modules/it/overlapping_it.h
Go to the documentation of this file.
00001 /*
00002  * File:   overlapping_it.h
00003  * Author: Amaxilatis
00004  */
00005 
00006 
00007 #ifndef _OVERLAPPING_IT_H
00008 #define  _OVERLAPPING_IT_H
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 OverlappingIterator {
00022     public:
00023         //TYPEDEFS
00024         typedef OsModel_P OsModel;
00025         typedef typename OsModel::Radio Radio;
00026         typedef typename OsModel::Timer Timer;
00027         typedef typename OsModel::Debug Debug;
00028         typedef OverlappingIterator<OsModel_P> self_t;
00029 
00030         // data types
00031         typedef int cluster_id_t;
00032         typedef typename Radio::node_id_t node_id_t;
00033         typedef typename Radio::block_data_t block_data_t;
00034 
00035         /*
00036          * Constructor
00037          * */
00038         OverlappingIterator() :
00039         node_type_(UNCLUSTERED) {
00040             neighbors_.clear();
00041             cluster_neighbors_.clear();
00042             AC_table_.clear();
00043             CH_table_.clear();
00044 
00045         };
00046 
00047         /*
00048          * Destructor
00049          * */
00050         ~OverlappingIterator() {
00051         };
00052 
00053         /*
00054          * INIT
00055          * initializes the values of radio timer and debug
00056          */
00057         void init(Radio& radio, Timer& timer, Debug& debug) {
00058             radio_ = &radio;
00059             timer_ = &timer;
00060             debug_ = &debug;
00061         };
00062 
00063 
00064         /* SET functions */
00065 
00066         //set the parent value for the given cluster
00067 
00068         void set_parent(node_id_t parent, cluster_id_t cluster_id_) {
00069             for (size_t i = 0; i < CH_table_.size(); i++) {
00070                 if (CH_table_.at(i).cluster_id == cluster_id_) {
00071                     CH_table_.at(i).previous = parent;
00072                 }
00073             }
00074         };
00075 
00076         //set the hops from head for the given cluster
00077 
00078         void set_hops(int hops, cluster_id_t cluster_id) {
00079             for (size_t i = 0; i < CH_table_.size(); i++) {
00080                 if (CH_table_.at(i).cluster_id == cluster_id) {
00081                     CH_table_.at(i).hops = hops;
00082                 }
00083             }
00084         };
00085 
00086         size_t hops(cluster_id_t cluster_id = 0) {
00087             for (size_t i = 0; i < CH_table_.size(); i++) {
00088                 if (CH_table_.at(i).cluster_id == cluster_id) {
00089                     return CH_table_.at(i).hops;
00090                 }
00091             }
00092 
00093         }
00094 
00095         //set the node type
00096 
00097         void set_node_type(int node_type) {
00098             node_type_ = node_type;
00099         };
00100 
00101         //set the node's id
00102 
00103         void set_id(node_id_t id) {
00104             id_ = id;
00105         };
00106 
00107         //clear neighbors list
00108 
00109         void clear_neighbors() {
00110             neighbors_.clear();
00111         }
00112 
00113         /* GET functions */
00114 
00115         //get the parent
00116 
00117         node_id_t parent(cluster_id_t cluster_id_) {
00118             for (size_t i = 0; i < CH_table_.size(); i++) {
00119                 if (CH_table_.at(i).cluster_id == cluster_id_) {
00120                     return CH_table_.at(i).previous;
00121                 }
00122             }
00123 
00124         };
00125 
00126         cluster_id_t cluster_id(size_t i) {
00127 
00128             return CH_table_.at(i).cluster_id;
00129         }
00130 
00131         //get the node type
00132 
00133         int node_type() {
00134             return node_type_;
00135         };
00136 
00137         //get the JREQ payload
00138 
00139         void get_join_request_payload(block_data_t * mess, cluster_id_t cluster_id) {
00140             uint8_t type = JOIN_REQUEST;
00141 
00142 
00143             //ret[0] = type; // type of message
00144             memcpy(mess, &type, sizeof (uint8_t));
00145 
00146             //ret[1] = id_ % 256;
00147             //ret[2] = id_ / 256;
00148             memcpy(mess + 1, &id_, sizeof (id_));
00149             //ret[3] = cluster_id % 256; // cluster_id
00150             //ret[4] = cluster_id / 256;
00151             memcpy(mess + 1 + sizeof (node_id_t), &cluster_id, sizeof (cluster_id));
00152 
00153             size_t clusters_joined_ = clusters_joined();
00154             memcpy(mess + 1 + sizeof (node_id_t) + sizeof (cluster_id_t), &clusters_joined_, sizeof (size_t));
00155 
00156             debug().debug("[%d,%x,%x,%d", type, id_, cluster_id, clusters_joined_);
00157 
00158             for (size_t i = 0; i < clusters_joined(); i++) {
00159                 //ret[6 + 2 * i] = CH_table_.at(i).cluster_id % 256;
00160                 //ret[6 + 1 + 2 * i] = CH_table_.at(i).cluster_id / 256;
00161                 cluster_id_t clust = CH_table_.at(i).cluster_id;
00162                 memcpy(mess + 1 + sizeof (node_id_t) + sizeof (cluster_id_t) + sizeof (size_t) + i * sizeof (cluster_id_t), &clust, sizeof (cluster_id_t));
00163                 debug().debug(",%x", clust);
00164 
00165             }
00166             debug().debug("]\n");
00167 
00168             //memcpy(mess, ret, get_payload_length(JOIN_REQUEST, cluster_id));
00169         };
00170 
00171         size_t get_payload_length(int type, cluster_id_t cluster_id) {
00172             if (type == JOIN_REQUEST)
00173                 return 1 + sizeof (node_id_t) + sizeof (cluster_id_t) + sizeof (size_t) + sizeof (cluster_id_t) * clusters_joined();
00174             else
00175                 return 0;
00176         };
00177 
00178         void enable(void) {
00179             node_type_ = UNCLUSTERED;
00180             cluster_neighbors_.clear();
00181             neighbors_.clear();
00182 
00183         };
00184 
00185         void disable(void) {
00186         };
00187 
00188 
00189         //add node to cluster neighbors
00190 
00191         void node_joined(node_id_t node) {
00192             for (size_t i = 0; i < cluster_neighbors_.size(); i++) {
00193                 if (node == cluster_neighbors_.at(i)) {
00194                     return;
00195                 }
00196             }
00197             cluster_neighbors_.push_back(node);
00198         };
00199 
00200         bool add_cluster(cluster_id_t cluster_id, int hops, node_id_t previous) {
00201             bool exists = false;
00202             for (size_t i = 0; i < CH_table_.size(); i++) {
00203                 if (cluster_id == CH_table_.at(i).cluster_id) {
00204                     exists = true;
00205                     if (hops < CH_table_.at(i).hops) {
00206                         CH_table_.at(i).hops = hops;
00207                         CH_table_.at(i).previous = previous;
00208 
00209                         if (node_type_ != HEAD)
00210                             return true;
00211                     }
00212                     if (node_type_ != HEAD)
00213                         return false;
00214                 }
00215             }
00216             if ((!exists) && (cluster_id != id_)) {
00217                 CH_table_entry new_row;
00218                 new_row.cluster_id = cluster_id;
00219                 new_row.hops = hops;
00220                 new_row.previous = previous;
00221                 CH_table_.push_back(new_row);
00222                 if (node_type_ != HEAD)
00223                     return true;
00224             }
00225             //this part is run only by the cluster heads
00226             if (node_type_ == HEAD) {
00227                 if (cluster_id == id_) return false;
00228 
00229                 for (size_t i = 0; i < AC_table_.size(); i++) {
00230                     if (cluster_id == AC_table_.at(i).cluster_id) {
00231                         return false;
00232                     }
00233                 }
00234                 AC_table_entry new_row;
00235                 new_row.cluster_id = cluster_id;
00236                 new_row.gateways.clear();
00237                 AC_table_.push_back(new_row);
00238                 return true;
00239             }
00240         };
00241 
00242         size_t clusters_joined() {
00243             return CH_table_.size();
00244         }
00245 
00246         /* controls vector neighbor with all nearby nodes */
00247         void add_neighbor(node_id_t node_id) {
00248             for (size_t i = 0; i < neighbors_.size(); i++) {
00249                 if (neighbors_.at(i) == node_id) return;
00250             }
00251             neighbors_.push_back(node_id);
00252         }
00253 
00254         node_id_t next_neighbor() {
00255             if (neighbors_.size() == 0) {
00256                 return Radio::NULL_NODE_ID;
00257             } else {
00258                 node_id_t ret_val = neighbors_.back();
00259                 neighbors_.pop_back();
00260 
00261                 return ret_val;
00262             }
00263         }
00264 
00265         bool eat_request(size_t len, block_data_t * mess) {
00266             if (node_type() == HEAD) {
00267 
00268                 block_data_t ret[len];
00269                 memcpy(ret, mess, len);
00270 
00271                 node_id_t sender;
00272                 memcpy(&sender, mess + 1, sizeof (node_id_t));
00273 
00274                 cluster_id_t mess_cluster;
00275                 memcpy(&mess_cluster, mess + 1 + sizeof (node_id_t), sizeof (cluster_id_t));
00276                 size_t nc;
00277                 memcpy(&nc, mess + 1 + sizeof (node_id_t) + sizeof (cluster_id_t), sizeof (size_t));
00278                 // if the request message was for me
00279                 if (mess_cluster == id_) {
00280                     // search all nc values contained to add to AC_table
00281                     for (size_t i = 0; i < nc; i++) {
00282                         cluster_id_t mess_nc_id;
00283                         memcpy(&mess_nc_id, mess + 1 + sizeof (node_id_t) + sizeof (cluster_id_t) + sizeof (size_t) + i * sizeof (cluster_id_t), sizeof (cluster_id_t));
00284 
00285                         // add sender to the AC_table_ nodes
00286                         if (mess_nc_id != id_) {
00287                             bool found = false;
00288                             // find the AC_table_ entry
00289                             for (size_t j = 0; j < AC_table_.size(); j++) {
00290                                 if (AC_table_.at(j).cluster_id == mess_nc_id) {
00291                                     AC_table_.at(j).gateways.push_back(sender);
00292 
00293                                     found = true;
00294                                     break;
00295                                 }
00296 
00297                             }
00298 
00299                             if (!found) {
00300 #ifdef DEBUG
00301                                 debug().debug("No entry found %x cluster %x , from %x \n", id_, mess_nc_id, sender);
00302 #endif
00303                                 AC_table_entry new_row;
00304                                 new_row.cluster_id = mess_nc_id;
00305                                 new_row.gateways.clear();
00306                                 new_row.gateways.push_back(sender);
00307                                 AC_table_.push_back(new_row);
00308                             }
00309 
00310                         }
00311                     }
00312                     node_joined(sender);
00313                     return true;
00314                 } else {
00315                     return false;
00316                 }
00317             } else
00318                 return false;
00319 
00320 
00321         }
00322 
00323         /* SHOW all the known nodes */
00324         void present_neighbors() {
00325 #ifdef DEBUG
00326             debug().debug("Present Node %x type %d\n", radio().id(), node_type());
00327             if (node_type() == HEAD) {
00328                 debug().debug("mCluster %d : ", cluster_neighbors_.size());
00329                 for (size_t i = 0; i < cluster_neighbors_.size(); i++) {
00330                     debug().debug("%x ", cluster_neighbors_.at(i));
00331                 }
00332                 debug().debug("\n");
00333                 for (size_t i = 0; i < AC_table_.size(); i++) {
00334                     debug().debug("kCluster %d: ", AC_table_.at(i).cluster_id);
00335                     for (size_t j = 0; j < AC_table_.at(i).gateways.size(); j++) {
00336                         debug().debug(" %x", AC_table_.at(i).gateways.at(j));
00337                     }
00338                     debug().debug("\n");
00339                 }
00340 
00341             }
00342             debug().debug("Routing Table:\n");
00343             for (size_t i = 0; i < CH_table_.size(); i++) {
00344                 debug().debug("Cluster %x dist %d prev %x ", CH_table_.at(i).cluster_id, CH_table_.at(i).hops, CH_table_.at(i).previous);
00345                 debug().debug("\n");
00346             }
00347 #endif
00348         };
00349 
00350 
00351 
00352     private:
00353 
00354         // for every node
00355 
00356         struct CH_table_entry {
00357             cluster_id_t cluster_id;
00358             uint8_t hops;
00359             node_id_t previous;
00360         };
00361         typedef wiselib::vector_static<OsModel, CH_table_entry, 100 > CH_table_t;
00362         CH_table_t CH_table_;
00363 
00364         // only for cluster head
00365         typedef wiselib::vector_static<OsModel, node_id_t, 300 > gateway_list_t;
00366 
00367         struct AC_table_entry {
00368             cluster_id_t cluster_id;
00369             gateway_list_t gateways;
00370         };
00371         typedef wiselib::vector_static<OsModel, AC_table_entry, 100 > AC_table_t;
00372         AC_table_t AC_table_;
00373 
00374         typedef wiselib::vector_static<OsModel, node_id_t, 100 > vector_t;
00375 
00376         vector_t cluster_neighbors_; // contains cluster node (heads)
00377         vector_t neighbors_; // contains 1 hop neighbors
00378         node_id_t id_; // node id
00379 
00380         static const int time_slice_ = 1000;
00381         int node_type_; // type of node
00382         int kappa_; // clustering parameter
00383 
00384         Radio * radio_;
00385 
00386         Radio& radio() {
00387             return *radio_;
00388         }
00389         Timer * timer_;
00390 
00391         Timer& timer() {
00392             return *timer_;
00393         }
00394         Debug * debug_;
00395 
00396         Debug& debug() {
00397             return *debug_;
00398         }
00399 
00400     };
00401 }
00402 #endif
00403 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines