Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef __ALGORITHMS_COLORING_IMJUDGED_COLORING_H__ 00020 #define __ALGORITHMS_COLORING_IMJUDGED_COLORING_H__ 00021 00022 #include "algorithms/coloring/imjudged/IMjudged_coloring_message.h" 00023 #include "algorithms/routing/tree/tree_routing.h" 00024 #include "util/pstl/vector_static.h" 00025 #include "util/pstl/pair.h" 00026 #include "color_table.h" 00027 00028 //#define DEBUG_IMJUDGEDCOLORING 00029 #define INFO_IMJUDGEDCOLORING 00030 00031 #define MAX_NB 5 00032 #define MAX_NODES 5 00033 00034 #define ENABLE_AGGREGATION 00035 00036 namespace wiselib 00037 { 00047 template<typename OsModel_P, 00048 typename Radio_P, 00049 typename Debug_P, 00050 typename Routing_P> 00051 class IMJudgedColoring 00052 { 00053 public: 00054 typedef OsModel_P OsModel; 00055 typedef Radio_P Radio; 00056 typedef Debug_P Debug; 00057 typedef Routing_P routing_t; 00058 00059 typedef typename OsModel_P::Timer Timer; 00060 typedef typename OsModel::Rand Rand; 00061 typedef typename OsModel::Os Os; 00062 00063 typedef IMJudgedColoring<OsModel, Radio, Debug, Routing_P> self_type; 00064 00065 typedef typename Radio::node_id_t node_id_t; 00066 typedef typename Radio::size_t size_t; 00067 typedef typename Radio::block_data_t block_data_t; 00068 typedef typename Timer::millis_t millis_t; 00069 00070 typedef TreeRouting<OsModel, Timer, Radio, Debug> tree_routing_t; 00071 typedef typename tree_routing_t::children_t children_t; 00072 typedef typename tree_routing_t::children_t::iterator children_iterator_t; 00073 00074 typedef node_id_t color_value_type; 00075 typedef IMJudgedColoringMessage<OsModel, Radio, color_value_type> coloring_message; 00076 00077 typedef pair<node_id_t, color_value_type> node_color; 00078 typedef vector_static<OsModel, node_color, MAX_NB> Neighborhood_t; 00079 typedef typename Neighborhood_t::iterator Neighborhood_iterator_t; 00080 00081 typedef ColorsTable<OsModel, color_value_type, MAX_NODES> color_table_t; 00082 00083 typedef pair<color_value_type, color_value_type> old_new_color; 00084 00085 struct stats { 00086 uint32_t IMJCMsgIdBroadcastCL; 00087 uint32_t IMJCMsgIdColorRQ; 00088 uint32_t IMJCMsgIdColorRSP; 00089 uint32_t IMJCMsgIdColorCHG; 00090 }msg_info; 00091 bool cycle_changes_detected; 00092 routing_t *tree_routing; 00093 00094 // typedef int uint; 00097 IMJudgedColoring(); 00098 ~IMJudgedColoring(); 00100 00103 void enable(void); 00104 void disable(void); 00106 00109 void timer_elapsed(void *userdata); 00111 00114 void color_timeout(void *userdata); 00116 00119 void neighborhood_timeout(void *userdata); 00121 00124 void receive(node_id_t from, size_t len, block_data_t *data); 00126 00129 void rcv_routing_message(node_id_t from, size_t len, block_data_t *data); 00131 00134 void tree_state_chage(uint8_t state); 00136 00139 const uint16_t get_neighboors(); 00141 00144 uint16_t get_color_nodes(); 00146 00147 #ifdef ENABLE_AGGREGATION 00148 void check_rcd_fragments(); 00149 #endif 00150 00151 bool exist_color_in_neighborhood( color_value_type ncolor ) 00152 { 00153 for (Neighborhood_iterator_t 00154 it = neighborhood.begin(); 00155 it != neighborhood.end(); 00156 it++ ) 00157 { 00158 if ( (*it).second == ncolor ) 00159 { 00160 return true; 00161 } 00162 } 00163 return false; 00164 } 00165 00166 color_value_type change_color() 00167 { 00168 uint16_t mycard = colors.cardinality(color); 00169 for (uint16_t i = 0; i < colors.size(); i++ ) 00170 { 00171 if( (colors[i] != color) 00172 && (!exist_color_in_neighborhood( colors[i] )) ) 00173 { 00174 if ( colors.cardinalityAt(i) >= mycard ) 00175 { 00176 return colors[i]; 00177 } 00178 else 00179 { 00180 break; 00181 } 00182 } 00183 } 00184 return 255; 00185 } 00186 00187 void set_timer_in(millis_t future) 00188 { 00189 Timer().template set_timer<self_type, &self_type::timer_elapsed>( 00190 os(), future, this, 0 ); 00191 } 00192 00193 bool check_for_cycle_changes() 00194 { 00195 if ( colors.size() != old_colors.size() ) 00196 return false; 00197 00198 for (uint16_t i = 0; i < colors.size(); i++ ) 00199 { 00200 color_value_type cl = colors[i]; 00201 if( colors.cardinality(cl) != old_colors.cardinality(cl) ) 00202 { 00203 return false; 00204 } 00205 } 00206 00207 return true; 00208 } 00209 /* 00210 inline int fm(int arr[], int b, int n) 00211 { 00212 int f = b; 00213 int c; 00214 00215 for (c = b + 1; c < n; c++) 00216 if (arr[c] > arr[f]) 00217 f = c; 00218 00219 return f; 00220 } 00221 00222 inline void isort(int arr[], int n) 00223 { 00224 int s, w; 00225 int sm; 00226 00227 for (s = 0; s < n - 1; s++) { 00228 w = fm(arr, s, n); 00229 sm = arr[w]; 00230 arr[w] = arr[s]; 00231 arr[s] = sm; 00232 } 00233 }*/ 00234 00235 inline void set_os(Os* os) 00236 { os_ = os; } 00237 00238 inline Os* os() 00239 { return os_; } 00240 00241 inline void set_judge() 00242 { is_judge = true; } 00243 00244 inline bool completed() 00245 { return completed_; } 00246 00247 inline uint16_t ncolors() 00248 { 00249 uint16_t i = 0; 00250 for (; i < colors.size() && colors.cardinalityAt(i)!=0 ; i++ ) 00251 ; 00252 00253 return i; 00254 } 00255 00256 inline bool judge() 00257 { return is_judge; } 00258 00259 inline void set_color(color_value_type color_) 00260 { color = color_; } 00261 00262 inline color_value_type get_color() 00263 { return color; } 00264 00265 inline void set_tree_routing(routing_t *tr) 00266 { tree_routing = tr; } 00267 00268 private: 00269 00272 enum IMJCMsgIds { 00273 IMJCMsgIdBroadcastCL = 110, 00274 IMJCMsgIdColorRQ = 111, 00275 IMJCMsgIdColorRSP = 112, 00276 IMJCMsgIdColorCHG = 113, 00277 IMJCMsgIdColorRSPF = 114 00278 }; 00279 00280 enum IMJCState { 00281 GatheringColors, 00282 ChangingColors 00283 }; 00284 00285 Radio& radio() 00286 { return *radio_; } 00287 00288 Timer& timer() 00289 { return *timer_; } 00290 00291 Debug& debug() 00292 { return *debug_; } 00293 00294 Radio * radio_; 00295 Timer * timer_; 00296 Debug * debug_; 00297 00298 Rand rand; 00299 uint32_t seed; 00300 Os *os_; 00301 Neighborhood_t neighborhood; 00302 uint8_t state; 00303 color_value_type color,old_color; 00304 color_table_t colors,old_colors; 00305 uint16_t my_id,colors_chages_in_round,node_count,msgs_received; 00306 old_new_color aggragated_values[MAX_NODES]; 00307 old_new_color* aggragated_values_finish_; 00308 uint8_t children_; 00309 bool changed_color_in_the_prv_round,is_judge,color_changed,completed_,first_round; 00310 00311 }; 00312 // ----------------------------------------------------------------------- 00313 // -----------Judged Coloring Constructor--------------------------------- 00314 // ----------------------------------------------------------------------- 00315 00316 template<typename OsModel_P, 00317 typename Radio_P, 00318 typename Debug_P, 00319 typename Routing_P> 00320 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00321 IMJudgedColoring() 00322 : os_ ( 0 ), 00323 state ( GatheringColors ), 00324 color ( 255 ), 00325 is_judge ( false ), 00326 color_changed ( false ), 00327 completed_ ( false ), 00328 first_round ( true ) 00329 { 00330 msg_info.IMJCMsgIdBroadcastCL = 0; 00331 msg_info.IMJCMsgIdColorRQ = 0; 00332 msg_info.IMJCMsgIdColorRSP = 0; 00333 msg_info.IMJCMsgIdColorCHG = 0; 00334 cycle_changes_detected = false; 00335 changed_color_in_the_prv_round = false; 00336 aggragated_values_finish_ = &(aggragated_values[0]); 00337 node_count = 0; 00338 colors_chages_in_round = 0; 00339 }; 00340 // -----------Judged Coloring De-Constructor------------------------------ 00341 00342 template<typename OsModel_P, 00343 typename Radio_P, 00344 typename Debug_P, 00345 typename Routing_P> 00346 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00347 ~IMJudgedColoring() 00348 { 00349 }; 00350 // ----------------------------------------------------------------------- 00351 00352 00353 #ifdef ENABLE_AGGREGATION 00354 template<typename OsModel_P, 00355 typename Radio_P, 00356 typename Debug_P, 00357 typename Routing_P> 00358 void 00359 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00360 check_rcd_fragments() 00361 { 00362 if( children_ == 0 ) 00363 { 00364 if ( is_judge ) 00365 { 00366 old_new_color *tmp = &(aggragated_values[0]); 00367 00368 for ( ;tmp!=aggragated_values_finish_ ;tmp++ ) 00369 { 00370 colors.remove( tmp->first ); 00371 colors.insert( tmp->second ); 00372 colors_chages_in_round++; 00373 color_changed = true; 00374 } 00375 00376 if ( !color_changed ) 00377 { 00378 completed_ = true; 00379 } 00380 else 00381 { 00382 if ( color_changed ) 00383 { 00384 if ( check_for_cycle_changes() ) 00385 { 00386 cycle_changes_detected = true; 00387 } 00388 old_colors.parse_array(colors.data(), colors.bytes()); 00389 } 00390 00391 00392 coloring_message message( IMJCMsgIdColorRQ, 0 ); 00393 if( cycle_changes_detected ) 00394 { 00395 message.set_color(1); 00396 cycle_changes_detected = false; 00397 } 00398 00399 message.set_payload( colors.bytes(), colors.data() ); 00400 msg_info.IMJCMsgIdColorRQ++; 00401 tree_routing->send( Radio().BROADCAST_ADDRESS, message.buffer_size(), (uint8_t*)&message ); 00402 00403 if ( change_color() != 255 && state == GatheringColors ) 00404 { 00405 set_timer_in(1000); 00406 // Timer().template set_timer<self_type, &self_type::timer_elapsed>( 00407 // os(), 2000, this, 0 ); 00408 state = ChangingColors; 00409 } 00410 else 00411 { 00412 children_--; 00413 } 00414 #ifdef INFO_IMJUDGEDCOLORING 00415 // Debug().debug(os(), "IMJC: Fr sending color vector (size: %i)\n" , message.buffer_size() ); 00416 Debug().debug(os(), "IMJC Fr Nodes that changed color: %i colors: %i\n" , colors_chages_in_round, colors.size() ); 00417 #endif 00418 // Debug().debug(os(), "IMJC: Node %i want to change from %i to %i\n", my_id, color, change_color() ); 00419 00420 } 00421 // new_round = true; 00422 colors_chages_in_round = 0; 00423 color_changed = false; 00424 msgs_received = 0; 00425 get_color_nodes(); 00426 } 00427 else 00428 { 00429 msg_info.IMJCMsgIdColorRSP++; 00430 coloring_message message( IMJCMsgIdColorRSPF, color ); 00431 message.set_payload((aggragated_values_finish_-&(aggragated_values[0]))*sizeof(old_new_color),(uint8_t*)&(aggragated_values[0])); 00432 tree_routing->send( 1, message.buffer_size(), (uint8_t*)&message ); 00433 } 00434 00435 children_ += tree_routing->get_children().size() + 1; 00436 aggragated_values_finish_ = &(aggragated_values[0]); 00437 } 00438 } 00439 #endif 00440 00441 00442 template<typename OsModel_P, 00443 typename Radio_P, 00444 typename Debug_P, 00445 typename Routing_P> 00446 void 00447 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00448 rcv_routing_message( node_id_t from, size_t len, block_data_t* data) 00449 { 00450 if ( from == my_id ) 00451 return; 00452 00453 #ifdef DEBUG_IMJUDGEDCOLORING 00454 Debug().debug(os(), "IMJC: Node %i Received message from %i with type %i len %i\n", my_id, from, *data, len); 00455 #endif 00456 00457 coloring_message *message = reinterpret_cast<coloring_message*>(data); 00458 00459 if( *data == IMJCMsgIdColorRSP ) 00460 { 00461 if( is_judge ) 00462 { 00463 if ( message->source() != 255 ) 00464 { 00465 colors.remove( message->source() ); 00466 } 00467 colors.insert( message->color() ); 00468 00469 if( message->source() != message->color() ) 00470 { 00471 colors_chages_in_round++; 00472 color_changed = true; 00473 } 00474 00475 msgs_received++; 00476 00477 00478 #ifdef INFO_IMJUDGEDCOLORING 00479 Debug().debug(os(), "IMJC: Node %i has color %i (:%i,%i)\n", from, message->color(), msgs_received, node_count ); 00480 #endif 00481 //CHECK: do we need the tree check? add delay to tree completed 00482 if( ( ( node_count - 1 ) == msgs_received ) ) 00483 { 00484 if ( !color_changed ) 00485 { 00486 completed_ = true; 00487 } 00488 else 00489 { 00490 //CHECK: do we need this? we can 00491 if ( first_round ) 00492 { 00493 old_colors.parse_array(colors.data(), colors.bytes()); 00494 first_round = false; 00495 } 00496 else if ( color_changed ) 00497 { 00498 if ( check_for_cycle_changes() ) 00499 { 00500 cycle_changes_detected = true; 00501 } 00502 old_colors.parse_array(colors.data(), colors.bytes()); 00503 } 00504 00505 coloring_message message( IMJCMsgIdColorRQ, 0 ); 00506 if( cycle_changes_detected ) 00507 { 00508 message.set_color(1); 00509 cycle_changes_detected = false; 00510 } 00511 message.set_payload( colors.bytes(), colors.data() ); 00512 tree_routing->send( Radio().BROADCAST_ADDRESS, message.buffer_size(), (uint8_t*)&message ); 00513 msg_info.IMJCMsgIdColorRQ++; 00514 00515 if ( change_color() != 255 && state == GatheringColors ) 00516 { 00517 set_timer_in(2001); 00518 // Timer().template set_timer<self_type, &self_type::timer_elapsed>( 00519 // os(), 2001, this, 0 ); 00520 state = ChangingColors; 00521 } 00522 else 00523 { 00524 children_--; 00525 } 00526 // Debug().debug(os(), "IMJC: Node %i want to change from %i to %i\n", my_id, msgs_received, node_count ); 00527 00528 #ifdef INFO_IMJUDGEDCOLORING 00529 Debug().debug(os(), "IMJC: sending color vector (size: %i)\n" , message.buffer_size() ); 00530 Debug().debug(os(), "IMJC: Nodes that changed color: %i colors: %i\n" , colors_chages_in_round, colors.size() ); 00531 #endif 00532 } 00533 colors_chages_in_round = 0; 00534 color_changed = false; 00535 msgs_received = 0; 00536 // get_color_nodes(); 00537 } 00538 } 00539 } 00540 #ifdef ENABLE_AGGREGATION 00541 else if ( *data == IMJCMsgIdColorRSPF ) 00542 { 00543 children_--; 00544 uint16_t elements = message->payload_size()/sizeof(old_new_color); 00545 //TODO: remove 00546 // Debug().debug(os(), "The all mighty gw %i: from %i size: %i\n" ,children_, from, message->payload_size() ); 00547 if ( elements != 0 ) 00548 { 00549 memcpy(aggragated_values_finish_,message->payload(),message->payload_size()); 00550 aggragated_values_finish_ += elements; 00551 } 00552 check_rcd_fragments(); 00553 } 00554 #endif 00555 else if( *data == IMJCMsgIdColorRQ ) 00556 { 00557 colors.parse_array( message->payload(), message->payload_size() ); 00558 00559 if ( change_color() != 255 && state == GatheringColors ) 00560 { 00561 if ( (message->color() != 0) && changed_color_in_the_prv_round ) 00562 { 00563 seed += rand.rand(100); 00564 rand.srand( seed ); 00565 uint32_t ran_num = rand.rand(2); 00566 if ( ran_num == 0 ) 00567 { 00568 state = ChangingColors; 00569 set_timer_in(1000); 00570 // Timer().template set_timer<self_type, &self_type::timer_elapsed>( 00571 // os(), 1000, this, 0 ); 00572 } 00573 else 00574 { 00575 #ifdef ENABLE_AGGREGATION 00576 children_--; 00577 check_rcd_fragments(); 00578 #else 00579 coloring_message message( IMJCMsgIdColorRSP, color ); 00580 message.set_source( color ); 00581 msg_info.IMJCMsgIdColorRSP++; 00582 tree_routing->send( 0, message.buffer_size(), (uint8_t*)&message ); 00583 #endif 00584 } 00585 } 00586 else 00587 { 00588 state = ChangingColors; 00589 00590 set_timer_in(1000); 00591 // Timer().template set_timer<self_type, &self_type::timer_elapsed>( 00592 // os(), 1000, this, 0 ); 00593 } 00594 } 00595 else 00596 { 00597 #ifdef ENABLE_AGGREGATION 00598 children_--; 00599 check_rcd_fragments(); 00600 #else 00601 coloring_message message( IMJCMsgIdColorRSP, color ); 00602 message.set_source( color ); 00603 msg_info.IMJCMsgIdColorRSP++; 00604 tree_routing->send( 0, message.buffer_size(), (uint8_t*)&message ); 00605 #endif 00606 } 00607 00608 changed_color_in_the_prv_round = false; 00609 #ifdef DEBUG_IMJUDGEDCOLORING 00610 if ( change_color() != 255 ) 00611 Debug().debug(os(), "GatheringColors IMJC: Node %i want to change from %i to %i\n", my_id, color, change_color() ); 00612 #endif 00613 } 00614 #ifdef DEBUG_IMJUDGEDCOLORING 00615 else 00616 { 00617 Debug().debug(os(), "IMJC: Node %i received msg of Unknown type [:= %i] \n", my_id, *data); 00618 } 00619 #endif 00620 00621 } 00622 // ----------------------------------------------------------------------- 00623 00624 template<typename OsModel_P, 00625 typename Radio_P, 00626 typename Debug_P, 00627 typename Routing_P> 00628 void 00629 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00630 enable( void ) 00631 { 00632 Radio().enable( os() ); 00633 Radio().template reg_recv_callback<self_type, &self_type::receive>( os(), this ); 00634 00635 tree_routing->set_os( os() ); 00636 tree_routing->template reg_recv_callback<self_type, &self_type::rcv_routing_message>( this ); 00637 tree_routing->template reg_state_callback<self_type, &self_type::tree_state_chage>( this ); 00638 00639 colors.clear(); 00640 seed = my_id = color = Radio().id( os() ); 00641 old_color = 255; 00642 00643 if ( is_judge ) 00644 { 00645 tree_routing->set_sink( true ); 00646 colors.insert( color ); 00647 old_colors.clear(); 00648 Timer().template set_timer<self_type, &self_type::neighborhood_timeout>( 00649 os(), 200, this, 0 ); 00650 } 00651 else 00652 { 00653 Timer().template set_timer<self_type, &self_type::neighborhood_timeout>( 00654 os(), 100, this, 0 ); 00655 } 00656 00657 #ifdef INFO_IMJUDGEDCOLORING 00658 Debug().debug(os(), "IMJC: Enabled - starting on node %i with color %i\n", my_id, color ); 00659 #endif 00660 } 00661 // ----------------------------------------------------------------------- 00662 00663 template<typename OsModel_P, 00664 typename Radio_P, 00665 typename Debug_P, 00666 typename Routing_P> 00667 void 00668 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00669 neighborhood_timeout(void *userdata) 00670 { 00671 00672 coloring_message message( IMJCMsgIdBroadcastCL, color ); 00673 Radio().send( os(), Radio().BROADCAST_ADDRESS, message.buffer_size(), (uint8_t*)&message ); 00674 msg_info.IMJCMsgIdBroadcastCL++; 00675 00676 tree_routing->enable(); 00677 } 00678 // ----------------------------------------------------------------------- 00679 00680 template<typename OsModel_P, 00681 typename Radio_P, 00682 typename Debug_P, 00683 typename Routing_P> 00684 void 00685 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00686 disable(void) 00687 { 00688 #ifdef DEBUG_IMJUDGEDCOLORING 00689 Debug().debug(os(), "IMJC: Disabled\n"); 00690 #endif 00691 } 00692 // ----------------------------------------------------------------------- 00693 00694 template<typename OsModel_P, 00695 typename Radio_P, 00696 typename Debug_P, 00697 typename Routing_P> 00698 void 00699 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00700 timer_elapsed(void *userdata) 00701 { 00702 #ifdef DEBUG_IMJUDGEDCOLORING 00703 Debug().debug(os(), "IMJC: timer_elapsed\n"); 00704 #endif 00705 00706 if ( change_color() != 255 && state == ChangingColors ) 00707 { 00708 msg_info.IMJCMsgIdColorCHG++; 00709 coloring_message message( IMJCMsgIdColorCHG, color ); 00710 message.set_source(change_color()); 00711 Radio().send( os(), Radio().BROADCAST_ADDRESS, message.buffer_size(), (uint8_t*)&message ); 00712 Timer().template set_timer<self_type, &self_type::color_timeout>( 00713 os(), 2000, this, 0 ); 00714 #ifdef DEBUG_IMJUDGEDCOLORING 00715 Debug().debug(os(), "IMJC: %i: not changing color\n", my_id ); 00716 #endif 00717 } 00718 else 00719 { 00720 #ifdef ENABLE_AGGREGATION 00721 children_--; 00722 check_rcd_fragments(); 00723 #else 00724 if ( !is_judge ) 00725 { 00726 msg_info.IMJCMsgIdColorRSP++; 00727 coloring_message message( IMJCMsgIdColorRSP, color ); 00728 message.set_source( color ); 00729 tree_routing->send( 0, message.buffer_size(), (uint8_t*)&message ); 00730 } 00731 #endif 00732 00733 #ifdef DEBUG_IMJUDGEDCOLORING 00734 Debug().debug(os(), "IMJC: %i: not changing color\n", my_id ); 00735 #endif 00736 } 00737 } 00738 // ----------------------------------------------------------------------- 00739 00740 template<typename OsModel_P, 00741 typename Radio_P, 00742 typename Debug_P, 00743 typename Routing_P> 00744 void 00745 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00746 color_timeout(void *userdata) 00747 { 00748 #ifdef DEBUG_IMJUDGEDCOLORING 00749 Debug().debug(os(), "IMJC: color_timeout\n"); 00750 #endif 00751 00752 if ( change_color() != 255 && state == ChangingColors ) 00753 { 00754 state = GatheringColors; 00755 #ifdef DEBUG_IMJUDGEDCOLORING 00756 Debug().debug(os(), "IMJC: %i: changing color from %i to %i\n", my_id, color, change_color()); 00757 #endif 00758 00759 #ifdef ENABLE_AGGREGATION 00760 aggragated_values_finish_->first = color; 00761 aggragated_values_finish_->second = change_color(); 00762 aggragated_values_finish_++; 00763 children_--; 00764 check_rcd_fragments(); 00765 #else 00766 if ( !is_judge ) 00767 { 00768 msg_info.IMJCMsgIdColorRSP++; 00769 coloring_message message2( IMJCMsgIdColorRSP, change_color() ); 00770 message2.set_source( color ); 00771 tree_routing->send( 0, message2.buffer_size(), (uint8_t*)&message2 ); 00772 } 00773 else 00774 { 00775 colors.remove( color ); 00776 colors.insert( change_color() ); 00777 } 00778 #endif 00779 00780 changed_color_in_the_prv_round = true; 00781 old_color = color; 00782 color = change_color(); 00783 coloring_message message( IMJCMsgIdBroadcastCL, color ); 00784 Radio().send( os(), Radio().BROADCAST_ADDRESS, message.buffer_size(), (uint8_t*)&message ); 00785 msg_info.IMJCMsgIdBroadcastCL++; 00786 } 00787 else 00788 { 00789 00790 #ifdef ENABLE_AGGREGATION 00791 children_--; 00792 check_rcd_fragments(); 00793 #else 00794 if ( !is_judge ) 00795 { 00796 msg_info.IMJCMsgIdColorRSP++; 00797 coloring_message message( IMJCMsgIdColorRSP, color ); 00798 message.set_source( color ); 00799 tree_routing->send( 0, message.buffer_size(), (uint8_t*)&message ); 00800 } 00801 #endif 00802 00803 #ifdef DEBUG_IMJUDGEDCOLORING 00804 Debug().debug(os(), "IMJC: %i: not changing color\n", my_id ); 00805 #endif 00806 } 00807 } 00808 00809 // ----------------------------------------------------------------------- 00810 00811 template<typename OsModel_P, 00812 typename Radio_P, 00813 typename Debug_P, 00814 typename Routing_P> 00815 void 00816 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00817 tree_state_chage(uint8_t state) 00818 { 00819 switch ( state ) 00820 { 00821 case routing_t::TrNodeConnected : 00822 { 00823 00824 #ifdef INFO_IMJUDGEDCOLORING 00825 Debug().debug(os(), "IMJC: Node %i Tree state changed: TrNodeConnected tree size: %i\n", 00826 my_id, tree_routing->get_tree_size() ); 00827 00828 children_t children = tree_routing->get_children(); 00829 for (children_iterator_t it = children.begin(); it != children.end(); it++) 00830 { 00831 Debug().debug(os(), " child: %i \n",(node_id_t)*it); 00832 } 00833 #endif 00834 node_count = tree_routing->get_tree_size(); 00835 children_ = tree_routing->get_children().size() + 1; 00836 00837 if( !is_judge ) 00838 { 00839 coloring_message message( IMJCMsgIdColorRSP, color ); 00840 message.set_source(255); 00841 tree_routing->send( 0, message.buffer_size(), (uint8_t*)&message ); 00842 msg_info.IMJCMsgIdColorRSP++; 00843 } 00844 break; 00845 } 00846 case routing_t::TrLeafConnected : 00847 { 00848 #ifdef DEBUG_IMJUDGEDCOLORING 00849 Debug().debug(os(), "IMJC: Node %i Tree state changed: TrLeafConnected\n",my_id); 00850 #endif 00851 00852 node_count = 0; 00853 children_ = 1; 00854 00855 coloring_message message( IMJCMsgIdColorRSP, color ); 00856 message.set_source(255); 00857 tree_routing->send( 0, message.buffer_size(), (uint8_t*)&message ); 00858 msg_info.IMJCMsgIdColorRSP++; 00859 break; 00860 } 00861 #ifdef DEBUG_IMJUDGEDCOLORING 00862 default : 00863 Debug().debug(os(), "IMJC: Node %i Tree state changed: Error\n",my_id); 00864 #endif 00865 00866 } 00867 00868 } 00869 00870 // ----------------------------------------------------------------------- 00871 00872 template<typename OsModel_P, 00873 typename Radio_P, 00874 typename Debug_P, 00875 typename Routing_P> 00876 void 00877 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00878 receive(node_id_t from, size_t len, block_data_t *data) 00879 { 00880 00881 if ( from == my_id ) 00882 return; 00883 00884 uint8_t msg_id = *data; 00885 coloring_message *message = reinterpret_cast<coloring_message*>(data);; 00886 00887 if ( IMJCMsgIdColorCHG == msg_id ) 00888 { 00889 if ( state == ChangingColors ) 00890 { 00891 if ( from > my_id ) 00892 { 00893 state = GatheringColors; 00894 } 00895 } 00896 } 00897 else if ( IMJCMsgIdBroadcastCL == msg_id ) 00898 { 00899 //CHECK: do we need this? 00900 state = GatheringColors; 00901 message = reinterpret_cast<coloring_message*>(data); 00902 00903 Neighborhood_iterator_t it = neighborhood.begin(); 00904 for ( ; it != neighborhood.end() ; it++ ) 00905 { 00906 if ( (*it).first == from ) 00907 { 00908 (*it).second = message->color(); 00909 break; 00910 } 00911 } 00912 if ( it == neighborhood.end() ) 00913 { 00914 neighborhood.push_back( node_color( from, message->color() ) ); 00915 } 00916 00917 #ifdef INFO_IMJUDGEDCOLORING 00918 Debug().debug(os(), "IMJC: %i: received color %i from %i\n", my_id, message->color(), from ); 00919 #endif 00920 00921 } 00922 00923 } 00924 // ----------------------------------------------------------------------- 00925 00926 template<typename OsModel_P, 00927 typename Radio_P, 00928 typename Debug_P, 00929 typename Routing_P> 00930 const uint16_t //FIX: change type to uint8_t 00931 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00932 get_neighboors() 00933 { 00934 00935 for (Neighborhood_iterator_t it = neighborhood.begin(); it != neighborhood.end(); it++ ) 00936 { 00937 Debug().debug(os(), "IMJC: %i: received color %i from neigbor %i \n", my_id, (*it).first, (*it).second ); 00938 } 00939 return neighborhood.size(); 00940 } 00941 // ----------------------------------------------------------------------- 00942 00943 template<typename OsModel_P, 00944 typename Radio_P, 00945 typename Debug_P, 00946 typename Routing_P> 00947 uint16_t 00948 IMJudgedColoring<OsModel_P, Radio_P, Debug_P, Routing_P>:: 00949 get_color_nodes() 00950 { 00951 00952 #ifdef INFO_IMJUDGEDCOLORING 00953 Debug().debug(os(), "IMJC: %i: My color vector has %i elements:\n", my_id, colors.size() ); 00954 00955 for (uint16_t i = 0; i < colors.size(); i++ ) 00956 Debug().debug(os(), "IMJC: Color %i has Count %i\n", colors[i], colors.cardinalityAt(i) ); 00957 #endif 00958 00959 return colors.size(); 00960 } 00961 00962 } 00963 #endif