Wiselib
|
00001 00002 #ifndef __ALGORITHMS_COLORING_PKJUDGED_COLORING_H__ 00003 #define __ALGORITHMS_COLORING_PKJUDGED_COLORING_H__ 00004 00005 #include "algorithms/coloring/judged_coloring_message_types.h" 00006 #include "algorithms/routing/tree_routing.h" 00007 #include "algorithms/routing/tora_routing.h" 00008 #include "internal_interface/routing_table/routing_table_static_array.h" 00009 #include "algorithms/routing/tora_routing_message.h" 00010 #include "internal_interface/coloring_table/color_table_map.h" 00011 #include "internal_interface/coloring_table/colors_sorted.h" 00012 #include "judged_coloring.h" 00013 #include <string.h> 00014 #include <string> 00015 00016 #define DEBUG_PKJUDGEDCOLORING 00017 00018 namespace wiselib { 00019 #ifndef DATA_STRUCTS 00020 #define DATA_STRUCTS 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 template<typename OsModel_P, 00037 typename Radio_P = typename OsModel_P::Radio, 00038 typename Debug_P = typename OsModel_P::Debug> 00039 class PKJudgedColoring { 00040 public: 00041 typedef OsModel_P OsModel; 00042 typedef Radio_P Radio; 00043 typedef Debug_P Debug; 00044 00045 typedef typename OsModel_P::Timer Timer; 00046 00047 00048 00049 typedef PKJudgedColoring<OsModel, Radio, Debug> self_type; 00050 typedef TreeRouting<OsModel, Timer, Radio, Debug> tree_route; 00051 typedef typename OsModel::Os Os; 00052 00053 typedef wiselib::StaticArrayRoutingTable<OsModel, Radio, 8, wiselib::ToraRoutingTableValue<OsModel, Radio> > 00054 ToraRoutingTable; 00055 typedef ToraRouting<OsModel, ToraRoutingTable, Radio, Debug> tora_routing_t; 00056 typedef ToraRoutingMessage<OsModel_P, Radio> tora_message; 00057 typedef typename Radio::node_id_t node_id_t; 00058 typedef typename Radio::size_t size_t; 00059 typedef typename Radio::block_data_t block_data_t; 00060 00061 typedef typename Timer::millis_t millis_t; 00062 //Needed Message Types 00063 typedef TreeRoutingMessage<OsModel, Radio> tree_message; 00064 typedef TreeBroadcastMessage<OsModel, Radio> tree_broad_message; 00065 typedef JudgedColoringMessage<OsModel, Radio> coloring_message; 00066 typedef StlMapColorTable<OsModel, Radio, node_id_t> ColorTable; 00067 typedef ColorsTable<OsModel, Radio> ColorsSorted; 00068 00071 PKJudgedColoring(); 00072 ~PKJudgedColoring(); 00074 00077 void enable(void); 00078 void disable(void); 00080 00083 void timer_elapsed(void *userdata); 00085 00088 void in_game(void *userdata); 00090 00091 00094 void send(node_id_t receiver, size_t len, block_data_t *data); 00096 00099 void receive(node_id_t from, size_t len, block_data_t *data); 00101 00104 void bootstrap_message(); 00106 00109 void broadcast_judge(uint8_t); 00111 00112 00113 void send_message(uint payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type); 00114 00115 00116 void send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type); 00117 00120 uint16_t get_neighboors(); 00122 00125 uint16_t get_color_nodes(); 00127 00128 int fm(int arr[], int b, int n) { 00129 int f = b; 00130 int c; 00131 00132 for (c = b + 1; c < n; c++) 00133 if (arr[c] > arr[f]) 00134 f = c; 00135 00136 return f; 00137 } 00138 00139 void isort(int arr[], int n) { 00140 int s, w; 00141 int sm; 00142 00143 for (s = 0; s < n - 1; s++) { 00144 w = fm(arr, s, n); 00145 sm = arr[w]; 00146 arr[w] = arr[s]; 00147 arr[s] = sm; 00148 } 00149 } 00150 00151 inline void set_os(Os* os) { 00152 os_ = os; 00153 }; 00154 00155 inline Os* os() { 00156 return os_; 00157 }; 00158 00159 inline void set_judge() { 00160 is_judge = 1; 00161 judge = Radio::id(os()); 00162 }; 00163 00164 inline int get_color() { 00165 return color; 00166 } 00167 00168 inline void set_color(int color_) { 00169 color = color_; 00170 } 00171 00172 inline void set_max_color(int max_color_) { 00173 max_color = max_color_; 00174 } 00175 00176 inline uint16_t get_is_judge() { 00177 return is_judge; 00178 }; 00179 00180 inline uint16_t has_min_priority() { 00181 int has = 1; 00182 for (int i = 1; i < 20; i++) { 00183 if (judges[i] == -1)break; 00184 else if (judges[i] < Radio::id(os())) 00185 has = 0; 00186 } 00187 return has; 00188 }; 00189 00190 inline uint16_t active_colors_size() { 00191 return color_numbers.get_active_colors(); 00192 }; 00193 00194 inline uint16_t add_neighboor(uint neighboor_id, uint color_num) { 00195 neighboors_colors.insert(std::make_pair(neighboor_id, color_num)); 00196 }; 00197 00198 inline uint16_t add_node_color(uint node_id, uint color_num) { 00199 if (Radio::id(os()) == 75 && node_id == 3) 00200 cout << "my jusdge is:" << judge << endl; 00201 color_nodes.insert(std::make_pair(node_id, color_num)); 00202 }; 00203 00204 inline void change_node_color(uint node_id, uint color_num) { 00205 std::map<uint, uint>::iterator it = color_nodes.find(node_id); 00206 it->second = color_num; 00207 }; 00208 00209 inline void set_tree_routing(tree_route* tree) { 00210 tree_routing = tree; 00211 }; 00212 00213 inline void set_tora_routing(tora_routing_t* tora) { 00214 tora_routing = tora; 00215 }; 00216 00217 int compare(const void * a, const void * b){ 00218 if(*(int*)a==*(int*)b) return 0; 00219 else if (*(int*)a < *(int*)b) return 1; 00220 else return -1; 00221 } 00222 00223 inline void init_question() { 00224 last_question.answered = 0; 00225 last_question.position = 1; 00226 last_question.answer = color_numbers.get_color_from_position(last_question.position); 00227 }; 00228 00229 00230 private: 00231 int judge_suspended; 00232 int judges[20]; 00233 question last_question; 00234 tree_route *tree_routing; 00235 tora_routing_t *tora_routing; 00236 uint16_t permitted_count, answer_count, neigh_colors_count; 00237 uint8_t is_judge; 00238 uint8_t asked_color; 00239 int judge; 00240 int max_color; 00241 Os *os_; 00242 ColorTable neighboors_colors; 00243 ColorTable color_nodes; 00244 std::map<uint, uint>::iterator iter_color_nodes; 00245 ColorsSorted color_numbers; 00246 int color, color_under_cons, game_started; 00247 ask_color q_color; 00248 }; 00249 // ----------------------------------------------------------------------- 00250 // -----------Judged Coloring Constructor--------------------------------- 00251 // ----------------------------------------------------------------------- 00252 00253 template<typename OsModel_P, 00254 typename Radio_P, 00255 typename Debug_P> 00256 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00257 PKJudgedColoring() 00258 : os_(0), 00259 is_judge(0), 00260 judge(-1), 00261 color(-1), 00262 max_color(2), 00263 asked_color(0), 00264 permitted_count(0), 00265 answer_count(0), 00266 game_started(0), 00267 judge_suspended(0), 00268 neigh_colors_count(0){ 00269 }; 00270 // -----------Judged Coloring De-Constructor------------------------------ 00271 00272 template<typename OsModel_P, 00273 typename Radio_P, 00274 typename Debug_P> 00275 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00276 ~PKJudgedColoring() { 00277 00278 }; 00279 // ----------------------------------------------------------------------- 00280 00281 template<typename OsModel_P, 00282 typename Radio_P, 00283 typename Debug_P> 00284 void 00285 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00286 enable() { 00287 00288 for (int i = 0; i < 20; i++) judges[i] = -1; 00289 Radio::enable(os()); 00290 Radio::template reg_recv_callback<self_type, &self_type::receive > (os(), this); 00291 00292 Timer::template set_timer<self_type, &self_type::timer_elapsed > ( 00293 os(), 500, this, 0); 00294 if (is_judge) { 00295 color_numbers.init(); 00296 Timer::template set_timer<self_type, &self_type::timer_elapsed > ( 00297 os(), 10000, this, 0); 00298 } else { 00299 Timer::template set_timer<self_type, &self_type::timer_elapsed > ( 00300 os(), 500, this, 0); 00301 } 00302 bootstrap_message(); 00303 00304 00305 00306 } 00307 // ----------------------------------------------------------------------- 00308 00309 template<typename OsModel_P, 00310 typename Radio_P, 00311 typename Debug_P> 00312 void 00313 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00314 disable(void) { 00315 #ifdef DEBUG_PKJUDGEDCOLORING 00316 Debug::debug(os(), "PKJudgedColoring: Disable\n"); 00317 #endif 00318 } 00319 // ----------------------------------------------------------------------- 00320 00321 template<typename OsModel_P, 00322 typename Radio_P, 00323 typename Debug_P> 00324 void 00325 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00326 timer_elapsed(void *userdata) { 00327 if (color == -1 && !is_judge && (asked_color < 1)) { 00328 Timer::template set_timer<self_type, &self_type::timer_elapsed > ( 00329 os(), 50, this, 0); 00330 asked_color++; 00331 send_message(Radio::id(os()), Radio::id(os()), REQUEST_COLOR, judge, 1); 00332 cout << "I:" << (int) Radio::id(os()) << "sent color request to:" << judge << endl; 00333 } else if (is_judge && asked_color < 1) { 00334 asked_color++; 00335 color_numbers.insert(color); 00336 add_node_color(Radio::id(os()), color); 00337 iter_color_nodes = color_nodes.begin(); 00338 last_question.who = Radio::id(os()); 00339 last_question.con_no = 0; 00340 int i; 00341 this->isort(judges,20); 00342 00343 00344 Timer::template set_timer<self_type, &self_type::in_game > ( 00345 os(), 10000, this, 0); 00346 #ifdef DEBUG_PKJUDGEDCOLORING 00347 Debug::debug(os(), "Node %d Judge Started his game\n", Radio::id(os())); 00348 #endif 00349 00350 00351 00352 } 00353 } 00354 // ----------------------------------------------------------------------- 00355 00356 template<typename OsModel_P, 00357 typename Radio_P, 00358 typename Debug_P> 00359 void 00360 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00361 in_game(void *userdata) { 00362 get_neighboors(); 00363 // Timer::template set_timer<self_type, &self_type::in_game > ( 00364 // os(), 50, this, 0); 00365 game_started = 1; 00366 if (last_question.con_no < (color_nodes.size())) { 00367 if (last_question.who == Radio::id(os())) { 00368 get_color_nodes(); 00369 color_numbers.print(); 00370 if (!last_question.who)iter_color_nodes; 00371 if (iter_color_nodes->first == Radio::id(os())) { 00372 if (++iter_color_nodes == color_nodes.end()) 00373 iter_color_nodes = color_nodes.begin(); 00374 } 00375 last_question.who = iter_color_nodes->first; 00376 00377 last_question.curr_color_pos = color_numbers.get_position_from_color(color_nodes.find(last_question.who)->second); 00378 last_question.con_no = 1; 00379 init_question(); 00380 last_question.type = REASSIGN_COLOR; 00381 } else if (last_question.answered) { 00382 if (last_question.type == REASSIGN_COLOR) { 00383 if (last_question.answer == 0) { 00384 // std::cout << "O kombos:" << (int)last_question.who << "Apantise" << "\n"; 00385 00386 last_question.answered = 0; 00387 last_question.position++; 00388 last_question.answer = color_numbers.get_color_from_position(last_question.position); 00389 if ((last_question.position == color_numbers.get_size()) || (color_numbers.get_value_from_position(last_question.curr_color_pos) > color_numbers.get_value_from_position(last_question.position))) { 00390 last_question.con_no++; 00391 std::cout << "CON NOOOOOOOOOOOO:" << last_question.con_no << "\n"; 00392 if (++iter_color_nodes == color_nodes.end()) 00393 iter_color_nodes = color_nodes.begin(); 00394 if (iter_color_nodes->first == Radio::id(os())) { 00395 if (++iter_color_nodes == color_nodes.end()) 00396 iter_color_nodes = color_nodes.begin(); 00397 } 00398 last_question.who = iter_color_nodes->first; 00399 last_question.curr_color_pos = color_numbers.get_position_from_color(color_nodes.find(last_question.who)->second); 00400 init_question(); 00401 //if(last_question.con_no==99) cout << "After Print" << endl; 00402 00403 } 00404 00405 } else if (last_question.answer == color_numbers.get_color_from_position(last_question.position)) { 00406 //std::cout << "O kombos:" << (int)last_question.who << "Apantise" << "\n"; 00407 //std::cout << "tou arese to xrwma "<< (int)last_question.answer <<" omws \n"; 00408 last_question.con_no = 1; 00409 color_numbers.insert(last_question.answer); 00410 color_numbers.remove(color_nodes.find(last_question.who)->second); 00411 change_node_color(last_question.who, last_question.answer); 00412 if ((++iter_color_nodes) == color_nodes.end()) 00413 iter_color_nodes = color_nodes.begin(); 00414 if (iter_color_nodes->first == Radio::id(os())) { 00415 if (++iter_color_nodes == color_nodes.end()) 00416 iter_color_nodes = color_nodes.begin(); 00417 } 00418 last_question.who = iter_color_nodes->first; 00419 last_question.curr_color_pos = color_numbers.get_position_from_color(color_nodes.find(last_question.who)->second); 00420 init_question(); 00421 //color_numbers.print(); 00422 } 00423 00424 } 00425 } 00426 if (!last_question.answered) 00427 send_message((uint16_t) last_question.answer, Radio::id(os()), REASSIGN_COLOR, (uint16_t) last_question.who, 1); 00428 } else if (((last_question.con_no) == color_nodes.size())&&(judge_suspended == 0)) { 00429 send_message(-1,Radio::id(os()),ASK_COLOR,-1,0); 00430 judge_suspended = 1; 00431 } else if (judge_suspended == 1){ 00432 int i = 0,ok = 1; 00433 while (( i < color_numbers.get_size() ) && (color_numbers.get_value_from_color(color) <= color_numbers.get_value_from_position(i+1))) { 00434 00435 int color_under_cons = color_numbers.get_color_from_position(i+1); 00436 if(color_under_cons ==1801) 00437 int ui = 0; 00438 for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 00439 int neigh_id = (int)it->first; 00440 int neigh_color = neighboors_colors.find(neigh_id)->second; 00441 if((neigh_color == color_under_cons ) || (color == color_under_cons )) 00442 ok = 0; 00443 } 00444 if(ok){ 00445 color_numbers.remove(color); 00446 color = color_under_cons; 00447 color_numbers.insert(color_under_cons); 00448 color_nodes.find((uint)Radio::id(os()))->second = color; 00449 last_question.con_no = 1; 00450 last_question.who = Radio::id(os()); 00451 judge_suspended = 0; 00452 Timer::template set_timer<self_type, &self_type::in_game > ( 00453 os(), 10, this, 0); 00454 ok=1000; 00455 } 00456 if(ok!=1000) 00457 ok=1; 00458 else{ 00459 break; 00460 } 00461 i++; 00462 } 00463 00464 if(ok!=1000){ 00465 for(int i=0;i<20;i++){ 00466 int a = Radio::id(os()); 00467 int b = judges[i]; 00468 if((judges[i] < Radio::id(os())) && (judges[i]!=-1)){ 00469 send_special_message((uint8_t*)&color_numbers,Radio::id(os()),TAKE_TURN,judges[i],1); 00470 break; 00471 } 00472 } 00473 } 00474 } 00475 if(color_nodes.size() == 1){ 00476 for(int i=0;i<20;i++){ 00477 int a = Radio::id(os()); 00478 int b = judges[i]; 00479 if((judges[i] < Radio::id(os())) && (judges[i]!=-1)){ 00480 send_special_message((uint8_t*)&color_numbers,Radio::id(os()),TAKE_TURN,judges[i],1); 00481 break; 00482 } 00483 } 00484 } 00485 00486 } 00487 // ----------------------------------------------------------------------- 00488 00489 template<typename OsModel_P, 00490 typename Radio_P, 00491 typename Debug_P> 00492 void 00493 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00494 send(node_id_t destination, size_t len, block_data_t *data) { 00495 00496 } 00497 // ----------------------------------------------------------------------- 00498 00499 template<typename OsModel_P, 00500 typename Radio_P, 00501 typename Debug_P> 00502 void 00503 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00504 receive(node_id_t from, size_t len, block_data_t *data) { 00505 if (from == Radio::id(os())) 00506 return; 00507 00508 uint8_t msg_id = *data; 00509 uint destination, source, payload, msg_id2; 00510 coloring_message *message; 00511 //tree_message *t_message; 00512 tora_message *t_message; 00513 message = (coloring_message *) data; 00514 t_message = (tora_message *) data; 00515 00516 00517 if (((msg_id == 194) || ((msg_id > 2) && (msg_id < 10)))) { 00518 if (msg_id == 194) 00519 message = (coloring_message *) t_message->payload(); 00520 destination = message->destination(); 00521 if ((Radio::id(os()) == destination)) { 00522 source = message->source(); 00523 if (message->msg_id() != ASK_PERMISSION) { 00524 payload = *((uint*) message->payload()); 00525 } else { 00526 payload = *((uint*) (message->payload())); 00527 } 00528 msg_id2 = message->msg_id(); 00529 } 00530 } 00531 00532 if ((Radio::id(os()) == destination) && (0 < msg_id2 < 11)) 00533 std::cout << " Egw o:" << destination << " elaba minima apo ton: " << source << " me id:" << msg_id2 << "kai payload:" << payload << "\n"; 00534 00535 if (msg_id == JUDGE_START) { 00536 message = (coloring_message *) data; 00537 add_neighboor(message->source(), 0); 00538 broadcast_judge(message->source()); 00539 } else if (msg_id == NODE_START) { 00540 message = (coloring_message *) data; 00541 add_neighboor(message->source(), 0); 00542 } else if (msg_id == BROAD_JUDGE) { 00543 message = (coloring_message *) data; 00544 broadcast_judge(*((uint*) message->payload())); 00545 } else if (msg_id == ASK_COLOR){ 00546 send_message(color,Radio::id(os()),REPLY_COLOR,message->source(),0); 00547 } else if ((msg_id == REPLY_COLOR) && (message->destination() == Radio::id(os()))){ 00548 payload = *((uint*) message->payload()); 00549 std::map<uint, uint>::iterator it = neighboors_colors.find((int) message->source()); 00550 if (it == neighboors_colors.end()) 00551 neighboors_colors.insert(std::make_pair(message->source(), (uint) payload)); 00552 else it->second = payload; 00553 if(payload == 1801) 00554 cout<<"EGW O"<<Radio::id(os()) <<"-----------------------TIPOTa" << endl; 00555 int neigh_no = get_neighboors(); 00556 if(neigh_colors_count != neigh_no){ 00557 neigh_colors_count++; 00558 } 00559 if(neigh_colors_count == neigh_no){ 00560 neigh_colors_count = 0; 00561 if(is_judge) in_game(0); 00562 } 00563 }else if (is_judge) { 00564 if (msg_id == 194) {// If the messages are in tree_routing type get the color_message from payload 00565 message = (coloring_message *) t_message->payload(); 00566 int tmp = message->msg_id(); 00567 int payload = *((uint*) message->payload()); 00568 if ((tmp < 10) && (tmp > 0) && (message->destination() == Radio::id(os()))) 00569 tmp = tmp - 1; 00570 payload = payload; 00571 00572 } 00573 if ((message->destination() == Radio::id(os())) && (message->msg_id() == -1)) 00574 cout << "Ta pires ollaaaaaaaaaaaaaaaaaa ki efuges" << endl; 00575 int payload = 0; 00576 if ((message->msg_id() == TAKE_TURN) && (message->destination() == Radio::id(os()))) { 00577 ColorsSorted cn_temp = *((ColorsSorted*) message->payload()); 00578 for (int i = 0; i < cn_temp.get_size(); i++) { 00579 int color_tmp = cn_temp.get_color_from_position(i + 1); 00580 int count_tmp = cn_temp.get_value_from_color(color_tmp); 00581 for (int j = 0; j < count_tmp; j++) { 00582 color_numbers.insert(color_tmp); 00583 } 00584 } 00585 last_question.con_no = 1; 00586 if(judge_suspended>0){ 00587 in_game(0); 00588 judge_suspended == 0; 00589 } 00590 color_numbers.print(); 00591 } 00592 if (message->msg_id() != ASK_PERMISSION) { 00593 payload = *((uint*) message->payload()); 00594 } else { 00595 q_color = *((ask_color*) message->payload()); 00596 } 00597 if ((message->msg_id() == TAKE_TURN) && (message->destination() == Radio::id(os()))) { 00598 cout << "Judge:" << (int) Radio::id(os()) << " started his game!" << endl; 00599 in_game(0); 00600 } else if (((message->msg_id() == COLOR_ASSIGNED) || (message->msg_id() == COLOR_NOT_ASSIGNED)) && (message->source() == last_question.who) && (message->destination() == Radio::id(os()))) { 00601 if ((payload > 0) && (message->msg_id() == COLOR_ASSIGNED)) { 00602 if ((source == 0 && destination == 25)) 00603 cout << "edw eisai" << endl; 00604 std::map<uint, uint>::iterator it = color_nodes.find((int) message->source()); 00605 if (it == color_nodes.end()) { 00606 color_nodes.insert(std::make_pair(message->source(), (uint) payload)); 00607 color_numbers.insert((uint) payload); 00608 00609 } else { 00610 00611 if ((it->second != payload) && (payload == color_numbers.get_color_from_position(last_question.position))) { 00612 last_question.answered = 1; 00613 last_question.answer = (uint) payload; 00614 in_game(0); 00615 00616 } 00617 } 00618 } else if (message->msg_id() == COLOR_NOT_ASSIGNED) { 00619 if (((uint) message->source() == (int) last_question.who) && ((uint) payload == (int) last_question.answer)) { 00620 last_question.answered = 1; 00621 last_question.answer = 0; 00622 in_game(0); 00623 } 00624 } 00625 } else if ((message->msg_id() == REQUEST_COLOR) && (message->destination() == Radio::id(os()))) { 00626 int routing_type; 00627 if (msg_id == 194) 00628 routing_type = 1; 00629 else routing_type = 0; 00630 00631 //cout << "Received color request from:" << message->source() << endl; 00632 std::map<uint, uint>::iterator it = color_nodes.find((uint) (message->source())); 00633 if (it == color_nodes.end()) { 00634 add_node_color(message->source(), max_color); 00635 color_numbers.insert(max_color); 00636 send_message(max_color, Radio::id(os()), ASSIGN_COLOR, (uint16_t) (message->source()), routing_type); 00637 max_color++; 00638 00639 } else { 00640 send_message((uint) it->second, Radio::id(os()), ASSIGN_COLOR, (uint16_t) (message->source()), routing_type); 00641 } 00642 } else if (message->msg_id() == ASK_PERMISSION) { 00643 uint permission = 1; 00644 if ((q_color.color == color) && (q_color.priority <= Radio::id(os()))) 00645 permission = 0; 00646 if (permission) { 00647 send_message((uint) q_color.color, Radio::id(os()), GIVE_PERMISSION, message->source(), 0); 00648 } else { 00649 send_message((uint) q_color.color, Radio::id(os()), DECL_PERMISSION, message->source(), 0); 00650 } 00651 } 00652 00653 } else if ((!is_judge) || (judge_suspended==1)) { 00654 if (msg_id == 194) // If the messages are in tree_routing type get the color_message from payload 00655 message = (coloring_message *) t_message->payload(); 00656 int payload = 0; 00657 int tmp = message->msg_id(); 00658 if (message->msg_id() != ASK_PERMISSION) { 00659 payload = *((uint*) message->payload()); 00660 } else { 00661 q_color = *((ask_color*) message->payload()); 00662 } 00663 if (message->msg_id() == ASSIGN_COLOR && message->destination() == Radio::id(os()) && (payload != color)) { 00664 color = (int) payload; 00665 if (msg_id == ASSIGN_COLOR) { 00666 send_message(color, (uint16_t) Radio::id(os()), COLOR_ASSIGNED, judge, 0); 00667 } else { 00668 send_message(color, (uint16_t) Radio::id(os()), COLOR_ASSIGNED, judge, 1); 00669 } 00670 } else if (message->msg_id() == REASSIGN_COLOR && message->destination() == Radio::id(os()) && (answer_count < 1)) { 00671 00672 if ((payload != color) && (payload != color_under_cons)) { 00673 answer_count = 0; 00674 permitted_count = 0; 00675 q_color.color = payload; 00676 q_color.priority = judge; 00677 send_special_message((uint8_t*) & q_color, Radio::id(os()), ASK_PERMISSION, -1, 0); 00678 color_under_cons = payload; 00679 00680 } else { 00681 send_message(payload, Radio::id(os()), COLOR_NOT_ASSIGNED, judge, 1); 00682 } 00683 } else if ((message->msg_id() == ASK_PERMISSION)) { 00684 if ((q_color.color == color) && (q_color.priority <= judge)) { 00685 send_message((uint) q_color.color, Radio::id(os()), DECL_PERMISSION, message->source(), 0); 00686 //std::cout<< " Ase katw re to xrwma:"<< color << " " << ((int)(*(message->payload()))) <<" ilithie:" << message->source() << " giati to lew egw" << Radio::id(os()) <<"\n"; 00687 } else { 00688 send_message((uint) q_color.color, Radio::id(os()), GIVE_PERMISSION, message->source(), 0); 00689 } 00690 } else if (((message->msg_id() == GIVE_PERMISSION) || (message->msg_id() == DECL_PERMISSION)) && message->destination() == Radio::id(os()) && color_under_cons != 0) { 00691 if (payload == color_under_cons) { 00692 answer_count++; 00693 if (message->msg_id() == GIVE_PERMISSION) { 00694 permitted_count++; 00695 } else { 00696 //std::cout << " Den to thelw re to xrwma:" << color_under_cons << " blaka!!!!" << message->source() << "\n"; 00697 } 00698 if (answer_count == neighboors_colors.size()) { 00699 if (answer_count == permitted_count) { 00700 color = color_under_cons; 00701 color_under_cons = 0; 00702 if(!is_judge){ 00703 send_message(color, Radio::id(os()), COLOR_ASSIGNED, judge, 1); 00704 }else{ 00705 in_game(0); 00706 } 00707 } else { 00708 if(!is_judge){ 00709 send_message(color_under_cons, Radio::id(os()), COLOR_NOT_ASSIGNED, judge, 1); 00710 }else{ 00711 last_question.answer = color; 00712 judge_suspended = 3; 00713 in_game(0); 00714 } 00715 color_under_cons = 0; 00716 } 00717 answer_count = 0; 00718 } 00719 } 00720 } else if (message->msg_id() == REASSIGN_COLOR && message->destination() == Radio::id(os())) { 00721 } 00722 } 00723 00724 } 00725 // ----------------------------------------------------------------------- 00726 00727 template<typename OsModel_P, 00728 typename Radio_P, 00729 typename Debug_P> 00730 void 00731 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00732 bootstrap_message() { 00733 #ifdef DEBUG_PKJUDGEDCOLORING 00734 //Debug::debug(os(), "Node number %d sends bootstrap message\n", Radio::id(os())); 00735 #endif 00736 int type; 00737 if (this->is_judge) { 00738 type = JUDGE_START; 00739 judges[0] = Radio::id(os()); 00740 } else if (!this->is_judge) { 00741 type = NODE_START; 00742 } 00743 send_message(-1, Radio::id(os()), type, -1, 0); 00744 00745 } 00746 // ----------------------------------------------------------------------- 00747 00748 template<typename OsModel_P, 00749 typename Radio_P, 00750 typename Debug_P> 00751 void 00752 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00753 broadcast_judge(uint8_t judge_id) { 00754 int got_it_before = 0, i; 00755 for (i = 0; i < 20; i++) { 00756 if (judges[i] == judge_id) { 00757 got_it_before = 1; 00758 break; 00759 } else if (judges[i] == -1) { 00760 judges[i] = judge_id; 00761 break; 00762 } 00763 } 00764 00765 if (got_it_before == 0) { 00766 00767 if (judge == -1) { 00768 00769 judge = judge_id; 00770 if (!is_judge) { 00771 send_message(Radio::id(os()), Radio::id(os()), REQUEST_COLOR, judge_id, 1); 00772 } 00773 00774 00775 } else if (is_judge) { 00776 //cout << "Sta stila ola ki emeina ston aso" << endl; 00777 send_message(Radio::id(os()), Radio::id(os()), -1, judge_id, 1); 00778 } 00779 00780 #ifdef DEBUG_PKJUDGEDCOLORING 00781 Debug::debug(os(), "Node number %d acknowledges %d as Judge!\n", Radio::id(os()), judge_id); 00782 #endif 00783 send_message(judge_id, Radio::id(os()), BROAD_JUDGE, -1, 0); 00784 00785 00786 00787 } 00788 00789 } 00790 // ----------------------------------------------------------------------- 00791 00792 template<typename OsModel_P, 00793 typename Radio_P, 00794 typename Debug_P> 00795 void 00796 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00797 send_message(uint payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type) { 00798 coloring_message data; 00799 data.set_msg_id(msg_id); 00800 data.set_source(source); 00801 data.set_destination(destination); 00802 data.set_payload(sizeof (uint), (uint8_t*) & payload); 00803 if (routing_type == 0) { 00804 Radio::send(os(), Radio::BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00805 //std::cout << " Egw o:" <<(int) source <<" estila minima ston: " <<(int) destination << " me id:"<<(int)msg_id << "kai payload:" << (int)*((uint*)data.payload())<< "\n"; 00806 } else if (routing_type == 1) { 00807 // if(source == 4) 00808 std::cout << " Egw o:" << (int) source << " estila minima ston: " << (int) destination << " me id:" << (int) msg_id << "kai payload:" << (int) *((uint*) data.payload()) << "\n"; 00809 tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 00810 // tree_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 00811 } 00812 } 00813 00814 // ----------------------------------------------------------------------- 00815 00816 template<typename OsModel_P, 00817 typename Radio_P, 00818 typename Debug_P> 00819 void 00820 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00821 send_special_message(uint8_t* payload, uint source, uint8_t msg_id, uint destination, uint8_t routing_type) { 00822 coloring_message data; 00823 data.set_msg_id(msg_id); 00824 data.set_source(source); 00825 data.set_destination(destination); 00826 00827 if (routing_type == 0) { 00828 data.set_payload(sizeof (ask_color), (uint8_t*) payload); 00829 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"; 00830 Radio::send(os(), Radio::BROADCAST_ADDRESS, data.buffer_size(), (uint8_t*) & data); 00831 } else if (routing_type == 1) { 00832 data.set_payload(sizeof (ColorsSorted), (uint8_t*) payload); 00833 tora_routing->send(destination, data.buffer_size(), (block_data_t*) & data); 00834 } 00835 } 00836 00837 00838 // ----------------------------------------------------------------------- 00839 00840 template<typename OsModel_P, 00841 typename Radio_P, 00842 typename Debug_P> 00843 uint16_t 00844 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00845 get_neighboors() { 00846 #ifdef DEBUG_PKJUDGEDCOLORING 00847 int i = 0; 00848 // Debug::debug(os(), "%d:My neighboors are the following:\n", Radio::id(os())); 00849 for (std::map<uint, uint>::iterator it = neighboors_colors.begin(); it != neighboors_colors.end(); it++) { 00850 // std::cout << (int) it->first << "-"; 00851 00852 i = i + 1; 00853 } 00854 00855 //std::cout << "END!!!\n"; 00856 #endif 00857 return (uint16_t) neighboors_colors.size(); 00858 } 00859 00860 00861 00862 // ------------------------------- 00863 // ----------------------------------------------------------------------- 00864 00865 template<typename OsModel_P, 00866 typename Radio_P, 00867 typename Debug_P> 00868 uint16_t 00869 PKJudgedColoring<OsModel_P, Radio_P, Debug_P>:: 00870 get_color_nodes() { 00871 #ifdef DEBUG_PKJUDGEDCOLORING 00872 int i = 0; 00873 Debug::debug(os(), "%d:The node-color diagram are the following:\n", Radio::id(os())); 00874 for (std::map<uint, uint>::iterator it = color_nodes.begin(); it != color_nodes.end(); it++) { 00875 std::cout << "Node:" << (int) it->first << "--Color:" << (int) it->second << "\n"; 00876 i = i + 1; 00877 } 00878 //std::cout << '\n'; 00879 #endif 00880 return (uint16_t) color_nodes.size(); 00881 } 00882 // ----------------------------------------------------------------------- 00883 00884 // ----------------------------------------------------------------------- 00885 00886 } 00887 #endif 00888 00889