blob: fcc1ccf2b4b4873f37937be13a010ef074aef181 [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
95 .AddAttribute ("RTO",
96 "Initial retransmission timeout",
97 StringValue ("1s"),
98 MakeTimeAccessor (&CcnxConsumer::m_rto),
99 MakeTimeChecker ())
100 .AddAttribute ("RetxTimer",
101 "Timeout defining how frequent retransmission timeouts should be checked",
102 StringValue ("1s"),
103 MakeTimeAccessor (&CcnxConsumer::GetRetxTimer, &CcnxConsumer::SetRetxTimer),
104 MakeTimeChecker ())
105
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800106 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
107 MakeTraceSourceAccessor (&CcnxConsumer::m_transmittedInterests))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700109
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700111}
112
113CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800114 : m_rand (0, std::numeric_limits<uint32_t>::max ())
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800115 , m_payloadSize (1040)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 , m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700117{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700119}
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800120
121void
122CcnxConsumer::SetRetxTimer (Time retxTimer)
123{
124 m_retxTimer = retxTimer;
125 if (m_retxEvent.IsRunning ())
126 m_retxEvent.Cancel (); // cancel any scheduled cleanup events
127
128 // schedule even with new timeout
129 m_retxEvent = Simulator::Schedule (m_retxTimer,
130 &CcnxConsumer::CheckRetxTimeout, this);
131}
132
133Time
134CcnxConsumer::GetRetxTimer () const
135{
136 return m_retxTimer;
137}
138
139void
140CcnxConsumer::CheckRetxTimeout ()
141{
142 Time now = Simulator::Now ();
143
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800144 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
145
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800146 while (!m_seqTimeouts.empty ())
147 {
148 SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
149 m_seqTimeouts.get<i_timestamp> ().begin ();
150 if (entry->time + m_rto <= now) // timeout expired?
151 {
152 m_retxSeqs.insert (entry->seq);
153 m_seqTimeouts.get<i_timestamp> ().modify (entry,
154 ll::bind(&SeqTimeout::time, ll::_1) = now);
155 }
156 else
157 break; // nothing else to do. All later packets need not be retransmitted
158 }
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800159
160 if (m_retxSeqs.size () > 0)
161 {
162 ScheduleNextPacket ();
163 }
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800164
165 m_retxEvent = Simulator::Schedule (m_retxTimer,
166 &CcnxConsumer::CheckRetxTimeout, this);
167}
168
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800169uint32_t
170CcnxConsumer::GetPayloadSize () const
171{
172 return m_payloadSize;
173}
174
175void
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800176CcnxConsumer::SetPayloadSize (uint32_t payload)
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800177{
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800178 m_payloadSize = payload;
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800179}
180
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800181double
182CcnxConsumer::GetMaxSize () const
183{
184 if (m_seqMax == 0)
185 return -1.0;
186
187 return m_seqMax * m_payloadSize / 1024.0 / 1024.0;
188}
189
190void
191CcnxConsumer::SetMaxSize (double size)
192{
193 if (size < 0)
194 {
195 m_seqMax = 0;
196 return;
197 }
198
199 m_seqMax = floor(1.0 + size * 1024.0 * 1024.0 / m_payloadSize);
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800200 NS_LOG_DEBUG ("MaxSeqNo: " << m_seqMax);
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800201}
202
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700203// Application Methods
204void
205CcnxConsumer::StartApplication () // Called at time specified by Start
206{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800207 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700208
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800209 // do base stuff
210 CcnxApp::StartApplication ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800211
212 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700213}
214
215void
216CcnxConsumer::StopApplication () // Called at time specified by Stop
217{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800218 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700219
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800220 // cancel periodic packet generation
221 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700222
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800223 // cleanup base stuff
224 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700225}
226
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700227void
228CcnxConsumer::SendPacket ()
229{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800230 if (!m_active) return;
231
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800232 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800233
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800234 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
235
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800236 uint32_t seq;
237
238 if (m_retxSeqs.size () != 0)
239 {
240 seq = *m_retxSeqs.begin ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800241 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800242 m_retxSeqs.erase (m_retxSeqs.begin ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800243 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800244 }
245 else
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800246 {
247 if (m_seqMax > 0)
248 {
249 if (m_seq >= m_seqMax)
250 {
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800251 return; // we are totally done
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800252 }
253 }
254
255 seq = m_seq++;
256 }
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800257
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800258 //
259 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800260 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800261 //
262
263 CcnxInterestHeader interestHeader;
264 interestHeader.SetNonce (m_rand.GetValue ());
265 interestHeader.SetName (nameWithSequence);
266 interestHeader.SetInterestLifetime (m_interestLifeTime);
267 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800268 if (m_exclude.size ()>0)
269 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800270 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800271 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800272 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
273 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700274
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800275 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
276 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700277
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800278 Ptr<Packet> packet = Create<Packet> ();
279 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800280
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800281 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800282
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800283 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
284
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800285 std::pair<SeqTimeoutsContainer::iterator, bool>
286 res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800287
Alexander Afanasyeva845f722011-12-16 13:11:02 -0800288 if (!res.second)
289 m_seqTimeouts.modify (res.first,
290 ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800291
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800292 m_transmittedInterests (&interestHeader, this, m_face);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800293
294 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700295}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700296
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800297///////////////////////////////////////////////////
298// Process incoming packets //
299///////////////////////////////////////////////////
300
301
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700302void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700303CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
304 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700305{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800306 if (!m_active) return;
307
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800308 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
309
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800310 NS_LOG_FUNCTION (this << contentObject << payload);
311
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800312 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
313
314 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
315 NS_LOG_INFO ("< DATA for " << seq);
316
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800317 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
318
Alexander Afanasyev8dcfa5d2012-01-05 14:22:25 -0700319 // SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800320
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800321 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
322 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800323
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800324 // if (entry != m_seqTimeouts.end ())
325 // m_seqTimeouts.erase (entry);
326
327 m_seqTimeouts.erase (seq);
328 m_retxSeqs.erase (seq);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800329}
330
331void
332CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
333{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800334 if (!m_active) return;
335
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800336 CcnxApp::OnNack (interest); // tracing inside
337
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800338 NS_LOG_DEBUG ("Nack type: " << interest->GetNack ());
339 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
340
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800341 NS_LOG_FUNCTION (this << interest);
342
343 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
344 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
345 NS_LOG_INFO ("< NACK for " << seq);
346
347 // put in the queue of interests to be retransmitted
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800348 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800349 m_retxSeqs.insert (seq);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800350 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800351
352 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700353}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700354
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800355} // namespace ns3