RouteTable.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "RouteTable.h"
00019 #include "contacts/Link.h"
00020 
00021 namespace dtn {
00022 
00026 RouteTable::RouteTable(const std::string& router_name)
00027     : Logger("RouteTable", "/dtn/routing/%s/table", router_name.c_str())
00028 {
00029 }
00030 
00034 RouteTable::~RouteTable()
00035 {
00036 }
00037 
00041 bool
00042 RouteTable::add_entry(RouteEntry* entry)
00043 {
00044     log_debug("add_route *%p", entry);
00045     
00046     route_table_.push_back(entry);
00047     
00048     return true;
00049 }
00050 
00054 bool
00055 RouteTable::del_entry(const EndpointIDPattern& dest, Link* next_hop)
00056 {
00057     oasys::ScopeLock l(&lock_, "RouteTable");
00058 
00059     RouteEntryVec::iterator iter;
00060     RouteEntry* entry;
00061 
00062     for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00063         entry = *iter;
00064 
00065         if (entry->dest_pattern_.equals(dest) && entry->next_hop_ == next_hop) {
00066             log_debug("del_entry *%p", entry);
00067             
00068             route_table_.erase(iter);
00069             delete entry;
00070             return true;
00071         }
00072     }    
00073 
00074     log_debug("del_entry %s -> %s: no match!",
00075               dest.c_str(), next_hop->nexthop());
00076     return false;
00077 }
00078 
00082 size_t
00083 RouteTable::del_entries(const EndpointIDPattern& dest)
00084 {
00085     oasys::ScopeLock l(&lock_, "RouteTable");
00086 
00087     RouteEntryVec::iterator iter;
00088     RouteEntry* entry;
00089 
00090     // since deleting from the middle of a vector invalidates
00091     // iterators for that vector, we have to loop multiple times until
00092     // we don't find any more entries that match
00093     int num_found = 0;
00094     bool found;
00095     do {
00096         found = false;
00097         for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00098             entry = *iter;
00099             
00100             if (dest.equals(entry->dest_pattern_)) {
00101                 log_debug("del_route *%p", entry);
00102                 
00103                 route_table_.erase(iter);
00104                 delete entry;
00105                 found = true;
00106                 ++num_found;
00107                 break;
00108             }
00109         }
00110     } while (found);
00111 
00112     if (num_found == 0) {
00113         log_debug("del_entries %s: no matches!", dest.c_str());
00114     } else {
00115         log_debug("del_entries %s: removed %d routes", dest.c_str(), num_found);
00116     }
00117     
00118     return num_found;
00119 }
00120 
00121 size_t
00122 RouteTable::del_entries_for_nexthop(Link* next_hop)
00123 {
00124     oasys::ScopeLock l(&lock_, "RouteTable");
00125 
00126     RouteEntryVec::iterator iter;
00127     RouteEntry* entry;
00128 
00129     // since deleting from the middle of a vector invalidates
00130     // iterators for that vector, we have to loop multiple times.
00131     
00132     // since deleting from the middle of a vector invalidates
00133     // iterators for that vector, we have to loop multiple times until
00134     // we don't find any more entries that match
00135     int num_found = 0;
00136     bool found;
00137     do {
00138         found = false;
00139         for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00140             entry = *iter;
00141 
00142             if (entry->next_hop_ == next_hop) {
00143                 log_debug("del_route *%p", entry);
00144 
00145                 route_table_.erase(iter);
00146                 delete entry;
00147                 found = true;
00148                 ++num_found;
00149                 break;
00150             }
00151         }
00152     } while (found);
00153 
00154     if (num_found == 0) {
00155         log_debug("del_entries_for_nexthop %s: no matches!",
00156                   next_hop->name());
00157     } else {
00158         log_debug("del_entries_for_nexthop %s: removed %d routes",
00159                   next_hop->name(), num_found);
00160     }
00161     
00162     return num_found;
00163 }
00164 
00171 size_t
00172 RouteTable::get_matching(const EndpointID& eid, Link* next_hop,
00173                          RouteEntryVec* entry_vec) const
00174 {
00175     oasys::ScopeLock l(&lock_, "RouteTable");
00176 
00177     RouteEntryVec::const_iterator iter;
00178     RouteEntry* entry;
00179     size_t count = 0;
00180 
00181     log_debug("get_matching %s", eid.c_str());
00182     
00183     for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00184         entry = *iter;
00185 
00186         log_debug("check entry *%p", entry);
00187         
00188         if ((next_hop == NULL || entry->next_hop_ == next_hop) &&
00189             entry->dest_pattern_.match(eid))
00190         {
00191             ++count;
00192             
00193             log_debug("match entry *%p", entry);
00194             
00195             entry_vec->push_back(entry);
00196         }
00197     }
00198 
00199     log_debug("get_matching %s done, %zu match(es)", eid.c_str(), count);
00200     return count;
00201 }
00202 
00206 void
00207 RouteTable::dump(oasys::StringBuffer* buf, EndpointIDVector* long_eids) const
00208 {
00209     RouteEntry::dump_header(buf);
00210     RouteEntryVec::const_iterator iter;
00211     for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00212         (*iter)->dump(buf, long_eids);
00213     }
00214 }
00215 
00220 const RouteEntryVec *
00221 RouteTable::route_table()
00222 {
00223     ASSERTF(lock_.is_locked_by_me(),
00224             "RouteTable::route_table must be called while holding lock");
00225     return &route_table_;
00226 }
00227 
00228 } // namespace dtn

Generated on Thu Jun 7 12:54:29 2007 for DTN Reference Implementation by  doxygen 1.5.1