Wiselib
|
00001 00002 #ifndef __ALGORITHMS_COLORING_JUDGED_COLORING_H__ 00003 #define __ALGORITHMS_COLORING_JUDGED_COLORING_H__ 00004 00005 #include "algorithms/coloring/judged/judged_coloring_message_types.h" 00006 #include "algorithms/routing/tree/tree_routing.h" 00007 #include "algorithms/routing/tora/tora_routing.h" 00008 #include "internal_interface/routing_table/routing_table_static_array.h" 00009 #include "internal_interface/coloring_table/color_table_map.h" 00010 #include "internal_interface/coloring_table/colors_sorted.h" 00011 #include <string.h> 00012 #include <string> 00013 #define DEBUG_JUDGEDCOLORING 00014 00015 namespace wiselib { 00016 #ifndef DATA_STRUCTS 00017 #define DATA_STRUCTS 00018 struct question { 00019 uint16_t who; // Poion rwtisame 00020 uint16_t type; 00021 uint16_t answer; // I apantisi. 00022 uint16_t answered; // An dothike apantis 0 h 1. Elegxetai alla itan gia tuxon dipla minimata 00023 uint16_t position; // thesi sto map color_nodes tou teleutaiou node pou rwtisame 00024 uint16_t con_no; // Arithmos sinexomenwn kombwn pou den allaksan xrwma. 00025 uint16_t curr_color_pos; // Position ston pinaka color_scores tou teleutaiou xrwmatos pou steilame 00026 }; 00027 00028 struct ask_color { // Domi poy stelnoume gia pollaplous dikastes 00029 int color; 00030 int priority; 00031 }; 00032 #endif 00033 00042 template<typename OsModel_P, 00043 typename Radio_P = typename OsModel_P::Radio, 00044 typename Debug_P = typename OsModel_P::Debug> 00045 class JudgedColoring { 00046 public: 00047 typedef OsModel_P OsModel; 00048 typedef Radio_P Radio; 00049 typedef Debug_P Debug; 00050 00051 typedef typename OsModel_P::Timer Timer; 00052 00053 typedef JudgedColoring<OsModel, Radio, Debug> self_type; 00054 typedef TreeRouting<OsModel, Timer, Radio, Debug> tree_route; 00055 00056 typedef wiselib::StaticArrayRoutingTable<OsModel, Radio, 8, wiselib::ToraRoutingTableValue<OsModel, Radio> > 00057 ToraRoutingTable; 00058 typedef ToraRouting<OsModel, ToraRoutingTable, Radio, Debug> tora_routing_t; 00059 typedef ToraRoutingMessage<OsModel_P, Radio> tora_message; 00060 typedef typename Radio::node_id_t node_id_t; 00061 typedef typename Radio::size_t size_t; 00062 typedef typename Radio::block_data_t block_data_t; 00063 00064 typedef typename Timer::millis_t millis_t; 00065 //Needed Message Types 00066 typedef JudgedColoringMessage<OsModel, Radio> coloring_message; 00067 typedef StlMapColorTable<OsModel, Radio, node_id_t> ColorTable; 00068 typedef ColorsTable<OsModel, Radio> ColorsSorted; 00069 00072 JudgedColoring(); 00073 ~JudgedColoring(); 00075 00078 void enable(void); 00079 void disable(void); 00081 00084 void timer_elapsed(void *userdata); 00086 00089 void in_game(void *userdata); 00091 00092 00095 void send(node_id_t receiver, size_t len, block_data_t *data); 00097 00100 void receive(node_id_t from, size_t len, block_data_t *data); 00102 00105 void judge_receive(coloring_message* message,uint8_t msg_id); 00107 00110 void node_receive(coloring_message* message,uint8_t msg_id); 00112 00115 void bootstrap_message(); 00117 00120 void broadcast_judge(uint8_t); 00122 00123 00125 void send_message(uint payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type); 00126 void send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type); 00127 00130 uint16_t get_neighboors(); 00132 00135 uint16_t get_color_nodes(); 00137 00138 int fm(int arr[], int b, int n) { 00139 int f = b; 00140 int 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(int arr[], int n) { 00150 int s, w; 00151 int 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 void set_judge() { 00162 is_judge = 1; 00163 judge = radio().id(); 00164 }; 00165 00166 inline int get_judge() { 00167 return judge; 00168 }; 00169 00170 inline int get_appointed_judges() { 00171 return j_ass; 00172 }; 00173 00174 inline int get_alg_messages(){ 00175 return alg_messages; 00176 } 00177 00178 inline int get_game_started() { 00179 return game_started; 00180 } 00181 00182 inline int get_color() { 00183 return color; 00184 } 00185 00186 inline int get_ended_all() { 00187 return ended_all; 00188 } 00189 00190 inline int get_ended_centralized() { 00191 return ended_centralized; 00192 } 00193 00194 inline void set_color(int color_) { 00195 color = color_; 00196 } 00197 00198 inline void set_world_size(int nc) { 00199 node_count = nc; 00200 } 00201 00202 inline void set_judge_count(int jc) { 00203 judge_count = jc; 00204 } 00205 00206 inline void set_max_color(int max_color_) { 00207 max_color = max_color_; 00208 } 00209 00210 inline uint16_t get_is_judge() { 00211 return is_judge; 00212 }; 00213 00214 inline uint16_t is_terminated() { 00215 return end; 00216 }; 00217 00218 inline uint16_t has_min_priority() { 00219 int has = 1; 00220 for (int i = 1; i < 20; i++) { 00221 if (judges[i] == -1)break; 00222 else if (judges[i] < radio().id()) 00223 has = 0; 00224 } 00225 return has; 00226 }; 00227 00228 inline uint16_t has_max_priority() { 00229 int has = 1; 00230 for (int i = 1; i < 20; i++) { 00231 if (judges[i] == -1)break; 00232 else if (judges[i] > radio().id()) 00233 has = 0; 00234 } 00235 return has; 00236 }; 00237 00238 inline uint16_t active_colors_size() { 00239 return color_scores.get_active_colors(); 00240 }; 00241 00242 inline uint16_t add_neighboor(uint neighboor_id, uint color_num) { 00243 neighboors_colors.insert(std::make_pair(neighboor_id, color_num)); 00244 }; 00245 00246 inline uint16_t add_node_color(uint node_id, uint color_num) { 00247 color_nodes.insert(std::make_pair(node_id, color_num)); 00248 }; 00249 00250 inline void try_start() { 00251 if(is_judge && !game_started){ 00252 if((c_ass == node_count - 1)&&(j_ass = judge_count - 1) && (has_max_priority())){ 00253 00254 coloring_message data; 00255 data.set_msg_id(CENT_END); 00256 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00257 if(is_judge) this->isort(judges,20); 00258 in_game(0); 00259 } 00260 if(c_ass == node_count -1) 00261 ended_centralized = 1; 00262 } 00263 }; 00264 00265 inline void change_node_color(uint node_id, uint color_num) { 00266 std::map<uint, uint>::iterator it = color_nodes.find(node_id); 00267 it->second = color_num; 00268 }; 00269 00270 inline void set_tora_routing(tora_routing_t* tora) { 00271 tora_routing = tora; 00272 }; 00273 00274 int compare(const void * a, const void * b){ 00275 if(*(int*)a==*(int*)b) return 0; 00276 else if (*(int*)a < *(int*)b) return 1; 00277 else return -1; 00278 } 00279 00280 inline void init_question() { 00281 lq.answered = 0; 00282 lq.position = 1; 00283 lq.answer = color_scores.get_color_from_position(lq.position); 00284 }; 00285 00286 void init( Radio& radio, Timer& timer, Debug& debug ) { 00287 radio_ = &radio; 00288 timer_ = &timer; 00289 debug_ = &debug; 00290 } 00291 00292 void destruct() { 00293 } 00294 00295 private: 00296 Radio& radio() 00297 { return *radio_; } 00298 00299 Timer& timer() 00300 { return *timer_; } 00301 00302 Debug& debug() 00303 { return *debug_; } 00304 00305 Radio * radio_; 00306 Timer * timer_; 00307 Debug * debug_; 00308 00309 int judge_suspended; 00310 int judges[20]; 00311 question lq; 00312 tora_routing_t *tora_routing; 00313 uint16_t permitted_count, answer_count, neigh_colors_count; 00314 uint8_t is_judge; 00315 uint8_t asked_color; 00316 int judge; 00317 int max_color; 00318 ColorTable neighboors_colors; 00319 ColorTable color_nodes; 00320 std::map<uint, uint>::iterator iter_color_nodes; 00321 ColorsSorted color_scores; 00322 int color, color_under_cons, game_started,alg_messages,end,node_count,judge_count,c_ass,j_ass,ended_centralized,ended_all; 00323 ask_color q_color; 00324 }; 00325 // ----------------------------------------------------------------------- 00326 // -----------Judged Coloring Constructor--------------------------------- 00327 // ----------------------------------------------------------------------- 00328 00329 template<typename OsModel_P, 00330 typename Radio_P, 00331 typename Debug_P> 00332 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00333 JudgedColoring() 00334 : is_judge(0), 00335 judge(-1), 00336 color(-1), 00337 max_color(2), 00338 asked_color(0), 00339 permitted_count(0), 00340 answer_count(0), 00341 game_started(0), 00342 judge_suspended(0), 00343 neigh_colors_count(0), 00344 alg_messages(0), 00345 end(0), 00346 node_count(0), 00347 c_ass(0), 00348 j_ass(0), 00349 judge_count(0), 00350 ended_centralized(0), 00351 ended_all(0) { 00352 }; 00353 // -----------Judged Coloring De-Constructor------------------------------ 00354 00355 template<typename OsModel_P, 00356 typename Radio_P, 00357 typename Debug_P> 00358 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00359 ~JudgedColoring() { 00360 00361 }; 00362 // ----------------------------------------------------------------------- 00363 00364 template<typename OsModel_P, 00365 typename Radio_P, 00366 typename Debug_P> 00367 void 00368 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00369 enable() { 00370 00371 for (int i = 0; i < 20; i++) judges[i] = -1; 00372 radio().enable_radio(); 00373 radio().template reg_recv_callback<self_type, &self_type::receive > (this); 00374 00375 if (is_judge) { 00376 color_scores.init(); 00377 timer().template set_timer<self_type, &self_type::timer_elapsed > ( 00378 501, this, 0); 00379 } 00380 bootstrap_message(); 00381 } 00382 // ----------------------------------------------------------------------- 00383 00384 template<typename OsModel_P, 00385 typename Radio_P, 00386 typename Debug_P> 00387 void 00388 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00389 disable(void) { 00390 #ifdef DEBUG_JUDGEDCOLORING 00391 debug().debug("JudgedColoring: Disable\n"); 00392 #endif 00393 00394 } 00395 // ----------------------------------------------------------------------- 00396 00397 template<typename OsModel_P, 00398 typename Radio_P, 00399 typename Debug_P> 00400 void 00401 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00402 timer_elapsed(void *userdata) { 00403 if (color == -1 && !is_judge && (asked_color < 1)) { 00404 timer().template set_timer<self_type, &self_type::timer_elapsed > ( 00405 0, this, 0); 00406 asked_color++; 00407 //send_message(radio().id(), radio().id(), REQUEST_COLOR, judge, 1); 00408 } else if (is_judge && asked_color < 1) { 00409 asked_color++; 00410 color_scores.insert(color); 00411 add_node_color(radio().id(), color); 00412 iter_color_nodes = color_nodes.begin(); 00413 lq.who = radio().id(); 00414 lq.con_no = 0; 00415 int i; 00416 this->isort(judges,20); 00417 } 00418 } 00419 // ----------------------------------------------------------------------- 00420 00421 template<typename OsModel_P, 00422 typename Radio_P, 00423 typename Debug_P> 00424 void 00425 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00426 in_game(void *userdata) { 00427 get_neighboors(); 00428 00429 game_started = 1; 00430 if (lq.con_no < (color_nodes.size())) { 00431 if (lq.who == radio().id()) { // I prwti fora pou mpainei einai o idios 00432 cout<< "This is the start..."<< endl; 00433 if (!lq.who)iter_color_nodes; 00434 if (iter_color_nodes->first == radio().id()) { 00435 if (++iter_color_nodes == color_nodes.end()) 00436 iter_color_nodes = color_nodes.begin(); 00437 } 00438 lq.who = iter_color_nodes->first; 00439 lq.curr_color_pos = color_scores.get_position_from_color(color_nodes.find(lq.who)->second); 00440 lq.con_no = 1; 00441 init_question(); 00442 lq.type = REASSIGN_COLOR; 00443 } else if (lq.answered) { 00444 if (lq.type == REASSIGN_COLOR) { 00445 if (lq.answer == 0) { // An i apantisti einai 0 den peirame xrwma 00446 lq.answered = 0; 00447 lq.position++; 00448 lq.answer = color_scores.get_color_from_position(lq.position); 00449 if ((lq.position == color_scores.get_size()) || (color_scores.get_value_from_position(lq.curr_color_pos) > color_scores.get_value_from_position(lq.position))) { 00450 lq.con_no++; 00451 if (++iter_color_nodes == color_nodes.end()) 00452 iter_color_nodes = color_nodes.begin(); 00453 if (iter_color_nodes->first == radio().id()) { 00454 if (++iter_color_nodes == color_nodes.end()) 00455 iter_color_nodes = color_nodes.begin(); 00456 } 00457 lq.who = iter_color_nodes->first; 00458 lq.curr_color_pos = color_scores.get_position_from_color(color_nodes.find(lq.who)->second); 00459 init_question(); 00460 } 00461 } else if (lq.answer == color_scores.get_color_from_position(lq.position)) { // An i apantisi einai to xrwma pou stilame 00462 lq.con_no = 1; 00463 color_scores.insert(lq.answer); 00464 color_scores.remove(color_nodes.find(lq.who)->second); 00465 change_node_color(lq.who, lq.answer); 00466 if ((++iter_color_nodes) == color_nodes.end()) 00467 iter_color_nodes = color_nodes.begin(); 00468 if (iter_color_nodes->first == radio().id()) { 00469 if (++iter_color_nodes == color_nodes.end()) 00470 iter_color_nodes = color_nodes.begin(); 00471 } 00472 lq.who = iter_color_nodes->first; 00473 lq.curr_color_pos = color_scores.get_position_from_color(color_nodes.find(lq.who)->second); 00474 init_question(); 00475 } 00476 } 00477 } 00478 00479 if (!lq.answered){ // mpikame tuxaia min steileis tipota ( prwti ekdosi i sinartisi etrexe me timer ) 00480 send_message((uint16_t) lq.answer, radio().id(), REASSIGN_COLOR, (uint16_t) lq.who, 1); 00481 cout<< "I am node = " << radio().id() << " Sending to " << lq.who <<endl; 00482 } 00483 } else if (((lq.con_no) == color_nodes.size())&&(judge_suspended == 0)) { // An exoume con_no == color_nodes.size() thn prwti fora 00484 send_message(-1,radio().id(),ASK_COLOR,-1,0); // Rwta tous geitones an exeis kalo xrwma 00485 judge_suspended = 1; 00486 } else if (judge_suspended == 1){ // Molis sou apantisoun elegkse to diko sou kai allakse en anagki 00487 int i = 0,ok = 1; 00488 while (( i < color_scores.get_size() ) && (color_scores.get_value_from_color(color) <= color_scores.get_value_from_position(i+1))) { 00489 int color_under_cons = color_scores.get_color_from_position(i+1); 00490 for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 00491 int neigh_id = (int)it->first; 00492 int neigh_color = neighboors_colors.find(neigh_id)->second; 00493 if((neigh_color == color_under_cons ) || (color == color_under_cons )) 00494 ok = 0; 00495 } 00496 if(ok){ 00497 color_scores.remove(color); 00498 color = color_under_cons; 00499 color_scores.insert(color_under_cons); 00500 color_nodes.find((uint)radio().id())->second = color; 00501 lq.con_no = 1; // An allakseis trekse kai pali olo ton algorithmo 00502 lq.who = radio().id(); 00503 judge_suspended = 0; 00504 timer().template set_timer<self_type, &self_type::in_game > ( 00505 10, this, 0); 00506 ok=1000; 00507 } 00508 if(ok!=1000) 00509 ok=1; 00510 else{ 00511 break; 00512 } 00513 i++; 00514 } 00515 if (ok != 1000) { // An den allakses tote teleiwse i seira sou steile ston epomeno an uparxei 00516 cout << "This is the end..." << endl; 00517 00518 int tmp = 0; 00519 this->isort(judges,20); 00520 for (int i = 0; i < 20; i++) { 00521 int a = radio().id(); 00522 int b = judges[i]; 00523 if ((judges[i] < radio().id()) && (judges[i] != -1)) { 00524 send_special_message((uint8_t*) & color_scores, radio().id(), TAKE_TURN, judges[i], 1); 00525 tmp = i; 00526 break; 00527 } 00528 00529 } 00530 if (tmp==0){ 00531 end = 1; 00532 ended_all = 1; 00533 coloring_message data; 00534 data.set_msg_id(ALL_END); 00535 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00536 } 00537 } 00538 } 00539 00540 if(color_nodes.size() == 1){ // An exeis mono ton eauto sou dwse tin skutali 00541 int tmp = 0; 00542 this->isort(judges,20); 00543 for(int i=0;i<20;i++){ 00544 int a = radio().id(); 00545 int b = judges[i]; 00546 if((judges[i] < radio().id()) && (judges[i]!=-1)){ 00547 send_special_message((uint8_t*)&color_scores,radio().id(),TAKE_TURN,judges[i],1); 00548 tmp = i; 00549 break; 00550 } 00551 } 00552 if (tmp==0){ 00553 end = 1; 00554 ended_all = 1; 00555 coloring_message data; 00556 data.set_msg_id(ALL_END); 00557 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00558 } 00559 } 00560 00561 } 00562 // ----------------------------------------------------------------------- 00563 00564 template<typename OsModel_P, 00565 typename Radio_P, 00566 typename Debug_P> 00567 void 00568 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00569 send(node_id_t destination, size_t len, block_data_t *data) { 00570 00571 } 00572 // ----------------------------------------------------------------------- 00573 00574 template<typename OsModel_P, 00575 typename Radio_P, 00576 typename Debug_P> 00577 void 00578 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00579 receive(node_id_t from, size_t len, block_data_t *data) { 00580 /*if (from == radio().id()) 00581 return; 00582 00583 uint8_t msg_id = *data; 00584 uint source, payload, msg_id2; 00585 coloring_message *message; 00586 tora_message *t_message; 00587 message = (coloring_message *) data; 00588 t_message = (tora_message *) data; 00589 00590 00591 if (((msg_id == 194) || ((msg_id > 2) && (msg_id < 10)))) { 00592 if (msg_id == 194) 00593 message = (coloring_message *) t_message->payload(); 00594 } 00595 00596 */ 00597 00598 if (from == radio().id()) 00599 return; 00600 00601 uint8_t msg_id = *data; 00602 uint destination, source, payload, msg_id2; 00603 coloring_message *message; 00604 //tree_message *t_message; 00605 tora_message *t_message; 00606 message = (coloring_message *) data; 00607 t_message = (tora_message *) data; 00608 00609 if (((msg_id == 194) || ((msg_id > 2) && (msg_id < 10)))) { 00610 if (msg_id == 194) 00611 message = (coloring_message *) t_message->payload(); 00612 destination = message->destination(); 00613 if ((radio().id() == destination)) { 00614 source = message->source(); 00615 if (message->msg_id() != ASK_PERMISSION) { 00616 payload = *((uint*) message->payload()); 00617 } else { 00618 payload = *((uint*) (message->payload())); 00619 } 00620 msg_id2 = message->msg_id(); 00621 } 00622 } 00623 00624 if ((radio().id() == destination) && (0 < msg_id2 < 11) ) 00625 std::cout << " Egw o:" << destination << " elaba minima apo ton: " << source << " me id:" << msg_id2 << "kai payload:" << payload << "\n"; 00626 00627 if (msg_id == JUDGE_START) { 00628 message = (coloring_message *) data; 00629 add_neighboor(message->source(), 0); 00630 broadcast_judge(message->source()); 00631 } else if (msg_id == NODE_START) { 00632 message = (coloring_message *) data; 00633 add_neighboor(message->source(), 0); 00634 } else if (msg_id == BROAD_JUDGE) { 00635 message = (coloring_message *) data; 00636 broadcast_judge(*((uint*) message->payload())); 00637 } else if (msg_id == ASK_COLOR){ 00638 send_message(color,radio().id(),REPLY_COLOR,message->source(),0); 00639 } else if(msg_id == CENT_END) { 00640 if(!ended_centralized){ 00641 ended_centralized = 1; 00642 coloring_message data; 00643 data.set_msg_id(CENT_END); 00644 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00645 } 00646 } else if (msg_id == ALL_END) { 00647 if(!ended_all){ 00648 ended_all = 1; 00649 coloring_message data; 00650 data.set_msg_id(ALL_END); 00651 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00652 } 00653 }else if ((msg_id == REPLY_COLOR) && (message->destination() == radio().id())){ 00654 payload = *((uint*) message->payload()); 00655 std::map<uint, uint>::iterator it = neighboors_colors.find((int) message->source()); 00656 if (it == neighboors_colors.end()) 00657 neighboors_colors.insert(std::make_pair(message->source(), (uint) payload)); 00658 else it->second = payload; 00659 int neigh_no = get_neighboors(); 00660 if(neigh_colors_count != neigh_no){ 00661 neigh_colors_count++; 00662 } 00663 if(neigh_colors_count == neigh_no){ 00664 neigh_colors_count = 0; 00665 if(is_judge) in_game(0); 00666 } 00667 }else if (is_judge) { 00668 if (msg_id == 194) {// If the messages are in tree_routing type get the color_message from payload 00669 message = (coloring_message *) t_message->payload(); 00670 } 00671 judge_receive(message,msg_id); 00672 00673 } else if ((!is_judge) || (judge_suspended==1)) { 00674 if (msg_id == 194) // If the messages are in tree_routing type get the color_message from payload 00675 message = (coloring_message *) t_message->payload(); 00676 node_receive(message,msg_id); 00677 } 00678 00679 } 00680 // ----------------------------------------------------------------------- 00681 00682 template<typename OsModel_P, 00683 typename Radio_P, 00684 typename Debug_P> 00685 void 00686 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00687 bootstrap_message() { 00688 int type; 00689 if (this->is_judge) { 00690 type = JUDGE_START; 00691 judges[0] = radio().id(); 00692 } else if (!this->is_judge) { 00693 type = NODE_START; 00694 } 00695 send_message(-1, radio().id(), type, -1, 0); 00696 00697 } 00698 // ----------------------------------------------------------------------- 00699 00700 template<typename OsModel_P, 00701 typename Radio_P, 00702 typename Debug_P> 00703 void 00704 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00705 broadcast_judge(uint8_t judge_id) { 00706 int got_it_before = 0, i; 00707 for (i = 0; i < 20; i++) { 00708 if (judges[i] == judge_id) { 00709 got_it_before = 1; 00710 break; 00711 } else if (judges[i] == -1) { 00712 judges[i] = judge_id; 00713 j_ass++; 00714 if(is_judge) try_start(); 00715 break; 00716 } 00717 } 00718 00719 if (got_it_before == 0) { 00720 cout << "Egw o " << radio().id() << " acknowledge judge:" << judge_id << endl; 00721 if (judge == -1) { 00722 00723 judge = judge_id; 00724 if (!is_judge) { 00725 send_message(radio().id(), radio().id(), REQUEST_COLOR, judge_id, 1); 00726 00727 } 00728 00729 00730 } else if (is_judge) { 00731 send_message(radio().id(), radio().id(), -1, judge_id, 1); 00732 } 00733 00734 send_message(judge_id, radio().id(), BROAD_JUDGE, -1, 0); 00735 } 00736 00737 } 00738 // ----------------------------------------------------------------------- 00739 00740 template<typename OsModel_P, 00741 typename Radio_P, 00742 typename Debug_P> 00743 void 00744 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00745 send_message(uint payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type) { 00746 coloring_message data; 00747 data.set_msg_id(msg_id); 00748 data.set_source(source); 00749 data.set_destination(destination); 00750 data.set_payload(sizeof (uint), (uint8_t*) & payload); 00751 if (routing_type == 0) { 00752 alg_messages++; 00753 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00754 } else if (routing_type == 1) { 00755 std::cout << " Egw o:" << (int) source << " estila minima ston: " << (int) destination << " me id:" << (int) msg_id << "kai payload:" << (int) *((uint*) data.payload()) << "\n"; 00756 tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 00757 } 00758 } 00759 00760 // ----------------------------------------------------------------------- 00761 00762 template<typename OsModel_P, 00763 typename Radio_P, 00764 typename Debug_P> 00765 void 00766 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00767 send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type) { 00768 coloring_message data; 00769 data.set_msg_id(msg_id); 00770 data.set_source(source); 00771 data.set_destination(destination); 00772 00773 if (routing_type == 0) { 00774 alg_messages++; 00775 data.set_payload(sizeof (ask_color), (uint8_t*) payload); 00776 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00777 } else if (routing_type == 1) { 00778 std::cout << " Egw o:" << (int) source << " estila minima ston: " << (int) destination << " me id:" << (int) msg_id << "kai payload:" << (int) *((uint*) data.payload()) << "\n"; 00779 data.set_payload((uint8_t) sizeof (ColorsSorted), (uint8_t*) payload); 00780 tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 00781 } 00782 } 00783 00784 00785 // ----------------------------------------------------------------------- 00786 00787 template<typename OsModel_P, 00788 typename Radio_P, 00789 typename Debug_P> 00790 uint16_t 00791 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00792 get_neighboors() { 00793 #ifdef DEBUG_JUDGEDCOLORING 00794 int i = 0; 00795 //debug().debug("%d:My neighboors are the following:\n", radio().id()); 00796 for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 00797 //std::cout << (int) it->first << "-"; 00798 i = i + 1; 00799 } 00800 //std::cout << "END!!!\n"; 00801 #endif 00802 return (uint16_t) neighboors_colors.size(); 00803 } 00804 00805 00806 00807 // ------------------------------- 00808 // ----------------------------------------------------------------------- 00809 00810 template<typename OsModel_P, 00811 typename Radio_P, 00812 typename Debug_P> 00813 uint16_t 00814 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00815 get_color_nodes() { 00816 #ifdef DEBUG_JUDGEDCOLORING 00817 int i = 0; 00818 //debug().debug("%d:The node-color diagram are the following:\n", radio().id()); 00819 for (std::map<uint, uint>::iterator it = color_nodes.begin(); it != color_nodes.end(); it++) { 00820 //std::cout << "Node:" << (int) it->first << "--Color:" << (int) it->second << "\n"; 00821 i = i + 1; 00822 } 00823 //std::cout << '\n'; 00824 #endif 00825 return (uint16_t) color_nodes.size(); 00826 } 00827 00828 // ----------------------------------------------------------------------- 00829 00830 template<typename OsModel_P, 00831 typename Radio_P, 00832 typename Debug_P> 00833 void 00834 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00835 node_receive(coloring_message* message,uint8_t msg_id) { 00836 int payload = 0; 00837 int tmp = message->msg_id(); 00838 if((message->source() == judge) && (message->destination() == radio().id())) 00839 cout << "I " << radio().id() << " received message from judge:" << endl; 00840 if (message->msg_id() != ASK_PERMISSION) { 00841 payload = *((uint*) message->payload()); 00842 } else { 00843 q_color = *((ask_color*) message->payload()); 00844 } 00845 if (message->msg_id() == ASSIGN_COLOR && message->destination() == radio().id() && (payload != color)) { 00846 color = (int) payload; 00847 if (msg_id == ASSIGN_COLOR) { 00848 send_message(color, (uint16_t) radio().id(), COLOR_ASSIGNED, judge, 0); 00849 } else { 00850 send_message(color, (uint16_t) radio().id(), COLOR_ASSIGNED, judge, 1); 00851 } 00852 } else if (message->msg_id() == REASSIGN_COLOR && message->destination() == radio().id() && (answer_count < 1)) { 00853 00854 if ((payload != color) && (payload != color_under_cons)) { 00855 answer_count = 0; 00856 permitted_count = 0; 00857 q_color.color = payload; 00858 q_color.priority = judge; 00859 send_special_message((uint8_t*) & q_color, radio().id(), ASK_PERMISSION, -1, 0); 00860 color_under_cons = payload; 00861 00862 } else { 00863 send_message(payload, radio().id(), COLOR_NOT_ASSIGNED, judge, 1); 00864 } 00865 } else if ((message->msg_id() == ASK_PERMISSION)) { 00866 if ((q_color.color == color) && (q_color.priority <= judge)) { 00867 send_message((uint) q_color.color, radio().id(), DECL_PERMISSION, message->source(), 0); 00868 00869 } else { 00870 send_message((uint) q_color.color, radio().id(), GIVE_PERMISSION, message->source(), 0); 00871 } 00872 } else if (((message->msg_id() == GIVE_PERMISSION) || (message->msg_id() == DECL_PERMISSION)) && message->destination() == radio().id() && color_under_cons != 0) { 00873 if (payload == color_under_cons) { 00874 answer_count++; 00875 if (message->msg_id() == GIVE_PERMISSION) { 00876 permitted_count++; 00877 } 00878 if (answer_count == neighboors_colors.size()) { 00879 if (answer_count == permitted_count) { 00880 color = color_under_cons; 00881 color_under_cons = 0; 00882 if (!is_judge) { 00883 send_message(color, radio().id(), COLOR_ASSIGNED, judge, 1); 00884 } else { 00885 in_game(0); 00886 } 00887 } else { 00888 if (!is_judge) { 00889 send_message(color_under_cons, radio().id(), COLOR_NOT_ASSIGNED, judge, 1); 00890 } else { 00891 lq.answer = color; 00892 judge_suspended = 3; 00893 in_game(0); 00894 } 00895 color_under_cons = 0; 00896 } 00897 answer_count = 0; 00898 } 00899 } 00900 } else if (message->msg_id() == REASSIGN_COLOR && message->destination() == radio().id()) { 00901 } 00902 00903 } 00904 00905 // ----------------------------------------------------------------------- 00906 00907 template<typename OsModel_P, 00908 typename Radio_P, 00909 typename Debug_P> 00910 void 00911 JudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00912 judge_receive(coloring_message* message,uint8_t msg_id) { 00913 int payload = 0; 00914 if ((message->msg_id() == TAKE_TURN) && (message->destination() == radio().id())) { 00915 ColorsSorted cn_temp = *((ColorsSorted*) message->payload()); 00916 for (int i = 0; i < cn_temp.get_size(); i++) { 00917 int color_tmp = cn_temp.get_color_from_position(i + 1); 00918 int count_tmp = cn_temp.get_value_from_color(color_tmp); 00919 for (int j = 0; j < count_tmp; j++) { 00920 color_scores.insert(color_tmp); 00921 } 00922 } 00923 } 00924 if (message->msg_id() != ASK_PERMISSION) { 00925 payload = *((uint*) message->payload()); 00926 } else { 00927 q_color = *((ask_color*) message->payload()); 00928 } 00929 if ((message->msg_id() == TAKE_TURN) && (message->destination() == radio().id())) { 00930 in_game(0); 00931 } else if (((message->msg_id() == COLOR_ASSIGNED) || (message->msg_id() == COLOR_NOT_ASSIGNED)) && (message->source() == lq.who) && (message->destination() == radio().id())) { 00932 if ((payload > 0) && (message->msg_id() == COLOR_ASSIGNED)) { 00933 std::map<uint, uint>::iterator it = color_nodes.find((int) message->source()); 00934 if (it == color_nodes.end()) { 00935 color_nodes.insert(std::make_pair(message->source(), (uint) payload)); 00936 color_scores.insert((uint) payload); 00937 00938 } else { 00939 00940 if ((it->second != payload) && (payload == color_scores.get_color_from_position(lq.position))) { 00941 lq.answered = 1; 00942 lq.answer = (uint) payload; 00943 in_game(0); 00944 00945 } 00946 } 00947 } else if (message->msg_id() == COLOR_NOT_ASSIGNED) { 00948 if (((uint) message->source() == (int) lq.who) && ((uint) payload == (int) lq.answer)) { 00949 lq.answered = 1; 00950 lq.answer = 0; 00951 in_game(0); 00952 } 00953 } 00954 }else if((message->msg_id() == COLOR_ASSIGNED) && (message->destination() == radio().id())){ 00955 c_ass++; 00956 try_start(); 00957 } else if ((message->msg_id() == REQUEST_COLOR) && (message->destination() == radio().id())) { 00958 int routing_type; 00959 if (msg_id == 194) 00960 routing_type = 1; 00961 else routing_type = 0; 00962 00963 std::map<uint, uint>::iterator it = color_nodes.find((uint) (message->source())); 00964 if (it == color_nodes.end()) { 00965 add_node_color(message->source(), max_color); 00966 color_scores.insert(max_color); 00967 send_message(max_color, radio().id(), ASSIGN_COLOR, (uint16_t) (message->source()), routing_type); 00968 max_color++; 00969 00970 } else { 00971 send_message((uint) it->second, radio().id(), ASSIGN_COLOR, (uint16_t) (message->source()), routing_type); 00972 } 00973 } else if (message->msg_id() == ASK_PERMISSION) { 00974 uint permission = 1; 00975 if ((q_color.color == color) && (q_color.priority <= radio().id())) 00976 permission = 0; 00977 if (permission) { 00978 send_message((uint) q_color.color, radio().id(), GIVE_PERMISSION, message->source(), 0); 00979 } else { 00980 send_message((uint) q_color.color, radio().id(), DECL_PERMISSION, message->source(), 0); 00981 } 00982 } 00983 00984 } 00985 00986 // ----------------------------------------------------------------------- 00987 00988 // ----------------------------------------------------------------------- 00989 00990 } 00991 #endif 00992 00993