Wiselib
wiselib.testing/algorithms/routing/tora/tora_routing.h
Go to the documentation of this file.
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_ROUTING_TORA_ROUTING_H__
00020 #define __ALGORITHMS_ROUTING_TORA_ROUTING_H__
00021 
00022 #include "util/base_classes/routing_base.h"
00023 #include "algorithms/routing/tora/tora_routing_types.h"
00024 #include "algorithms/routing/tora/tora_routing_message.h"
00025 #include "algorithms/routing/tora/tora_broadcast_message.h"
00026 #include <string.h>
00027 #include <map>
00028 #include <limits.h>
00029 
00030 #undef DEBUG
00031 #define DEBUG
00032 #define UN 0
00033 #define DN 1
00034 #define UP 2
00035 
00036 #define ECHO_INTERVAL 2
00037 #define ALLOWED_LOSS ECHO_INTERVAL * 2
00038 
00039 #define NET_DIAM 30
00040 #define TORA_TIMEOUT NET_DIAM * 2
00041 
00042 using namespace std;
00043 namespace wiselib {
00044 
00053     template<typename OsModel_P,
00054             typename RoutingTable_P,
00055             typename Radio_P = typename OsModel_P::Radio,
00056             //             typename Timer_P = typename OsModel_P::Timer,
00057             typename Debug_P = typename OsModel_P::Debug>
00058             class ToraRouting
00059             : public RoutingBase<OsModel_P, Radio_P> {
00060     public:
00061         typedef OsModel_P OsModel;
00062         typedef Radio_P Radio;
00063         //       typedef Timer_P Timer;
00064         typedef Debug_P Debug;
00065 
00066         typedef RoutingTable_P RoutingTable;
00067         typedef typename RoutingTable::iterator RoutingTableIterator;
00068         typedef typename RoutingTable::value_type RoutingTableValue;
00069         typedef typename RoutingTable::mapped_type RoutingTableEntry;
00070         typedef ToraRouting<OsModel, RoutingTable, Radio, Debug> self_type;
00071         typedef typename OsModel_P::Timer Timer;
00072 
00073         typedef typename Radio::node_id_t node_id_t;
00074         typedef typename Radio::size_t size_t;
00075         typedef typename Radio::block_data_t block_data_t;
00076 
00077         typedef typename Timer::millis_t millis_t;
00078         typedef ToraRoutingMessage<OsModel, Radio> RoutingMessage;
00079         typedef ToraBroadcastMessage<OsModel, Radio, RoutingTableValue, 8 > BroadcastMessage;
00080 
00081         typedef map<int, RoutingMessage*> pend_msg_t;
00082 
00083         typedef map<int, height> NeighborsHeight;
00084         typedef std::map<int, bool> route_required_flag;
00085         typedef std::map<int, int> link_state;
00086         // --------------------------------------------------------------------
00087         enum SpecialNodeIds {
00088            BROADCAST_ADDRESS = Radio_P::BROADCAST_ADDRESS, 
00089            NULL_NODE_ID      = Radio_P::NULL_NODE_ID      
00090         };
00091         // --------------------------------------------------------------------
00092         enum Restrictions {
00093            MAX_MESSAGE_LENGTH = Radio_P::MAX_MESSAGE_LENGTH - RoutingMessage::PAYLOAD_POS  
00094         };
00095         // --------------------------------------------------------------------
00098         ToraRouting();
00099         ~ToraRouting();
00101 
00104         void enable_radio(void);
00105         void disable_radio(void);
00107 
00110 
00112         void send( node_id_t receiver, size_t len, block_data_t *data );
00115         void receive( node_id_t from, size_t len, block_data_t *data );
00118         typename Radio::node_id_t id()
00119         {
00120            return radio_->id();
00121         };
00123 
00126         void timer_elapsed(void *userdata);
00128 
00129         //Receive and process a BDC message
00130         void recvBDC(node_id_t);
00131 
00132         //Receive and process a QRY message
00133         void recvQRY(node_id_t, int);
00134 
00135         //Receive and process an UPD message
00136         void recvUPD(node_id_t, RoutingMessage*);
00137 
00138         //Receive and process a CLR message
00139         void recvCLR(node_id_t, RoutingMessage*);
00140 
00141         //Receive and process a DATA message
00142         void recvDATA(node_id_t, RoutingMessage*);
00143 
00144         //Clear Height for all destination after TIMEOUT period
00145         void clearDest();
00146 
00150         //Print Neighbors
00151         inline void printNeighbors();
00152         
00153         //Print height 
00154         inline void printHeight(int);
00155 
00156         void init( Radio& radio, Timer& timer, Debug& debug ) {
00157           radio_ = &radio;
00158           timer_ = &timer;
00159           debug_ = &debug;
00160         }
00161         
00162         void destruct() {
00163         }
00164         
00165     private:
00166         Radio& radio()
00167         { return *radio_; }
00168         
00169         Timer& timer()
00170         { return *timer_; }
00171         
00172         Debug& debug()
00173         { return *debug_; }
00174       
00175         Radio * radio_;
00176         Timer * timer_;
00177         Debug * debug_;
00178 
00179         //Map: Destination ID, Route reruest flag
00180         route_required_flag RR;
00181 
00182         millis_t startup_time_;
00183         millis_t work_period_;
00184 
00185         //Map: Destination ID, timestamp
00186         map<int, int> destinations;
00187 
00188         //Pending messages
00189         pend_msg_t pend_msgs;
00190         
00191         //Node Height per destination
00192         map<int, height> H;
00193 
00194         //Neighbors Height per destination
00195         map<int, NeighborsHeight> NH;
00196 
00197         //Link state per destination
00198         map<int, link_state> LS;
00199 
00200         //Map with neigbors id
00201         map<int, int> neighbors;
00202 
00203         //Downstream links
00204         int DNlinks;
00205 
00206         //Map: Neighbor ID, Heisht Struct
00207         NeighborsHeight::iterator NHIter;
00208 
00209         //Iterator for neighbors map
00210         map<int, int>::iterator neighborsIter;
00211 
00212         //Iterator for destinations map
00213         map<int, int>::iterator destinationsIter;
00214 
00215         //Iterator for Pending Messages
00216         typedef typename pend_msg_t::iterator pend_msg_iter;
00217 
00218         //Link state iterator
00219         link_state::iterator LSIter;
00220 
00221         //RR iterator
00222         route_required_flag::iterator RRIter;
00223 
00224         //Determine whether a Neighbor Height was set
00225         bool NH_exists(int, int);
00226 
00227         //Determine whether a Neighbor exists
00228         bool neighbor_exists(int);
00229 
00230         //Determine whether a destination exist
00231         bool destination_exists(int);
00232 
00233         //Determine whether a RR flag for a specific destination was set
00234         bool RR_flag_is_set(int);
00235 
00236         //Count downstream links for a specific destination
00237         int count_DNs(int);
00238 
00239         //Updates link state map for a specific destination
00240         void updateLS(int);
00241 
00242         //Determine whether all reference levels for a destination are equal
00243         bool ref_lvls_eq(int);
00244 
00245         //Returns the node id with the minimun reference level for a specific destination
00246         int find_min_ref_lvl(int);
00247 
00248         //Determine whether all r on Neighbors Height are equal with 1
00249         bool r_eq_one(int);
00250 
00251         //Determine whether originator ID is equal with the node ID for a specific destination
00252         bool oid_eq_id(int);
00253 
00254         //Empties the buffer and sends stored messages
00255         void empty_buffer(int);
00256 
00257         //Determine whether the Height for a specific destination is Zero
00258         bool heightIsZero(height tmpHeight) {
00259             return (tmpHeight.time == 0 && tmpHeight.originator_id == 0
00260                     && tmpHeight.r == 0 && tmpHeight.delta == 0);
00261         }
00262 
00263         //Determine whether the Height for a specific destination is Null
00264         bool heightIsNull(height tmpHeight) {
00265             return (tmpHeight.time == SHRT_MIN && tmpHeight.originator_id == SHRT_MIN
00266                     && tmpHeight.r == SHRT_MIN && tmpHeight.delta == SHRT_MIN);
00267         }
00268         // ----------------------------------------------------------------------
00269         //                          Private ECHO functions
00270         // ----------------------------------------------------------------------
00271 
00272         void decrease();
00273         
00274         void clean_Dead_Neighbors();
00275 
00276         void broadcaster();
00277 
00278         int timer_cnt;
00279     };
00280     // -----------------------------------------------------------------------
00281     // -----------------------------------------------------------------------
00282     // -----------------------------------------------------------------------
00283 
00284     template<typename OsModel_P,
00285     typename RoutingTable_P,
00286     typename Radio_P,
00287     typename Debug_P>
00288     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
00289     ToraRouting()
00290     : startup_time_(1000),
00291     work_period_(5000),
00292     timer_cnt(0){
00293     };
00294     // -----------------------------------------------------------------------
00295 
00296     template<typename OsModel_P,
00297     typename RoutingTable_P,
00298     typename Radio_P,
00299     typename Debug_P>
00300     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
00301     ~ToraRouting() {
00302 #ifdef DEBUG
00303         debug().debug("Tora Routing Destroyed\n");
00304 #endif
00305     };
00306     // -----------------------------------------------------------------------
00307 
00308     template<typename OsModel_P,
00309     typename RoutingTable_P,
00310     typename Radio_P,
00311     typename Debug_P>
00312     void
00313     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
00314     enable_radio(void) {
00315 #ifdef DEBUG
00316         debug().debug("ToraRouting Boots for %i\n", radio().id());
00317 #endif
00318         broadcaster();
00319         radio().enable_radio();
00320         radio().template reg_recv_callback<self_type, &self_type::receive > (this);
00321         timer().template set_timer<self_type, &self_type::timer_elapsed > (startup_time_, this, 0);
00322         
00323     }
00324     // -----------------------------------------------------------------------
00325 
00326     template<typename OsModel_P,
00327     typename RoutingTable_P,
00328     typename Radio_P,
00329     typename Debug_P>
00330     void
00331     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
00332     disable_radio(void) {
00333 #ifdef DEBUG
00334         debug().debug("Called ToraRouting::disable\n");
00335 #endif
00336     }
00337     // -----------------------------------------------------------------------
00338 
00339     template<typename OsModel_P,
00340     typename RoutingTable_P,
00341     typename Radio_P,
00342     typename Debug_P>
00343     void
00344     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
00345     send(node_id_t destination, size_t len, block_data_t * data) {
00346 
00347 
00348         if (neighbor_exists(destination)){
00349             cout << "\tDestination  " << destination << " is a neighbor " << endl;
00350         }else{
00351             cout << "\tNo route from " << radio().id() << " to " << destination << " -- Initializing TORA " << endl;
00352             RoutingMessage QRYmsg;
00353             QRYmsg.set_msg_id(QRY);
00354             QRYmsg.set_destination(destination);
00355             radio().send(radio().BROADCAST_ADDRESS, QRYmsg.buffer_size(), (uint8_t*) & QRYmsg);
00356             RR[destination] = true;
00357             pend_msgs[destination] ;
00358 
00359         }
00360 
00361     }
00362     // ----------------------------------------------------------------------
00363     //                          Private ECHO functions
00364     // ----------------------------------------------------------------------
00365     template<typename OsModel_P,
00366     typename RoutingTable_P,
00367     typename Radio_P,
00368     typename Debug_P>
00369     void
00370     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::recvBDC(node_id_t from) {
00371 #ifdef DEBUG
00372       //  debug().debug("BCD Rcvd at: %i  from: %i\n", radio().id(), from);
00373 #endif
00374         //Add new neighbor
00375         neighbors[from] = ALLOWED_LOSS;
00376     }
00377     
00378     // -----------------------------------------------------------------------
00379     template<typename OsModel_P,
00380     typename RoutingTable_P,
00381     typename Radio_P,
00382     typename Debug_P>
00383     void
00384     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::broadcaster(){
00385         
00386         //Decrease neighbors timestamp
00387         decrease();
00388         /*cout<< "Node :\t" << owner().id() << " Neighbors : ";
00389         map<int,int>::iterator iterator;
00390         for (iterator = neighbors.begin(); iterator != neighbors.end(); iterator++) {
00391             cout << "("<<iterator->first << " , " << iterator->second << ") " ;
00392         }
00393         cout << endl;*/
00394 
00395         if(timer_cnt % ECHO_INTERVAL == 0){
00396             BroadcastMessage message;
00397             message.set_msg_id(ToraBroadcastMsgId);
00398             radio().send(radio().BROADCAST_ADDRESS, message.buffer_size(), (uint8_t*) & message);
00399         }
00400         
00401         if(timer_cnt == ALLOWED_LOSS){
00402             clean_Dead_Neighbors();
00403             timer_cnt = 0;
00404          }
00405 
00406          timer_cnt++;
00407 
00408     }
00409 
00410 // ----------------------------------------------------------------------
00411     template<typename OsModel_P,
00412     typename RoutingTable_P,
00413     typename Radio_P,
00414     typename Debug_P>
00415     void
00416     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::decrease(){
00417         map<int,int>::iterator iterator;
00418         for (iterator = neighbors.begin(); iterator != neighbors.end(); iterator++) {
00419             --iterator->second;
00420         }
00421     }
00422 
00423 // ----------------------------------------------------------------------
00424     template<typename OsModel_P,
00425     typename RoutingTable_P,
00426     typename Radio_P,
00427     typename Debug_P>
00428     void
00429     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::clean_Dead_Neighbors(){
00430         map<int,int>::iterator iterator;
00431         int tmp_id;
00432         int tmp_s;
00433         for (iterator = neighbors.begin(); iterator != neighbors.end() ; iterator++) {
00434             tmp_id = iterator->first;
00435             tmp_s = iterator->second;
00436             
00437             if(tmp_s <= 0){
00438 
00439                map<int, NeighborsHeight>::iterator NHiter;
00440                 for(NHiter = NH.begin(); NHiter != NH.end(); NHiter++){
00441                     if(NH_exists( tmp_id,NHiter->first)){
00442                         NH[NHiter->first].erase(tmp_id);
00443                         LS[NHiter->first].erase(tmp_id);
00444                         
00445                         cout << radio().id()<<" : "<< " Delete DEAD neighbor " << tmp_id  << "-- "<<iterator->second << " destination " << NHiter->first << endl;
00446                         updateLS(NHiter->first);
00447 
00448 
00449 
00450                            if (count_DNs(NHiter->first) == 0 && radio().id() != NHiter->first ){
00451                                 cout << radio().id() << " : DnLinks = " <<  count_DNs(NHiter->first) << " for destination : "<< NHiter->first<<endl;
00452 
00453                                 //Propagate new ref lvl
00454                                 //Propagates the new Height
00455                                 int msg_dest = NHiter->first;
00456                                 H[msg_dest].time = H[msg_dest].time + 2;
00457                                 H[msg_dest].originator_id = radio().id();
00458                                 H[msg_dest].r = 0;
00459                                 H[msg_dest].delta = 0;
00460                                 H[msg_dest].ID = radio().id();
00461                                 updateLS(msg_dest);
00462 
00463                                 RoutingMessage UPDmsg;
00464                                 UPDmsg.set_msg_id(UPD);
00465                                 UPDmsg.set_destination(msg_dest);
00466                                 UPDmsg.set_height(H[msg_dest]);
00467                                 radio().send(radio().BROADCAST_ADDRESS, UPDmsg.buffer_size(), (uint8_t*) & UPDmsg);
00468                                 cout<<"\tBroadcating an UPD message"<<endl;
00469                             }
00470                             neighbors.erase(tmp_id);
00471                     }
00472                 }
00473 
00474 
00475             }
00476 
00477         }
00478     }
00479 
00480 
00481 // ----------------------------------------------------------------------
00482 //                          Private Tora functions
00483 // ----------------------------------------------------------------------
00484     
00485 
00486     template<typename OsModel_P,
00487     typename RoutingTable_P,
00488     typename Radio_P,
00489     typename Debug_P>
00490     void
00491     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
00492     timer_elapsed(void* userdata) {
00493 
00494 
00495         //Excecutes the Broadcaster
00496         broadcaster();
00497         
00498         //Clear Destinations table
00499         clearDest();
00500         
00501         
00502         timer().template set_timer<self_type, &self_type::timer_elapsed > (startup_time_, this, 0);
00503 
00504     }
00505     // -----------------------------------------------------------------------
00506 
00507     template<typename OsModel_P,
00508     typename RoutingTable_P,
00509     typename Radio_P,
00510     typename Debug_P>
00511     void
00512     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
00513     receive(node_id_t from, size_t len, block_data_t *data) {
00514 
00515         if (from == radio().id())
00516             return;
00517 
00518         uint8_t msg_id = *data;
00519         switch (msg_id){
00520             case ToraBroadcastMsgId:
00521             {
00522                 recvBDC(from);
00523                 break;
00524             }
00525             case QRY:
00526             {
00527                 RoutingMessage *message = reinterpret_cast<RoutingMessage*> (data);
00528                 recvQRY(from, message->destination());
00529                 break;
00530             }
00531             case UPD:
00532             {
00533                 RoutingMessage *message = reinterpret_cast<RoutingMessage*> (data);
00534                 recvUPD(from, message);
00535                 break;
00536             }
00537             case CLR:
00538             {
00539                 RoutingMessage *message = reinterpret_cast<RoutingMessage*> (data);
00540                 recvCLR(from, message);
00541                 break;
00542             }
00543             case DATA:
00544             {
00545                 RoutingMessage *message = reinterpret_cast<RoutingMessage*> (data);
00546                 recvDATA(from, message);                
00547                 break;
00548             }
00549         }
00550     }
00551     
00552     // -----------------------------------------------------------------------
00553     template<typename OsModel_P,
00554     typename RoutingTable_P,
00555     typename Radio_P,
00556     typename Debug_P>
00557     void
00558     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::recvDATA(node_id_t from, RoutingMessage* hmsg) {
00559         if(hmsg->next_node() ==  radio().id()){
00560             cout<< radio().id() << " : Received a DATA message from "<< from << " with destination "<< hmsg->destination()<<endl;
00561             if(hmsg->destination() == radio().id()){
00562                 cout<<"\tData message delivered to destination"<< endl;
00563             }else{
00564                 //Check if message destination is a neighbor
00565                 if(neighbor_exists(hmsg->destination())){
00566                     RoutingMessage DATAmsg;
00567                     DATAmsg.set_msg_id(DATA);
00568                     DATAmsg.set_next_node(hmsg->destination());
00569                     DATAmsg.set_destination(hmsg->destination());
00570                     radio().send(radio().BROADCAST_ADDRESS, DATAmsg.buffer_size(), (uint8_t*) & DATAmsg);
00571                     cout<<"\tForwarding a Data message to destination "<< hmsg->destination()<<endl;
00572                 }else{
00573                     int tmpH = SHRT_MAX;
00574                     int next_node = SHRT_MAX;
00575                     for (LSIter = LS[hmsg->destination()].begin(); LSIter != LS[hmsg->destination()].end(); LSIter++) {
00576                         if (LSIter->second == DN){
00577                             if(NH[hmsg->destination()][LSIter->first].delta > 0 && NH[hmsg->destination()][LSIter->first].delta < tmpH){
00578                                 tmpH = NH[hmsg->destination()][LSIter->first].delta;
00579                                 next_node = LSIter->first;
00580                             }else if (NH[hmsg->destination()][LSIter->first].delta < 0){
00581                                 next_node = LSIter->first;
00582                             }
00583                         }
00584                     }
00585                     RoutingMessage DATAmsg;
00586                     DATAmsg.set_msg_id(DATA);
00587                     DATAmsg.set_next_node(next_node);
00588                     DATAmsg.set_destination(hmsg->destination());
00589                     radio().send(radio().BROADCAST_ADDRESS, DATAmsg.buffer_size(), (uint8_t*) & DATAmsg);
00590                     cout<<"\tForwarding a Data message with destination "<< hmsg->destination() << " to "<< next_node <<endl;
00591 
00592                 }
00593 
00594             }
00595             
00596         }
00597 
00598      }
00599      // -----------------------------------------------------------------------
00600 
00601     template<typename OsModel_P,
00602     typename RoutingTable_P,
00603     typename Radio_P,
00604     typename Debug_P>
00605     void
00606     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::recvCLR(node_id_t from, RoutingMessage* hmsg) {
00607         int msg_dest = hmsg->destination();
00608         int msg_sour = from;
00609 
00610         cout<< radio().id() << " Received a CLR message from "<< msg_sour <<endl;
00611 
00612         //Update Neighbors Height
00613         NH[msg_dest][msg_sour].time = hmsg->get_height().time;
00614         NH[msg_dest][msg_sour].originator_id = hmsg->get_height().originator_id;
00615         NH[msg_dest][msg_sour].r = hmsg->get_height().r;
00616         NH[msg_dest][msg_sour].delta = hmsg->get_height().delta;
00617 
00618         if(!heightIsNull(H[msg_dest])){
00619 
00620             if(!neighbor_exists(msg_dest)){
00621                 //Update Height
00622                 H[msg_dest].time = SHRT_MIN;
00623                 H[msg_dest].originator_id = SHRT_MIN;
00624                 H[msg_dest].r = SHRT_MIN;
00625                 H[msg_dest].delta = SHRT_MIN;
00626             }else{
00627                 H[msg_dest].time = 0;
00628                 H[msg_dest].originator_id = 0;
00629                 H[msg_dest].r = 0;
00630                 H[msg_dest].delta = 0;
00631             }
00632 
00633 
00634             updateLS(msg_dest);
00635 
00636             RoutingMessage CLRmsg;
00637             CLRmsg.set_msg_id(CLR);
00638             CLRmsg.set_destination(msg_dest);
00639             CLRmsg.set_height(H[msg_dest]);
00640             radio().send(radio().BROADCAST_ADDRESS, CLRmsg.buffer_size(), (uint8_t*) & CLRmsg);
00641      
00642             cout<<"\tRebroadcasting the CLR message"<<endl;
00643         }
00644     }
00645     // -----------------------------------------------------------------------
00646 
00647     template<typename OsModel_P,
00648     typename RoutingTable_P,
00649     typename Radio_P,
00650     typename Debug_P>
00651     void
00652     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::recvUPD(node_id_t from, RoutingMessage* hmsg) {
00653         int msg_dest = hmsg->destination();
00654         int msg_sour = from;
00655 
00656         destinations[msg_dest] = TORA_TIMEOUT;
00657 
00658         cout << radio().id() << " : Received UPD from " << msg_sour << " : ("
00659                 << hmsg->get_height().time << "," << hmsg->get_height().originator_id << ","
00660                 << hmsg->get_height().r << "," << hmsg->get_height().delta << ","
00661                 << hmsg->get_height().ID << ")" <<endl;
00662 
00663         //Update neighbors Height
00664         NH[msg_dest][msg_sour].time = hmsg->get_height().time;
00665         NH[msg_dest][msg_sour].originator_id = hmsg->get_height().originator_id;
00666         NH[msg_dest][msg_sour].r = hmsg->get_height().r;
00667         NH[msg_dest][msg_sour].delta = hmsg->get_height().delta;
00668         NH[msg_dest][msg_sour].ID = hmsg->get_height().ID;
00669 
00670         //Update Height only if flag is set
00671         
00672         if (((heightIsNull(H[msg_dest]) && RR[msg_dest] == true) ) ||
00673                (!heightIsNull(H[msg_dest]) && (hmsg->get_height().delta +1 ) < H[msg_dest].delta)) {
00674 
00675             H[msg_dest].time = hmsg->get_height().time;
00676             H[msg_dest].originator_id = hmsg->get_height().originator_id;
00677             H[msg_dest].r = hmsg->get_height().r;
00678             H[msg_dest].delta = hmsg->get_height().delta + 1;
00679 
00680             RR[msg_dest] = false;
00681             updateLS(msg_dest);
00682 
00683             RoutingMessage UPDmsg;
00684             UPDmsg.set_msg_id(UPD);
00685             UPDmsg.set_destination(msg_dest);
00686             UPDmsg.set_height(H[msg_dest]);
00687             radio().send(radio().BROADCAST_ADDRESS, UPDmsg.buffer_size(), (uint8_t*) & UPDmsg);
00688             cout<< "\tBroadcasting an UPD message"<<endl;
00689 
00690 
00691 
00692         }//Update LS
00693         else {
00694 
00695             updateLS(msg_dest);
00696             DNlinks = count_DNs(msg_dest);            
00697 
00698             //Link failure. No downstream links
00699             if (DNlinks == 0 && msg_dest!=radio().id()) {
00700 
00701                 //Propagate Case
00702              if (!ref_lvls_eq(msg_dest)) {
00703                       cout<< "\t --Propagate case 2"<<endl;
00704 
00705                     //Find the reference level of its highest neighbor and selects a height
00706                     //which is lower than all neighbors with that reference level
00707                     int neighID = find_min_ref_lvl(msg_dest);
00708 
00709                     //Update height
00710                     H[msg_dest].time = NH[msg_dest][neighID].time  ;
00711                     H[msg_dest].originator_id = NH[msg_dest][neighID].originator_id;
00712                     H[msg_dest].r = NH[msg_dest][neighID].r;
00713                     H[msg_dest].delta = NH[msg_dest][neighID].delta - 1;
00714 
00715                     //Propagates the new Height
00716                     RoutingMessage UPDmsg;
00717                     UPDmsg.set_msg_id(UPD);
00718                     UPDmsg.set_destination(msg_dest);
00719                     UPDmsg.set_height(H[msg_dest]);
00720                     radio().send(radio().BROADCAST_ADDRESS, UPDmsg.buffer_size(), (uint8_t*) & UPDmsg);
00721                     cout<<"\tBroadcasting an UPD message"<<endl;
00722                     updateLS(msg_dest);
00723                 //cout<< count_DNs(msg_dest) <<endl;
00724                 }
00725                 else{
00726                     //Reflect case with r = 0
00727                     if(!r_eq_one(msg_dest)){
00728                         //Update height
00729                         cout<< "\t --Reflect case 3"<<endl;
00730                         H[msg_dest].time = NH[msg_dest][msg_sour].time ;
00731                         H[msg_dest].originator_id = NH[msg_dest][msg_sour].originator_id;
00732                         H[msg_dest].r = 1;
00733                         H[msg_dest].delta = 0;
00734 
00735                         //Reflects back a higher sub-level
00736                         RoutingMessage UPDmsg;
00737                         UPDmsg.set_msg_id(UPD);
00738                         UPDmsg.set_destination(msg_dest);
00739                         UPDmsg.set_height(H[msg_dest]);
00740                         radio().send(radio().BROADCAST_ADDRESS, UPDmsg.buffer_size(), (uint8_t*) & UPDmsg);
00741                         cout<<"\tBroadcasting an UPD message"<<endl;
00742                         updateLS(msg_dest);
00743                         //cout<< count_DNs(msg_dest) <<endl;
00744 
00745                     }//r = 1
00746                     else{
00747                         //Detect case with originator_id == owner().id()
00748                         if (oid_eq_id(msg_dest)) {
00749                            H[msg_dest].time = SHRT_MIN;
00750                             H[msg_dest].originator_id = SHRT_MIN;
00751                             H[msg_dest].r = SHRT_MIN;
00752                             H[msg_dest].delta = SHRT_MIN;
00753 
00754                             RoutingMessage CLRmsg;
00755                             CLRmsg.set_msg_id(CLR);
00756                             CLRmsg.set_destination(msg_dest);
00757                             CLRmsg.set_height(H[msg_dest]);
00758                             radio().send(radio().BROADCAST_ADDRESS, CLRmsg.buffer_size(), (uint8_t*) & CLRmsg);
00759                             cout<<"\tBroadcasting a CLR message"<<endl;
00760 
00761                             updateLS(msg_dest);
00762 
00763 
00764                         }//Generate case with originator_id != owner().id()
00765                         else {
00766                             cout << "\t --Generate Case 5" << endl;
00767                             //Update Height
00768                             H[msg_dest].time = NH[msg_dest][msg_sour].time;
00769                             H[msg_dest].originator_id = NH[msg_dest][msg_sour].originator_id;
00770                             H[msg_dest].r = 0;
00771                             H[msg_dest].delta = 0;
00772 
00773                             //Propagates the new Height
00774                             RoutingMessage UPDmsg;
00775                             UPDmsg.set_msg_id(UPD);
00776                             UPDmsg.set_destination(msg_dest);
00777                             UPDmsg.set_height(H[msg_dest]);
00778                             radio().send(radio().BROADCAST_ADDRESS, UPDmsg.buffer_size(), (uint8_t*) & UPDmsg);
00779                             cout<<"\tBroadcasting an UPD message"<<endl;
00780 
00781                             updateLS(msg_dest);
00782                         }
00783                     }
00784 
00785                 }
00786 
00787             }
00788 
00789         }
00790         if (count_DNs(msg_dest) > 0) {
00791             //Sends pending messages for the specific destination
00792             empty_buffer(msg_dest);
00793        }
00794        
00795     }
00796     // -----------------------------------------------------------------------
00797 
00798     template<typename OsModel_P,
00799     typename RoutingTable_P,
00800     typename Radio_P,
00801     typename Debug_P>
00802     void
00803     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::printHeight(int dest) {
00804     cout<< radio().id()<<" HEIGHT "<< " : ("<< H[dest].time << "," << H[dest].originator_id << ","
00805                 << H[dest].r << "," << H[dest].delta << ","
00806                 << H[dest].ID << ")" <<endl;
00807     }
00808     // -----------------------------------------------------------------------
00809 
00810     template<typename OsModel_P,
00811     typename RoutingTable_P,
00812     typename Radio_P,
00813     typename Debug_P>
00814     void
00815     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::printNeighbors() {
00816         map<int, int>::iterator iterator;
00817 #ifdef DEBUG
00818         debug().debug(" %i neigbors: ", radio().id());
00819 #endif
00820         for (iterator = neighbors.begin(); iterator != neighbors.end(); iterator++) {
00821 #ifdef DEBUG
00822             debug().debug("%i ", iterator->first);
00823 #endif
00824         }
00825 #ifdef DEBUG
00826         debug().debug("\n");
00827 #endif
00828     }
00829     // -----------------------------------------------------------------------
00830 
00831     template<typename OsModel_P,
00832     typename RoutingTable_P,
00833     typename Radio_P,
00834     typename Debug_P>
00835     void
00836     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::recvQRY(node_id_t from, int destination) {
00837         int msg_dest = destination;
00838         int msg_sour = from;
00839         //Add new destination and initialize Height
00840            if (!destination_exists(msg_dest)) {
00841 
00842                destinations[msg_dest] = TORA_TIMEOUT;
00843                H[msg_dest].time = SHRT_MIN;
00844                H[msg_dest].originator_id = SHRT_MIN;
00845                H[msg_dest].r = SHRT_MIN;
00846                H[msg_dest].delta = SHRT_MIN ;
00847                H[msg_dest].ID =  radio().id();
00848             }
00849 
00850            //Initialize neighbors HEIGHT/destination
00851            for (neighborsIter = neighbors.begin(); neighborsIter != neighbors.end(); neighborsIter++) {
00852                if (!NH_exists(neighborsIter->first, msg_dest)) {
00853                    height tmp;
00854                    tmp.time = SHRT_MIN;
00855                    tmp.originator_id = SHRT_MIN;
00856                    tmp.r = SHRT_MIN;
00857                    tmp.delta = SHRT_MIN;
00858                    tmp.ID = neighborsIter->first;
00859                    NH[msg_dest][neighborsIter->first] = tmp;
00860                    LS[msg_dest][neighborsIter->first] = UN;
00861                }
00862            }
00863 
00864            //If destination node is neighbor set Height to Zero
00865            if (NH_exists(msg_dest, msg_dest)) {
00866                NH[msg_dest][msg_sour].time = 0;
00867                NH[msg_dest][msg_sour].originator_id = 0;
00868                NH[msg_dest][msg_sour].r = 0;
00869                NH[msg_dest][msg_sour].delta = 0;
00870                NH[msg_dest][msg_sour].ID = msg_sour;
00871 
00872            }
00873 
00874            //Update links
00875            if (NH_exists(msg_sour, msg_dest)) {
00876                if (H[msg_dest].delta == SHRT_MIN) {
00877                    if (NH[msg_dest][msg_sour].delta != SHRT_MIN) {
00878                        LS[msg_dest][msg_sour] = DN;
00879                    } else {
00880                        LS[msg_dest][msg_sour] = UN;
00881                    }
00882                } else {
00883                    if (NH[msg_dest][msg_sour].delta == SHRT_MIN) {
00884                        LS[msg_dest][msg_sour] = UN;
00885                    } else {
00886                        if (NH[msg_dest][msg_sour].delta > H[msg_dest].delta) {
00887                            LS[msg_dest][msg_sour] = UP;
00888                        } else if (NH[msg_dest][msg_sour].delta < H[msg_dest].delta) {
00889                            LS[msg_dest][msg_sour] = DN;
00890                        }
00891                    }
00892                }
00893            }
00894 
00895            DNlinks = count_DNs(msg_dest);
00896 
00897            cout << radio().id() << " : Received QRY - From: " << msg_sour << " Destination: " << msg_dest << " "<<DNlinks << endl;;
00898         //There are no downstream links
00899         if (DNlinks == 0) {
00900             //Checks if the RR flag is set
00901             if (!RR_flag_is_set(msg_dest)) {
00902                 RR[msg_dest] = false;
00903             }
00904             if (RR[msg_dest] == false && H[msg_dest].delta == SHRT_MIN) {
00905                 RR[msg_dest] = true;
00906                 RoutingMessage QRYmsg;
00907                 QRYmsg.set_msg_id(QRY);
00908                 QRYmsg.set_destination(msg_dest);
00909                 radio().send(radio().BROADCAST_ADDRESS, QRYmsg.buffer_size(), (uint8_t*) & QRYmsg);
00910                 cout<<"\tRebroadcasting the QRY message"<<endl;
00911                 destinations[msg_dest] = TORA_TIMEOUT;
00912             } else {
00913                 //Discards the package
00914                 cout << "\tQRY message discarded" << endl;
00915             }
00916 
00917         }//There is at least one downstream link
00918         else {
00919             destinations[msg_dest] = TORA_TIMEOUT;
00920             //if height is NULL
00921             if (heightIsNull(H[msg_dest])) {
00922 
00923                 //finds the minimum height of its not-NULL neighbor
00924                 int neighID = SHRT_MAX;
00925                 int neighDelta = SHRT_MAX;
00926                 for (NHIter = NH[msg_dest].begin(); NHIter != NH[msg_dest].end(); NHIter++) {
00927                     if (NHIter->second.delta != SHRT_MIN && NHIter->second.delta < neighDelta) {
00928                         neighID = NHIter->first;
00929                         neighDelta = NHIter->second.delta;
00930                     }
00931                 }
00932                 H[msg_dest].time = NH[msg_dest][neighID].time;
00933                 H[msg_dest].originator_id = NH[msg_dest][neighID].originator_id;
00934                 H[msg_dest].r = NH[msg_dest][neighID].r;
00935                 H[msg_dest].delta = NH[msg_dest][neighID].delta + 1;
00936 
00937                 cout << "\tDestination "
00938                         << NH[msg_dest][msg_dest].ID << " found " << endl;
00939 
00940                 RoutingMessage UPDmsg;
00941                 UPDmsg.set_msg_id(UPD);
00942                 UPDmsg.set_destination(msg_dest);
00943                 UPDmsg.set_height(H[msg_dest]);
00944                 radio().send(radio().BROADCAST_ADDRESS, UPDmsg.buffer_size(), (uint8_t*) & UPDmsg);
00945                 cout << "\tBroadcasting an UPD message " << endl;
00946 
00947             } else {
00948                 cout << "\t***FIX THIS!!!" << endl;
00949 
00950 
00951             }
00952         }
00953 
00954 
00955     }
00956     // ----------------------------------------------------------------------
00957 
00958     template<typename OsModel_P,
00959     typename RoutingTable_P,
00960     typename Radio_P,
00961     typename Debug_P>
00962     bool
00963     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::NH_exists(int node_id, int msg_dest) {
00964         NHIter = NH[msg_dest].find(NeighborsHeight::key_type(node_id));
00965         if (NHIter != NH[msg_dest].end()) {
00966             return true;
00967         }
00968         return false;
00969     }
00970 
00971     // ----------------------------------------------------------------------
00972 
00973     template<typename OsModel_P,
00974     typename RoutingTable_P,
00975     typename Radio_P,
00976     typename Debug_P>
00977     bool
00978     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::neighbor_exists(int node_id) {
00979         neighborsIter = neighbors.find(map<int, int>::key_type(node_id));
00980         if (neighborsIter != neighbors.end()) {
00981             return true;
00982         }
00983         return false;
00984     }
00985 
00986     // ----------------------------------------------------------------------
00987 
00988     template<typename OsModel_P,
00989     typename RoutingTable_P,
00990     typename Radio_P,
00991     typename Debug_P>
00992     bool
00993     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::destination_exists(int node_id) {
00994         destinationsIter = destinations.find(node_id);
00995         if (destinationsIter != destinations.end()) {
00996             return true;
00997         }
00998         return false;
00999     }
01000 
01001     // ----------------------------------------------------------------------
01002 
01003     template<typename OsModel_P,
01004     typename RoutingTable_P,
01005     typename Radio_P,
01006     typename Debug_P>
01007     bool
01008     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::RR_flag_is_set(int node_id) {
01009         RRIter = RR.find(node_id);
01010         if (RRIter != RR.end()) {
01011             return true;
01012         }
01013         return false;
01014     }
01015 
01016     // ----------------------------------------------------------------------
01017 
01018     template<typename OsModel_P,
01019     typename RoutingTable_P,
01020     typename Radio_P,
01021     typename Debug_P>
01022     int
01023     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::count_DNs(int node_id) {
01024         int links = 0;
01025         for (LSIter = LS[node_id].begin(); LSIter != LS[node_id].end(); LSIter++) {
01026             if (LSIter->second == DN) {
01027                 links++;
01028             }
01029         }
01030         return links;
01031     }
01032 
01033     // ----------------------------------------------------------------------
01034 
01035     template<typename OsModel_P,
01036     typename RoutingTable_P,
01037     typename Radio_P,
01038     typename Debug_P>
01039     void
01040     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
01041     updateLS(int msg_dest) {
01042         int msg_sour;
01043         for (neighborsIter = neighbors.begin(); neighborsIter != neighbors.end(); neighborsIter++) {
01044             if ((neighborsIter->first == msg_dest) && (NH_exists(msg_dest, msg_dest))) {
01045                 NH[msg_dest][msg_dest].time = 0;
01046                 NH[msg_dest][msg_dest].originator_id = 0;
01047                 NH[msg_dest][msg_dest].r = 0;
01048                 NH[msg_dest][msg_dest].delta = 0;
01049                 NH[msg_dest][msg_dest].ID = msg_dest;
01050                 LS[msg_dest][msg_dest] = DN;
01051                 cout<< "\tDN to "<< msg_dest << " --Destination : " <<  msg_dest <<endl;
01052             }
01053             else if (NH_exists(neighborsIter->first, msg_dest)) {
01054                 msg_sour = neighborsIter->first;
01055                 if (heightIsNull(NH[msg_dest][msg_sour]) && !heightIsNull(NH[msg_dest][msg_sour])) {
01056                     LS[msg_dest][msg_sour] = DN;
01057                      cout<< "\tDN to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01058                 } else if (heightIsNull(NH[msg_dest][msg_sour]) && heightIsNull(NH[msg_dest][msg_sour])) {
01059                     LS[msg_dest][msg_sour] = UN;
01060                      cout<< "\tUP to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01061                 } else {
01062                     if (NH[msg_dest][msg_sour].time == H[msg_dest].time) {
01063                         if (NH[msg_dest][msg_sour].originator_id == H[msg_dest].originator_id) {
01064                             if (NH[msg_dest][msg_sour].r == H[msg_dest].r) {
01065                                 if (NH[msg_dest][msg_sour].delta >= H[msg_dest].delta) {
01066                                     LS[msg_dest][msg_sour] = UP;
01067                                     cout<< "\tUP to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01068                                 } else if (NH[msg_dest][msg_sour].delta < H[msg_dest].delta) {
01069                                     LS[msg_dest][msg_sour] = DN;
01070                                     cout<< "\tDN to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01071                                 }
01072                             } else {
01073                                 if (NH[msg_dest][msg_sour].r >= H[msg_dest].r) {
01074                                         cout<< "\tUP to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01075                                         LS[msg_dest][msg_sour] = UP;
01076                                 } else if (NH[msg_dest][msg_sour].r < H[msg_dest].r) {
01077                                     LS[msg_dest][msg_sour] = DN;
01078                                     cout<< "\tDN to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01079                                 }
01080                             }
01081                         } else {
01082                             if (NH[msg_dest][msg_sour].originator_id > H[msg_dest].originator_id) {
01083                                     cout<< "\tUP to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01084                                     LS[msg_dest][msg_sour] = UP;
01085                             } else if (NH[msg_dest][msg_sour].originator_id < H[msg_dest].originator_id) {
01086                                 LS[msg_dest][msg_sour] = DN;
01087                                 cout<< "\tDN to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01088                             }
01089                         }
01090                     } else {
01091                         if (NH[msg_dest][msg_sour].time > H[msg_dest].time) {
01092                                 cout<< "\tUP to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01093                                 LS[msg_dest][msg_sour] = UP;
01094                         } else if (NH[msg_dest][msg_sour].time < H[msg_dest].time) {
01095                             LS[msg_dest][msg_sour] = DN;
01096                             cout<< "\tDN to " << msg_sour << " --Destination : " <<  msg_dest <<endl;
01097                         }
01098                     }
01099                 }
01100             }
01101         }
01102 
01103     }
01104     // -----------------------------------------------------------------------
01105 
01106     template<typename OsModel_P,
01107     typename RoutingTable_P,
01108     typename Radio_P,
01109     typename Debug_P>
01110     bool
01111     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
01112     ref_lvls_eq(int msg_dest) {
01113         height tmp;
01114         tmp.time = SHRT_MIN;
01115         tmp.originator_id = SHRT_MIN;
01116         tmp.r = SHRT_MIN;
01117         for (neighborsIter = neighbors.begin(); neighborsIter != neighbors.end(); neighborsIter++) {
01118             if (NH_exists(neighborsIter->first, msg_dest)) {
01119                 if (tmp.time == SHRT_MIN) {
01120                     tmp.time = NH[msg_dest][neighborsIter->first].time;
01121                     tmp.originator_id = NH[msg_dest][neighborsIter->first].originator_id;
01122                     tmp.r = NH[msg_dest][neighborsIter->first].r;
01123                 } else {
01124                     if (tmp.time != NH[msg_dest][neighborsIter->first].time
01125                             || tmp.originator_id != NH[msg_dest][neighborsIter->first].originator_id
01126                             || tmp.r != NH[msg_dest][neighborsIter->first].r) {
01127                         return false;
01128                     }
01129                 }
01130 
01131             }
01132         }
01133         return true;
01134 
01135     }
01136     // ----------------------------------------------------------------------
01137 
01138     template<typename OsModel_P,
01139     typename RoutingTable_P,
01140     typename Radio_P,
01141     typename Debug_P>
01142     int
01143     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
01144     find_min_ref_lvl(int msg_dest) {
01145         int tmp_time = SHRT_MIN;
01146         int neighID = SHRT_MIN;
01147         int tmp_d = SHRT_MAX;
01148         int tmp_oid = SHRT_MIN;
01149         int tmp_r = SHRT_MIN;
01150         for (NHIter = NH[msg_dest].begin(); NHIter != NH[msg_dest].end(); NHIter++) {
01151             if (NHIter->second.time != SHRT_MIN && NHIter->second.time > tmp_time) {
01152                 neighID = NHIter->first;
01153                 tmp_time = NHIter->second.time;
01154             }
01155         }
01156         for (NHIter = NH[msg_dest].begin(); NHIter != NH[msg_dest].end(); NHIter++) {
01157             if (NHIter->second.time == tmp_time && NHIter->second.originator_id > tmp_oid) {
01158                 neighID = NHIter->first;
01159                 tmp_oid = NHIter->second.originator_id;
01160             }
01161         }
01162         for (NHIter = NH[msg_dest].begin(); NHIter != NH[msg_dest].end(); NHIter++) {
01163             if (NHIter->second.time == tmp_time && NHIter->second.originator_id == tmp_oid && NHIter->second.r > tmp_r) {
01164                 neighID = NHIter->first;
01165                 tmp_r = NHIter->second.r;
01166             }
01167         }
01168 
01169         for (NHIter = NH[msg_dest].begin(); NHIter != NH[msg_dest].end(); NHIter++) {
01170             if (NHIter->second.time == tmp_time && NHIter->second.originator_id == tmp_oid && NHIter->second.r == tmp_r && NHIter->second.delta < tmp_d) {
01171                 neighID = NHIter->first;
01172 
01173             }
01174         }
01175         return neighID;
01176 
01177     }
01178     // ----------------------------------------------------------------------
01179 
01180     template<typename OsModel_P,
01181     typename RoutingTable_P,
01182     typename Radio_P,
01183     typename Debug_P>
01184     bool
01185     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
01186     r_eq_one(int msg_dest) {
01187         for (NHIter = NH[msg_dest].begin(); NHIter != NH[msg_dest].end(); NHIter++) {
01188             if (NHIter->second.r != 1) {
01189                 return false;
01190             }
01191         }
01192         return true;
01193     }
01194     // ----------------------------------------------------------------------
01195 
01196     template<typename OsModel_P,
01197     typename RoutingTable_P,
01198     typename Radio_P,
01199     typename Debug_P>
01200     bool
01201     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
01202     oid_eq_id(int msg_dest) {
01203         for (neighborsIter = neighbors.begin(); neighborsIter != neighbors.end(); neighborsIter++) {
01204             if (NH[msg_dest][neighborsIter->first].originator_id != radio().id()) {
01205                 return false;
01206             }
01207         }
01208 
01209         return true;
01210     }
01211     // ----------------------------------------------------------------------
01212 
01213     template<typename OsModel_P,
01214     typename RoutingTable_P,
01215     typename Radio_P,
01216     typename Debug_P>
01217     void
01218     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
01219     empty_buffer(int dest) {
01220         pend_msg_iter iter = pend_msgs.find(dest);
01221         if (iter != pend_msgs.end()) {
01222             int tmpH = SHRT_MAX;
01223             int next_node = SHRT_MAX;
01224              for (LSIter = LS[dest].begin(); LSIter != LS[dest].end(); LSIter++) {
01225                 if (LSIter->second == DN){
01226                     if(NH[dest][LSIter->first].delta > 0 && NH[dest][LSIter->first].delta < tmpH){
01227                         tmpH = NH[dest][LSIter->first].delta;
01228                         next_node = LSIter->first;
01229                     }else if (NH[dest][LSIter->first].delta < 0){
01230                         next_node = LSIter->first;
01231                     }
01232                 }
01233             }
01234             RoutingMessage DATAmsg;
01235             DATAmsg.set_msg_id(DATA);
01236             DATAmsg.set_next_node(next_node);
01237             DATAmsg.set_destination(dest);
01238             radio().send(radio().BROADCAST_ADDRESS, DATAmsg.buffer_size(), (uint8_t*) & DATAmsg);
01239             cout<< "\t\tSending a Data message with destination "<< dest << " to "<< next_node <<endl;
01240         }
01241          pend_msgs.erase(dest);
01242     }
01243     // ----------------------------------------------------------------------
01244     template<typename OsModel_P,
01245     typename RoutingTable_P,
01246     typename Radio_P,
01247     typename Debug_P>
01248     void
01249     ToraRouting<OsModel_P, RoutingTable_P, Radio_P, Debug_P>::
01250     clearDest() {
01251         for (destinationsIter = destinations.begin(); destinationsIter != destinations.end(); destinationsIter++) {
01252             destinationsIter->second--;
01253             if (destinationsIter->second <= 0) {
01254                 NH.erase(destinationsIter->first);
01255                 LS.erase(destinationsIter->first);
01256                 RR.erase(destinationsIter->first);
01257                 cout << radio().id() << " : Delete destination " << destinationsIter->first << endl;
01258                 pend_msg_iter iter = pend_msgs.find(destinationsIter->first);
01259                 if (iter != pend_msgs.end()) {
01260                     cout <<"\t Message to destination: " << destinationsIter->first << " could not be delivered... NO ROUTE found" << endl;
01261                     pend_msgs.erase(destinationsIter->first);
01262                 }
01263                 destinations.erase(destinationsIter->first);
01264             }
01265         }
01266 
01267     }
01268 }
01269 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines