Wiselib
wiselib.testing/algorithms/cluster/modules/jd/maxmind_jd.h
Go to the documentation of this file.
00001 #ifndef __MAXMIND_JOIN_DECISION_H_
00002 #define __MAXMIND_JOIN_DECISION_H_
00003 
00004 #include "util/pstl/vector_static.h"
00005 #include "util/delegates/delegate.hpp"
00006 
00007 namespace wiselib {
00008 
00014     template<typename OsModel_P>
00015     class MaxmindJoinDecision {
00016     public:
00017 
00018         //TYPEDEFS
00019         typedef OsModel_P OsModel;
00020         typedef typename OsModel::Radio Radio;
00021         typedef typename OsModel::Debug Debug;
00022         typedef MaxmindJoinDecision<OsModel_P> self_t;
00023         
00024         //data types
00025         typedef int cluster_id_t;
00026         typedef typename Radio::node_id_t node_id_t;
00027         typedef typename Radio::block_data_t block_data_t;
00028         typedef typename Radio::size_t size_t;
00029 
00030         /*
00031          * Constructor
00032          * */
00033         MaxmindJoinDecision() :
00034         cluster_id_(-1),
00035         round_(0),
00036         d_(0) {
00037         };
00038 
00039         /*
00040          * Destructor
00041          * */
00042         ~MaxmindJoinDecision() {
00043         };
00044 
00045         /*
00046          * INIT
00047          * initializes the values of radio timer and debug
00048          */
00049         void init(Radio& radio, Debug& debug) {
00050             radio_ = &radio;
00051             debug_ = &debug;
00052         };
00053 
00054         /* SET functions */
00055         // set the theta value
00056 
00057         void set_theta(int theta) {
00058             d_ = theta;
00059         };
00060         // set my id_
00061 
00062         void set_id(node_id_t id) {
00063             id_ = id;
00064         };
00065 
00066         // set my cluster_id_
00067 
00068         void set_cluster_id(cluster_id_t cluster_id) {
00069             cluster_id_ = cluster_id;
00070         }
00071 
00072         /* GET functions */
00073         
00074         // get my id_
00075 
00076         node_id_t id(void) {
00077             return id_;
00078         };
00079 
00080         // get my cluster_id_
00081 
00082         cluster_id_t cluster_id(void) {
00083             return cluster_id_;
00084         };
00085 
00086         // sets the sender value for current calculation round_
00087 
00088         void set_sender(node_id_t sender) {
00089             sender_[round_ - 1] = sender;
00090         }
00091         // sets the winner value for current calculation round_
00092 
00093         void set_winner(node_id_t winner) {
00094             winner_[round_ - 1] = winner;
00095         }
00096 
00097         /* PAYLOADS */
00098         // Flooding payload for the first 2d_ rounds of maxmind
00099 
00100         void get_flood_payload(block_data_t * mess) {           
00101             //size_t mess_size = get_payload_length(FLOOD);
00102             //block_data_t ret[mess_size];
00103 
00104             node_id_t mess_winner;
00105             // if round is 1 then starts the flooding rounds
00106             if (round_ == 0) {
00107 
00108                 mess_winner=id_;
00109 
00110                 winner_[0] = id_;
00111                 sender_[0] = id_;
00112 
00113                 winner_[1] = winner_[0];
00114                 sender_[1] = sender_[0];
00115                 
00116 
00117             }// send the previous winner value
00118             else {
00119 
00120                 mess_winner = winner_[round_ - 1];
00121 
00122                 winner_[round_] = winner_[round_ - 1];
00123                 sender_[round_] = sender_[round_ - 1];
00124             }
00125 
00126 
00127             round_++;
00128             // add type to message
00129             uint8_t type = FLOOD;
00130             memcpy(mess, &type, 1);
00131             // add winner to message
00132             //ret[1] = mess_winner % 256;
00133             //ret[2] = mess_winner / 256;
00134             memcpy(mess + 1, &mess_winner, sizeof (node_id_t));
00135             //memcpy(ret+1, (void *)&mess_winner, 2);
00136 
00137             // add sender to message
00138             //ret[3] = id_ % 256;
00139             //ret[4] = id_ / 256;
00140             memcpy(mess + 1 + sizeof (node_id_t), &id_, sizeof (node_id_t));
00141             //memcpy(ret+3, (void *)&id_, sizeof);
00142 
00143 #ifdef DEBUG
00144             debug().debug("[%x|%x]\n", mess_winner, id_);
00145 #endif
00146             //memcpy(mess, ret, mess_size);
00147         };
00148 
00149         // return the payload length for my message types
00150 
00151         size_t get_payload_length(int type) {
00152 
00153             if (type == FLOOD)
00154                 return 1 + sizeof (node_id_t) + sizeof (node_id_t);
00155             else
00156                 return 0;
00157         };
00158 
00159         // proccess a flood message and inform the winner sender vectors
00160 
00161         bool join(block_data_t *payload, size_t length) {
00162             // get the FLOOD message            
00163 
00164             // get the received node id
00165             node_id_t recv_id;
00166             memcpy(&recv_id, payload + 1, sizeof (node_id_t));
00167             // get the node id that sent it
00168             node_id_t recv_from;
00169             memcpy(&recv_from, payload + 1 + sizeof (node_id_t), sizeof (node_id_t));
00170             // add the ids to the vectors
00171 
00172             if (round_ <= d_) {
00173                 if (recv_id >= winner_[round_ - 1]) {
00174                     set_winner(recv_id);
00175                     set_sender(recv_from);
00176                 }
00177             } else if (round_ <= 2 * d_) {
00178                 if (recv_id <= winner_[round_ - 1]) {
00179                     set_winner(recv_id);
00180                     set_sender(recv_from);
00181                 }
00182             }
00183 
00184             return false;
00185 
00186         };
00187 
00188         // copy the winner list to the cluster_head_decision_ module
00189 
00190         void get_winner(node_id_t * winner_list) {
00191             memcpy(winner_list, winner_, 2 * d_ * sizeof (node_id_t));
00192         }
00193         // copy the sender list to the cluster_head_decision_ module
00194 
00195         void get_sender(node_id_t * sender_list) {
00196             memcpy(sender_list, sender_, 2 * d_ * sizeof (node_id_t));
00197         }
00198 
00199         /* CALLBACKS */
00200 
00201         /*
00202          * Enable
00203          * initializes values
00204          * */
00205         void enable() {
00206             cluster_id_ = -1;
00207             round_ = 0;
00208             d_ = 0;
00209             for (size_t i = 0; i < 20; i++) {
00210                 winner_[i] = 0;
00211                 sender_[i] = 0;
00212             }
00213 
00214         }
00215 
00216 
00217 
00218     private:
00219         // winner list
00220         node_id_t winner_[20];
00221         // sender list
00222         node_id_t sender_[20];
00223 
00224         cluster_id_t cluster_id_;
00225         node_id_t id_;
00226 
00227         int round_; //round counter
00228 
00229         int d_;
00230 
00231 
00232         Radio * radio_;
00233         Debug * debug_;
00234 
00235         Radio& radio() {
00236             return *radio_;
00237         }
00238 
00239         Debug& debug() {
00240             return *debug_;
00241         }
00242 
00243     };
00244 }
00245 
00246 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines