blob: 6b63693d911793b755ef200c8dc0be4d638234f4 [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")
124 , m_payloadSize (1024)
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 Afanasyevb3e4b852011-12-23 15:58:20 -0800233}
234
235
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800236void
237CcnxConsumer::ScheduleNextPacket ()
238{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800239 if (!m_sendEvent.IsRunning ())
240 m_sendEvent = Simulator::Schedule (
241 Seconds(m_randExp.GetValue ()),
242 &CcnxConsumer::SendPacket, this);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800243}
244
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700245// Application Methods
246void
247CcnxConsumer::StartApplication () // Called at time specified by Start
248{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800249 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700250
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800251 // do base stuff
252 CcnxApp::StartApplication ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800253
254 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700255}
256
257void
258CcnxConsumer::StopApplication () // Called at time specified by Stop
259{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800260 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700261
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800262 // cancel periodic packet generation
263 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700264
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800265 // cleanup base stuff
266 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700267}
268
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700269void
270CcnxConsumer::SendPacket ()
271{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800272 if (!m_active) return;
273
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800274 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800275
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800276 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
277
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800278 uint32_t seq;
279
280 if (m_retxSeqs.size () != 0)
281 {
282 seq = *m_retxSeqs.begin ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800283 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800284 m_retxSeqs.erase (m_retxSeqs.begin ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800285 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800286 }
287 else
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800288 {
289 if (m_seqMax > 0)
290 {
291 if (m_seq >= m_seqMax)
292 {
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800293 return; // we are totally done
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800294 }
295 }
296
297 seq = m_seq++;
298 }
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800299
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800300 //
301 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800302 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800303 //
304
305 CcnxInterestHeader interestHeader;
306 interestHeader.SetNonce (m_rand.GetValue ());
307 interestHeader.SetName (nameWithSequence);
308 interestHeader.SetInterestLifetime (m_interestLifeTime);
309 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800310 if (m_exclude.size ()>0)
311 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800312 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800313 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800314 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
315 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700316
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800317 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
318 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700319
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800320 Ptr<Packet> packet = Create<Packet> ();
321 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800322
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800323 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800324
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800325 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
326
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800327 std::pair<SeqTimeoutsContainer::iterator, bool>
328 res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800329
Alexander Afanasyeva845f722011-12-16 13:11:02 -0800330 if (!res.second)
331 m_seqTimeouts.modify (res.first,
332 ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800333
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800334 m_transmittedInterests (&interestHeader, this, m_face);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800335
336 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700337}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700338
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800339///////////////////////////////////////////////////
340// Process incoming packets //
341///////////////////////////////////////////////////
342
343
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700344void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700345CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
346 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700347{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800348 if (!m_active) return;
349
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800350 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
351
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800352 NS_LOG_FUNCTION (this << contentObject << payload);
353
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800354 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
355
356 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
357 NS_LOG_INFO ("< DATA for " << seq);
358
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800359 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
360
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800361 SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
362
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800363 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
364 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800365
366 if (entry != m_seqTimeouts.end ())
367 m_seqTimeouts.erase (entry);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800368}
369
370void
371CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
372{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800373 if (!m_active) return;
374
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800375 CcnxApp::OnNack (interest); // tracing inside
376
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800377 NS_LOG_DEBUG ("Nack type: " << interest->GetNack ());
378 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
379
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800380 NS_LOG_FUNCTION (this << interest);
381
382 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
383 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
384 NS_LOG_INFO ("< NACK for " << seq);
385
386 // put in the queue of interests to be retransmitted
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800387 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800388 m_retxSeqs.insert (seq);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800389 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800390
391 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700392}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700393
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800394} // namespace ns3