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