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