blob: b5984e7c35e2ad336403950cdb194e9fe58bc669 [file] [log] [blame]
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07001/* -*- 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 Afanasyevc5a23e22011-09-07 00:37:36 -070022#include "ns3/ptr.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/log.h"
24#include "ns3/simulator.h"
25#include "ns3/packet.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070026#include "ns3/callback.h"
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080027#include "ns3/string.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080028#include "ns3/boolean.h"
29#include "ns3/uinteger.h"
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080030#include "ns3/double.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080031
32#include "ns3/ccnx.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080033#include "../model/ccnx-local-face.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080034#include "ns3/ccnx-interest-header.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070035#include "ns3/ccnx-content-object-header.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070036
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080037#include <boost/ref.hpp>
Alexander Afanasyev781ea812011-12-15 22:42:09 -080038#include <boost/lexical_cast.hpp>
39#include <boost/lambda/lambda.hpp>
40#include <boost/lambda/bind.hpp>
41
42namespace ll = boost::lambda;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080043
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070044NS_LOG_COMPONENT_DEFINE ("CcnxConsumer");
45
46namespace ns3
47{
48
49NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer);
50
51TypeId
52CcnxConsumer::GetTypeId (void)
53{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080054 static TypeId tid = TypeId ("ns3::CcnxConsumer")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080055 .SetParent<CcnxApp> ()
Alexander Afanasyev781ea812011-12-15 22:42:09 -080056 .AddAttribute ("StartSeq", "Initial sequence number",
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080057 IntegerValue (0),
Alexander Afanasyev781ea812011-12-15 22:42:09 -080058 MakeIntegerAccessor(&CcnxConsumer::m_seq),
59 MakeIntegerChecker<int32_t>())
Alexander Afanasyev011b8592011-12-21 14:45:27 -080060
Alexander Afanasyev011b8592011-12-21 14:45:27 -080061 .AddAttribute ("PayloadSize", "Average size of content object size (to calculate interest generation rate)",
62 UintegerValue (1040),
63 MakeUintegerAccessor (&CcnxConsumer::GetPayloadSize, &CcnxConsumer::SetPayloadSize),
64 MakeUintegerChecker<uint32_t>())
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080065 .AddAttribute ("Size", "Amount of data in megabytes to request (relies on PayloadSize parameter)",
66 DoubleValue (-1), // don't impose limit by default
67 MakeDoubleAccessor (&CcnxConsumer::GetMaxSize, &CcnxConsumer::SetMaxSize),
68 MakeDoubleChecker<double> ())
Alexander Afanasyev011b8592011-12-21 14:45:27 -080069
Alexander Afanasyev4975f732011-12-20 17:52:19 -080070 .AddAttribute ("Prefix","CcnxName of the Interest",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080071 StringValue ("/"),
72 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
73 MakeCcnxNameComponentsChecker ())
Ilya Moiseenko956d0542012-01-02 15:26:40 -080074 .AddAttribute ("LifeTime", "LifeTime for interest packet",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080075 StringValue ("2s"),
76 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
77 MakeTimeChecker ())
78 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
79 IntegerValue(-1),
80 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
81 MakeIntegerChecker<int32_t>())
82 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
83 IntegerValue(-1),
84 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
85 MakeIntegerChecker<int32_t>())
86 .AddAttribute ("ChildSelector", "ChildSelector",
87 BooleanValue(false),
88 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
89 MakeBooleanChecker())
90 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
91 CcnxNameComponentsValue (),
92 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
93 MakeCcnxNameComponentsChecker ())
Alexander Afanasyev781ea812011-12-15 22:42:09 -080094
Alexander Afanasyev781ea812011-12-15 22:42:09 -080095 .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 Afanasyevbdc0d982011-12-16 01:15:26 -0800101 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
102 MakeTraceSourceAccessor (&CcnxConsumer::m_transmittedInterests))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700106}
107
108CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 : m_rand (0, std::numeric_limits<uint32_t>::max ())
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800110 , m_payloadSize (1040)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111 , m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700112{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800114
115 m_rtt = CreateObject<RttMeanDeviation> ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700116}
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800117
118void
119CcnxConsumer::SetRetxTimer (Time retxTimer)
120{
121 m_retxTimer = retxTimer;
122 if (m_retxEvent.IsRunning ())
123 m_retxEvent.Cancel (); // cancel any scheduled cleanup events
124
125 // schedule even with new timeout
126 m_retxEvent = Simulator::Schedule (m_retxTimer,
127 &CcnxConsumer::CheckRetxTimeout, this);
128}
129
130Time
131CcnxConsumer::GetRetxTimer () const
132{
133 return m_retxTimer;
134}
135
136void
137CcnxConsumer::CheckRetxTimeout ()
138{
139 Time now = Simulator::Now ();
140
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800141 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
142
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800143 Time rto = m_rtt->RetransmitTimeout ();
144
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800145 while (!m_seqTimeouts.empty ())
146 {
147 SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
148 m_seqTimeouts.get<i_timestamp> ().begin ();
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800149 if (entry->time + rto <= now) // timeout expired?
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800150 {
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800151 m_seqTimeouts.get<i_timestamp> ().erase (entry);
152 OnTimeout (entry->seq);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800153 }
154 else
155 break; // nothing else to do. All later packets need not be retransmitted
156 }
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800157
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800158 m_retxEvent = Simulator::Schedule (m_retxTimer,
159 &CcnxConsumer::CheckRetxTimeout, this);
160}
161
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800162uint32_t
163CcnxConsumer::GetPayloadSize () const
164{
165 return m_payloadSize;
166}
167
168void
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800169CcnxConsumer::SetPayloadSize (uint32_t payload)
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800170{
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800171 m_payloadSize = payload;
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800172}
173
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800174double
175CcnxConsumer::GetMaxSize () const
176{
177 if (m_seqMax == 0)
178 return -1.0;
179
180 return m_seqMax * m_payloadSize / 1024.0 / 1024.0;
181}
182
183void
184CcnxConsumer::SetMaxSize (double size)
185{
186 if (size < 0)
187 {
188 m_seqMax = 0;
189 return;
190 }
191
192 m_seqMax = floor(1.0 + size * 1024.0 * 1024.0 / m_payloadSize);
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800193 NS_LOG_DEBUG ("MaxSeqNo: " << m_seqMax);
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800194}
195
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700196// Application Methods
197void
198CcnxConsumer::StartApplication () // Called at time specified by Start
199{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800200 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700201
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800202 // do base stuff
203 CcnxApp::StartApplication ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800204
205 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700206}
207
208void
209CcnxConsumer::StopApplication () // Called at time specified by Stop
210{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800211 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700212
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800213 // cancel periodic packet generation
214 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700215
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800216 // cleanup base stuff
217 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700218}
219
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700220void
221CcnxConsumer::SendPacket ()
222{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800223 if (!m_active) return;
224
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800225 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800226
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800227 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
228
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800229 uint32_t seq;
230
231 if (m_retxSeqs.size () != 0)
232 {
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800233 // for (RetxSeqsContainer::const_iterator i=m_retxSeqs.begin (); i!=m_retxSeqs.end (); i++)
234 // {
235 // std::cout << *i << " ";
236 // }
237 // std::cout << "\n";
238
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800239 seq = *m_retxSeqs.begin ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800240 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800241 m_retxSeqs.erase (m_retxSeqs.begin ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800242 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800243 }
244 else
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800245 {
246 if (m_seqMax > 0)
247 {
248 if (m_seq >= m_seqMax)
249 {
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800250 return; // we are totally done
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800251 }
252 }
253
254 seq = m_seq++;
255 }
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800256
257 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800258
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800259 //
260 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800261 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800262 //
263
264 CcnxInterestHeader interestHeader;
265 interestHeader.SetNonce (m_rand.GetValue ());
266 interestHeader.SetName (nameWithSequence);
267 interestHeader.SetInterestLifetime (m_interestLifeTime);
268 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800269 if (m_exclude.size ()>0)
270 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800271 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800272 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800273 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
274 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700275
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800276 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
277 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700278
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800279 Ptr<Packet> packet = Create<Packet> ();
280 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800281
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800282 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800283
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800284 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
285
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800286 m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800287 m_transmittedInterests (&interestHeader, this, m_face);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800288
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800289 m_rtt->SentSeq (SequenceNumber32 (seq), 1);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800290 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700291}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700292
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800293///////////////////////////////////////////////////
294// Process incoming packets //
295///////////////////////////////////////////////////
296
297
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700298void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700299CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
300 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700301{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800302 if (!m_active) return;
303
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800304 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
305
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800306 NS_LOG_FUNCTION (this << contentObject << payload);
307
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800308 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
309
310 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
311 NS_LOG_INFO ("< DATA for " << seq);
312
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800313 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
314
Alexander Afanasyev8dcfa5d2012-01-05 14:22:25 -0700315 // SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800316
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800317 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
318 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800319
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800320 // if (entry != m_seqTimeouts.end ())
321 // m_seqTimeouts.erase (entry);
322
323 m_seqTimeouts.erase (seq);
324 m_retxSeqs.erase (seq);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800325
326 m_rtt->AckSeq (SequenceNumber32 (seq));
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800327}
328
329void
330CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
331{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800332 if (!m_active) return;
333
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800334 CcnxApp::OnNack (interest); // tracing inside
335
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800336 NS_LOG_DEBUG ("Nack type: " << interest->GetNack ());
337 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
338
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800339 NS_LOG_FUNCTION (this << interest);
340
341 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
342 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
343 NS_LOG_INFO ("< NACK for " << seq);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800344 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << "NACK for " << seq << "\n";
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800345
346 // put in the queue of interests to be retransmitted
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800347 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800348 m_retxSeqs.insert (seq);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800349 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800350
351 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700352}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700353
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800354void
355CcnxConsumer::OnTimeout (uint32_t sequenceNumber)
356{
357 // std::cout << "TO: " << sequenceNumber << "\n";
358 // m_retxSeqs.insert (sequenceNumber);
359 // std::cout << "Current RTO: " << m_rtt->RetransmitTimeout ().ToDouble (Time::S) << "s\n";
360 ScheduleNextPacket ();
361}
362
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800363} // namespace ns3