McHandler.cpp
Go to the documentation of this file.
00001 /* 
00002  * File:   McHandler.cpp
00003  * Author: cwioro
00004  * 
00005  * Created on May 21, 2014, 12:11 PM
00006  */
00007 
00008 
00009 
00010 #include <string>
00011 #include <list>
00012 
00013 #include "McHandler.h"
00014 #include "defines.h"
00015 
00016 McHandler::McHandler(list<McTree*> *groups)
00017 {
00018     this->groups_ = groups;
00019 }
00020 
00021 McHandler::McHandler(const McHandler& orig)
00022 {
00023 }
00024 
00025 McHandler::~McHandler()
00026 {
00027 }
00028 
00029 McTree* McHandler::getMcGroup(std::string* group_name)
00030 {
00031 
00032 
00033 
00034     for (std::list<McTree*>::iterator it = groups_->begin(); it != groups_->end(); ++it)
00035     {
00036 
00037 
00038         if ((*it)->group_name_.compare(*group_name) == 0)
00039             return *it;
00040     }
00041 
00042 
00043 
00044     return NULL;
00045 
00046 }
00047 
00048 std::vector<McTree*> McHandler::lostConnectionDownlinks(unsigned char* mac_a)
00049 {
00050 
00051 
00052     std::vector<McTree*> affected_trees;
00053     for (std::list<McTree*>::iterator it = groups_->begin(); it != groups_->end(); ++it)
00054     {
00055 
00056         McTree* tree = *it;
00057 
00058 
00059         if (tree->removeMacIfExsists(mac_a))
00060         {
00061 
00062 
00063             affected_trees.push_back(*it);
00064         }
00065     }
00066 
00067     return affected_trees;
00068 
00069 }
00070 
00071 std::vector<McTree*> McHandler::lostConnectionUplinks(unsigned char* mac_a)
00072 {
00073 
00074 
00075     std::vector<McTree*> affected_trees;
00076     for (std::list<McTree*>::iterator it = groups_->begin(); it != groups_->end(); ++it)
00077     {
00078         McTree* tree = *it;
00079 
00080         if (compareMac(tree->route_uplink_->next_hop, mac_a))
00081         {
00082 
00083             tree->connected = false;
00084             affected_trees.push_back(tree);
00085         }
00086     }
00087 
00088 
00089     return affected_trees;
00090 
00091 }
00092 
00093 bool McHandler::removeGroup(std::string* group_name)
00094 {
00095     McTree* t = getMcGroup(group_name);
00096     if (t == NULL)
00097         return false;
00098     
00099     ROS_ERROR("remove %s",group_name->c_str());
00100 
00101     groups_->remove(t);
00102     delete t;
00103 
00104 }
00105 
00106 
00107 McTree* McHandler::getMcGroup(std::string* hostname_source, uint32_t* route_id)
00108 {
00109 
00110 
00111 
00112     for (std::list<McTree*>::iterator it = groups_->begin(); it != groups_->end(); ++it)
00113     {
00114         McTree* tree = *it;
00115 
00116 
00117         for (std::list<routing_entry*>::iterator it_r = tree->routing_entries_l_.begin(); it_r != tree->routing_entries_l_.end(); ++it_r)
00118         {
00119 
00120 
00121             if ((*it_r)->id == *route_id && (*it_r)->hostname_source.compare(*hostname_source) == 0)
00122                 return *it;
00123         }
00124     }
00125 
00126 
00127     return NULL;
00128 }
00129 
00130 void McHandler::createGroupAsRoot(std::string* group_name)
00131 {
00132 
00133 
00134     /* Create own mc group */
00135 
00136     this->createGroup(group_name, true, true, true, true, 0);
00137 
00138 #ifdef MC_HANDLER_OUPUT
00139     ROS_ERROR("CREATE GROUP AS ROOT: %s", group_name->c_str());
00140 #endif
00141 
00142 }
00143 
00144 void McHandler::addGroup(std::string* group_name)
00145 {
00146 
00147     if (getMcGroup(group_name) == NULL)
00148     {
00149         this->createGroup(group_name, false, false, false, false, -1);
00150 
00151     }
00152 
00153 
00154 #ifdef MC_HANDLER_OUPUT
00155     ROS_ERROR("CREATE GROUP AS ROOT: %s", group_name->c_str());
00156 #endif
00157 
00158 }
00159 
00160 bool McHandler::addUplinkRoute(routing_entry* route)
00161 {
00162 
00163     McTree* t = this->getMcGroup(&route->hostname_destination);
00164     if (t == NULL)
00165         createGroup(&route->hostname_destination, false, false, true, false, route->root_distance);
00166 
00167     t = this->getMcGroup(&route->hostname_destination);
00168 
00169 
00170 
00171     /* check if response is not from a node with a lower root distance and if the route dont exsists already*/
00172     if (route->root_distance <= t->route_uplink_->root_distance && t->routeIsNew(route))
00173     {
00174 
00175         t->routing_entries_l_.push_front(route);
00176         return true;
00177     }
00178     else
00179     {
00180         return false;
00181     }
00182 
00183 
00184 
00185 
00186 }
00187 
00188 void McHandler::addDownlinkRoute(routing_entry* route)
00189 {
00190 
00191     McTree* t = this->getMcGroup(&route->hostname_destination);
00192     if (t == NULL)
00193         ROS_ERROR("Unexpected failure: want insert downlink route, but mc tree does not exists");
00194 
00195 
00196     t->routing_entries_downlinks_l_.push_front(route);
00197 
00198 
00199 
00200 
00201 
00202 }
00203 
00204 void McHandler::createGroup(std::string* group_name, bool root, bool member, bool connected, bool activated, uint16_t root_distance)
00205 {
00206 
00207 
00208     McTree *my_mc_group = new McTree();
00209     my_mc_group->group_name_ = *group_name;
00210     my_mc_group->root = root;
00211     my_mc_group->member = member;
00212     my_mc_group->connected = connected;
00213     my_mc_group->activated = activated;
00214 
00215     my_mc_group->outgoing_request_ = NULL;
00216 
00217     /* Create routing entry for uplink*/
00218     my_mc_group->route_uplink_ = new routing_entry;
00219 
00220     my_mc_group->route_uplink_->hostname_destination = string(*group_name);
00221     my_mc_group->route_uplink_->hostname_source = "";
00222     my_mc_group->route_uplink_->id = 0 - 1;
00223 
00224     if (root)
00225     {
00226         my_mc_group->route_uplink_->hobs = 0;
00227         my_mc_group->route_uplink_->current_hop = 0;
00228         my_mc_group->route_uplink_->root_distance = 0;
00229 
00230     }
00231     else
00232     {
00233         my_mc_group->route_uplink_->hobs = 0 - 1;
00234         my_mc_group->route_uplink_->current_hop = 0 - 1;
00235         my_mc_group->route_uplink_->root_distance = 0 - 1;
00236     }
00237 
00238 
00239     this->groups_->push_front(my_mc_group);
00240 
00241 }
00242 
00243 void McHandler::setMembership(std::string* group_name, bool membership)
00244 {
00245     this->getMcGroup(group_name)->member = membership;
00246 }
00247 
00248 #ifdef DEBUG
00249 
00250 void McHandler::printMcGroups()
00251 {
00252 
00253     for (std::list<McTree*>::iterator it = groups_->begin(); it != groups_->end(); ++it)
00254     {
00255         McTree* tree = *it;
00256         ROS_ERROR("NAME:[%s] ACTIVE[%u] ENTRIES: UP[%lu] DOWN[%lu] WAITING[%lu] DOWNLINKS[%lu]", tree->group_name_.c_str(), tree->activated, tree->routing_entries_l_.size(), tree->routing_entries_downlinks_l_.size(), tree->waiting_requests_l_.size(), tree->downlinks_l_.size());
00257     }
00258 }
00259 #endif 


adhoc_communication
Author(s): Guenter Cwioro , Torsten Andre
autogenerated on Thu Aug 27 2015 11:56:40