Wiselib
|
00001 00002 #ifndef __ALGORITHMS_COLORING_PKJUDGED_COLORING_H__ 00003 #define __ALGORITHMS_COLORING_PKJUDGED_COLORING_H__ 00004 00005 #include "algorithms/coloring/judged/judged_coloring.h" 00006 #include "algorithms/coloring/judged/judged_coloring_message_types.h" 00007 #include "algorithms/routing/tora/tora_routing.h" 00008 #include "algorithms/routing/tree/tree_routing.h" 00009 #include "internal_interface/routing_table/routing_table_static_array.h" 00010 #include "internal_interface/coloring_table/color_table_map.h" 00011 #include "internal_interface/coloring_table/colors_sorted.h" 00012 #include <string.h> 00013 #include <string> 00014 00015 #define DEBUG_PKJUDGEDCOLORING 00016 00017 namespace wiselib { 00018 #ifndef DATA_STRUCTS 00019 #define DATA_STRUCTS 00020 00021 struct question { 00022 uint16_t who; 00023 uint16_t type; 00024 uint16_t answer; 00025 uint16_t answered; 00026 uint16_t position; 00027 uint16_t con_no; 00028 uint16_t curr_color_pos; 00029 }; 00030 00031 struct ask_color { 00032 int color; 00033 int priority; 00034 }; 00035 #endif 00036 00045 template<typename OsModel_P, 00046 typename Radio_P = typename OsModel_P::Radio, 00047 typename Debug_P = typename OsModel_P::Debug> 00048 class PKJudgedColoring { 00049 public: 00050 typedef OsModel_P OsModel; 00051 typedef Radio_P Radio; 00052 typedef Debug_P Debug; 00053 00054 typedef typename OsModel_P::Timer Timer; 00055 00056 typedef PKJudgedColoring<OsModel, Radio, Debug> self_type; 00057 typedef TreeRouting<OsModel, Timer, Radio, Debug> tree_route; 00058 00059 typedef wiselib::StaticArrayRoutingTable<OsModel, Radio, 8, wiselib::ToraRoutingTableValue<OsModel, Radio> > 00060 ToraRoutingTable; 00061 typedef ToraRouting<OsModel, ToraRoutingTable, Radio, Debug> tora_routing_t; 00062 typedef ToraRoutingMessage<OsModel_P, Radio> tora_message; 00063 typedef typename Radio::node_id_t node_id_t; 00064 typedef typename Radio::size_t size_t; 00065 typedef typename Radio::block_data_t block_data_t; 00066 00067 typedef typename Timer::millis_t millis_t; 00068 //Needed Message Types 00069 typedef TreeRoutingMessage<OsModel, Radio> tree_message; 00070 typedef TreeBroadcastMessage<OsModel, Radio> tree_broad_message; 00071 typedef JudgedColoringMessage<OsModel, Radio> coloring_message; 00072 typedef StlMapColorTable<OsModel, Radio, node_id_t> ColorTable; 00073 typedef ColorsTable<OsModel, Radio> ColorsSorted; 00074 00077 PKJudgedColoring(); 00078 ~PKJudgedColoring(); 00080 00083 void enable(void); 00084 void disable(void); 00086 00089 void timer_elapsed(void *userdata); 00091 00094 void in_game(void *userdata); 00096 00097 00098 00101 void send(node_id_t receiver, size_t len, block_data_t *data); 00103 00106 void receive(node_id_t from, size_t len, block_data_t *data); 00108 00111 void bootstrap_message(); 00113 00116 void broadcast_judge(uint8_t); 00118 00119 00120 void send_message(uint payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type); 00121 00122 00123 void send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type); 00124 00127 uint16_t get_neighboors(); 00129 00132 uint16_t get_color_nodes(); 00134 00135 int fm(int arr[], int b, int n) { 00136 int f = b; 00137 int c; 00138 00139 for (c = b + 1; c < n; c++) 00140 if (arr[c] > arr[f]) 00141 f = c; 00142 00143 return f; 00144 } 00145 00146 void isort(int arr[], int n) { 00147 int s, w; 00148 int sm; 00149 00150 for (s = 0; s < n - 1; s++) { 00151 w = fm(arr, s, n); 00152 sm = arr[w]; 00153 arr[w] = arr[s]; 00154 arr[s] = sm; 00155 } 00156 } 00157 00158 inline void set_judge() { 00159 is_judge = 1; 00160 judge = radio().id(); 00161 }; 00162 00163 inline int get_judge() { 00164 return judge; 00165 }; 00166 00167 inline int get_color() { 00168 return color; 00169 } 00170 00171 inline int get_alg_messages(){ 00172 return alg_messages; 00173 } 00174 00175 inline void set_color(int color_) { 00176 color = color_; 00177 } 00178 00179 inline void set_max_color(int max_color_) { 00180 max_color = max_color_; 00181 } 00182 00183 inline uint16_t get_is_judge() { 00184 return is_judge; 00185 }; 00186 00187 inline uint16_t has_min_priority() { 00188 int has = 1; 00189 for (int i = 1; i < 20; i++) { 00190 if (judges[i] == -1)break; 00191 else if (judges[i] < radio().id()) 00192 has = 0; 00193 } 00194 return has; 00195 }; 00196 00197 inline uint16_t active_colors_size() { 00198 color_numbers.print(); 00199 return color_numbers.get_active_colors(); 00200 }; 00201 00202 inline void set_judge_count(int jc) { 00203 judge_count = jc; 00204 }; 00205 00206 inline int get_appointed_judges() { 00207 return j_ass; 00208 }; 00209 00210 inline uint16_t add_neighboor(uint neighboor_id, uint color_num) { 00211 neighboors_colors.insert(std::make_pair(neighboor_id, color_num)); 00212 }; 00213 00214 inline uint16_t add_node_color(uint node_id, uint color_num) { 00215 color_nodes.insert(std::make_pair(node_id, color_num)); 00216 try_start(); 00217 }; 00218 00219 inline void set_world_size(int nc) { 00220 node_count = nc; 00221 }; 00222 00223 inline int get_game_started() { 00224 return game_started; 00225 }; 00226 00227 inline void try_start() { 00228 if(is_judge && !game_started){ 00229 if((c_ass == node_count - 1)&&(j_ass = judge_count - 1) ){ 00230 if(is_judge) this->isort(judges,20); 00231 in_game(0); 00232 } 00233 } 00234 }; 00235 00236 inline uint16_t has_max_priority() { 00237 int has = 1; 00238 for (int i = 1; i < 20; i++) { 00239 if (judges[i] == -1)break; 00240 else if (judges[i] > radio().id()) 00241 has = 0; 00242 } 00243 return has; 00244 }; 00245 00246 inline int get_ended_all() { 00247 return ended_all; 00248 }; 00249 00250 inline uint16_t is_terminated() { 00251 return end; 00252 }; 00253 00254 inline void change_node_color(uint node_id, uint color_num) { 00255 std::map<uint, uint>::iterator it = color_nodes.find(node_id); 00256 it->second = color_num; 00257 }; 00258 00259 inline void change_neigh_color(uint node_id, uint color_num) { 00260 std::map<uint, uint>::iterator it = neighboors_colors.find(node_id); 00261 if(it != neighboors_colors.end()) 00262 it->second = color_num; 00263 }; 00264 00265 inline void set_tree_routing(tree_route* tree) { 00266 tree_routing = tree; 00267 }; 00268 00269 inline void set_tora_routing(tora_routing_t* tora) { 00270 tora_routing = tora; 00271 }; 00272 00273 int compare(const void * a, const void * b) { 00274 if (*(int*) a == *(int*) b) return 0; 00275 else if (*(int*) a < *(int*) b) return 1; 00276 else return -1; 00277 } 00278 00279 inline void init_question() { 00280 last_question.answered = 0; 00281 last_question.position = 1; 00282 last_question.answer = color_numbers.get_color_from_position(last_question.position); 00283 }; 00284 00285 void init( Radio& radio, Timer& timer, Debug& debug ) { 00286 radio_ = &radio; 00287 timer_ = &timer; 00288 debug_ = &debug; 00289 } 00290 00291 void destruct() { 00292 } 00293 00294 private: 00295 Radio& radio() 00296 { return *radio_; } 00297 00298 Timer& timer() 00299 { return *timer_; } 00300 00301 Debug& debug() 00302 { return *debug_; } 00303 00304 Radio * radio_; 00305 Timer * timer_; 00306 Debug * debug_; 00307 00308 int judge_suspended, baton, last_herd_count,checked_nodes,round; 00309 int judges[20]; 00310 question last_question; 00311 tree_route *tree_routing; 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,c_ass_help; 00320 std::map<uint, uint>::iterator iter_color_nodes; 00321 ColorsSorted color_numbers; 00322 int color, color_under_cons, game_started,end,node_count,judge_count,c_ass,j_ass; 00323 ask_color q_color; 00324 int exeption,alg_messages,ended_all; 00325 }; 00326 // ----------------------------------------------------------------------- 00327 // -----------Judged Coloring Constructor--------------------------------- 00328 // ----------------------------------------------------------------------- 00329 00330 template<typename OsModel_P, 00331 typename Radio_P, 00332 typename Debug_P> 00333 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00334 PKJudgedColoring() 00335 : is_judge(0), 00336 judge(-1), 00337 color(-1), 00338 max_color(2), 00339 asked_color(0), 00340 permitted_count(0), 00341 answer_count(0), 00342 game_started(0), 00343 judge_suspended(0), 00344 neigh_colors_count(0), 00345 baton(0), 00346 last_herd_count(0), 00347 checked_nodes(1), 00348 round(0), 00349 exeption(0), 00350 alg_messages(0), 00351 end(0), 00352 node_count(0), 00353 judge_count(0), 00354 ended_all(0), 00355 c_ass(0), 00356 j_ass(0){ 00357 }; 00358 // -----------Judged Coloring De-Constructor------------------------------ 00359 00360 template<typename OsModel_P, 00361 typename Radio_P, 00362 typename Debug_P> 00363 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00364 ~PKJudgedColoring() { 00365 00366 }; 00367 // ----------------------------------------------------------------------- 00368 00369 template<typename OsModel_P, 00370 typename Radio_P, 00371 typename Debug_P> 00372 void 00373 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00374 enable() { 00375 00376 for (int i = 0; i < 20; i++) judges[i] = -1; 00377 radio().enable_radio(); 00378 radio().template reg_recv_callback<self_type, &self_type::receive > (this); 00379 00380 if (is_judge) { 00381 color_numbers.init(); 00382 timer().template set_timer<self_type, &self_type::timer_elapsed > ( 00383 501, this, 0); 00384 } 00385 bootstrap_message(); 00386 00387 00388 00389 } 00390 // -------------------------------------tiemr---------------------------------- 00391 00392 template<typename OsModel_P, 00393 typename Radio_P, 00394 typename Debug_P> 00395 void 00396 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00397 disable(void) { 00398 #ifdef DEBUG_PKJUDGEDCOLORING 00399 debug().debug("PKJudgedColoring: Disable\n"); 00400 #endif 00401 } 00402 // ----------------------------------------------------------------------- 00403 00404 template<typename OsModel_P, 00405 typename Radio_P, 00406 typename Debug_P> 00407 void 00408 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00409 timer_elapsed(void *userdata) { 00410 if (color == -1 && !is_judge && (asked_color < 1)) { 00411 //timer().template set_timer<self_type, &self_type::timer_elapsed > ( 00412 // 50, this, 0); 00413 asked_color++; 00414 //send_message(radio().id(), radio().id(), REQUEST_COLOR, judge, 1); 00415 // cout << "I:" << (int) radio().id() << "sent color request to:" << judge << endl; 00416 } else if (is_judge && asked_color < 1) { 00417 asked_color++; 00418 color_numbers.insert(color); 00419 add_node_color(radio().id(), color); 00420 iter_color_nodes = color_nodes.begin(); 00421 last_question.who = radio().id(); 00422 last_question.con_no = 0; 00423 int i; 00424 this->isort(judges, 20); 00425 if(radio().id()==judges[0]) 00426 round = round + 1; 00427 try_start(); 00428 #ifdef DEBUG_PKJUDGEDCOLORING 00429 debug().debug("Node %d Judge Started his game\n", radio().id()); 00430 #endif 00431 00432 00433 00434 } 00435 } 00436 // ----------------------------------------------------------------------- 00437 00438 template<typename OsModel_P, 00439 typename Radio_P, 00440 typename Debug_P> 00441 void 00442 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00443 in_game(void *userdata) { 00444 get_neighboors(); 00445 // timer().template set_timer<self_type, &self_type::in_game > ( 00446 // 50, this, 0); 00447 if(radio().id() == 7) 00448 int uio = 1; 00449 game_started = 1; 00450 if ((last_question.con_no < (color_nodes.size())) || (exeption)) { 00451 if (last_question.who == radio().id()) { 00452 get_color_nodes(); 00453 color_numbers.print(); 00454 if (!last_question.who)iter_color_nodes; 00455 if(round && baton) iter_color_nodes = color_nodes.begin(); 00456 //cout << "Problem:" << color_nodes.size() <<endl; 00457 if (iter_color_nodes->first == radio().id()) { 00458 if (++iter_color_nodes == color_nodes.end()) 00459 iter_color_nodes = color_nodes.begin(); 00460 } 00461 last_question.who = iter_color_nodes->first; 00462 00463 last_question.curr_color_pos = color_numbers.get_position_from_color(color_nodes.find(last_question.who)->second); 00464 last_question.con_no = 1; 00465 init_question(); 00466 last_question.type = REASSIGN_COLOR; 00467 } else if (last_question.answered) { 00468 if (last_question.type == REASSIGN_COLOR) { 00469 if (last_question.answer == 0) { 00470 // std::cout << "O kombos:" << (int)last_question.who << "Apantise" << "\n"; 00471 00472 last_question.answered = 0; 00473 last_question.position++; 00474 last_question.answer = color_numbers.get_color_from_position(last_question.position); 00475 if (((last_question.position == color_numbers.get_size()) || 00476 (color_numbers.get_value_from_position(last_question.curr_color_pos) > color_numbers.get_value_from_position(last_question.position))) 00477 && ((round==0) || (radio().id() == judges[0]))) { 00478 last_question.con_no++; 00479 //std::cout << radio().id() << "----CON NOOOOOOOOOOOO:" << last_question.con_no << "\n"; 00480 if (++iter_color_nodes == color_nodes.end()) 00481 iter_color_nodes = color_nodes.begin(); 00482 if (iter_color_nodes->first == radio().id()) { 00483 if (++iter_color_nodes == color_nodes.end()) 00484 iter_color_nodes = color_nodes.begin(); 00485 } 00486 last_question.who = iter_color_nodes->first; 00487 last_question.curr_color_pos = color_numbers.get_position_from_color(color_nodes.find(last_question.who)->second); 00488 init_question(); 00489 //if(last_question.con_no==99) cout << "After Print" << endl; 00490 00491 } else if((last_question.position >= color_numbers.get_size() 00492 || (color_numbers.get_value_from_position(last_question.curr_color_pos) < color_numbers.get_value_from_position(last_question.position))) 00493 &&((round == 1) && (radio().id() != judges[0]))){ 00494 last_question.answered = 0; 00495 last_question.position++; 00496 last_question.answer = ++max_color; 00497 } 00498 00499 } else if ((last_question.answer == color_numbers.get_color_from_position(last_question.position)) || ( ((round) && (radio().id() != judges[0]) ) && (last_question.answer == max_color) )) { 00500 //std::cout << "O kombos:" << (int)last_question.who << "Apantise" << "\n"; 00501 //std::cout << "tou arese to xrwma "<< (int)last_question.answer <<" omws \n"; 00502 last_question.con_no = 1; 00503 color_numbers.insert(last_question.answer); 00504 color_numbers.remove(color_nodes.find(last_question.who)->second); 00505 if(round && (radio().id() != judges[0])) { 00507 00508 change_node_color(last_question.who, last_question.answer); 00509 change_neigh_color(last_question.who, last_question.answer); 00510 if ((++iter_color_nodes) == color_nodes.end()){ 00511 last_question.con_no = color_nodes.size(); 00512 exeption=0; 00513 } 00514 } else { 00515 change_node_color(last_question.who, last_question.answer); 00516 if ((++iter_color_nodes) == color_nodes.end()) 00517 iter_color_nodes = color_nodes.begin(); 00518 if (iter_color_nodes->first == radio().id()) { 00519 if (++iter_color_nodes == color_nodes.end()) 00520 iter_color_nodes = color_nodes.begin(); 00521 } 00522 } 00523 00524 if(last_question.con_no != color_nodes.size()){ 00525 last_question.who = iter_color_nodes->first; 00526 last_question.curr_color_pos = color_numbers.get_position_from_color(color_nodes.find(last_question.who)->second); 00527 init_question(); 00528 }else{ 00529 in_game(0); 00530 } 00531 //color_numbers.print(); 00532 } 00533 00534 } 00535 } 00536 if (!last_question.answered) 00537 send_message((uint16_t) last_question.answer, radio().id(), REASSIGN_COLOR, (uint16_t) last_question.who, 1); 00538 } else if (((last_question.con_no) == color_nodes.size()) && (judge_suspended == 0)) { 00539 send_message(-1, radio().id(), ASK_COLOR, -1, 0); 00540 judge_suspended = 1; 00541 } else if ((judge_suspended == 1) || (judge_suspended == 4)) { 00542 int i = 0, ok = 1; 00543 if(judge_suspended != 4){ 00544 while (((i < color_numbers.get_size()) && ((color_numbers.get_value_from_color(color) <= color_numbers.get_value_from_position(i + 1)))) || ((round) && (radio().id()!=judges[0]))) { 00545 if(radio().id()==15) 00546 cout << endl; 00547 int color_under_cons; 00548 if(round){ 00549 int ioio = radio().id(); 00550 00551 } 00552 if(i < color_numbers.get_size()) 00553 color_under_cons = color_numbers.get_color_from_position(i + 1); 00554 else 00555 color_under_cons = ++max_color; 00556 for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 00557 int neigh_id = (int) it->first; 00558 int neigh_color = neighboors_colors.find(neigh_id)->second; 00559 if ((neigh_color == color_under_cons) || (color == color_under_cons)) 00560 ok = 0; 00561 } 00562 if (ok) { 00563 color_numbers.remove(color); 00564 color = color_under_cons; 00565 color_numbers.insert(color_under_cons); 00566 color_nodes.find((uint) radio().id())->second = color; 00567 if(!round)last_question.con_no = 1; 00568 last_question.who = radio().id(); 00569 if(round) 00570 judge_suspended = 4; 00571 else 00572 judge_suspended = 0; 00573 timer().template set_timer<self_type, &self_type::in_game > ( 00574 10, this, 0); 00575 ok = 1000; 00576 } 00577 if (ok != 1000) 00578 ok = 1; 00579 else { 00580 break; 00581 } 00582 i++; 00583 } 00584 }else{ 00585 ok = 1; 00586 } 00587 00588 if (ok != 1000) { 00589 cout<< "Helloooooooooooooooooooooooo:" << radio().id() << endl; 00590 00591 if (round == 1) { 00592 int tmp = 0; 00593 this->isort(judges,20); 00594 int b; 00595 for (int i = 0; i < 20; i++) { 00596 int a = radio().id(); 00597 b = judges[i]; 00598 if ((judges[i] < radio().id()) && (judges[i] != -1)) { 00599 send_special_message((uint8_t*) & color_numbers, radio().id(), TAKE_TURN, judges[i], 1); 00600 b = judges[i]; 00601 tmp=i; 00602 break; 00603 } 00604 } 00605 if (tmp==0 && baton){ 00606 end = 1; 00607 ended_all = 1; 00608 coloring_message data; 00609 data.set_msg_id(ALL_END); 00610 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00611 } 00612 cout << "Egw o server:" << radio().id() << " edwsa thn sktali ston:" << b << endl; 00613 round = 0; 00614 } 00615 if (baton) { 00616 for (std::map<uint, uint>::iterator it = color_nodes.begin(); it != color_nodes.end(); it++) { 00617 if (radio().id() != it->first) 00618 send_message(-1, radio().id(), FINAL_CHECK,it->first, 1); 00619 } 00620 last_herd_count = color_nodes.size(); 00621 color_nodes.clear(); 00622 baton = 0; 00623 } 00624 00625 } 00626 } 00627 if ((color_nodes.size() == 1) && ((round) || (baton) || (radio().id() == judges[0]))) { 00628 int b; 00629 int tmp = 0; 00630 this->isort(judges,20); 00631 for (int i = 0; i < 20; i++) { 00632 int a = radio().id(); 00633 00634 if ((judges[i] < radio().id()) && (judges[i] != -1)) { 00635 b = judges[i]; 00636 send_special_message((uint8_t*) & color_numbers, radio().id(), TAKE_TURN, judges[i], 1); 00637 tmp =i; 00638 break; 00639 } 00640 } 00641 if (tmp==0 && baton){ 00642 end = 1; 00643 ended_all = 1; 00644 coloring_message data; 00645 data.set_msg_id(ALL_END); 00646 radio().send(, radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00647 } 00648 color = 1000 * radio().id(); 00649 cout << "Egw o server:" << radio().id() << " edwsa thn sktali ston:" << b << endl; 00650 if(radio().id() == judges[0]) round = 0; 00651 baton = 0; 00652 00653 } 00654 00655 } 00656 // ----------------------------------------------------------------------- 00657 00658 template<typename OsModel_P, 00659 typename Radio_P, 00660 typename Debug_P> 00661 void 00662 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00663 send(node_id_t destination, size_t len, block_data_t *data) { 00664 00665 } 00666 // ----------------------------------------------------------------------- 00667 00668 template<typename OsModel_P, 00669 typename Radio_P, 00670 typename Debug_P> 00671 void 00672 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00673 receive(node_id_t from, size_t len, block_data_t *data) { 00674 if (from == radio().id()) 00675 return; 00676 00677 uint8_t msg_id = *data; 00678 uint destination, source, payload, msg_id2; 00679 coloring_message *message; 00680 //tree_message *t_message; 00681 tora_message *t_message; 00682 message = (coloring_message *) data; 00683 t_message = (tora_message *) data; 00684 00685 00686 if (((msg_id == 194) || ((msg_id > 2) && (msg_id < 10)))) { 00687 if (msg_id == 194) 00688 message = (coloring_message *) t_message->payload(); 00689 destination = message->destination(); 00690 if ((radio().id() == destination)) { 00691 source = message->source(); 00692 if (message->msg_id() != ASK_PERMISSION) { 00693 payload = *((uint*) message->payload()); 00694 } else { 00695 payload = *((uint*) (message->payload())); 00696 } 00697 msg_id2 = message->msg_id(); 00698 } 00699 } 00700 00701 if ((radio().id() == destination) && (0 < msg_id2 < 11) ) 00702 // std::cout << " Egw o:" << destination << " elaba minima apo ton: " << source << " me id:" << msg_id2 << "kai payload:" << payload << "\n"; 00703 00704 if(message->source() == radio().id()) 00705 return; 00706 00707 if (msg_id == JUDGE_START) { 00708 message = (coloring_message *) data; 00709 add_neighboor(message->source(), 0); 00710 broadcast_judge(message->source()); 00711 } else if (msg_id == NODE_START) { 00712 message = (coloring_message *) data; 00713 add_neighboor(message->source(), 0); 00714 } else if (msg_id == BROAD_JUDGE) { 00715 message = (coloring_message *) data; 00716 broadcast_judge(*((uint*) message->payload())); 00717 } else if (msg_id == ASK_COLOR) { 00718 send_message(color, radio().id(), REPLY_COLOR, message->source(), 0); 00719 //cout << "Elaba ASK_COLOR apo ton:" << message->source() << endl; 00720 } else if (msg_id == ALL_END) { 00721 if(!ended_all){ 00722 ended_all = 1; 00723 coloring_message data; 00724 data.set_msg_id(ALL_END); 00725 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00726 } 00727 }else if ((msg_id == REPLY_COLOR) && (message->destination() == radio().id())) { 00728 payload = *((uint*) message->payload()); 00729 std::map<uint, uint>::iterator it = neighboors_colors.find((int) message->source()); 00730 if (it == neighboors_colors.end()) 00731 neighboors_colors.insert(std::make_pair(message->source(), (uint) payload)); 00732 else it->second = payload; 00733 int neigh_no = get_neighboors(); 00734 if (neigh_colors_count != neigh_no) { 00735 neigh_colors_count++; 00736 } 00737 if (neigh_colors_count == neigh_no) { 00738 neigh_colors_count = 0; 00739 if (is_judge) { 00740 in_game(0); 00741 } else { 00742 int ok = 1; 00743 for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 00744 if (it->second == color) { 00745 send_message(color, radio().id(),CHECK_NOT_OK, judge, 1); 00746 cout << "O kombos:" << radio().id() << " dilwse problima ston:" << judge << endl; 00747 ok = 0; 00748 break; 00749 } 00750 } 00751 if(ok) { 00752 send_message(color, radio().id(), CHECK_OK, judge, 1); 00753 } 00754 } 00755 } 00756 } else if (is_judge) { 00757 if (msg_id == 194) {// If the messages are in tree_routing type get the color_message from payload 00758 message = (coloring_message *) t_message->payload(); 00759 int tmp = message->msg_id(); 00760 int payload = *((uint*) message->payload()); 00761 if ((tmp < 10) && (tmp > 0) && (message->destination() == radio().id())) 00762 tmp = tmp - 1; 00763 payload = payload; 00764 00765 } 00766 int payload = 0; 00767 if ((message->msg_id() == TAKE_TURN) && (message->destination() == radio().id())) { 00768 cout << "Judge:" << (int) radio().id() << " started his game!" << endl; 00769 baton = 1; 00770 ColorsSorted cn_temp = *((ColorsSorted*) message->payload()); 00771 for (int i = 0; i < (cn_temp.get_size() - 1); i++) { 00772 int color_tmp = cn_temp.get_color_from_position(i + 1); 00773 int count_tmp = cn_temp.get_value_from_color(color_tmp); 00774 for (int j = 0; j < count_tmp; j++) { 00775 color_numbers.insert(color_tmp); 00776 } 00777 } 00778 if (judge_suspended > 0) { 00779 last_question.con_no = 1; 00780 if((c_ass == node_count - 1)&&(j_ass = judge_count - 1) ){ 00781 if(is_judge) this->isort(judges,20); 00782 in_game(0); 00783 } 00784 judge_suspended == 0; 00785 }else{ 00786 if((c_ass == node_count - 1)&&(j_ass = judge_count - 1) ){ 00787 if(is_judge) this->isort(judges,20); 00788 in_game(0); 00789 } 00790 } 00791 color_numbers.print(); 00792 00793 } 00794 if (message->msg_id() != ASK_PERMISSION) { 00795 payload = *((uint*) message->payload()); 00796 } else { 00797 q_color = *((ask_color*) message->payload()); 00798 } 00799 if (((message->msg_id() == COLOR_ASSIGNED) || (message->msg_id() == COLOR_NOT_ASSIGNED)) && (message->source() == last_question.who) && (message->destination() == radio().id())) { 00800 if ((payload > 0) && (message->msg_id() == COLOR_ASSIGNED)) { 00801 std::map<uint, uint>::iterator it = color_nodes.find((int) message->source()); 00802 00803 if (it == color_nodes.end()) { 00804 color_nodes.insert(std::make_pair(message->source(), (uint) payload)); 00805 color_numbers.insert((uint) payload); 00806 00807 } else { 00808 00809 if ((it->second != payload) && ((payload == color_numbers.get_color_from_position(last_question.position)) || (round))) { 00810 last_question.answered = 1; 00811 last_question.answer = (uint) payload; 00812 in_game(0); 00813 00814 } 00815 } 00816 } else if (message->msg_id() == COLOR_NOT_ASSIGNED) { 00817 if (((uint) message->source() == (int) last_question.who) && ((uint) payload == (int) last_question.answer)) { 00818 last_question.answered = 1; 00819 last_question.answer = 0; 00820 in_game(0); 00821 } 00822 } 00823 }else if((message->msg_id() == COLOR_ASSIGNED) && (message->destination() == radio().id())){ 00824 if(c_ass_help.find(message->source()) == c_ass_help.end()){ 00825 c_ass_help.insert(std::make_pair(message->source(),(uint)-1)); 00826 c_ass++; 00827 if(radio().id() == 8) 00828 cout << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa" << c_ass << ":" << node_count << endl; 00829 try_start(); 00830 } 00831 } else if (message->msg_id() == CHECK_NOT_OK && radio().id()==message->destination()) { 00832 add_node_color(message->source(), (uint) payload); 00833 checked_nodes++; 00834 if (checked_nodes == last_herd_count) { 00835 if(color_nodes.size() == 1){ 00836 exeption = 1; 00837 iter_color_nodes = color_nodes.begin(); 00838 } 00839 init_question(); 00840 last_question.who = radio().id(); 00841 last_question.con_no = 1; 00842 iter_color_nodes = color_nodes.begin(); 00843 round++; 00844 in_game(0); 00845 } 00846 } else if (message->msg_id() == CHECK_OK && radio().id()==message->destination()) { 00847 checked_nodes++; 00848 if (checked_nodes == last_herd_count) { 00849 if(color_nodes.size() == 1){ 00850 exeption = 1; 00851 iter_color_nodes = color_nodes.begin(); 00852 } 00853 init_question(); 00854 last_question.who = radio().id(); 00855 last_question.con_no = 1; 00856 iter_color_nodes = color_nodes.begin(); 00857 round++; 00858 in_game(0); 00859 } 00860 } else if ((message->msg_id() == REQUEST_COLOR) && (message->destination() == radio().id())) { 00861 int routing_type; 00862 if (msg_id == 194) 00863 routing_type = 1; 00864 else routing_type = 0; 00865 00866 cout << "Received color request from:" << message->source() << endl; 00867 std::map<uint, uint>::iterator it = color_nodes.find((uint) (message->source())); 00868 if (it == color_nodes.end()) { 00869 add_node_color(message->source(), max_color); 00870 color_numbers.insert(max_color); 00871 send_message(max_color, radio().id(), ASSIGN_COLOR, (uint16_t) (message->source()), routing_type); 00872 max_color++; 00873 00874 } else { 00875 send_message((uint) it->second, radio().id(), ASSIGN_COLOR, (uint16_t) (message->source()), routing_type); 00876 } 00877 } else if (message->msg_id() == ASK_PERMISSION) { 00878 uint permission = 1; 00879 if ((q_color.color == color) && (q_color.priority <= radio().id())) 00880 permission = 0; 00881 if (permission) { 00882 send_message((uint) q_color.color, radio().id(), GIVE_PERMISSION, message->source(), 0); 00883 } else { 00884 send_message((uint) q_color.color, radio().id(), DECL_PERMISSION, message->source(), 0); 00885 } 00886 } 00887 00888 } else if ((!is_judge) || (judge_suspended == 1)) { 00889 if (msg_id == 194) // If the messages are in tree_routing type get the color_message from payload 00890 message = (coloring_message *) t_message->payload(); 00891 int payload = 0; 00892 int tmp = message->msg_id(); 00893 if (message->msg_id() != ASK_PERMISSION) { 00894 payload = *((uint*) message->payload()); 00895 } else { 00896 q_color = *((ask_color*) message->payload()); 00897 } 00898 if (message->msg_id() == ASSIGN_COLOR && message->destination() == radio().id() && (payload != color)) { 00899 color = (int) payload; 00900 if (msg_id == ASSIGN_COLOR) { 00901 send_message(color, (uint16_t) radio().id(), COLOR_ASSIGNED, judge, 0); 00902 } else { 00903 send_message(color, (uint16_t) radio().id(), COLOR_ASSIGNED, judge, 1); 00904 } 00905 } else if (message->msg_id() == REASSIGN_COLOR && message->destination() == radio().id() && (answer_count < 1)) { 00906 00907 if ((payload != color) && (payload != color_under_cons)) { 00908 answer_count = 0; 00909 permitted_count = 0; 00910 q_color.color = payload; 00911 q_color.priority = judge; 00912 send_special_message((uint8_t*) & q_color, radio().id(), ASK_PERMISSION, -1, 0); 00913 color_under_cons = payload; 00914 00915 } else { 00916 send_message(payload, radio().id(), COLOR_NOT_ASSIGNED, judge, 1); 00917 } 00918 } else if ((message->msg_id() == ASK_PERMISSION)) { 00919 if ((q_color.color == color) && (q_color.priority <= judge)) { 00920 send_message((uint) q_color.color, radio().id(), DECL_PERMISSION, message->source(), 0); 00921 //std::cout<< " Ase katw re to xrwma:"<< color << " " << ((int)(*(message->payload()))) <<" ilithie:" << message->source() << " giati to lew egw" << radio().id() <<"\n"; 00922 } else { 00923 send_message((uint) q_color.color, radio().id(), GIVE_PERMISSION, message->source(), 0); 00924 } 00925 } else if (((message->msg_id() == GIVE_PERMISSION) || (message->msg_id() == DECL_PERMISSION)) && message->destination() == radio().id() && color_under_cons != 0) { 00926 if (payload == color_under_cons) { 00927 answer_count++; 00928 if (message->msg_id() == GIVE_PERMISSION) { 00929 permitted_count++; 00930 } else { 00931 //std::cout << " Den to thelw re to xrwma:" << color_under_cons << " blaka!!!!" << message->source() << "\n"; 00932 } 00933 if (answer_count == neighboors_colors.size()) { 00934 if (answer_count == permitted_count) { 00935 color = color_under_cons; 00936 color_under_cons = 0; 00937 if (!is_judge) { 00938 send_message(color, radio().id(), COLOR_ASSIGNED, judge, 1); 00939 } else { 00940 in_game(0); 00941 } 00942 } else { 00943 if (!is_judge) { 00944 send_message(color_under_cons, radio().id(), COLOR_NOT_ASSIGNED, judge, 1); 00945 } else { 00946 last_question.answer = color; 00947 judge_suspended = 3; 00948 in_game(0); 00949 } 00950 color_under_cons = 0; 00951 } 00952 answer_count = 0; 00953 } 00954 } 00955 } else if (message->msg_id() == REASSIGN_COLOR && message->destination() == radio().id()) { 00956 } else if ((message->msg_id()) == FINAL_CHECK && (message->destination() == radio().id())){ 00957 send_message(-1, radio().id(), ASK_COLOR, -1, 0); 00958 //cout << " MY FINAL CHECK IS NIGH" << endl; 00959 } 00960 } 00961 00962 } 00963 // ----------------------------------------------------------------------- 00964 00965 template<typename OsModel_P, 00966 typename Radio_P, 00967 typename Debug_P> 00968 void 00969 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00970 bootstrap_message() { 00971 #ifdef DEBUG_PKJUDGEDCOLORING 00972 //debug().debug("Node number %d sends bootstrap message\n", radio().id()); 00973 #endif 00974 int type; 00975 if (this->is_judge) { 00976 type = JUDGE_START; 00977 judges[0] = radio().id(); 00978 } else if (!this->is_judge) { 00979 type = NODE_START; 00980 } 00981 send_message(-1, radio().id(), type, -1, 0); 00982 00983 } 00984 // ----------------------------------------------------------------------- 00985 00986 template<typename OsModel_P, 00987 typename Radio_P, 00988 typename Debug_P> 00989 void 00990 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00991 broadcast_judge(uint8_t judge_id) { 00992 int got_it_before = 0, i; 00993 for (i = 0; i < 20; i++) { 00994 if (judges[i] == judge_id) { 00995 got_it_before = 1; 00996 break; 00997 } else if (judges[i] == -1) { 00998 judges[i] = judge_id; 00999 j_ass++; 01000 if(is_judge) try_start(); 01001 break; 01002 } 01003 } 01004 01005 if (got_it_before == 0) { 01006 01007 if (judge == -1) { 01008 01009 judge = judge_id; 01010 if (!is_judge) { 01011 send_message(radio().id(), radio().id(), REQUEST_COLOR, judge_id, 1); 01012 } 01013 01014 01015 } else if (is_judge) { 01016 cout << "Sta stila ola ki emeina ston aso" << endl; 01017 send_message(radio().id(), radio().id(), -1, judge_id, 1); 01018 } 01019 01020 #ifdef DEBUG_PKJUDGEDCOLORING 01021 debug().debug("Node number %d acknowledges %d as Judge!\n", radio().id(), judge_id); 01022 #endif 01023 send_message(judge_id, radio().id(), BROAD_JUDGE, -1, 0); 01024 01025 01026 01027 } 01028 01029 } 01030 // ----------------------------------------------------------------------- 01031 01032 template<typename OsModel_P, 01033 typename Radio_P, 01034 typename Debug_P> 01035 void 01036 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 01037 send_message(uint payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type) { 01038 coloring_message data; 01039 data.set_msg_id(msg_id); 01040 data.set_source(source); 01041 data.set_destination(destination); 01042 data.set_payload(sizeof (uint), (uint8_t*) & payload); 01043 if (routing_type == 0) { 01044 alg_messages++; 01045 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 01046 //std::cout << " Egw o:" <<(int) source <<" estila minima ston: " <<(int) destination << " me id:"<<(int)msg_id << "kai payload:" << (int)*((uint*)data.payload())<< "\n"; 01047 } else if (routing_type == 1) { 01048 std::cout << " Egw o:" << (int) source << " estila minima ston: " << (int) destination << " me id:" << (int) msg_id << "kai payload:" << (int) *((uint*) data.payload()) << "\n"; 01049 tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 01050 // tree_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 01051 } 01052 } 01053 01054 // ----------------------------------------------------------------------- 01055 01056 template<typename OsModel_P, 01057 typename Radio_P, 01058 typename Debug_P> 01059 void 01060 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 01061 send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type) { 01062 coloring_message data; 01063 data.set_msg_id(msg_id); 01064 data.set_source(source); 01065 data.set_destination(destination); 01066 01067 if (routing_type == 0) { 01068 alg_messages++; 01069 data.set_payload(sizeof (ask_color), (uint8_t*) payload); 01070 //std::cout << "------Egw o:" << (int) source << " estila minima ston: " << (int) destination << " me id:" << (int) msg_id << "kai payload:" << (int) (*((ask_color*) data.payload())).color << "\n"; 01071 radio().send(radio().BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 01072 } else if (routing_type == 1) { 01073 data.set_payload((uint8_t) sizeof (ColorsSorted), (uint8_t*) payload); 01074 tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 01075 } 01076 } 01077 01078 01079 // ----------------------------------------------------------------------- 01080 01081 template<typename OsModel_P, 01082 typename Radio_P, 01083 typename Debug_P> 01084 uint16_t 01085 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 01086 get_neighboors() { 01087 #ifdef DEBUG_PKJUDGEDCOLORING 01088 int i = 0; 01089 // debug().debug("%d:My neighboors are the following:\n", radio().id()); 01090 for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 01091 //std::cout << (int) it->first << "-"; 01092 01093 i = i + 1; 01094 } 01095 01096 //std::cout << "END!!!\n"; 01097 #endif 01098 return (uint16_t) neighboors_colors.size(); 01099 } 01100 01101 01102 01103 // ------------------------------- 01104 // ----------------------------------------------------------------------- 01105 01106 template<typename OsModel_P, 01107 typename Radio_P, 01108 typename Debug_P> 01109 uint16_t 01110 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 01111 get_color_nodes() { 01112 #ifdef DEBUG_PKJUDGEDCOLORING 01113 int i = 0; 01114 //debug().debug("%d:The node-color diagram are the following:\n", radio().id()); 01115 for (std::map<uint, uint>::iterator it = color_nodes.begin(); it != color_nodes.end(); it++) { 01116 //std::cout << "Node:" << (int) it->first << "--Color:" << (int) it->second << "\n"; 01117 i = i + 1; 01118 } 01119 //std::cout << '\n'; 01120 #endif 01121 return (uint16_t) color_nodes.size(); 01122 } 01123 // ----------------------------------------------------------------------- 01124 01125 // ----------------------------------------------------------------------- 01126 01127 } 01128 #endif 01129 01130