blob: fb92c43373508884e013d83d4fa801efd449a43c [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"
30
31#include "ns3/ccnx.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080032#include "../model/ccnx-local-face.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080033#include "ns3/ccnx-interest-header.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070034#include "ns3/ccnx-content-object-header.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070035
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080036#include <boost/ref.hpp>
Alexander Afanasyev781ea812011-12-15 22:42:09 -080037#include <boost/lexical_cast.hpp>
38#include <boost/lambda/lambda.hpp>
39#include <boost/lambda/bind.hpp>
40
41namespace ll = boost::lambda;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080042
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070043NS_LOG_COMPONENT_DEFINE ("CcnxConsumer");
44
45namespace ns3
46{
47
48NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer);
49
50TypeId
51CcnxConsumer::GetTypeId (void)
52{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080053 static TypeId tid = TypeId ("ns3::CcnxConsumer")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080054 .SetParent<CcnxApp> ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080055 .AddConstructor<CcnxConsumer> ()
Alexander Afanasyev781ea812011-12-15 22:42:09 -080056 .AddAttribute ("StartSeq", "Initial sequence number",
57 IntegerValue(0),
58 MakeIntegerAccessor(&CcnxConsumer::m_seq),
59 MakeIntegerChecker<int32_t>())
Alexander Afanasyev011b8592011-12-21 14:45:27 -080060
61 ///////
62 .AddAttribute ("PayloadSize", "Average size of content object size (to calculate interest generation rate)",
63 UintegerValue (1040),
64 MakeUintegerAccessor (&CcnxConsumer::GetPayloadSize, &CcnxConsumer::SetPayloadSize),
65 MakeUintegerChecker<uint32_t>())
66 .AddAttribute ("MeanRate", "Mean data packet rate (relies on the PayloadSize parameter)",
67 StringValue ("100Kbps"),
68 MakeDataRateAccessor (&CcnxConsumer::GetDesiredRate, &CcnxConsumer::SetDesiredRate),
69 MakeDataRateChecker ())
70 ///////
71
Alexander Afanasyev4975f732011-12-20 17:52:19 -080072 .AddAttribute ("Prefix","CcnxName of the Interest",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080073 StringValue ("/"),
74 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
75 MakeCcnxNameComponentsChecker ())
76 .AddAttribute ("LifeTime", "LifeTime fo interest packet",
77 StringValue ("2s"),
78 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
79 MakeTimeChecker ())
80 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
81 IntegerValue(-1),
82 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
83 MakeIntegerChecker<int32_t>())
84 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
85 IntegerValue(-1),
86 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
87 MakeIntegerChecker<int32_t>())
88 .AddAttribute ("ChildSelector", "ChildSelector",
89 BooleanValue(false),
90 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
91 MakeBooleanChecker())
92 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
93 CcnxNameComponentsValue (),
94 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
95 MakeCcnxNameComponentsChecker ())
Alexander Afanasyev781ea812011-12-15 22:42:09 -080096
97 .AddAttribute ("RTO",
98 "Initial retransmission timeout",
99 StringValue ("1s"),
100 MakeTimeAccessor (&CcnxConsumer::m_rto),
101 MakeTimeChecker ())
102 .AddAttribute ("RetxTimer",
103 "Timeout defining how frequent retransmission timeouts should be checked",
104 StringValue ("1s"),
105 MakeTimeAccessor (&CcnxConsumer::GetRetxTimer, &CcnxConsumer::SetRetxTimer),
106 MakeTimeChecker ())
107
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800108 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
109 MakeTraceSourceAccessor (&CcnxConsumer::m_transmittedInterests))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700111
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700113}
114
115CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 : m_rand (0, std::numeric_limits<uint32_t>::max ())
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800117 , m_desiredRate ("10Kbps")
118 , m_payloadSize (1024)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800119 , m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700120{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800121 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800122
123 UpdateMean (); // not necessary (will be called by ns3 object system anyways), but doesn't hurt
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700124}
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800125
126void
127CcnxConsumer::SetRetxTimer (Time retxTimer)
128{
129 m_retxTimer = retxTimer;
130 if (m_retxEvent.IsRunning ())
131 m_retxEvent.Cancel (); // cancel any scheduled cleanup events
132
133 // schedule even with new timeout
134 m_retxEvent = Simulator::Schedule (m_retxTimer,
135 &CcnxConsumer::CheckRetxTimeout, this);
136}
137
138Time
139CcnxConsumer::GetRetxTimer () const
140{
141 return m_retxTimer;
142}
143
144void
145CcnxConsumer::CheckRetxTimeout ()
146{
147 Time now = Simulator::Now ();
148
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800149 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
150
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800151 while (!m_seqTimeouts.empty ())
152 {
153 SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
154 m_seqTimeouts.get<i_timestamp> ().begin ();
155 if (entry->time + m_rto <= now) // timeout expired?
156 {
157 m_retxSeqs.insert (entry->seq);
158 m_seqTimeouts.get<i_timestamp> ().modify (entry,
159 ll::bind(&SeqTimeout::time, ll::_1) = now);
160 }
161 else
162 break; // nothing else to do. All later packets need not be retransmitted
163 }
164
165 m_retxEvent = Simulator::Schedule (m_retxTimer,
166 &CcnxConsumer::CheckRetxTimeout, this);
167}
168
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800169void
170CcnxConsumer::UpdateMean ()
171{
172 double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
173 m_randExp = ExponentialVariable (mean, 10000 * mean); // set upper limit to inter-arrival time
174}
175
176void
177CcnxConsumer::SetPayloadSize (uint32_t payload)
178{
179 m_payloadSize = payload;
180 UpdateMean ();
181}
182
183uint32_t
184CcnxConsumer::GetPayloadSize () const
185{
186 return m_payloadSize;
187}
188
189void
190CcnxConsumer::SetDesiredRate (DataRate rate)
191{
192 m_desiredRate = rate;
193 UpdateMean ();
194}
195
196DataRate
197CcnxConsumer::GetDesiredRate () const
198{
199 return m_desiredRate;
200}
201
202void
203CcnxConsumer::ScheduleNextPacket ()
204{
205 // schedule periodic packet generation
206
207 m_sendEvent = Simulator::Schedule (
208 Seconds(m_randExp.GetValue ()),
209 &CcnxConsumer::SendPacket, this);
210}
211
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700212// Application Methods
213void
214CcnxConsumer::StartApplication () // Called at time specified by Start
215{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800216 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700217
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800218 // do base stuff
219 CcnxApp::StartApplication ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800220
221 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700222}
223
224void
225CcnxConsumer::StopApplication () // Called at time specified by Stop
226{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800227 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700228
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800229 // cancel periodic packet generation
230 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700231
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800232 // cleanup base stuff
233 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700234}
235
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700236void
237CcnxConsumer::SendPacket ()
238{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800239 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800240
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800241 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
242
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800243 uint32_t seq;
244
245 if (m_retxSeqs.size () != 0)
246 {
247 seq = *m_retxSeqs.begin ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800248 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800249 m_retxSeqs.erase (m_retxSeqs.begin ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800250 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800251 }
252 else
253 seq = m_seq++;
254
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800255 //
256 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800257 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800258 //
259
260 CcnxInterestHeader interestHeader;
261 interestHeader.SetNonce (m_rand.GetValue ());
262 interestHeader.SetName (nameWithSequence);
263 interestHeader.SetInterestLifetime (m_interestLifeTime);
264 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800265 if (m_exclude.size ()>0)
266 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800267 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800268 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800269 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
270 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700271
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800272 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
273 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700274
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800275 Ptr<Packet> packet = Create<Packet> ();
276 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800277
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800278 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800279
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800280 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
281
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800282 std::pair<SeqTimeoutsContainer::iterator, bool>
283 res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800284
Alexander Afanasyeva845f722011-12-16 13:11:02 -0800285 if (!res.second)
286 m_seqTimeouts.modify (res.first,
287 ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800288
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800289 m_transmittedInterests (&interestHeader, this, m_face);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800290
291 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700292}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700293
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800294///////////////////////////////////////////////////
295// Process incoming packets //
296///////////////////////////////////////////////////
297
298
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700299void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700300CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
301 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700302{
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800303 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
304
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800305 NS_LOG_FUNCTION (this << contentObject << payload);
306
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800307 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
308
309 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
310 NS_LOG_INFO ("< DATA for " << seq);
311
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800312 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
313
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800314 SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
315
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800316 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
317 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800318
319 if (entry != m_seqTimeouts.end ())
320 m_seqTimeouts.erase (entry);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800321}
322
323void
324CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
325{
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800326 CcnxApp::OnNack (interest); // tracing inside
327
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800328 NS_LOG_DEBUG ("Nack type: " << interest->GetNack ());
329 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
330
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800331 NS_LOG_FUNCTION (this << interest);
332
333 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
334 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
335 NS_LOG_INFO ("< NACK for " << seq);
336
337 // put in the queue of interests to be retransmitted
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800338 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800339 m_retxSeqs.insert (seq);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800340 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700341}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700342
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800343} // namespace ns3