Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -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-pit-entry.h" |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 23 | #include "ns3/ndn-fib.h" |
| 24 | #include "ns3/ndn-name-components.h" |
| 25 | #include "ns3/ndn-interest-header.h" |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 27 | #include "ns3/simulator.h" |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 28 | #include "ns3/log.h" |
| 29 | |
| 30 | #include <boost/lambda/lambda.hpp> |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 31 | #include <boost/lambda/bind.hpp> |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 32 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 33 | namespace ll = boost::lambda; |
| 34 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 35 | NS_LOG_COMPONENT_DEFINE ("ndn.pit.Entry"); |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | namespace ns3 { |
| 38 | namespace ndn { |
| 39 | namespace pit { |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 41 | Entry::Entry (Pit &container, |
| 42 | Ptr<const InterestHeader> header, |
| 43 | Ptr<fib::Entry> fibEntry) |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 44 | : m_container (container) |
| 45 | , m_prefix (header->GetNamePtr ()) |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 46 | , m_fibEntry (fibEntry) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 47 | , m_expireTime (Simulator::Now () + (!header->GetInterestLifetime ().IsZero ()? |
| 48 | header->GetInterestLifetime (): |
| 49 | Seconds (1.0))) |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 50 | , m_maxRetxCount (0) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 51 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 52 | // note that if interest lifetime is not set, the behavior is undefined |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 55 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 56 | Entry::UpdateLifetime (const Time &offsetTime) |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 57 | { |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 58 | NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S)); |
| 59 | |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 60 | Time newExpireTime = Simulator::Now () + offsetTime; |
| 61 | if (newExpireTime > m_expireTime) |
| 62 | m_expireTime = newExpireTime; |
Alexander Afanasyev | 1aeaf92 | 2012-04-23 13:48:09 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 64 | NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S)); |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 67 | Entry::in_iterator |
| 68 | Entry::AddIncoming (Ptr<Face> face) |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 69 | { |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame] | 70 | std::pair<in_iterator,bool> ret = |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 71 | m_incoming.insert (IncomingFace (face)); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 73 | // NS_ASSERT_MSG (ret.second, "Something is wrong"); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 75 | return ret.first; |
| 76 | } |
| 77 | |
Alexander Afanasyev | 9d313d4 | 2011-11-25 13:36:15 -0800 | [diff] [blame] | 78 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 79 | Entry::RemoveIncoming (Ptr<Face> face) |
Alexander Afanasyev | 9d313d4 | 2011-11-25 13:36:15 -0800 | [diff] [blame] | 80 | { |
| 81 | m_incoming.erase (face); |
| 82 | } |
| 83 | |
| 84 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 85 | Entry::out_iterator |
| 86 | Entry::AddOutgoing (Ptr<Face> face) |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame] | 88 | std::pair<out_iterator,bool> ret = |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 89 | m_outgoing.insert (OutgoingFace (face)); |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 91 | if (!ret.second) |
| 92 | { // outgoing face already exists |
| 93 | m_outgoing.modify (ret.first, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 94 | ll::bind (&OutgoingFace::UpdateOnRetransmit, ll::_1)); |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 95 | } |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 96 | |
| 97 | return ret.first; |
| 98 | } |
| 99 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 100 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 101 | Entry::RemoveAllReferencesToFace (Ptr<Face> face) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame] | 103 | in_iterator incoming = m_incoming.find (face); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 105 | if (incoming != m_incoming.end ()) |
| 106 | m_incoming.erase (incoming); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame] | 108 | out_iterator outgoing = |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 109 | m_outgoing.find (face); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 111 | if (outgoing != m_outgoing.end ()) |
| 112 | m_outgoing.erase (outgoing); |
| 113 | } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | 786936a | 2012-07-17 19:48:15 -0700 | [diff] [blame] | 115 | // void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 116 | // Entry::SetWaitingInVain (Entry::out_iterator face) |
Alexander Afanasyev | 786936a | 2012-07-17 19:48:15 -0700 | [diff] [blame] | 117 | // { |
| 118 | // NS_LOG_DEBUG (boost::cref (*face->m_face)); |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | 786936a | 2012-07-17 19:48:15 -0700 | [diff] [blame] | 120 | // m_outgoing.modify (face, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 121 | // (&ll::_1)->*&EntryOutgoingFace::m_waitingInVain = true); |
Alexander Afanasyev | 786936a | 2012-07-17 19:48:15 -0700 | [diff] [blame] | 122 | // } |
| 123 | |
| 124 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 125 | Entry::SetWaitingInVain (Ptr<Face> face) |
Alexander Afanasyev | 786936a | 2012-07-17 19:48:15 -0700 | [diff] [blame] | 126 | { |
| 127 | // NS_LOG_DEBUG (boost::cref (*face->m_face)); |
| 128 | |
| 129 | out_iterator item = m_outgoing.find (face); |
| 130 | if (item == m_outgoing.end ()) |
| 131 | return; |
| 132 | |
| 133 | m_outgoing.modify (item, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 134 | (&ll::_1)->*&OutgoingFace::m_waitingInVain = true); |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | 786936a | 2012-07-17 19:48:15 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 138 | bool |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 139 | Entry::AreAllOutgoingInVain () const |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 140 | { |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 141 | NS_LOG_DEBUG (m_outgoing.size ()); |
| 142 | |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 143 | bool inVain = true; |
| 144 | std::for_each (m_outgoing.begin (), m_outgoing.end (), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 145 | ll::var(inVain) &= (&ll::_1)->*&OutgoingFace::m_waitingInVain); |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 146 | |
| 147 | NS_LOG_DEBUG ("inVain " << inVain); |
| 148 | return inVain; |
| 149 | } |
| 150 | |
Alexander Afanasyev | a7a2b8b | 2011-11-28 18:19:09 -0800 | [diff] [blame] | 151 | bool |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 152 | Entry::AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const |
Alexander Afanasyev | a7a2b8b | 2011-11-28 18:19:09 -0800 | [diff] [blame] | 153 | { |
| 154 | bool inVain = true; |
| 155 | std::for_each (m_outgoing.begin (), m_outgoing.end (), |
| 156 | ll::var(inVain) &= |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 157 | ((&ll::_1)->*&OutgoingFace::m_face == face || |
| 158 | (&ll::_1)->*&OutgoingFace::m_waitingInVain)); |
Alexander Afanasyev | a7a2b8b | 2011-11-28 18:19:09 -0800 | [diff] [blame] | 159 | |
| 160 | return !inVain; |
| 161 | } |
Alexander Afanasyev | 5a59507 | 2011-11-25 14:49:07 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 163 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 164 | Entry::IncreaseAllowedRetxCount () |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 165 | { |
Alexander Afanasyev | 120bf31 | 2011-12-19 01:24:47 -0800 | [diff] [blame] | 166 | NS_LOG_ERROR (this); |
Alexander Afanasyev | 9a517db | 2012-04-30 13:58:47 -0700 | [diff] [blame] | 167 | if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100)) |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 168 | { |
| 169 | // cheat: |
Alexander Afanasyev | 9a517db | 2012-04-30 13:58:47 -0700 | [diff] [blame] | 170 | // don't allow retransmission faster than every 100ms |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 171 | m_maxRetxCount++; |
Alexander Afanasyev | 9a517db | 2012-04-30 13:58:47 -0700 | [diff] [blame] | 172 | m_lastRetransmission = Simulator::Now (); |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 173 | } |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 176 | std::ostream& operator<< (std::ostream& os, const Entry &entry) |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 177 | { |
| 178 | os << "Prefix: " << *entry.m_prefix << "\n"; |
| 179 | os << "In: "; |
| 180 | bool first = true; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 181 | BOOST_FOREACH (const IncomingFace &face, entry.m_incoming) |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 182 | { |
| 183 | if (!first) |
| 184 | os << ","; |
| 185 | else |
| 186 | first = false; |
| 187 | |
| 188 | os << *face.m_face; |
| 189 | } |
| 190 | |
| 191 | os << "\nOut: "; |
| 192 | first = true; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 193 | BOOST_FOREACH (const OutgoingFace &face, entry.m_outgoing) |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 194 | { |
| 195 | if (!first) |
| 196 | os << ","; |
| 197 | else |
| 198 | first = false; |
| 199 | |
| 200 | os << *face.m_face; |
| 201 | } |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 202 | os << "\nNonces: "; |
| 203 | first = true; |
| 204 | BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces) |
| 205 | { |
| 206 | if (!first) |
| 207 | os << ","; |
| 208 | else |
| 209 | first = false; |
| 210 | |
| 211 | os << nonce; |
| 212 | } |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 213 | |
| 214 | return os; |
| 215 | } |
| 216 | |
| 217 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 218 | } // namespace pit |
| 219 | } // namespace ndn |
| 220 | } // namespace ns3 |