blob: 44573b6c64ae6a60bf37d4d5ba334c6e290a67ea [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 Afanasyev09c7deb2011-11-23 14:50:10 -080056 .AddConstructor<CcnxConsumer> ()
Alexander Afanasyev781ea812011-12-15 22:42:09 -080057 .AddAttribute ("StartSeq", "Initial sequence number",
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080058 IntegerValue (0),
Alexander Afanasyev781ea812011-12-15 22:42:09 -080059 MakeIntegerAccessor(&CcnxConsumer::m_seq),
60 MakeIntegerChecker<int32_t>())
Alexander Afanasyev011b8592011-12-21 14:45:27 -080061
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080062 .AddAttribute ("Size", "Amount of data in megabytes to request (relies on PayloadSize parameter)",
63 DoubleValue (-1), // don't impose limit by default
64 MakeDoubleAccessor (&CcnxConsumer::GetMaxSize, &CcnxConsumer::SetMaxSize),
65 MakeDoubleChecker<double> ())
66
Alexander Afanasyev011b8592011-12-21 14:45:27 -080067 ///////
68 .AddAttribute ("PayloadSize", "Average size of content object size (to calculate interest generation rate)",
69 UintegerValue (1040),
70 MakeUintegerAccessor (&CcnxConsumer::GetPayloadSize, &CcnxConsumer::SetPayloadSize),
71 MakeUintegerChecker<uint32_t>())
72 .AddAttribute ("MeanRate", "Mean data packet rate (relies on the PayloadSize parameter)",
73 StringValue ("100Kbps"),
74 MakeDataRateAccessor (&CcnxConsumer::GetDesiredRate, &CcnxConsumer::SetDesiredRate),
75 MakeDataRateChecker ())
76 ///////
77
Alexander Afanasyev4975f732011-12-20 17:52:19 -080078 .AddAttribute ("Prefix","CcnxName of the Interest",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080079 StringValue ("/"),
80 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
81 MakeCcnxNameComponentsChecker ())
82 .AddAttribute ("LifeTime", "LifeTime fo interest packet",
83 StringValue ("2s"),
84 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
85 MakeTimeChecker ())
86 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
87 IntegerValue(-1),
88 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
89 MakeIntegerChecker<int32_t>())
90 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
91 IntegerValue(-1),
92 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
93 MakeIntegerChecker<int32_t>())
94 .AddAttribute ("ChildSelector", "ChildSelector",
95 BooleanValue(false),
96 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
97 MakeBooleanChecker())
98 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
99 CcnxNameComponentsValue (),
100 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
101 MakeCcnxNameComponentsChecker ())
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800102
103 .AddAttribute ("RTO",
104 "Initial retransmission timeout",
105 StringValue ("1s"),
106 MakeTimeAccessor (&CcnxConsumer::m_rto),
107 MakeTimeChecker ())
108 .AddAttribute ("RetxTimer",
109 "Timeout defining how frequent retransmission timeouts should be checked",
110 StringValue ("1s"),
111 MakeTimeAccessor (&CcnxConsumer::GetRetxTimer, &CcnxConsumer::SetRetxTimer),
112 MakeTimeChecker ())
113
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800114 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
115 MakeTraceSourceAccessor (&CcnxConsumer::m_transmittedInterests))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700117
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700119}
120
121CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800122 : m_rand (0, std::numeric_limits<uint32_t>::max ())
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800123 , m_desiredRate ("10Kbps")
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800124 , m_payloadSize (1040)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125 , m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700126{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800127 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800128
129 UpdateMean (); // not necessary (will be called by ns3 object system anyways), but doesn't hurt
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700130}
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800131
132void
133CcnxConsumer::SetRetxTimer (Time retxTimer)
134{
135 m_retxTimer = retxTimer;
136 if (m_retxEvent.IsRunning ())
137 m_retxEvent.Cancel (); // cancel any scheduled cleanup events
138
139 // schedule even with new timeout
140 m_retxEvent = Simulator::Schedule (m_retxTimer,
141 &CcnxConsumer::CheckRetxTimeout, this);
142}
143
144Time
145CcnxConsumer::GetRetxTimer () const
146{
147 return m_retxTimer;
148}
149
150void
151CcnxConsumer::CheckRetxTimeout ()
152{
153 Time now = Simulator::Now ();
154
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800155 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
156
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800157 while (!m_seqTimeouts.empty ())
158 {
159 SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
160 m_seqTimeouts.get<i_timestamp> ().begin ();
161 if (entry->time + m_rto <= now) // timeout expired?
162 {
163 m_retxSeqs.insert (entry->seq);
164 m_seqTimeouts.get<i_timestamp> ().modify (entry,
165 ll::bind(&SeqTimeout::time, ll::_1) = now);
166 }
167 else
168 break; // nothing else to do. All later packets need not be retransmitted
169 }
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800170
171 if (m_retxSeqs.size () > 0)
172 {
173 ScheduleNextPacket ();
174 }
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800175
176 m_retxEvent = Simulator::Schedule (m_retxTimer,
177 &CcnxConsumer::CheckRetxTimeout, this);
178}
179
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800180void
181CcnxConsumer::UpdateMean ()
182{
183 double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
184 m_randExp = ExponentialVariable (mean, 10000 * mean); // set upper limit to inter-arrival time
185}
186
187void
188CcnxConsumer::SetPayloadSize (uint32_t payload)
189{
190 m_payloadSize = payload;
191 UpdateMean ();
192}
193
194uint32_t
195CcnxConsumer::GetPayloadSize () const
196{
197 return m_payloadSize;
198}
199
200void
201CcnxConsumer::SetDesiredRate (DataRate rate)
202{
203 m_desiredRate = rate;
204 UpdateMean ();
205}
206
207DataRate
208CcnxConsumer::GetDesiredRate () const
209{
210 return m_desiredRate;
211}
212
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800213double
214CcnxConsumer::GetMaxSize () const
215{
216 if (m_seqMax == 0)
217 return -1.0;
218
219 return m_seqMax * m_payloadSize / 1024.0 / 1024.0;
220}
221
222void
223CcnxConsumer::SetMaxSize (double size)
224{
225 if (size < 0)
226 {
227 m_seqMax = 0;
228 return;
229 }
230
231 m_seqMax = floor(1.0 + size * 1024.0 * 1024.0 / m_payloadSize);
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800232 NS_LOG_DEBUG ("MaxSeqNo: " << m_seqMax);
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800233
234 std::cout << "MaxSeqNo = " << m_seqMax << "\n";
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800235}
236
237
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800238void
239CcnxConsumer::ScheduleNextPacket ()
240{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800241 if (!m_sendEvent.IsRunning ())
242 m_sendEvent = Simulator::Schedule (
243 Seconds(m_randExp.GetValue ()),
244 &CcnxConsumer::SendPacket, this);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800245}
246
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700247// Application Methods
248void
249CcnxConsumer::StartApplication () // Called at time specified by Start
250{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800251 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700252
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800253 // do base stuff
254 CcnxApp::StartApplication ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800255
256 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700257}
258
259void
260CcnxConsumer::StopApplication () // Called at time specified by Stop
261{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800262 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700263
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800264 // cancel periodic packet generation
265 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700266
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800267 // cleanup base stuff
268 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700269}
270
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700271void
272CcnxConsumer::SendPacket ()
273{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800274 if (!m_active) return;
275
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800276 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800277
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800278 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
279
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800280 uint32_t seq;
281
282 if (m_retxSeqs.size () != 0)
283 {
284 seq = *m_retxSeqs.begin ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800285 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800286 m_retxSeqs.erase (m_retxSeqs.begin ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800287 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800288 }
289 else
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800290 {
291 if (m_seqMax > 0)
292 {
293 if (m_seq >= m_seqMax)
294 {
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800295 return; // we are totally done
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800296 }
297 }
298
299 seq = m_seq++;
300 }
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800301
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800302 //
303 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800304 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800305 //
306
307 CcnxInterestHeader interestHeader;
308 interestHeader.SetNonce (m_rand.GetValue ());
309 interestHeader.SetName (nameWithSequence);
310 interestHeader.SetInterestLifetime (m_interestLifeTime);
311 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800312 if (m_exclude.size ()>0)
313 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800314 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800315 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800316 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
317 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700318
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800319 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
320 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700321
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800322 Ptr<Packet> packet = Create<Packet> ();
323 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800324
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800325 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800326
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800327 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
328
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800329 std::pair<SeqTimeoutsContainer::iterator, bool>
330 res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800331
Alexander Afanasyeva845f722011-12-16 13:11:02 -0800332 if (!res.second)
333 m_seqTimeouts.modify (res.first,
334 ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800335
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800336 m_transmittedInterests (&interestHeader, this, m_face);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800337
338 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700339}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700340
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800341///////////////////////////////////////////////////
342// Process incoming packets //
343///////////////////////////////////////////////////
344
345
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700346void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700347CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
348 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700349{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800350 if (!m_active) return;
351
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800352 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
353
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800354 NS_LOG_FUNCTION (this << contentObject << payload);
355
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800356 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
357
358 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
359 NS_LOG_INFO ("< DATA for " << seq);
360
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800361 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
362
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800363 SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
364
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800365 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
366 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800367
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800368 // if (entry != m_seqTimeouts.end ())
369 // m_seqTimeouts.erase (entry);
370
371 m_seqTimeouts.erase (seq);
372 m_retxSeqs.erase (seq);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800373}
374
375void
376CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
377{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800378 if (!m_active) return;
379
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800380 CcnxApp::OnNack (interest); // tracing inside
381
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800382 NS_LOG_DEBUG ("Nack type: " << interest->GetNack ());
383 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
384
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800385 NS_LOG_FUNCTION (this << interest);
386
387 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
388 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
389 NS_LOG_INFO ("< NACK for " << seq);
390
391 // put in the queue of interests to be retransmitted
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800392 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800393 m_retxSeqs.insert (seq);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800394 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800395
396 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700397}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700398
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800399} // namespace ns3