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 | |
| 21 | #include "ccnx-fib-impl.h" |
| 22 | |
| 23 | #include "ccnx.h" |
| 24 | #include "ccnx-face.h" |
| 25 | #include "ccnx-interest-header.h" |
| 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 | |
| 37 | NS_LOG_COMPONENT_DEFINE ("CcnxFibImpl"); |
| 38 | |
| 39 | namespace ns3 { |
| 40 | |
| 41 | NS_OBJECT_ENSURE_REGISTERED (CcnxFibImpl); |
| 42 | |
| 43 | TypeId |
| 44 | CcnxFibImpl::GetTypeId (void) |
| 45 | { |
| 46 | static TypeId tid = TypeId ("ns3::CcnxFib") // cheating ns3 object system |
| 47 | .SetParent<Object> () |
| 48 | .SetGroupName ("Ccnx") |
| 49 | .AddConstructor<CcnxFibImpl> () |
| 50 | ; |
| 51 | return tid; |
| 52 | } |
| 53 | |
| 54 | CcnxFibImpl::CcnxFibImpl () |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | CcnxFibImpl::NotifyNewAggregate () |
| 60 | { |
| 61 | Object::NotifyNewAggregate (); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | CcnxFibImpl::DoDispose (void) |
| 66 | { |
| 67 | clear (); |
| 68 | Object::DoDispose (); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | CcnxFib::iterator |
| 73 | CcnxFibImpl::LongestPrefixMatch (const CcnxInterestHeader &interest) const |
| 74 | { |
| 75 | super::iterator item = const_cast<CcnxFibImpl*> (this)->super::longest_prefix_match (interest.GetName ()); |
| 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 | |
| 85 | CcnxFib::iterator |
| 86 | CcnxFibImpl::Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric) |
| 87 | { |
| 88 | return Add (Create<CcnxNameComponents> (prefix), face, metric); |
| 89 | } |
| 90 | |
| 91 | CcnxFib::iterator |
| 92 | CcnxFibImpl::Add (const Ptr<const CcnxNameComponents> &prefix, Ptr<CcnxFace> face, int32_t metric) |
| 93 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 94 | NS_LOG_FUNCTION (this); |
| 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 | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 98 | Ptr<CcnxFibEntry> newEntry = Create<CcnxFibEntry> (prefix); |
| 99 | std::pair< super::iterator, bool > result = super::insert (*prefix, newEntry); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 100 | |
| 101 | NS_ASSERT_MSG (face != NULL, "Trying to modify NULL face"); |
| 102 | |
| 103 | super::modify (result.first, |
| 104 | ll::bind (&CcnxFibEntry::AddOrUpdateRoutingMetric, ll::_1, face, metric)); |
| 105 | |
| 106 | return result.first->payload (); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | CcnxFibImpl::Remove (const Ptr<const CcnxNameComponents> &prefix) |
| 111 | { |
| 112 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
| 113 | |
| 114 | super::erase (*prefix); |
| 115 | } |
| 116 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 117 | // void |
| 118 | // CcnxFibImpl::Invalidate (const Ptr<const CcnxNameComponents> &prefix) |
| 119 | // { |
| 120 | // NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 122 | // super::iterator foundItem, lastItem; |
| 123 | // bool reachLast; |
| 124 | // boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (*prefix); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 126 | // if (!reachLast || lastItem->payload () == 0) |
| 127 | // return; // nothing to invalidate |
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::modify (lastItem, |
| 130 | // ll::bind (&CcnxFibEntry::Invalidate, ll::_1)); |
| 131 | // } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 132 | |
| 133 | void |
| 134 | CcnxFibImpl::InvalidateAll () |
| 135 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 136 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId ()); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 138 | super::parent_trie::recursive_iterator item (super::getTrie ()); |
| 139 | super::parent_trie::recursive_iterator end (0); |
| 140 | for (; item != end; item++) |
| 141 | { |
| 142 | if (item->payload () == 0) continue; |
| 143 | |
| 144 | super::modify (&(*item), |
| 145 | ll::bind (&CcnxFibEntry::Invalidate, ll::_1)); |
| 146 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 150 | CcnxFibImpl::Remove (super::parent_trie &item, Ptr<CcnxFace> face) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 151 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 152 | if (item.payload () == 0) return; |
| 153 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 155 | super::modify (&item, |
| 156 | ll::bind (&CcnxFibEntry::RemoveFace, ll::_1, face)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void |
| 160 | CcnxFibImpl::RemoveFromAll (Ptr<CcnxFace> face) |
| 161 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 162 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 164 | std::for_each (super::parent_trie::recursive_iterator (super::getTrie ()), |
| 165 | super::parent_trie::recursive_iterator (0), |
| 166 | ll::bind (static_cast< void (CcnxFib::*) (super::parent_trie &, Ptr<CcnxFace>) > (&CcnxFibImpl::Remove), |
| 167 | this, ll::_1, face)); |
| 168 | |
| 169 | super::parent_trie::recursive_iterator trieNode (super::getTrie ()); |
| 170 | super::parent_trie::recursive_iterator end (0); |
| 171 | for (; trieNode != end; trieNode++) |
| 172 | { |
| 173 | if (trieNode->payload () == 0) continue; |
| 174 | |
| 175 | if (trieNode->payload ()->m_faces.size () == 0) |
| 176 | { |
| 177 | trieNode = super::parent_trie::recursive_iterator (trieNode->erase ()); |
| 178 | } |
| 179 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void |
| 183 | CcnxFibImpl::Print (std::ostream &os) const |
| 184 | { |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame^] | 185 | // !!! unordered_set imposes "random" order of item in the same level !!! |
| 186 | super::parent_trie::const_recursive_iterator item (super::getTrie ()); |
| 187 | super::parent_trie::const_recursive_iterator end (0); |
| 188 | for (; item != end; item++) |
| 189 | { |
| 190 | if (item->payload () == 0) continue; |
| 191 | |
| 192 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 193 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | } // namespace ns3 |