blob: 83bb444f2980ad7248b63b8c25fcdcdb11e8194e [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 }
170
171 m_retxEvent = Simulator::Schedule (m_retxTimer,
172 &CcnxConsumer::CheckRetxTimeout, this);
173}
174
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800175void
176CcnxConsumer::UpdateMean ()
177{
178 double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
179 m_randExp = ExponentialVariable (mean, 10000 * mean); // set upper limit to inter-arrival time
180}
181
182void
183CcnxConsumer::SetPayloadSize (uint32_t payload)
184{
185 m_payloadSize = payload;
186 UpdateMean ();
187}
188
189uint32_t
190CcnxConsumer::GetPayloadSize () const
191{
192 return m_payloadSize;
193}
194
195void
196CcnxConsumer::SetDesiredRate (DataRate rate)
197{
198 m_desiredRate = rate;
199 UpdateMean ();
200}
201
202DataRate
203CcnxConsumer::GetDesiredRate () const
204{
205 return m_desiredRate;
206}
207
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800208double
209CcnxConsumer::GetMaxSize () const
210{
211 if (m_seqMax == 0)
212 return -1.0;
213
214 return m_seqMax * m_payloadSize / 1024.0 / 1024.0;
215}
216
217void
218CcnxConsumer::SetMaxSize (double size)
219{
220 if (size < 0)
221 {
222 m_seqMax = 0;
223 return;
224 }
225
226 m_seqMax = floor(1.0 + size * 1024.0 * 1024.0 / m_payloadSize);
227}
228
229
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800230void
231CcnxConsumer::ScheduleNextPacket ()
232{
233 // schedule periodic packet generation
234
235 m_sendEvent = Simulator::Schedule (
236 Seconds(m_randExp.GetValue ()),
237 &CcnxConsumer::SendPacket, this);
238}
239
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700240// Application Methods
241void
242CcnxConsumer::StartApplication () // Called at time specified by Start
243{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800244 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700245
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800246 // do base stuff
247 CcnxApp::StartApplication ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800248
249 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700250}
251
252void
253CcnxConsumer::StopApplication () // Called at time specified by Stop
254{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800255 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700256
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800257 // cancel periodic packet generation
258 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700259
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800260 // cleanup base stuff
261 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700262}
263
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700264void
265CcnxConsumer::SendPacket ()
266{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800267 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800268
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800269 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
270
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800271 uint32_t seq;
272
273 if (m_retxSeqs.size () != 0)
274 {
275 seq = *m_retxSeqs.begin ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800276 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800277 m_retxSeqs.erase (m_retxSeqs.begin ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800278 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800279 }
280 else
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800281 {
282 if (m_seqMax > 0)
283 {
284 if (m_seq >= m_seqMax)
285 {
286 if (m_seqTimeouts.size () == 0)
287 return; // we are totally done
288 else
289 ScheduleNextPacket (); // we will probably need to retransmit something in the future
290 }
291 }
292
293 seq = m_seq++;
294 }
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800295
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800296 //
297 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800298 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800299 //
300
301 CcnxInterestHeader interestHeader;
302 interestHeader.SetNonce (m_rand.GetValue ());
303 interestHeader.SetName (nameWithSequence);
304 interestHeader.SetInterestLifetime (m_interestLifeTime);
305 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800306 if (m_exclude.size ()>0)
307 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800308 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800309 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800310 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
311 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700312
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800313 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
314 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700315
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800316 Ptr<Packet> packet = Create<Packet> ();
317 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800318
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800319 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800320
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800321 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
322
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800323 std::pair<SeqTimeoutsContainer::iterator, bool>
324 res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800325
Alexander Afanasyeva845f722011-12-16 13:11:02 -0800326 if (!res.second)
327 m_seqTimeouts.modify (res.first,
328 ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800329
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800330 m_transmittedInterests (&interestHeader, this, m_face);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800331
332 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700333}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700334
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800335///////////////////////////////////////////////////
336// Process incoming packets //
337///////////////////////////////////////////////////
338
339
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700340void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700341CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
342 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700343{
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800344 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
345
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800346 NS_LOG_FUNCTION (this << contentObject << payload);
347
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800348 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
349
350 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
351 NS_LOG_INFO ("< DATA for " << seq);
352
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800353 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
354
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800355 SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
356
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800357 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
358 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800359
360 if (entry != m_seqTimeouts.end ())
361 m_seqTimeouts.erase (entry);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800362}
363
364void
365CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
366{
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800367 CcnxApp::OnNack (interest); // tracing inside
368
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800369 NS_LOG_DEBUG ("Nack type: " << interest->GetNack ());
370 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
371
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800372 NS_LOG_FUNCTION (this << interest);
373
374 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
375 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
376 NS_LOG_INFO ("< NACK for " << seq);
377
378 // put in the queue of interests to be retransmitted
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800379 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800380 m_retxSeqs.insert (seq);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800381 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700382}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700383
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800384} // namespace ns3