Wiselib
wiselib.testing/algorithms/coloring/two_hops/two_hops_coloring.h
Go to the documentation of this file.
00001 #ifndef __ALGORITHMS_COLORING_TWO_HOPS_H__
00002 #define __ALGORITHMS_COLORING_TWO_HOPS_H__
00003 
00004 #include "algorithms/coloring/two_hops/two_hops_message_types.h"
00005 //#include "algorithms/routing/tora/tora_routing.h"
00006 #include "internal_interface/routing_table/routing_table_static_array.h"
00007 //#include "internal_interface/coloring_table/color_table_map.h"
00008 #include "internal_interface/coloring_table/colors_sorted.h"
00009 #include "util/pstl/map_static_vector.h"
00010 #include "util/pstl/pair.h"
00011 
00012 #include <string.h>
00013 //#include <string>
00014 #define DEBUG_TWOHOPSCOLORING
00015 
00016 namespace wiselib {
00017 
00018 #define DATA_STRUCTS
00019 
00020     struct rep_c_change {
00021         uint32_t color_assigned;
00022         uint32_t color_removed;
00023         uint32_t hops;
00024         uint32_t node_id;
00025         uint32_t change_round;
00026     };
00027 
00028     struct rep_fb {
00029         uint32_t color_foridden;
00030         uint32_t fb_round;
00031     };
00032 
00033     struct rep_sf {
00034         uint32_t satisfaction;
00035         uint32_t node_id;
00036         uint32_t sf_round;
00037     };
00047     template<typename OsModel_P,
00048             typename Radio_P = typename OsModel_P::Radio,
00049             typename Debug_P = typename OsModel_P::Debug>
00050             class TwoHopsColoring {
00051     public:
00052         typedef OsModel_P OsModel;
00053         typedef Radio_P Radio;
00054         typedef Debug_P Debug;
00055         typedef typename OsModel_P::Timer Timer;
00056         typedef TwoHopsColoring<OsModel, Radio, Debug> self_type;
00057 //        typedef wiselib::StaticArrayRoutingTable<OsModel, Radio, 8, wiselib::ToraRoutingTableValue<OsModel, Radio> >
00058 //        ToraRoutingTable;
00059      //   typedef ToraRouting<OsModel, ToraRoutingTable, Radio, Debug> tora_routing_t;
00060 //        typedef ToraRoutingMessage<OsModel_P, Radio> tora_message;
00061         typedef typename Radio::node_id_t node_id_t;
00062         typedef typename Radio::size_t size_t;
00063         typedef typename Radio::block_data_t block_data_t;
00064         typedef typename Timer::millis_t millis_t;
00065         //Needed Message Types
00066         typedef TwoHopsMessage<OsModel, Radio> coloring_message;
00067 
00068 //        typedef StlMapColorTable<OsModel, Radio, node_id_t> ColorTable;
00069         typedef MapStaticVector<OsModel , uint32_t, uint32_t, 50> ColorTable;
00070 //        typedef StlMapColorTable<OsModel, Radio, node_id_t> ForbiddenTable;
00071         typedef MapStaticVector<OsModel , uint32_t, uint32_t, 100> ForbiddenTable;
00072         typedef ColorsTable<OsModel, Radio> ColorsSorted;
00073 
00074         typedef wiselib::pair<uint32_t, uint32_t> pair_t;
00075 
00076         typedef typename  wiselib::MapStaticVector<OsModel , uint32_t, uint32_t, 50>::iterator ColorTable_iterator;
00077         typedef typename  wiselib::MapStaticVector<OsModel , uint32_t, uint32_t, 100>::iterator ForbiddenTable_iterator;
00078 
00081         TwoHopsColoring();
00082         ~TwoHopsColoring();
00084 
00087         void enable(void);
00088         void disable(void);
00090 
00093         void timer_elapsed(void *userdata);
00095 
00098         void receive(node_id_t from, size_t len, block_data_t *data);
00100 
00103         uint32_t satisfied(uint32_t i);
00105 
00108         void greed_move(uint32_t i);
00110 
00113         void try_greed();
00115 
00118         void try_change();
00120 
00123         void bootstrap_message();
00125 
00126         void send_special_message(uint8_t* payload, node_id_t source, uint8_t msg_id, node_id_t destination, uint8_t routing_type, uint hops, uint16_t msg_id_num);
00127 
00130         uint16_t get_neighboors();
00132 
00135         uint16_t get_color_nodes();
00137 
00138         uint32_t fm(uint32_t arr[], uint32_t b, uint32_t n) {
00139             uint32_t f = b;
00140             uint32_t c;
00141 
00142             for (c = b + 1; c < n; c++)
00143                 if (arr[c] > arr[f])
00144                     f = c;
00145 
00146             return f;
00147         }
00148 
00149         void isort(uint32_t arr[], uint32_t n) {
00150             uint32_t s, w;
00151             uint32_t sm;
00152 
00153             for (s = 0; s < n - 1; s++) {
00154                 w = fm(arr, s, n);
00155                 sm = arr[w];
00156                 arr[w] = arr[s];
00157                 arr[s] = sm;
00158             }
00159         }
00160 
00161         inline uint32_t get_alg_messages(){
00162             return alg_messages;
00163         }
00164 
00165         inline uint32_t get_color() {
00166             return color;
00167         }
00168 
00169         inline void set_color(uint32_t color_) {
00170             color = color_;
00171         }
00172 
00173         inline uint16_t active_colors_size() {
00174             return color_numbers.get_active_colors();
00175         };
00176 
00177         inline void add_neighboor(uint neighboor_id, uint color_num) {
00178 //            neighboors_colors.insert(  std::make_pair(neighboor_id, color_num));
00179             pair_t pp(neighboor_id,color_num);
00180             
00181             neighboors_colors.insert(  pp );
00182         };
00183 
00184         inline bool is_in_messages(uint node_id, uint payload) {
00185             ColorTable_iterator it = messages.find(node_id);
00186             
00187             if (it == messages.end()) {
00188                 messages.insert( pair_t(node_id,payload) );
00189                 return false;
00190             } else {
00191                 if (it->second == payload) {
00192                     return true;
00193                 } else {
00194                     uint32_t msgRC = node_id * 100 + 21;
00195                     uint32_t msgRS = node_id * 100 + 22;
00196                     uint32_t msgRF = node_id * 100 + 23;
00197                     if(((payload != msgRS) && (it->second == msgRF )) ||
00198                             ((payload != msgRC) && (it->second == msgRS )) ||
00199                             ((payload != msgRF) && (it->second == msgRC ))){
00200                         return true;
00201                     }else{
00202                         it->second = payload;
00203                         return false;
00204                     }
00205                 }
00206             }
00207         };
00208 
00209         inline void print_metrics(){
00210             /*
00211              * Not Wiselib compatible: I/O streams
00212             cout << "--------------------------" << endl;
00213             cout << "Eimai o :" << radio().id() << " me fb_round:" << fb_round << " kai fb_answer_count" << answerfb_count << endl;
00214             cout << "Eimai o :" << radio().id() << " me greed_round:" << greed_round << " kai rs_answer_count" << answerrs_count << endl;
00215             cout << "Eimai o :" << radio().id() << " me change_round:" << change_round << " kai change_answer_count" << answerrc_count << endl;
00216              * */
00217             debug().debug("Node %i: fb_round=%d fb_answer_count=%d\n",radio().id(),fb_round,answerfb_count);
00218             debug().debug("Node %i: greed_round=%d rs_answer_count=%d\n",radio().id(),greed_round,answerrs_count);
00219             debug().debug("Node %i: change_round=%d change_answer_count=%d\n",radio().id(),change_round,answerrc_count);
00220 
00221         }
00222 
00223         inline void get_metrics(uint8_t *buf,uint8_t pos){
00224             memcpy(buf + pos, &fb_round, 4);
00225             memcpy(buf + pos + 4, &answerfb_count, 2);
00226             memcpy(buf + pos + 6, &greed_round, 4);
00227             memcpy(buf + pos + 10, &answerrs_count, 2);
00228             memcpy(buf + pos + 12, &change_round, 4);
00229             memcpy(buf + pos + 16, &answerrc_count, 2);
00230         }
00231         
00232         inline void add_node_color(uint node_id, uint color_num) {
00233             color_nodes.insert(pair_t(node_id, color_num));
00234         };
00235 
00236         inline uint16_t add_forbidden_color(uint32_t color) {
00237             forbidden_colors.insert(pair_t(color, 0));
00238             return 0;
00239         };
00240 
00241         inline void change_node_color(uint node_id, uint color_num) {
00242             ColorTable_iterator it = color_nodes.find(node_id);
00243             it->second = color_num;
00244         };
00245 
00246    //     inline void set_tora_routing(tora_routing_t* tora) {
00247     //        tora_routing = tora;
00248     //    };
00249 
00250         inline void set_diameter(uint32_t diam){
00251             diameter = diam;
00252         }
00253 
00254         uint32_t compare(const void * a, const void * b) {
00255             if (*(uint32_t*) a == *(uint32_t*) b) return 0;
00256             else if (*(uint32_t*) a < *(uint32_t*) b) return 1;
00257             else return -1;
00258         }
00259         
00260         void init( Radio& radio, Timer& timer, Debug& debug ) {
00261           radio_ = &radio;
00262           timer_ = &timer;
00263           debug_ = &debug;
00264         }
00265         
00266         void destruct() {
00267         }
00268 
00269     private:
00270       
00271         Radio& radio()
00272         { return *radio_; }
00273         
00274         Timer& timer()
00275         { return *timer_; }
00276         
00277         Debug& debug()
00278         { return *debug_; }
00279       
00280         Radio * radio_;
00281         Timer * timer_;
00282         Debug * debug_;
00283         
00284    //     tora_routing_t *tora_routing;
00285         uint32_t step, ncount, color, m_color, temp_color;
00286         uint16_t permission, neigh_colors_count;
00287         uint32_t ncount_all, color_under_cons;
00288         uint16_t answerfb_count,answerrs_count,answerrc_count;
00289         uint8_t asked_color;
00290         ColorTable color_nodes, neighboors_colors, messages;
00291 //        std::map<uint, uint>::iterator iter_color_nodes;
00292         ColorsSorted color_numbers;
00293         ForbiddenTable forbidden_colors;
00294         uint32_t fb_round, greed_round, change_round, round_satisfaction,diameter,idle_rounds,alg_messages;
00295     };
00296     // -----------------------------------------------------------------------
00297     // -----------TwoHops Coloring Constructor---------------------------------
00298     // -----------------------------------------------------------------------
00299 
00300     template<typename OsModel_P,
00301     typename Radio_P,
00302     typename Debug_P>
00303     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00304     TwoHopsColoring()
00305     : step(0),
00306     ncount(0),
00307     temp_color(-1),
00308     permission(1),
00309     ncount_all(0),
00310     color_under_cons(0),
00311     answerfb_count(0),
00312     answerrs_count(0),
00313     answerrc_count(0),
00314     fb_round(0),
00315     greed_round(1),
00316     change_round(2),
00317     idle_rounds(0),
00318     alg_messages(0){
00319     };
00320     // -----------TwoHops Coloring De-Constructor------------------------------
00321 
00322     template<typename OsModel_P,
00323     typename Radio_P,
00324     typename Debug_P>
00325     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00326     ~TwoHopsColoring() {
00327 
00328     };
00329     // -----------------------------------------------------------------------
00330 
00331     template<typename OsModel_P,
00332     typename Radio_P,
00333     typename Debug_P>
00334     void
00335     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00336     enable() {
00337         radio().enable_radio();
00338         radio().template reg_recv_callback<self_type, &self_type::receive > (this);
00339         timer().template set_timer<self_type, &self_type::timer_elapsed > (
00340                 500, this, 0);
00341         color = (uint32_t) (radio().id()) + 1;
00342         color_numbers.init();
00343         color_numbers.insert(color);
00344         bootstrap_message();
00345 
00346 
00347     }
00348     // -----------------------------------------------------------------------
00349 
00350     template<typename OsModel_P,
00351     typename Radio_P,
00352     typename Debug_P>
00353     void
00354     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00355     disable(void) {
00356 #ifdef DEBUG_TWOHOPSCOLORING
00357         debug().debug("TwoHopsColoring: Disable\n");
00358 #endif
00359 //        cout << "Eimai o" << radio().id() << ". To teliko mou xrwma einai:" << get_color << endl;
00360     }
00361     // -----------------------------------------------------------------------
00362 
00363     template<typename OsModel_P,
00364     typename Radio_P,
00365     typename Debug_P>
00366     void
00367     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00368     timer_elapsed(void *userdata) {
00369         step = 1;
00370         ncount_all = get_neighboors();
00371         forbidden_colors.clear();
00372         temp_color = 0;
00373         permission = 1;
00374         color_under_cons = 0;
00375         m_color = 0;
00376         rep_fb fb;
00377         fb.color_foridden = color;
00378         fb.fb_round = fb_round;
00379         idle_rounds++;
00380         if(idle_rounds < (100*diameter+1)){
00381             send_special_message((uint8_t*) & fb, radio().id(), REP_FBID, -1, 0, 1, 0);
00382         }else{
00383             /*
00384              * Not Wiselib compatible: I/O streams
00385             cout<< "Baresa liksi: " << radio().id();
00386              * */
00387             debug().debug("Node %i: timer elapsed\n",radio().id());
00388         }
00389         step = 1;
00390     }
00391 
00392     // -----------------------------------------------------------------------
00393 
00394     template<typename OsModel_P,
00395     typename Radio_P,
00396     typename Debug_P>
00397     void
00398     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00399     receive(node_id_t from, size_t len, block_data_t *data) {
00400         if (from == radio().id())
00401             return;
00402         uint8_t msg_id = *data;
00403         coloring_message *message;
00404         message = (coloring_message *) data;
00405         uint hops = message->hops();
00406         uint8_t* payload;
00407         uint pay_tmp = *((uint*) message->payload());
00408         payload = (uint8_t*) & pay_tmp;
00409         if ((hops == 1)&&(diameter!=1)) {
00410             if (msg_id == REP_CHANGE) {
00411                     rep_c_change rc = *((rep_c_change*) message->payload());
00412                     send_special_message((uint8_t*) & rc, message->source(), msg_id, -1, 0, 2, 0);
00413                 
00414             } else {
00415                 send_special_message(payload, message->source(), msg_id, -1, 0, 2, 0);
00416             }
00417         }     
00418         rep_c_change rc = *((rep_c_change*) message->payload());
00419         if (!is_in_messages(message->source(), message->id_num())) {
00420 
00421             if (msg_id == NODE_STARTED) {
00422                 message = (coloring_message *) data;
00423                 uint load = (uint) *((uint8_t*) message->payload());
00424                 
00425                 ColorTable_iterator it = neighboors_colors.find(load);
00426                 if (it == neighboors_colors.end()) {
00427                     if (load != radio().id()) {
00428                         add_neighboor(load, load + 1);
00429                         color_numbers.insert(load + 1);
00430                     }
00431                 }
00432                 if (hops == 1) {
00433                     ncount++;
00434                 }
00435             } else if (msg_id == REP_CHANGE) {
00436 
00437                 rep_c_change rc = *((rep_c_change*) message->payload());
00438                 if (rc.node_id != radio().id()) {
00439                     if (rc.change_round == change_round) {
00440                         answerrc_count++;
00441                         if ((rc.color_assigned != 0) && (rc.node_id != radio().id())) {
00442                             color_numbers.insert(rc.color_assigned);
00443                             color_numbers.remove(rc.color_removed);
00444                             idle_rounds = 0;
00445                         }
00446                         if ((answerrc_count == ncount_all) && (fb_round > 14))
00447                             ;//cout << "edw" << endl;
00448                         if (answerrc_count > (ncount_all)) {
00449                             /*
00450                              * Not Wiselib compatible: I/O streams && 'int' date type
00451                             int me = radio().id();
00452                             cout << "I am:" << me << " and it is Problem RECHANGE BIG TIME" << endl;
00453                              */
00454                             debug().debug("Node %i: This should not have occurred (?REP_CHANGE?)\n",radio().id());
00455                         }
00456                         try_change();
00457                     }else{
00458                             /*
00459                              * Not Wiselib compatible: I/O streams
00460                              cout << "Egw o::" << radio().id() << " PIRA AKURO CHANGE MINIMA ROUND" <<  rc.change_round << " cr:" << change_round << endl;
00461                             */
00462                         debug().debug("Node %i: Received invalid REP message, dropping loudly\n",radio().id());
00463                     }
00464                 }
00465             } else if (msg_id == REP_SAT) {
00466                 if (hops == 1) {
00467 
00468 
00469                     rep_sf sf = *((rep_sf*) message->payload());
00470                     if (sf.sf_round == greed_round) {
00471                         answerrs_count++;
00472                         if (sf.node_id > radio().id()) {
00473                             if (sf.satisfaction == 0) {
00474                                 permission = 0;
00475                             }
00476                         }
00477                         if (answerrs_count > (ncount)) {
00478                             /*
00479                              * Not Wiselib compatible: I/O streams
00480                             int me = radio().id();
00481                             cout << "I am:" << me << " and it is Problem REPSAT BIG TIME" << endl;
00482                              */
00483                             debug().debug("Node %i: This should not have occured (?REPSAT?)\n",radio().id());
00484                         }
00485                         if (radio().id() == 1)
00486                             try_greed();
00487                         else
00488                             try_greed();
00489                     } else {
00490                             /*
00491                              * Not Wiselib compatible: I/O streams
00492                             cout << "Egw o::" << radio().id() << " PIRA AKURO GREED MINIMA ROUND" <<  rc.change_round << " cr:" << change_round << endl;
00493                             */
00494                         debug().debug("Node %i: Received invalid GREED message, dropping loudly\n",radio().id());
00495                     }
00496                 }
00497             } else if (msg_id == REP_FBID) {
00498                 if (hops == 1) {
00499                     rep_fb fb = *((rep_fb*) message->payload());
00500                     if (fb.fb_round == fb_round) {
00501                         answerfb_count++;
00502 
00503                         ForbiddenTable_iterator it = forbidden_colors.find(fb.color_foridden);
00504                         if (it == forbidden_colors.end()) {
00505                             add_forbidden_color(fb.color_foridden);
00506                         }
00507                         if (answerfb_count > (ncount)) {
00508                             /*
00509                              * Not Wiselib compatible: I/O streams
00510                             int me = radio().id();
00511                             cout << "I am:" << me << " and it is Problem REPFBID BIG TIME" << endl;
00512                              */
00513                             debug().debug("Node %i: This should not have occured (?REPFBID?)\n",radio().id());
00514                         }
00515                         if ((answerfb_count == ncount)) {
00516                             step++;
00517                             answerfb_count = 0;
00518                             uint32_t sat = satisfied(radio().id());
00519                             rep_sf sf;
00520                             sf.satisfaction = sat;
00521                             sf.sf_round = greed_round;
00522                             sf.node_id = radio().id();
00523                             send_special_message((uint8_t*) & sf, radio().id(), REP_SAT, -1, 0, 1, 0);
00524                             fb_round++;
00525                             try_greed();
00526                         }
00527                     } else {
00528                         /*
00529                          * Not Wiselib compatible: I/O streams
00530                         cout << "Egw o::" << radio().id() << " PIRA AKURO FB MINIMA" <<  fb.fb_round << " cr:" << fb_round << endl;
00531                         */
00532                         debug().debug("Node %i: Received invalid FB message, dropping loudly\n",radio().id());
00533                     }
00534                 }
00535             }
00536         } else {
00537             //cout<< "Egw o:"<< radio().id() << "to exw ksanaparei to minima me id:" <<(int)msg_id << " apo ton:" << (int)message->source() << "me id_num:" << message->id_num() <<  endl;
00538         }
00539 
00540     }
00541     // -----------------------------------------------------------------------
00542 
00543     template<typename OsModel_P,
00544     typename Radio_P,
00545     typename Debug_P>
00546     void
00547     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00548     bootstrap_message() {
00549 #ifdef DEBUG_TWOHOPSCOLORING
00550         //debug().debug("Node number %d sends bootstrap message\n", radio().id());
00551 #endif
00552         uint32_t type;
00553         uint8_t payload = radio().id();
00554         type = NODE_STARTED;
00555         send_special_message(&payload, radio().id(), type, -1, 0, 1, 0);
00556 
00557     }
00558 
00559     // -----------------------------------------------------------------------
00560 
00561     template<typename OsModel_P,
00562     typename Radio_P,
00563     typename Debug_P>
00564     void
00565     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00566     send_special_message(uint8_t* payload, node_id_t source, uint8_t msg_id, node_id_t destination, uint8_t routing_type, uint hops, uint16_t msg_id_num) {
00567         alg_messages++;
00568         coloring_message data;
00569         data.set_msg_id(msg_id);
00570         data.set_source(source);
00571         data.set_destination(destination);
00572         data.set_hops(hops);
00573         uint32_t id_nn = (uint32_t) source * 100 + msg_id;
00574         data.set_id_num(id_nn);
00575         if (routing_type == 0) {
00576             uint8_t len;
00577             if (msg_id == REP_CHANGE) {
00578                 len = sizeof (rep_c_change);
00579                 if (hops == 2) {
00580                     if(radio().id() == 41)
00581                     {
00582                     //rep_c_change rc = *((rep_c_change*) payload);
00583                     //cout<< "Estila to:" << rc.color_removed << " apo ton:" << rc.node_id << " me id:"<< data.id_num() << endl;
00584                     }
00585                 }
00586             } else {
00587                 len = sizeof (rep_sf);
00588             }
00589             data.set_payload(len, (uint8_t*) payload);
00590             //std::cout << "------Egw o:" << (int) source << " estila minima ston: " << (int) destination << " me id:" << (int) msg_id << " sto hop:" << hops << "mesw toy:"<< radio().id() << "kai payload:" << (int) (*((uint8_t*) data.payload())) << "\n";
00591             radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data);
00592         } else if (routing_type == 1) {
00593             data.set_payload((uint8_t)sizeof (ColorsSorted), (uint8_t*) payload);
00594          //   tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data);
00595         }
00596     }
00597 
00598 
00599     // -----------------------------------------------------------------------
00600 
00601     template<typename OsModel_P,
00602     typename Radio_P,
00603     typename Debug_P>
00604     uint16_t
00605     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00606     get_neighboors() {
00607 #ifdef DEBUG_TWOHOPSCOLORING
00608         uint32_t i = 0;
00609         //debug().debug("%d:My neighboors are the following:\n", radio().id());
00610         for (ColorTable_iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) {
00611             //std::cout << (int) it->first << "-";
00612 
00613             i = i + 1;
00614         }
00615 
00616         //std::cout << "END!!!\n";
00617 #endif
00618         return (uint16_t) neighboors_colors.size();
00619     }
00620 
00621 
00622 
00623     // -------------------------------
00624     // -----------------------------------------------------------------------
00625 
00626     template<typename OsModel_P,
00627     typename Radio_P,
00628     typename Debug_P>
00629     uint16_t
00630     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00631     get_color_nodes() {
00632 #ifdef DEBUG_TWOHOPSDCOLORING
00633         int i = 0;
00634         debug().debug("%d:The node-color diagram are the following:\n", radio().id());
00635         for (std::map<uint, uint>::iterator it = color_nodes.begin(); it != color_nodes.end(); it++) {
00636             std::cout << "Node:" << (int) it->first << "--Color:" << (int) it->second << "\n";
00637             i = i + 1;
00638         }
00639         //std::cout << '\n';
00640 #endif
00641         return (uint16_t) color_nodes.size();
00642     }
00643     // -----------------------------------------------------------------------
00644 
00645     template<typename OsModel_P,
00646     typename Radio_P,
00647     typename Debug_P>
00648     uint32_t
00649     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00650     satisfied(uint32_t i) {
00651         uint32_t greed_satisfied = 1;
00652         for (uint32_t x = 1; x < color_numbers.get_size(); x++) {
00653             color_under_cons = color_numbers.get_color_from_position(x);
00654             if ((color_numbers.get_value_from_position(x) >= color_numbers.get_value_from_color(color)) && (color_under_cons != color)) {
00655                 ForbiddenTable_iterator it = forbidden_colors.find(color_under_cons);
00656                 if (it == forbidden_colors.end()) {
00657                     greed_satisfied = 0;
00658                     /*
00659                      * Not Wiselib compatible: I/O streams
00660                     cout << "Egw o gamatos tupos::" << radio().id() << " mporw na paw apo xrwma::" << color << "se" << color_under_cons << " gia to round" << fb_round << endl;
00661                     */
00662                     debug().debug("Node %i: I can change color %d for %d in round %d\n",radio().id(),color,color_under_cons,fb_round);
00663                     break;
00664                 }
00665             } else {
00666                 if (x == (color_numbers.get_size() - 1))
00667                     //cout << "Egw o gamatos tupos::" << radio().id() << "Eimai komple gia to round" << fb_round << endl;
00668                 color_under_cons = -1;
00669             }
00670         }
00671         return greed_satisfied;
00672     }
00673 
00674     // -----------------------------------------------------------------------
00675 
00676     template<typename OsModel_P,
00677     typename Radio_P,
00678     typename Debug_P>
00679     void
00680     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00681     greed_move(uint32_t i) {
00682         /*
00683          * Not Wiselib compatible: I/O streams
00684         cout << "Egw o gamatos tupos::" << radio().id() << " mporw na paw apo xrwma::" << color << "se" << color_under_cons << " gia to round" << fb_round << endl;
00685         */
00686         debug().debug("Node %i: Changed color %d for %d in round %d\n",radio().id(),color,i,fb_round);
00687         
00688         color_numbers.remove(color);
00689         color = i;
00690         color_numbers.insert(color);
00691         idle_rounds = 0;
00692     }
00693 
00694     // -----------------------------------------------------------------------
00695 
00696     template<typename OsModel_P,
00697     typename Radio_P,
00698     typename Debug_P>
00699     void
00700     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00701     try_greed() {
00702         if (fb_round == greed_round) {
00703             if ((answerrs_count == ncount)) {
00704                 step++;
00705                 answerrs_count = 0;
00706                 if ((permission) && (!satisfied(0))) {
00707                     temp_color = color;
00708                     greed_move(color_under_cons);
00709                     m_color = color;
00710                 }
00711                 rep_c_change rc;
00712                 rc.color_assigned = m_color;
00713                 rc.color_removed = temp_color;
00714                 rc.hops = 1;
00715                 rc.node_id = (uint32_t) radio().id();
00716                 rc.change_round = change_round;
00717                 send_special_message((uint8_t*) & rc, radio().id(), REP_CHANGE, -1, 0, 1, 0);
00718                 greed_round++;
00719                 try_change();
00720             }
00721         }
00722     }
00723 
00724     // -----------------------------------------------------------------------
00725 
00726     template<typename OsModel_P,
00727     typename Radio_P,
00728     typename Debug_P>
00729     void
00730     TwoHopsColoring<OsModel_P, Radio_P, Debug_P>::
00731     try_change() {
00732         if (greed_round == change_round) {
00733             if ((answerrc_count == (ncount_all))) {
00734                 answerrc_count = 0;
00735                 change_round++;
00736                 step = 0;
00737                     timer().template set_timer<self_type, &self_type::timer_elapsed > (
00738                         200, this, 0);
00739                 //timer_elapsed(0);
00740                 
00741             }
00742 
00743         }
00744     }
00745 //     // -----------------------------------------------------------------------
00746 
00747     // -----------------------------------------------------------------------
00748 
00749 }
00750 #endif
00751 
00752 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines