RouteTable.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "RouteTable.h"
00040 #include "contacts/Link.h"
00041 
00042 namespace dtn {
00043 
00047 RouteTable::RouteTable(const std::string& router_name)
00048     : Logger("RouteTable", "/dtn/routing/%s/table", router_name.c_str())
00049 {
00050 }
00051 
00055 RouteTable::~RouteTable()
00056 {
00057 }
00058 
00062 bool
00063 RouteTable::add_entry(RouteEntry* entry)
00064 {
00065     log_debug("add_route *%p", entry);
00066     
00067     route_table_.push_back(entry);
00068     
00069     return true;
00070 }
00071 
00075 bool
00076 RouteTable::del_entry(const EndpointIDPattern& dest, Link* next_hop)
00077 {
00078     RouteEntryVec::iterator iter;
00079     RouteEntry* entry;
00080 
00081     for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00082         entry = *iter;
00083 
00084         if (entry->dest_pattern_.equals(dest) && entry->next_hop_ == next_hop) {
00085             log_debug("del_entry *%p", entry);
00086             
00087             route_table_.erase(iter);
00088             delete entry;
00089             return true;
00090         }
00091     }    
00092 
00093     log_debug("del_entry %s -> %s: no match!",
00094               dest.c_str(), next_hop->nexthop());
00095     return false;
00096 }
00097 
00101 size_t
00102 RouteTable::del_entries(const EndpointIDPattern& dest)
00103 {
00104     RouteEntryVec::iterator iter;
00105     RouteEntry* entry;
00106 
00107     // since deleting from the middle of a vector invalidates
00108     // iterators for that vector, we have to loop multiple times until
00109     // we don't find any more entries that match
00110     int num_found = 0;
00111     bool found;
00112     do {
00113         found = false;
00114         for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00115             entry = *iter;
00116             
00117             if (dest.equals(entry->dest_pattern_)) {
00118                 log_debug("del_route *%p", entry);
00119                 
00120                 route_table_.erase(iter);
00121                 delete entry;
00122                 found = true;
00123                 ++num_found;
00124                 break;
00125             }
00126         }
00127     } while (found);
00128 
00129     if (num_found == 0) {
00130         log_debug("del_entries %s: no matches!", dest.c_str());
00131     } else {
00132         log_debug("del_entries %s: removed %d routes", dest.c_str(), num_found);
00133     }
00134     
00135     return num_found;
00136 }
00137 
00138 size_t
00139 RouteTable::del_entries_for_nexthop(Link* next_hop)
00140 {
00141     RouteEntryVec::iterator iter;
00142     RouteEntry* entry;
00143 
00144     // since deleting from the middle of a vector invalidates
00145     // iterators for that vector, we have to loop multiple times.
00146     
00147     // since deleting from the middle of a vector invalidates
00148     // iterators for that vector, we have to loop multiple times until
00149     // we don't find any more entries that match
00150     int num_found = 0;
00151     bool found;
00152     do {
00153         found = false;
00154         for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00155             entry = *iter;
00156 
00157             if (entry->next_hop_ == next_hop) {
00158                 log_debug("del_route *%p", entry);
00159 
00160                 route_table_.erase(iter);
00161                 delete entry;
00162                 found = true;
00163                 ++num_found;
00164                 break;
00165             }
00166         }
00167     } while (found);
00168 
00169     if (num_found == 0) {
00170         log_debug("del_entries_for_nexthop %s: no matches!",
00171                   next_hop->name());
00172     } else {
00173         log_debug("del_entries_for_nexthop %s: removed %d routes",
00174                   next_hop->name(), num_found);
00175     }
00176     
00177     return num_found;
00178 }
00179 
00186 size_t
00187 RouteTable::get_matching(const EndpointID& eid, Link* next_hop,
00188                          RouteEntryVec* entry_vec) const
00189 {
00190     RouteEntryVec::const_iterator iter;
00191     RouteEntry* entry;
00192     size_t count = 0;
00193 
00194     log_debug("get_matching %s", eid.c_str());
00195     
00196     for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00197         entry = *iter;
00198 
00199         log_debug("check entry *%p", entry);
00200         
00201         if ((next_hop == NULL || entry->next_hop_ == next_hop) &&
00202             entry->dest_pattern_.match(eid))
00203         {
00204             ++count;
00205             
00206             log_debug("match entry *%p", entry);
00207             
00208             entry_vec->push_back(entry);
00209         }
00210     }
00211 
00212     log_debug("get_matching %s done, %zu match(es)", eid.c_str(), count);
00213     return count;
00214 }
00215 
00219 void
00220 RouteTable::dump(oasys::StringBuffer* buf, EndpointIDVector* long_eids) const
00221 {
00222     RouteEntry::dump_header(buf);
00223     RouteEntryVec::const_iterator iter;
00224     for (iter = route_table_.begin(); iter != route_table_.end(); ++iter) {
00225         (*iter)->dump(buf, long_eids);
00226     }
00227 }
00228 } // namespace dtn

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1