Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -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: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "ccnx-consumer.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 22 | #include "ns3/ptr.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 23 | #include "ns3/log.h" |
| 24 | #include "ns3/simulator.h" |
| 25 | #include "ns3/packet.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 26 | #include "ns3/callback.h" |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 27 | #include "ns3/string.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 28 | #include "ns3/boolean.h" |
| 29 | #include "ns3/uinteger.h" |
| 30 | |
| 31 | #include "ns3/ccnx.h" |
Alexander Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 32 | #include "../model/ccnx-local-face.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 33 | #include "ns3/ccnx-interest-header.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 34 | #include "ns3/ccnx-content-object-header.h" |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 36 | #include <boost/ref.hpp> |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 37 | #include <boost/lexical_cast.hpp> |
| 38 | #include <boost/lambda/lambda.hpp> |
| 39 | #include <boost/lambda/bind.hpp> |
| 40 | |
| 41 | namespace ll = boost::lambda; |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 42 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 43 | NS_LOG_COMPONENT_DEFINE ("CcnxConsumer"); |
| 44 | |
| 45 | namespace ns3 |
| 46 | { |
| 47 | |
| 48 | NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer); |
| 49 | |
| 50 | TypeId |
| 51 | CcnxConsumer::GetTypeId (void) |
| 52 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 53 | static TypeId tid = TypeId ("ns3::CcnxConsumer") |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 54 | .SetParent<CcnxApp> () |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 55 | .AddConstructor<CcnxConsumer> () |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 56 | .AddAttribute ("StartSeq", "Initial sequence number", |
| 57 | IntegerValue(0), |
| 58 | MakeIntegerAccessor(&CcnxConsumer::m_seq), |
| 59 | MakeIntegerChecker<int32_t>()) |
| 60 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 61 | .AddAttribute ("OffTime", "Time interval between packets", |
| 62 | StringValue ("100ms"), |
| 63 | MakeTimeAccessor (&CcnxConsumer::m_offTime), |
| 64 | MakeTimeChecker ()) |
| 65 | .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)", |
| 66 | StringValue ("/"), |
| 67 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName), |
| 68 | MakeCcnxNameComponentsChecker ()) |
| 69 | .AddAttribute ("LifeTime", "LifeTime fo interest packet", |
| 70 | StringValue ("2s"), |
| 71 | MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime), |
| 72 | MakeTimeChecker ()) |
| 73 | .AddAttribute ("MinSuffixComponents", "MinSuffixComponents", |
| 74 | IntegerValue(-1), |
| 75 | MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents), |
| 76 | MakeIntegerChecker<int32_t>()) |
| 77 | .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents", |
| 78 | IntegerValue(-1), |
| 79 | MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents), |
| 80 | MakeIntegerChecker<int32_t>()) |
| 81 | .AddAttribute ("ChildSelector", "ChildSelector", |
| 82 | BooleanValue(false), |
| 83 | MakeBooleanAccessor(&CcnxConsumer::m_childSelector), |
| 84 | MakeBooleanChecker()) |
| 85 | .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)", |
| 86 | CcnxNameComponentsValue (), |
| 87 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude), |
| 88 | MakeCcnxNameComponentsChecker ()) |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 89 | |
| 90 | .AddAttribute ("RTO", |
| 91 | "Initial retransmission timeout", |
| 92 | StringValue ("1s"), |
| 93 | MakeTimeAccessor (&CcnxConsumer::m_rto), |
| 94 | MakeTimeChecker ()) |
| 95 | .AddAttribute ("RetxTimer", |
| 96 | "Timeout defining how frequent retransmission timeouts should be checked", |
| 97 | StringValue ("1s"), |
| 98 | MakeTimeAccessor (&CcnxConsumer::GetRetxTimer, &CcnxConsumer::SetRetxTimer), |
| 99 | MakeTimeChecker ()) |
| 100 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 101 | .AddTraceSource ("TransmittedInterests", "TransmittedInterests", |
| 102 | MakeTraceSourceAccessor (&CcnxConsumer::m_transmittedInterests)) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 103 | ; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 105 | return tid; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | CcnxConsumer::CcnxConsumer () |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 109 | : m_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 110 | , m_seq (0) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 111 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 112 | NS_LOG_FUNCTION_NOARGS (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 113 | } |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 114 | |
| 115 | void |
| 116 | CcnxConsumer::SetRetxTimer (Time retxTimer) |
| 117 | { |
| 118 | m_retxTimer = retxTimer; |
| 119 | if (m_retxEvent.IsRunning ()) |
| 120 | m_retxEvent.Cancel (); // cancel any scheduled cleanup events |
| 121 | |
| 122 | // schedule even with new timeout |
| 123 | m_retxEvent = Simulator::Schedule (m_retxTimer, |
| 124 | &CcnxConsumer::CheckRetxTimeout, this); |
| 125 | } |
| 126 | |
| 127 | Time |
| 128 | CcnxConsumer::GetRetxTimer () const |
| 129 | { |
| 130 | return m_retxTimer; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | CcnxConsumer::CheckRetxTimeout () |
| 135 | { |
| 136 | Time now = Simulator::Now (); |
| 137 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 138 | boost::mutex::scoped_lock (m_seqTimeoutsGuard); |
| 139 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 140 | while (!m_seqTimeouts.empty ()) |
| 141 | { |
| 142 | SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry = |
| 143 | m_seqTimeouts.get<i_timestamp> ().begin (); |
| 144 | if (entry->time + m_rto <= now) // timeout expired? |
| 145 | { |
| 146 | m_retxSeqs.insert (entry->seq); |
| 147 | m_seqTimeouts.get<i_timestamp> ().modify (entry, |
| 148 | ll::bind(&SeqTimeout::time, ll::_1) = now); |
| 149 | } |
| 150 | else |
| 151 | break; // nothing else to do. All later packets need not be retransmitted |
| 152 | } |
| 153 | |
| 154 | m_retxEvent = Simulator::Schedule (m_retxTimer, |
| 155 | &CcnxConsumer::CheckRetxTimeout, this); |
| 156 | } |
| 157 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 158 | // Application Methods |
| 159 | void |
| 160 | CcnxConsumer::StartApplication () // Called at time specified by Start |
| 161 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 162 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 164 | // do base stuff |
| 165 | CcnxApp::StartApplication (); |
| 166 | |
| 167 | // schedule periodic packet generation |
| 168 | m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
| 172 | CcnxConsumer::StopApplication () // Called at time specified by Stop |
| 173 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 174 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 176 | // cancel periodic packet generation |
| 177 | Simulator::Cancel (m_sendEvent); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 179 | // cleanup base stuff |
| 180 | CcnxApp::StopApplication (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 183 | void |
| 184 | CcnxConsumer::SendPacket () |
| 185 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 186 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 188 | boost::mutex::scoped_lock (m_seqTimeoutsGuard); |
| 189 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 190 | uint32_t seq; |
| 191 | |
| 192 | if (m_retxSeqs.size () != 0) |
| 193 | { |
| 194 | seq = *m_retxSeqs.begin (); |
| 195 | m_retxSeqs.erase (m_retxSeqs.begin ()); |
| 196 | } |
| 197 | else |
| 198 | seq = m_seq++; |
| 199 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 200 | // |
| 201 | Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 202 | (*nameWithSequence) (seq); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 203 | // |
| 204 | |
| 205 | CcnxInterestHeader interestHeader; |
| 206 | interestHeader.SetNonce (m_rand.GetValue ()); |
| 207 | interestHeader.SetName (nameWithSequence); |
| 208 | interestHeader.SetInterestLifetime (m_interestLifeTime); |
| 209 | interestHeader.SetChildSelector (m_childSelector); |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 210 | if (m_exclude.size ()>0) |
| 211 | { |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 212 | interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude)); |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 213 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 214 | interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents); |
| 215 | interestHeader.SetMinSuffixComponents (m_minSuffixComponents); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 217 | // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader); |
| 218 | NS_LOG_INFO ("> Interest for " << seq); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 219 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 220 | Ptr<Packet> packet = Create<Packet> (); |
| 221 | packet->AddHeader (interestHeader); |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 223 | m_protocolHandler (packet); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 224 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 225 | NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items"); |
| 226 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 227 | std::pair<SeqTimeoutsContainer::iterator, bool> |
| 228 | res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ())); |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 229 | |
Alexander Afanasyev | a845f72 | 2011-12-16 13:11:02 -0800 | [diff] [blame] | 230 | if (!res.second) |
| 231 | m_seqTimeouts.modify (res.first, |
| 232 | ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ()); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 233 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 234 | m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 235 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 236 | m_transmittedInterests (&interestHeader, this, m_face); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 237 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 238 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 239 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 240 | CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
| 241 | const Ptr<const Packet> &payload) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 242 | { |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 243 | CcnxApp::OnContentObject (contentObject, payload); // tracing inside |
| 244 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 245 | NS_LOG_FUNCTION (this << contentObject << payload); |
| 246 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 247 | // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject)); |
| 248 | |
| 249 | uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ()); |
| 250 | NS_LOG_INFO ("< DATA for " << seq); |
| 251 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 252 | boost::mutex::scoped_lock (m_seqTimeoutsGuard); |
| 253 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 254 | SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq); |
| 255 | |
| 256 | NS_ASSERT_MSG (entry != m_seqTimeouts.end (), |
| 257 | "Comment out this assert, if it causes problems"); |
| 258 | |
| 259 | if (entry != m_seqTimeouts.end ()) |
| 260 | m_seqTimeouts.erase (entry); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void |
| 264 | CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest) |
| 265 | { |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 266 | CcnxApp::OnNack (interest); // tracing inside |
| 267 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame] | 268 | NS_LOG_FUNCTION (this << interest); |
| 269 | |
| 270 | // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest)); |
| 271 | uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ()); |
| 272 | NS_LOG_INFO ("< NACK for " << seq); |
| 273 | |
| 274 | // put in the queue of interests to be retransmitted |
| 275 | m_retxSeqs.insert (seq); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 276 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 277 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 278 | } // namespace ns3 |