McTree.cpp
Go to the documentation of this file.
00001 /* 
00002  * File:   McTree.cpp
00003  * Author: cwioro
00004  * 
00005  * Created on May 21, 2014, 11:54 AM
00006  */
00007 
00008 #include <list>
00009 #include <string>
00010 
00011 #include "McTree.h"
00012 #include "structs.h"
00013 #include "functions.h"
00014 #include "RouteRequest.h"
00015 #include "EthernetFrame.h"
00016 #include "header.h"
00017 
00018 #include "Logging.h"
00019 
00020 McTree::McTree()
00021 {
00022     outgoing_request_ = NULL;
00023     route_uplink_ = NULL;
00024     time_stamp_ = getMillisecondsTime();
00025 }
00026 
00027 McTree::McTree(const McTree& orig)
00028 {
00029 }
00030 
00031 McTree::~McTree()
00032 {
00033 
00034 }
00035 
00036 void McTree::safeOutgoingRequest(RouteRequest* req)
00037 {
00038     if (outgoing_request_ != NULL)
00039         delete outgoing_request_;
00040 
00041     time_stamp_ = getMillisecondsTime();
00042 
00043     outgoing_request_ = req;
00044 }
00045 
00046 bool McTree::addWaitingRequest(RouteRequest* req, unsigned char* source_mac)
00047 {
00048 
00049     for (list<RouteRequest* >::iterator it = waiting_requests_l_.begin(); it != waiting_requests_l_.end(); it++)
00050     {
00051         RouteRequest* r = *it;
00052         /* Check if same: source, host, incoming mac*/
00053         if (r->hostname_source_.compare(req->hostname_source_) == 0 && r->hostname_destination_.compare(req->hostname_destination_) == 0 && compareMac(r->eh_h_.eh_source, req->eh_h_.eh_source))
00054         {
00055             /* if hop count is less than the stored one, the old one will be deleted and the new one will be stored */
00056             if (r->header_.hop_count > req->header_.hop_count)
00057             {
00058                 waiting_requests_l_.erase(it);
00059                 delete r;
00060                 waiting_requests_l_.push_back(req);
00061                 return true;
00062             }
00063             else
00064                 return false;
00065         }
00066     }
00067 
00068     /* check if robots source mac is not in mac path to prevent loops */
00069     for (list<mac>::iterator it = req->path_l_.begin(); it != req->path_l_.end(); it++)
00070     {
00071         mac m = *it;
00072         if (compareMac(m.mac_adr, source_mac))
00073             return false;
00074     }
00075 
00076     waiting_requests_l_.push_back(req);
00077     return true;
00078 }
00079 
00080 bool McTree::propagateFrame(unsigned char* sender_mac)
00081 {
00082     //  ROS_DEBUG("UPLINK: %s",getHostnameFromMac( t->route_uplink.next_hop).c_str());
00083 
00084     /* propagate if frame is from uplink and downlinks exsists */
00085     if (!this->downlinks_l_.empty() && compareMac(sender_mac, route_uplink_->next_hop))
00086     {
00087         //  ROS_ERROR("propagate if frame is from uplink and downlinks exsists");
00088         return true;
00089     }
00090 
00091 
00092 
00093 
00094 
00095     /* Propagate frame if frame is from a downlink*/
00096 
00097 
00098     for (std::list<mac*>::iterator it = downlinks_l_.begin(); it != downlinks_l_.end(); ++it)
00099     {
00100         mac* m = *it;
00101         //            ROS_DEBUG("DOWNLINK: %s" ,getHostnameFromMac( m.mac).c_str());
00102         if (compareMac((unsigned char*) m->mac_adr, sender_mac))
00103         {
00104             if (!root) // propagate always if i am not the root, because there is an uplink
00105                 return true;
00106             else if (root && downlinks_l_.size() > 1) // if root propagate only if there is more than one downlinks
00107                 return true;
00108         }
00109 
00110     }
00111 
00112 
00113     return false;
00114 }
00115 
00116 bool McTree::activateBestRoute(route_request* rreq_logging)
00117 {
00118     for (list<routing_entry* >::iterator it = routing_entries_l_.begin(); it != routing_entries_l_.end();)
00119     {
00120 
00121 
00122         if (route_uplink_->root_distance > (*it)->root_distance)
00123         {
00124             route_uplink_ = *it;
00125             it = routing_entries_l_.erase(it);
00126 
00127         }
00128         else
00129             it++;
00130     }
00131 
00132 
00133     Logging::logRRequestInitiater(rreq_logging, route_uplink_);
00134 
00135 
00136     resetTmpFields();
00137 
00138     if (route_uplink_->id != (0 - 1))
00139     { //this->route_uplink_
00140         this->activated = true;
00141         this->connected = true;
00142 
00143         return true;
00144     }
00145     else
00146         return false;
00147 
00148 
00149 }
00150 
00151 void McTree::resetTmpFields()
00152 {
00153 
00154     for (list<routing_entry* >::iterator it = routing_entries_l_.begin(); it != routing_entries_l_.end();)
00155     {
00156         routing_entry* t = *it;
00157         it = routing_entries_l_.erase(it);
00158         delete t;
00159 
00160     }
00161     delete outgoing_request_;
00162     outgoing_request_ = NULL;
00163 
00164     /* DO not erase waitingRequests list -> is needed to answer those requests */
00165 }
00166 
00167 bool McTree::processFrame(unsigned char* src)
00168 {
00169 
00170 
00171     return downlinkExsists(src) || compareMac(src, route_uplink_->next_hop);
00172 }
00173 
00174 void McTree::printTree()
00175 {
00176     ROS_ERROR("GROUP NAME: %s", this->group_name_.c_str());
00177     std::string route_uplink = "";
00178     if (!this->root)
00179         route_uplink = std::string("Next hop:" + getMacAsStr(this->route_uplink_->next_hop) + " RD:" + getIntAsString(this->route_uplink_->root_distance) + " HOBS:" + getIntAsString(this->route_uplink_->hobs) + " CH:" + getIntAsString(this->route_uplink_-> current_hop));
00180 
00181     else
00182         ROS_ERROR("UPLINK: %s", route_uplink.c_str());
00183 
00184     if (!this->downlinks_l_.empty())
00185     {
00186         ROS_ERROR("DOWNLINKS:");
00187         int count = 0;
00188         for (std::list<mac*>::iterator i = downlinks_l_.begin(); i != downlinks_l_.end(); ++i)
00189         {
00190             count++;
00191 
00192             ROS_ERROR("%s:: %s", getIntAsString(count).c_str(), getMacAsStr((*i)->mac_adr).c_str());
00193 
00194         }
00195     }
00196     else ROS_ERROR("NO DOWNLINKS");
00197     if (!this->routing_entries_downlinks_l_.empty())
00198     {
00199         ROS_ERROR("POSSIBLE DOWNLINKS:");
00200         int count = 0;
00201         for (std::list<routing_entry*>::iterator i = routing_entries_downlinks_l_.begin(); i != routing_entries_downlinks_l_.end(); ++i)
00202         {
00203 
00204             count++;
00205 
00206             ROS_ERROR("%s:: %s", getIntAsString(count).c_str(), getMacAsStr((*i)->previous_hop).c_str());
00207 
00208         }
00209     }
00210 
00211 
00212     ROS_ERROR("CONNECTED: [%s] MEMBER[%s] ROOT[%s] ACTIVATED[%s] ", getBoolAsString(connected).c_str(), getBoolAsString(member).c_str(), getBoolAsString(root).c_str(), getBoolAsString(activated).c_str());
00213 
00214 }
00215 
00216 bool McTree::activateRoute(std::string* hostname_source, uint32_t* id, unsigned char* mac_adr)
00217 {
00218 
00219 
00220     bool route_found = false;
00221     for (list<routing_entry* >::iterator it = routing_entries_l_.begin(); it != routing_entries_l_.end();)
00222     {
00223 
00224         if (*id == (*it)->id && hostname_source->compare((*it)->hostname_source) == 0 && compareMac((*it)->previous_hop, mac_adr))
00225         {
00226             route_uplink_ = *it;
00227             route_found = true;
00228 
00229             this->activated = true;
00230             this->connected = true;
00231             it = routing_entries_l_.erase(it);
00232         }
00233         else
00234             it++;
00235     }
00236     if (route_found)
00237         resetTmpFields();
00238 
00239     return route_found;
00240 
00241 }
00242 
00243 bool McTree::routeIsNew(routing_entry* r)
00244 {
00245 
00246     for (list<routing_entry* >::iterator it = routing_entries_l_.begin(); it != routing_entries_l_.end(); it++)
00247     {
00248 
00249         if (r->hostname_source.compare((*it)->hostname_source) == 0 && (*it)->id == r->id && compareMac(r->next_hop, (*it)->next_hop))
00250             return false;
00251 
00252     }
00253 
00254     return true;
00255 }
00256 
00257 bool McTree::addDownlinkAsConnector(mac* m)
00258 {
00259     for (list<routing_entry* >::iterator it = this->routing_entries_l_.begin(); it != routing_entries_l_.end(); it++)
00260     {
00261         if (compareMac((*it)->previous_hop, m->mac_adr) && !downlinkExsists(m->mac_adr))
00262         {
00263           //  ROS_ERROR("add downlink as connector:   %s %s", getMacAsStr(m->mac_adr).c_str(), group_name_.c_str());
00264             this->downlinks_l_.push_back(m);
00265             return true;
00266         }
00267     }
00268     return false;
00269 }
00270 
00271 bool McTree::addDownlinkAsMember(mac* m)
00272 {
00273     routing_entry* entry = NULL;
00274     for (list<routing_entry* >::iterator it = routing_entries_downlinks_l_.begin(); it != routing_entries_downlinks_l_.end(); it++)
00275     {
00276 
00277         if (compareMac((*it)->previous_hop, m->mac_adr) && !downlinkExsists(m->mac_adr))
00278         {
00279             entry = *it;
00280            // ROS_ERROR("add downlink as member  %s %s", getMacAsStr(m->mac_adr).c_str(), group_name_.c_str());
00281             this->downlinks_l_.push_back(m);
00282             routing_entries_downlinks_l_.erase(it);
00283 
00284             //Logging::logRouteRequestReceiver(entry->hostname_source,entry->id,entry->); 
00285             break;
00286 
00287 
00288         }
00289       //  else
00290       //      ROS_ERROR("%u %u", compareMac((*it)->previous_hop, m->mac_adr), !downlinkExsists(m->mac_adr));
00291 
00292     }
00293 
00294 
00295     /* remove all other entries frome the same source*/
00296     if (entry != NULL)
00297     {
00298         for (list<routing_entry* >::iterator it = routing_entries_downlinks_l_.begin(); it != routing_entries_downlinks_l_.end();)
00299         {
00300             if ((*it)->hostname_source.compare(entry->hostname_source) == 0)
00301             {
00302                 delete *it;
00303                 it = routing_entries_downlinks_l_.erase(it);
00304             }
00305             else
00306                 it++;
00307         }
00308         delete entry;
00309         return true;
00310     }
00311     else
00312         return false;
00313 
00314 }
00315 
00316 bool McTree::operator=(const McTree* other)
00317 {
00318     return group_name_.compare(other->group_name_) == 0;
00319 }
00320 
00321 bool McTree::downlinkExsists(unsigned char* m)
00322 {
00323 
00324     for (list<mac* >::iterator it = downlinks_l_.begin(); it != downlinks_l_.end(); it++)
00325     {
00326 
00327         if (compareMac((*it)->mac_adr, m))
00328         {
00329 
00330             return true;
00331         }
00332     }
00333 
00334     return false;
00335 }
00336 
00337 bool McTree::removeMacIfExsists(unsigned char* m)
00338 {
00339     for (list<mac* >::iterator it = downlinks_l_.begin(); it != downlinks_l_.end(); it++)
00340     {
00341         if (compareMac((*it)->mac_adr, m))
00342         {
00343             delete *it;
00344             downlinks_l_.erase(it);
00345             return true;
00346         }
00347     }
00348     return false;
00349 }


adhoc_communication
Author(s): Guenter Cwioro , Torsten Andre
autogenerated on Thu Jun 6 2019 20:59:43