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 | { |
| 94 | NS_LOG_FUNCTION(this->GetObject<Node> ()->GetId () << boost::cref(*prefix) << boost::cref(*face) << metric); |
| 95 | |
| 96 | // will add entry if doesn't exists, or just return an iterator to the existing entry |
| 97 | std::pair< super::iterator, bool > result = super::insert (*prefix, Create<CcnxFibEntry> (prefix)); |
| 98 | |
| 99 | NS_ASSERT_MSG (face != NULL, "Trying to modify NULL face"); |
| 100 | |
| 101 | super::modify (result.first, |
| 102 | ll::bind (&CcnxFibEntry::AddOrUpdateRoutingMetric, ll::_1, face, metric)); |
| 103 | |
| 104 | return result.first->payload (); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | CcnxFibImpl::Remove (const Ptr<const CcnxNameComponents> &prefix) |
| 109 | { |
| 110 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
| 111 | |
| 112 | super::erase (*prefix); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | CcnxFibImpl::Invalidate (const Ptr<const CcnxNameComponents> &prefix) |
| 117 | { |
| 118 | NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix)); |
| 119 | |
| 120 | super::iterator foundItem, lastItem; |
| 121 | bool reachLast; |
| 122 | boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (*prefix); |
| 123 | |
| 124 | if (!reachLast || lastItem->payload () == 0) |
| 125 | return; // nothing to invalidate |
| 126 | |
| 127 | super::modify (lastItem, |
| 128 | ll::bind (&CcnxFibEntry::Invalidate, ll::_1)); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | CcnxFibImpl::InvalidateAll () |
| 133 | { |
| 134 | // NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId ()); |
| 135 | |
| 136 | // for (super::iterator entry = m_fib.begin (); |
| 137 | // entry != m_fib.end (); |
| 138 | // entry ++) |
| 139 | // { |
| 140 | // m_fib.modify (entry, |
| 141 | // ll::bind (&CcnxFibEntry::Invalidate, ll::_1)); |
| 142 | // } |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | CcnxFibImpl::Remove (const CcnxFibEntry &entry, Ptr<CcnxFace> face) |
| 147 | { |
| 148 | // NS_LOG_FUNCTION (this); |
| 149 | |
| 150 | // m_fib.modify (m_fib.iterator_to (entry), |
| 151 | // ll::bind (&CcnxFibEntry::RemoveFace, ll::_1, face)); |
| 152 | // if (entry.m_faces.size () == 0) |
| 153 | // { |
| 154 | // m_fib.erase (m_fib.iterator_to (entry)); |
| 155 | // } |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | CcnxFibImpl::RemoveFromAll (Ptr<CcnxFace> face) |
| 160 | { |
| 161 | // NS_LOG_FUNCTION (this); |
| 162 | |
| 163 | // for_each (m_fib.begin (), m_fib.end (), |
| 164 | // ll::bind (static_cast< void (CcnxFib::*) (const CcnxFibEntry &, Ptr<CcnxFace>) > (&CcnxFib::Remove), |
| 165 | // this, ll::_1, face)); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | CcnxFibImpl::Print (std::ostream &os) const |
| 170 | { |
| 171 | // for (super::iterator entry = fib.m_fib.begin (); |
| 172 | // entry != fib.m_fib.end (); |
| 173 | // entry++) |
| 174 | // { |
| 175 | // os << entry->GetPrefix () << "\t" << *entry << "\n"; |
| 176 | // } |
| 177 | } |
| 178 | |
| 179 | } // namespace ns3 |