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.h" |
| 24 | #include "ns3/ndn-face.h" |
| 25 | #include "ns3/ndn-interest-header.h" |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 26 | |
| 27 | #include "ns3/node.h" |
| 28 | #include "ns3/assert.h" |
| 29 | #include "ns3/names.h" |
| 30 | #include "ns3/log.h" |
| 31 | |
| 32 | #include <boost/ref.hpp> |
| 33 | #include <boost/lambda/lambda.hpp> |
| 34 | #include <boost/lambda/bind.hpp> |
| 35 | namespace ll = boost::lambda; |
| 36 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 37 | NS_LOG_COMPONENT_DEFINE ("NdnFibImpl"); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 38 | |
| 39 | namespace ns3 { |
| 40 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 41 | NS_OBJECT_ENSURE_REGISTERED (NdnFibImpl); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 42 | |
| 43 | TypeId |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 44 | NdnFibImpl::GetTypeId (void) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 45 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 46 | static TypeId tid = TypeId ("ns3::NdnFib") // cheating ns3 object system |
| 47 | .SetParent<NdnFib> () |
| 48 | .SetGroupName ("Ndn") |
| 49 | .AddConstructor<NdnFibImpl> () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 50 | ; |
| 51 | return tid; |
| 52 | } |
| 53 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 54 | NdnFibImpl::NdnFibImpl () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
| 58 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 59 | NdnFibImpl::NotifyNewAggregate () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 60 | { |
| 61 | Object::NotifyNewAggregate (); |
| 62 | } |
| 63 | |
| 64 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 65 | NdnFibImpl::DoDispose (void) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 66 | { |
| 67 | clear (); |
| 68 | Object::DoDispose (); |
| 69 | } |
| 70 | |
| 71 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 72 | Ptr<NdnFibEntry> |
| 73 | NdnFibImpl::LongestPrefixMatch (const NdnInterestHeader &interest) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 74 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 75 | super::iterator item = super::longest_prefix_match (interest.GetName ()); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 76 | // @todo use predicate to search with exclude filters |
| 77 | |
| 78 | if (item == super::end ()) |
| 79 | return 0; |
| 80 | else |
| 81 | return item->payload (); |
| 82 | } |
| 83 | |
| 84 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 85 | Ptr<NdnFibEntry> |
| 86 | NdnFibImpl::Add (const NdnNameComponents &prefix, Ptr<NdnFace> face, int32_t metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 87 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 88 | return Add (Create<NdnNameComponents> (prefix), face, metric); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 91 | Ptr<NdnFibEntry> |
| 92 | NdnFibImpl::Add (const Ptr<const NdnNameComponents> &prefix, Ptr<NdnFace> face, int32_t metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 93 | { |
Alexander Afanasyev | 1ba09b8 | 2012-07-09 09:16:14 -0700 | [diff] [blame] | 94 | 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] | 95 | |
| 96 | // 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] | 97 | std::pair< super::iterator, bool > result = super::insert (*prefix, 0); |
| 98 | if (result.first != super::end ()) |
| 99 | { |
| 100 | if (result.second) |
| 101 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 102 | Ptr<NdnFibEntryImpl> newEntry = Create<NdnFibEntryImpl> (prefix); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 103 | newEntry->SetTrie (result.first); |
| 104 | result.first->set_payload (newEntry); |
| 105 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 106 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 107 | super::modify (result.first, |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 108 | ll::bind (&NdnFibEntry::AddOrUpdateRoutingMetric, ll::_1, face, metric)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 109 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 110 | return result.first->payload (); |
| 111 | } |
| 112 | else |
| 113 | return 0; |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 117 | NdnFibImpl::Remove (const Ptr<const NdnNameComponents> &prefix) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 118 | { |
| 119 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
| 120 | |
| 121 | super::erase (*prefix); |
| 122 | } |
| 123 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 124 | // void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 125 | // NdnFibImpl::Invalidate (const Ptr<const NdnNameComponents> &prefix) |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 126 | // { |
| 127 | // NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 129 | // super::iterator foundItem, lastItem; |
| 130 | // bool reachLast; |
| 131 | // boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (*prefix); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 133 | // if (!reachLast || lastItem->payload () == 0) |
| 134 | // return; // nothing to invalidate |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 136 | // super::modify (lastItem, |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 137 | // ll::bind (&NdnFibEntry::Invalidate, ll::_1)); |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 138 | // } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 139 | |
| 140 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 141 | NdnFibImpl::InvalidateAll () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 142 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 143 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId ()); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 145 | super::parent_trie::recursive_iterator item (super::getTrie ()); |
| 146 | super::parent_trie::recursive_iterator end (0); |
| 147 | for (; item != end; item++) |
| 148 | { |
| 149 | if (item->payload () == 0) continue; |
| 150 | |
| 151 | super::modify (&(*item), |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 152 | ll::bind (&NdnFibEntry::Invalidate, ll::_1)); |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 157 | NdnFibImpl::RemoveFace (super::parent_trie &item, Ptr<NdnFace> face) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 158 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 159 | if (item.payload () == 0) return; |
| 160 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 162 | super::modify (&item, |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 163 | ll::bind (&NdnFibEntry::RemoveFace, ll::_1, face)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 167 | NdnFibImpl::RemoveFromAll (Ptr<NdnFace> face) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 168 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 169 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 171 | std::for_each (super::parent_trie::recursive_iterator (super::getTrie ()), |
| 172 | super::parent_trie::recursive_iterator (0), |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 173 | ll::bind (&NdnFibImpl::RemoveFace, |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 174 | this, ll::_1, face)); |
| 175 | |
| 176 | super::parent_trie::recursive_iterator trieNode (super::getTrie ()); |
| 177 | super::parent_trie::recursive_iterator end (0); |
| 178 | for (; trieNode != end; trieNode++) |
| 179 | { |
| 180 | if (trieNode->payload () == 0) continue; |
| 181 | |
| 182 | if (trieNode->payload ()->m_faces.size () == 0) |
| 183 | { |
| 184 | trieNode = super::parent_trie::recursive_iterator (trieNode->erase ()); |
| 185 | } |
| 186 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 190 | NdnFibImpl::Print (std::ostream &os) const |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 191 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 192 | // !!! unordered_set imposes "random" order of item in the same level !!! |
| 193 | super::parent_trie::const_recursive_iterator item (super::getTrie ()); |
| 194 | super::parent_trie::const_recursive_iterator end (0); |
| 195 | for (; item != end; item++) |
| 196 | { |
| 197 | if (item->payload () == 0) continue; |
| 198 | |
| 199 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 200 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 203 | uint32_t |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 204 | NdnFibImpl::GetSize () const |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 205 | { |
| 206 | return super::getPolicy ().size (); |
| 207 | } |
| 208 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 209 | Ptr<const NdnFibEntry> |
| 210 | NdnFibImpl::Begin () |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 211 | { |
| 212 | super::parent_trie::const_recursive_iterator item (super::getTrie ()); |
| 213 | super::parent_trie::const_recursive_iterator end (0); |
| 214 | for (; item != end; item++) |
| 215 | { |
| 216 | if (item->payload () == 0) continue; |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | if (item == end) |
| 221 | return End (); |
| 222 | else |
| 223 | return item->payload (); |
| 224 | } |
| 225 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 226 | Ptr<const NdnFibEntry> |
| 227 | NdnFibImpl::End () |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 228 | { |
| 229 | return 0; |
| 230 | } |
| 231 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 232 | Ptr<const NdnFibEntry> |
| 233 | NdnFibImpl::Next (Ptr<const NdnFibEntry> from) |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 234 | { |
| 235 | if (from == 0) return 0; |
| 236 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 237 | super::parent_trie::const_recursive_iterator item (*StaticCast<const NdnFibEntryImpl> (from)->to_iterator ()); |
Alexander Afanasyev | 95a4fa3 | 2012-07-09 15:23:59 -0700 | [diff] [blame] | 238 | super::parent_trie::const_recursive_iterator end (0); |
| 239 | for (item++; item != end; item++) |
| 240 | { |
| 241 | if (item->payload () == 0) continue; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | if (item == end) |
| 246 | return End (); |
| 247 | else |
| 248 | return item->payload (); |
| 249 | } |
| 250 | |
| 251 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 252 | } // namespace ns3 |