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