Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #include "ndn-fib-impl.h" |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 23 | #include "ns3/ndn-face.h" |
| 24 | #include "ns3/ndn-interest-header.h" |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 25 | |
| 26 | #include "ns3/node.h" |
| 27 | #include "ns3/assert.h" |
| 28 | #include "ns3/names.h" |
| 29 | #include "ns3/log.h" |
| 30 | |
| 31 | #include <boost/ref.hpp> |
| 32 | #include <boost/lambda/lambda.hpp> |
| 33 | #include <boost/lambda/bind.hpp> |
| 34 | namespace ll = boost::lambda; |
| 35 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 36 | NS_LOG_COMPONENT_DEFINE ("ndn.fib.FibImpl"); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 37 | |
| 38 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 39 | namespace ndn { |
| 40 | namespace fib { |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 42 | NS_OBJECT_ENSURE_REGISTERED (FibImpl); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 43 | |
| 44 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 45 | FibImpl::GetTypeId (void) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 46 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 47 | static TypeId tid = TypeId ("ns3::ndn::fib::Default") // cheating ns3 object system |
| 48 | .SetParent<Fib> () |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 49 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 50 | .AddConstructor<FibImpl> () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 51 | ; |
| 52 | return tid; |
| 53 | } |
| 54 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 55 | FibImpl::FibImpl () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 60 | FibImpl::NotifyNewAggregate () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 61 | { |
| 62 | Object::NotifyNewAggregate (); |
| 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 66 | FibImpl::DoDispose (void) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 67 | { |
| 68 | clear (); |
| 69 | Object::DoDispose (); |
| 70 | } |
| 71 | |
| 72 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 73 | Ptr<Entry> |
| 74 | FibImpl::LongestPrefixMatch (const InterestHeader &interest) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 75 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 76 | super::iterator item = super::longest_prefix_match (interest.GetName ()); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 77 | // @todo use predicate to search with exclude filters |
| 78 | |
| 79 | if (item == super::end ()) |
| 80 | return 0; |
| 81 | else |
| 82 | return item->payload (); |
| 83 | } |
| 84 | |
| 85 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 86 | Ptr<Entry> |
| 87 | FibImpl::Add (const NameComponents &prefix, Ptr<Face> face, int32_t metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 88 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 89 | return Add (Create<NameComponents> (prefix), face, metric); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 92 | Ptr<Entry> |
| 93 | FibImpl::Add (const Ptr<const NameComponents> &prefix, Ptr<Face> face, int32_t metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 94 | { |
Alexander Afanasyev | 1ba09b8 | 2012-07-09 09:16:14 -0700 | [diff] [blame] | 95 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix) << boost::cref(*face) << metric); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 96 | |
| 97 | // will add entry if doesn't exists, or just return an iterator to the existing entry |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 98 | std::pair< super::iterator, bool > result = super::insert (*prefix, 0); |
| 99 | if (result.first != super::end ()) |
| 100 | { |
| 101 | if (result.second) |
| 102 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 103 | Ptr<EntryImpl> newEntry = Create<EntryImpl> (prefix); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 104 | newEntry->SetTrie (result.first); |
| 105 | result.first->set_payload (newEntry); |
| 106 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 108 | super::modify (result.first, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 109 | ll::bind (&Entry::AddOrUpdateRoutingMetric, ll::_1, face, metric)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 111 | return result.first->payload (); |
| 112 | } |
| 113 | else |
| 114 | return 0; |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 118 | FibImpl::Remove (const Ptr<const NameComponents> &prefix) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 119 | { |
| 120 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
| 121 | |
| 122 | super::erase (*prefix); |
| 123 | } |
| 124 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 125 | // void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 126 | // FibImpl::Invalidate (const Ptr<const NameComponents> &prefix) |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 127 | // { |
| 128 | // NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 130 | // super::iterator foundItem, lastItem; |
| 131 | // bool reachLast; |
| 132 | // boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (*prefix); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 133 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 134 | // if (!reachLast || lastItem->payload () == 0) |
| 135 | // return; // nothing to invalidate |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 137 | // super::modify (lastItem, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 138 | // ll::bind (&Entry::Invalidate, ll::_1)); |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 139 | // } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 140 | |
| 141 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 142 | FibImpl::InvalidateAll () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 143 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 144 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId ()); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 146 | super::parent_trie::recursive_iterator item (super::getTrie ()); |
| 147 | super::parent_trie::recursive_iterator end (0); |
| 148 | for (; item != end; item++) |
| 149 | { |
| 150 | if (item->payload () == 0) continue; |
| 151 | |
| 152 | super::modify (&(*item), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 153 | ll::bind (&Entry::Invalidate, ll::_1)); |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 154 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 158 | FibImpl::RemoveFace (super::parent_trie &item, Ptr<Face> face) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 159 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 160 | if (item.payload () == 0) return; |
| 161 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 163 | super::modify (&item, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 164 | ll::bind (&Entry::RemoveFace, ll::_1, face)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 168 | FibImpl::RemoveFromAll (Ptr<Face> face) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 169 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 170 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 171 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 172 | std::for_each (super::parent_trie::recursive_iterator (super::getTrie ()), |
| 173 | super::parent_trie::recursive_iterator (0), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 174 | ll::bind (&FibImpl::RemoveFace, |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 175 | this, ll::_1, face)); |
| 176 | |
| 177 | super::parent_trie::recursive_iterator trieNode (super::getTrie ()); |
| 178 | super::parent_trie::recursive_iterator end (0); |
| 179 | for (; trieNode != end; trieNode++) |
| 180 | { |
| 181 | if (trieNode->payload () == 0) continue; |
| 182 | |
| 183 | if (trieNode->payload ()->m_faces.size () == 0) |
| 184 | { |
| 185 | trieNode = super::parent_trie::recursive_iterator (trieNode->erase ()); |
| 186 | } |
| 187 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 191 | FibImpl::Print (std::ostream &os) const |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 192 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 193 | // !!! unordered_set imposes "random" order of item in the same level !!! |
| 194 | super::parent_trie::const_recursive_iterator item (super::getTrie ()); |
| 195 | super::parent_trie::const_recursive_iterator end (0); |
| 196 | for (; item != end; item++) |
| 197 | { |
| 198 | if (item->payload () == 0) continue; |
| 199 | |
| 200 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 201 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 204 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 205 | FibImpl::GetSize () const |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 206 | { |
| 207 | return super::getPolicy ().size (); |
| 208 | } |
| 209 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 210 | Ptr<const Entry> |
| 211 | FibImpl::Begin () |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 212 | { |
| 213 | super::parent_trie::const_recursive_iterator item (super::getTrie ()); |
| 214 | super::parent_trie::const_recursive_iterator end (0); |
| 215 | for (; item != end; item++) |
| 216 | { |
| 217 | if (item->payload () == 0) continue; |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | if (item == end) |
| 222 | return End (); |
| 223 | else |
| 224 | return item->payload (); |
| 225 | } |
| 226 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 227 | Ptr<const Entry> |
| 228 | FibImpl::End () |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 229 | { |
| 230 | return 0; |
| 231 | } |
| 232 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 233 | Ptr<const Entry> |
| 234 | FibImpl::Next (Ptr<const Entry> from) |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 235 | { |
| 236 | if (from == 0) return 0; |
| 237 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 238 | super::parent_trie::const_recursive_iterator item (*StaticCast<const EntryImpl> (from)->to_iterator ()); |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 239 | super::parent_trie::const_recursive_iterator end (0); |
| 240 | for (item++; item != end; item++) |
| 241 | { |
| 242 | if (item->payload () == 0) continue; |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | if (item == end) |
| 247 | return End (); |
| 248 | else |
| 249 | return item->payload (); |
| 250 | } |
| 251 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 252 | } // namespace fib |
| 253 | } // namespace ndn |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 254 | } // namespace ns3 |