Wiselib
|
00001 #ifndef __ALGORITHMS_COLORING_RAND_H__ 00002 #define __ALGORITHMS_COLORING_RAND_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 "algorithms/routing/tora/tora_routing_message.h" 00008 // #include "internal_interface/coloring_table/color_table_map.h" 00009 // #include "internal_interface/coloring_table/colors_sorted.h" 00010 #include <string.h> 00011 #include <string> 00012 #include <stdlib.h> 00013 #include <ctime> 00014 #include <math.h> 00015 00016 00017 #define DEBUG_RANDCOLORING 00018 00019 namespace wiselib { 00020 00021 #define DATA_STRUCTS 00022 00023 struct rrep_c_change { 00024 int color_assigned; 00025 int color_removed; 00026 int hops; 00027 int node_id; 00028 int change_round; 00029 }; 00030 00031 struct rrep_fb { 00032 int color_foridden; 00033 int fb_round; 00034 }; 00035 00036 struct rrep_sf { 00037 int satisfaction; 00038 int node_id; 00039 int sf_round; 00040 }; 00050 template<typename OsModel_P, 00051 typename Radio_P = typename OsModel_P::Radio, 00052 typename Debug_P = typename OsModel_P::Debug> 00053 class RandColoring { 00054 public: 00055 typedef OsModel_P OsModel; 00056 typedef Radio_P Radio; 00057 typedef Debug_P Debug; 00058 typedef typename OsModel_P::Timer Timer; 00059 typedef RandColoring<OsModel, Radio, Debug> self_type; 00060 typedef wiselib::StaticArrayRoutingTable<OsModel, Radio, 8, wiselib::ToraRoutingTableValue<OsModel, Radio> > 00061 ToraRoutingTable; 00062 typedef ToraRouting<OsModel, ToraRoutingTable, Radio, Debug> tora_routing_t; 00063 typedef ToraRoutingMessage<OsModel_P, Radio> tora_message; 00064 typedef typename Radio::node_id_t node_id_t; 00065 typedef typename Radio::size_t size_t; 00066 typedef typename Radio::block_data_t block_data_t; 00067 typedef typename Timer::millis_t millis_t; 00068 //Needed Message Types 00069 typedef TwoHopsMessage<OsModel, Radio> coloring_message; 00070 typedef StlMapColorTable<OsModel, Radio, node_id_t> ColorTable; 00071 typedef StlMapColorTable<OsModel, Radio, node_id_t> ForbiddenTable; 00072 typedef ColorsTable<OsModel, Radio> ColorsSorted; 00073 00076 RandColoring(); 00077 ~RandColoring(); 00079 00082 void enable(void); 00083 void disable(void); 00085 00088 void timer_elapsed(void *userdata); 00090 00093 void resend_requests(void *userdata); 00095 00098 void receive(node_id_t from, size_t len, block_data_t *data); 00100 00103 int satisfied(int i); 00105 00108 void greed_move(int i); 00110 00113 void try_greed(); 00115 00118 void try_fb(); 00120 00123 void try_change(); 00125 00128 void bootstrap_message(); 00130 00133 void send_color_requests(); 00135 00136 void send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type, uint hops, uint16_t msg_id_num); 00137 00140 uint16_t get_neighboors(); 00142 00145 uint16_t get_color_nodes(); 00147 00148 int fm(int arr[], int b, int n) { 00149 int f = b; 00150 int c; 00151 00152 for (c = b + 1; c < n; c++) 00153 if (arr[c] > arr[f]) 00154 f = c; 00155 00156 return f; 00157 } 00158 00159 void isort(int arr[], int n) { 00160 int s, w; 00161 int sm; 00162 00163 for (s = 0; s < n - 1; s++) { 00164 w = fm(arr, s, n); 00165 sm = arr[w]; 00166 arr[w] = arr[s]; 00167 arr[s] = sm; 00168 } 00169 } 00170 00171 inline int get_alg_messages(){ 00172 return alg_messages; 00173 } 00174 00175 inline int get_color() { 00176 return color; 00177 } 00178 00179 inline void set_color(int color_) { 00180 color = color_; 00181 } 00182 00183 inline int get_network_size(){ 00184 return net_size; 00185 } 00186 00187 inline void set_network_size(int net_size_){ 00188 net_size = net_size_; 00189 } 00190 00191 inline uint16_t active_colors_size() { 00192 return color_numbers.get_active_colors(); 00193 }; 00194 00195 inline uint16_t add_neighboor(uint neighboor_id, uint color_num) { 00196 neighboors_colors.insert(std::make_pair(neighboor_id, color_num)); 00197 }; 00198 00199 inline bool is_in_messages(uint node_id, uint payload) { 00200 std::map<uint, uint>::iterator it = messages.find(node_id); 00201 if (it == messages.end()) { 00202 messages.insert(std::make_pair(node_id, payload)); 00203 return false; 00204 } else { 00205 if (it->second == payload) { 00206 return true; 00207 } else { 00208 int msgRC = node_id * 100 + 21; 00209 int msgRS = node_id * 100 + 22; 00210 int msgRF = node_id * 100 + 23; 00211 if(((payload != msgRS) && (it->second == msgRF )) || 00212 ((payload != msgRC) && (it->second == msgRS )) || 00213 ((payload != msgRF) && (it->second == msgRC ))){ 00214 return true; 00215 }else{ 00216 it->second = payload; 00217 return false; 00218 } 00219 } 00220 } 00221 }; 00222 00223 inline void print_metrics(){ 00224 //cout << "--------------------------" << endl; 00225 cout << "Eimai o :" << radio().id() << " me rec_count:" << rec_colors_count << " kai nodes_num:" << node_selection.size() << " kai fb_round:" << fb_round << " kai fb_ans:" << answerfb_count << " kai neigh:" << neighboors_colors.size()<<endl; 00226 if(neighboors_colors.size() != forbidden_colors.size()){ 00227 for(std::map<uint,uint>::iterator it=neighboors_colors.begin();it != neighboors_colors.end();it++){ 00228 int exist = 0; 00229 for(std::map<uint,uint>::iterator it21=forbidden_colors.begin();it21 != forbidden_colors.end();it21++){ 00230 if(it->first == it21->second) exist = 1; 00231 } 00232 if(!exist) cout<< radio().id() << ": Exw na dilwsw oti mou leipei tou:" << it->first << endl; 00233 } 00234 } 00235 //cout << "Eimai o :" << radio().id() << " me greed_round:" << greed_round << " kai rs_answer_count" << answerrs_count << endl; 00236 //cout << "Eimai o :" << radio().id() << " me change_round:" << change_round << " kai change_answer_count" << answerrc_count << endl; 00237 } 00238 inline uint16_t add_node_color(uint node_id, uint color_num) { 00239 color_nodes.insert(std::make_pair(node_id, color_num)); 00240 }; 00241 00242 inline uint16_t add_forbidden_color(int color,int neigh) { 00243 forbidden_colors.insert(std::make_pair(color, neigh)); 00244 }; 00245 00246 inline void change_node_color(uint node_id, uint color_num) { 00247 std::map<uint, uint>::iterator it = color_nodes.find(node_id); 00248 it->second = color_num; 00249 }; 00250 00251 inline void set_tora_routing(tora_routing_t* tora) { 00252 tora_routing = tora; 00253 }; 00254 00255 inline void set_diameter(int diam){ 00256 diameter = diam; 00257 } 00258 00259 inline void do_links(){ 00260 for(int i=0;i<net_size;i++){ 00261 int u = 0; 00262 send_special_message((uint8_t*)&u,radio().id(),0,i,1,0,0); 00263 } 00264 } 00265 00266 int compare(const void * a, const void * b) { 00267 if (*(int*) a == *(int*) b) return 0; 00268 else if (*(int*) a < *(int*) b) return 1; 00269 else return -1; 00270 } 00271 00272 void init( Radio& radio, Timer& timer, Debug& debug ) { 00273 radio_ = &radio; 00274 timer_ = &timer; 00275 debug_ = &debug; 00276 } 00277 00278 void destruct() { 00279 } 00280 00281 private: 00282 Radio& radio() 00283 { return *radio_; } 00284 00285 Timer& timer() 00286 { return *timer_; } 00287 00288 Debug& debug() 00289 { return *debug_; } 00290 00291 Radio * radio_; 00292 Timer * timer_; 00293 Debug * debug_; 00294 00295 tora_routing_t *tora_routing; 00296 uint16_t permission, answerfb_count, answerrs_count, answerrc_count, neigh_colors_count; 00297 uint8_t asked_color; 00298 ColorTable node_selection,color_nodes, neighboors_colors, messages; 00299 std::map<uint, uint>::iterator iter_color_nodes; 00300 ColorsSorted color_numbers; 00301 ForbiddenTable forbidden_colors; 00302 int color, m_color, temp_color, step, ncount, ncount_all, color_under_cons,rec_colors_count; 00303 int greed_round, change_round, fb_round, round_satisfaction,diameter,idle_rounds; 00304 int net_size,alg_messages; 00305 }; 00306 // ----------------------------------------------------------------------- 00307 // -----------Rand Coloring Constructor--------------------------------- 00308 // ----------------------------------------------------------------------- 00309 00310 template<typename OsModel_P, 00311 typename Radio_P, 00312 typename Debug_P> 00313 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00314 RandColoring() 00315 : step(0), 00316 ncount(0), 00317 permission(1), 00318 temp_color(-1), 00319 ncount_all(0), 00320 color_under_cons(0), 00321 answerfb_count(0), 00322 answerrs_count(0), 00323 answerrc_count(0), 00324 fb_round(0), 00325 greed_round(1), 00326 change_round(2), 00327 idle_rounds(0), 00328 rec_colors_count(0), 00329 alg_messages(0){ 00330 }; 00331 // -----------Rand Coloring De-Constructor------------------------------ 00332 00333 template<typename OsModel_P, 00334 typename Radio_P, 00335 typename Debug_P> 00336 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00337 ~RandColoring() { 00338 00339 }; 00340 // ----------------------------------------------------------------------- 00341 00342 template<typename OsModel_P, 00343 typename Radio_P, 00344 typename Debug_P> 00345 void 00346 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00347 enable() { 00348 radio().enable_radio(); 00349 radio().template reg_recv_callback<self_type, &self_type::receive > (this); 00350 timer().template set_timer<self_type, &self_type::timer_elapsed > ( 00351 500, this, 0); 00352 color = (int) (radio().id()) + 1; 00353 color_numbers.init(); 00354 color_numbers.insert(color); 00355 bootstrap_message(); 00356 do_links(); 00357 00358 00359 } 00360 // ----------------------------------------------------------------------- 00361 00362 template<typename OsModel_P, 00363 typename Radio_P, 00364 typename Debug_P> 00365 void 00366 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00367 disable(void) { 00368 #ifdef DEBUG_RANDCOLORING 00369 debug().debug("RandColoring: Disable\n"); 00370 #endif 00371 // cout << "Eimai o" << radio().id() << ". To teliko mou xrwma einai:" << get_color << endl; 00372 } 00373 // ----------------------------------------------------------------------- 00374 00375 template<typename OsModel_P, 00376 typename Radio_P, 00377 typename Debug_P> 00378 void 00379 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00380 timer_elapsed(void *userdata) { 00381 step = 1; 00382 ncount_all = get_neighboors(); 00383 forbidden_colors.clear(); 00384 temp_color = 0; 00385 permission = 1; 00386 color_under_cons = 0; 00387 m_color = 0; 00388 00389 idle_rounds++; 00390 node_selection.clear(); 00391 color_numbers.empty(); 00392 send_color_requests(); 00393 00394 } 00395 00396 // ----------------------------------------------------------------------- 00397 00398 template<typename OsModel_P, 00399 typename Radio_P, 00400 typename Debug_P> 00401 void 00402 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00403 receive(node_id_t from, size_t len, block_data_t *data) { 00404 if (from == radio().id()) 00405 return; 00406 uint8_t msg_id = *data; 00407 coloring_message *message; 00408 tora_message *t_message; 00409 message = (coloring_message *) data; 00410 t_message = (tora_message *) data; 00411 00412 if (msg_id == 194) {// If the messages are in tree_routing type get the color_message from payload 00413 message = (coloring_message *) t_message->payload(); 00414 int non_tora_id = message->msg_id(); 00415 int destination = message->destination(); 00416 if (destination == radio().id()) { 00417 if (non_tora_id == REQ_COLOR) { 00418 if (message->source() == 2) cout << "Egw o:" << radio().id() << "pira request apo ton 2" << endl; 00419 send_special_message((uint8_t*) & color, radio().id(), REP_COLOR, message->source(), 1, 0, 0); 00420 } else if (non_tora_id == REP_COLOR) { 00421 if ((node_selection.find(message->source()))->second == -1) { 00422 uint color_rec = (uint) *((uint8_t*) message->payload()); 00423 if (radio().id() == 2) cout << "Egw o 2 pira apantisi apo ton:" << message->source() << endl; 00424 node_selection[message->source()] = color_rec; 00425 color_numbers.insert(color_rec); 00426 rec_colors_count++; 00427 if (rec_colors_count == node_selection.size()) { 00428 cout << radio().id() << ":Ela re paidaki mou." << endl; 00429 if (idle_rounds < (800 * diameter + 1)) { 00430 rrep_fb fb; 00431 fb.color_foridden = color; 00432 fb.fb_round = fb_round; 00433 send_special_message((uint8_t*) & fb, radio().id(), REP_FBID, -1, 0, 1, 0); 00434 } else { 00435 cout << "Baresa liksi: " << radio().id(); 00436 } 00437 try_fb(); 00438 step = 1; 00439 } 00440 } 00441 } 00442 } 00443 } else { 00444 uint hops = message->hops(); 00445 uint8_t* payload; 00446 uint pay_tmp = *((uint*) message->payload()); 00447 payload = (uint8_t*) & pay_tmp; 00448 rrep_c_change rc = *((rrep_c_change*) message->payload()); 00449 if (!is_in_messages(message->source(), message->id_num())) { 00450 00451 if (msg_id == NODE_STARTED) { 00452 message = (coloring_message *) data; 00453 uint load = (uint) *((uint8_t*) message->payload()); 00454 std::map<uint, uint>::iterator it = neighboors_colors.find(load); 00455 if (it == neighboors_colors.end()) { 00456 if (load != radio().id()) { 00457 add_neighboor(load, load + 1); 00458 color_numbers.insert(load + 1); 00459 } 00460 } 00461 if (hops == 1) { 00462 ncount++; 00463 } 00464 } else if (msg_id == REP_CHANGE) { 00465 00466 rrep_c_change rc = *((rrep_c_change*) message->payload()); 00467 if ((rc.node_id != radio().id()) && (hops == 1)) { 00468 if (rc.change_round == change_round) { 00469 answerrc_count++; 00470 if ((rc.color_assigned != 0) && (rc.node_id != radio().id())) { 00471 color_numbers.insert(rc.color_assigned); 00472 color_numbers.remove(rc.color_removed); 00473 idle_rounds = 0; 00474 } 00475 if ((answerrc_count == ncount_all) && (fb_round > 14)) 00476 ; //cout << "edw" << endl; 00477 if (answerrc_count > (ncount_all)) { 00478 int me = radio().id(); 00479 cout << "I am:" << me << " and it is Problem RECHANGE BIG TIME" << endl; 00480 } 00481 try_change(); 00482 } else { 00483 cout << "Egw o::" << radio().id() << " PIRA AKURO CHANGE MINIMA ROUND" << rc.change_round << " cr:" << change_round << endl; 00484 } 00485 } 00486 } else if (msg_id == REP_SAT) { 00487 if (hops == 1) { 00488 00489 00490 rrep_sf sf = *((rrep_sf*) message->payload()); 00491 if (sf.sf_round == greed_round) { 00492 answerrs_count++; 00493 if (sf.node_id > radio().id()) { 00494 if (sf.satisfaction == 0) { 00495 permission = 0; 00496 } 00497 } 00498 if (answerrs_count > (ncount)) { 00499 int me = radio().id(); 00500 cout << "I am:" << me << " and it is Problem REPSAT BIG TIME" << endl; 00501 } 00502 if (radio().id() == 1) 00503 try_greed(); 00504 else 00505 try_greed(); 00506 } else { 00507 cout << "AKURO GREED MINIMA" << endl; 00508 } 00509 } 00510 } else if (msg_id == REP_FBID) { 00511 if (hops == 1) { 00512 rrep_fb fb = *((rrep_fb*) message->payload()); 00513 if (fb.fb_round == fb_round) { 00514 answerfb_count++; 00515 00516 std::map<uint, uint>::iterator it = forbidden_colors.find(fb.color_foridden); 00517 if (it == forbidden_colors.end()) { 00518 add_forbidden_color(fb.color_foridden,message->source()); 00519 } 00520 if (answerfb_count > (ncount)) { 00521 int me = radio().id(); 00522 cout << "I am:" << me << " and it is Problem REPFBID BIG TIME" << endl; 00523 } 00524 try_fb(); 00525 } else { 00526 cout << "Egw o::" << radio().id() << " PIRA AKURO FB MINIMA" << fb.fb_round << " cr:" << fb_round << endl; 00527 } 00528 } 00529 } 00530 } else { 00531 //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; 00532 } 00533 } 00534 00535 } 00536 // ----------------------------------------------------------------------- 00537 00538 template<typename OsModel_P, 00539 typename Radio_P, 00540 typename Debug_P> 00541 void 00542 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00543 bootstrap_message() { 00544 #ifdef DEBUG_RANDCOLORING 00545 //debug().debug("Node number %d sends bootstrap message\n", radio().id()); 00546 #endif 00547 int type; 00548 uint8_t payload = radio().id(); 00549 type = NODE_STARTED; 00550 send_special_message(&payload, radio().id(), type, -1, 0, 1, 0); 00551 00552 } 00553 00554 // ----------------------------------------------------------------------- 00555 00556 template<typename OsModel_P, 00557 typename Radio_P, 00558 typename Debug_P> 00559 void 00560 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00561 send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type, uint hops, uint16_t msg_id_num) { 00562 coloring_message data; 00563 data.set_msg_id(msg_id); 00564 data.set_source(source); 00565 data.set_destination(destination); 00566 data.set_hops(hops); 00567 int id_nn = (int) source * 100 + msg_id; 00568 data.set_id_num(id_nn); 00569 if (routing_type == 0) { 00570 alg_messages++; 00571 uint8_t len; 00572 if (msg_id == REP_CHANGE) { 00573 len = sizeof (rrep_c_change); 00574 if (hops == 2) { 00575 if(radio().id() == 41) 00576 { 00577 //rrep_c_change rc = *((rrep_c_change*) payload); 00578 //cout<< "Estila to:" << rc.color_removed << " apo ton:" << rc.node_id << " me id:"<< data.id_num() << endl; 00579 } 00580 } 00581 } else { 00582 len = sizeof (rrep_sf); 00583 } 00584 data.set_payload(len, (uint8_t*) payload); 00585 //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"; 00586 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00587 } else if (routing_type == 1) { 00588 data.set_payload(sizeof(uint),payload); 00589 tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 00590 } 00591 } 00592 00593 00594 // ----------------------------------------------------------------------- 00595 00596 template<typename OsModel_P, 00597 typename Radio_P, 00598 typename Debug_P> 00599 uint16_t 00600 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00601 get_neighboors() { 00602 #ifdef DEBUG_RANDCOLORING 00603 // int i = 0; 00604 //debug().debug("%d:My neighboors are the following:\n", radio().id()); 00605 //for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 00606 // std::cout << (int) it->first << "-"; 00607 00608 // i = i + 1; 00609 // } 00610 00611 // std::cout << "END!!!\n"; 00612 #endif 00613 return (uint16_t) neighboors_colors.size(); 00614 } 00615 00616 00617 00618 // ------------------------------- 00619 // ----------------------------------------------------------------------- 00620 00621 template<typename OsModel_P, 00622 typename Radio_P, 00623 typename Debug_P> 00624 uint16_t 00625 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00626 get_color_nodes() { 00627 #ifdef DEBUG_RANDDCOLORING 00628 int i = 0; 00629 debug().debug("%d:The node-color diagram are the following:\n", radio().id()); 00630 for (std::map<uint, uint>::iterator it = color_nodes.begin(); it != color_nodes.end(); it++) { 00631 std::cout << "Node:" << (int) it->first << "--Color:" << (int) it->second << "\n"; 00632 i = i + 1; 00633 } 00634 //std::cout << '\n'; 00635 #endif 00636 return (uint16_t) color_nodes.size(); 00637 } 00638 // ----------------------------------------------------------------------- 00639 00640 template<typename OsModel_P, 00641 typename Radio_P, 00642 typename Debug_P> 00643 int 00644 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00645 satisfied(int i) { 00646 int greed_satisfied = 1; 00647 for (int x = 1; x < color_numbers.get_size(); x++) { 00648 color_under_cons = color_numbers.get_color_from_position(x); 00649 if ((color_numbers.get_value_from_position(x) >= color_numbers.get_value_from_color(color)) && (color_under_cons != color)) { 00650 std::map<uint, uint>::iterator it = forbidden_colors.find(color_under_cons); 00651 if (it == forbidden_colors.end()) { 00652 greed_satisfied = 0; 00653 cout << "Egw o gamatos tupos::" << radio().id() << " mporw na paw apo xrwma::" << color << "se" << color_under_cons << " gia to round" << fb_round << endl; 00654 break; 00655 } 00656 } else { 00657 if (x == (color_numbers.get_size() - 1)) 00658 //cout << "Egw o gamatos tupos::" << radio().id() << "Eimai komple gia to round" << fb_round << endl; 00659 color_under_cons = -1; 00660 } 00661 } 00662 return greed_satisfied; 00663 } 00664 00665 // ----------------------------------------------------------------------- 00666 00667 template<typename OsModel_P, 00668 typename Radio_P, 00669 typename Debug_P> 00670 void 00671 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00672 greed_move(int i) { 00673 cout << "Egw o gamatos tupos::" << radio().id() << " allaksa xrwma apo::" << color << "se" << i << " gia to round" << fb_round << endl; 00674 color_numbers.remove(color); 00675 color = i; 00676 color_numbers.insert(color); 00677 idle_rounds = 0; 00678 } 00679 00680 // ----------------------------------------------------------------------- 00681 00682 template<typename OsModel_P, 00683 typename Radio_P, 00684 typename Debug_P> 00685 void 00686 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00687 try_fb() { 00688 if (rec_colors_count == node_selection.size()) { 00689 if ((answerfb_count == ncount)) { 00690 step++; 00691 answerfb_count = 0; 00692 int sat = satisfied(radio().id()); 00693 rrep_sf sf; 00694 sf.satisfaction = sat; 00695 sf.sf_round = greed_round; 00696 sf.node_id = radio().id(); 00697 rec_colors_count = 0; 00698 send_special_message((uint8_t*) & sf, radio().id(), REP_SAT, -1, 0, 1, 0); 00699 fb_round++; 00700 try_greed(); 00701 } 00702 } 00703 } 00704 00705 // ----------------------------------------------------------------------- 00706 00707 template<typename OsModel_P, 00708 typename Radio_P, 00709 typename Debug_P> 00710 void 00711 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00712 try_greed() { 00713 if (fb_round == greed_round) { 00714 if ((answerrs_count == ncount)) { 00715 step++; 00716 answerrs_count = 0; 00717 if ((permission) && (!satisfied(0))) { 00718 temp_color = color; 00719 greed_move(color_under_cons); 00720 m_color = color; 00721 } 00722 rrep_c_change rc; 00723 rc.color_assigned = m_color; 00724 rc.color_removed = temp_color; 00725 rc.hops = 1; 00726 rc.node_id = (int) radio().id(); 00727 rc.change_round = change_round; 00728 send_special_message((uint8_t*) & rc, radio().id(), REP_CHANGE, -1, 0, 1, 0); 00729 greed_round++; 00730 try_change(); 00731 } 00732 } 00733 } 00734 00735 // ----------------------------------------------------------------------- 00736 00737 template<typename OsModel_P, 00738 typename Radio_P, 00739 typename Debug_P> 00740 void 00741 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00742 try_change() { 00743 if (greed_round == change_round) { 00744 if ((answerrc_count == (ncount_all))) { 00745 answerrc_count = 0; 00746 change_round++; 00747 step = 0; 00748 cout << "Egw o:" << radio().id() << "-----Ksekinaw ton round------------" << change_round << endl; 00749 //if (change_round <= 38) 00750 timer().template set_timer<self_type, &self_type::timer_elapsed > ( 00751 200, this, 0); 00752 //timer_elapsed(0); 00753 00754 } 00755 00756 } 00757 } 00758 00759 // ----------------------------------------------------------------------- 00760 00761 template<typename OsModel_P, 00762 typename Radio_P, 00763 typename Debug_P> 00764 void 00765 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00766 send_color_requests() { 00767 srand((unsigned) time(0)); 00768 int random_integer; 00769 int lowest = 0, highest = net_size-1; 00770 int range = (highest - lowest); 00771 int n_of_nodes = (int)log2(net_size); 00772 if(n_of_nodes==0) n_of_nodes = 1; 00773 int index = 0; 00774 int n_of_nodes_to_ask; 00775 if((net_size-neighboors_colors.size()) >= n_of_nodes) 00776 n_of_nodes_to_ask = n_of_nodes; 00777 else 00778 n_of_nodes_to_ask = net_size-neighboors_colors.size(); 00779 while(index < n_of_nodes_to_ask) 00780 { 00781 random_integer = rand()%net_size; 00782 if((random_integer>49)||(random_integer<0)) cout << "OOOOXIIIIIIIIIII" << endl; 00783 if(neighboors_colors.find(random_integer) == neighboors_colors.end()){ 00784 if(node_selection.find(random_integer) == node_selection.end()){ 00785 if(radio().id() != random_integer){ 00786 node_selection.insert(std::make_pair(random_integer,-1)); 00787 index++; 00788 int payload = -1; 00789 if(radio().id()==2) cout << "Egw o 2 estila request ston:" << random_integer << endl; 00790 send_special_message((uint8_t*)&payload,radio().id(),REQ_COLOR,random_integer,1,0,0); 00791 } 00792 } 00793 } 00794 } 00795 timer().template set_timer<self_type, &self_type::resend_requests > ( 00796 500, this, 0); 00797 00798 } 00799 00800 // ----------------------------------------------------------------------- 00801 00802 template<typename OsModel_P, 00803 typename Radio_P, 00804 typename Debug_P> 00805 void 00806 RandColoring<OsModel_P, Radio_P, Debug_P>:: 00807 resend_requests(void *userdata) { 00808 int existed = 0; 00809 for(std::map<uint,uint>::iterator it=node_selection.begin(); it!=node_selection.end();it++) 00810 { 00811 if(it->second == -1){ 00812 existed = 1; 00813 int payload = -1; 00814 //if(radio().id()==2) 00815 cout << radio().id() << " :Egw ksana-estila request ston:" << it->first << endl; 00816 send_special_message((uint8_t*)&payload,radio().id(),REQ_COLOR,it->first,1,0,0); 00817 } 00818 if(existed){ 00819 // timer().template set_timer<self_type, &self_type::resend_requests > ( 00820 // 5000, this, 0); 00821 ; 00822 } 00823 } 00824 00825 } 00826 // ----------------------------------------------------------------------- 00827 00828 // ----------------------------------------------------------------------- 00829 00830 } 00831 #endif 00832 00833