blob: 9fe2f64fd5ba4f3625b0d7ab1b5845c1812ab7f6 [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"
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -080036#include "ns3/ccnx-path-stretch-tag.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070037
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080038#include <boost/ref.hpp>
Alexander Afanasyev781ea812011-12-15 22:42:09 -080039#include <boost/lexical_cast.hpp>
40#include <boost/lambda/lambda.hpp>
41#include <boost/lambda/bind.hpp>
42
43namespace ll = boost::lambda;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080044
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070045NS_LOG_COMPONENT_DEFINE ("CcnxConsumer");
46
47namespace ns3
48{
49
50NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer);
51
52TypeId
53CcnxConsumer::GetTypeId (void)
54{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080055 static TypeId tid = TypeId ("ns3::CcnxConsumer")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080056 .SetParent<CcnxApp> ()
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 Afanasyev4975f732011-12-20 17:52:19 -080062 .AddAttribute ("Prefix","CcnxName of the Interest",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080063 StringValue ("/"),
64 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
65 MakeCcnxNameComponentsChecker ())
Ilya Moiseenko956d0542012-01-02 15:26:40 -080066 .AddAttribute ("LifeTime", "LifeTime for interest packet",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067 StringValue ("2s"),
68 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
69 MakeTimeChecker ())
70 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
71 IntegerValue(-1),
72 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
73 MakeIntegerChecker<int32_t>())
74 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
75 IntegerValue(-1),
76 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
77 MakeIntegerChecker<int32_t>())
78 .AddAttribute ("ChildSelector", "ChildSelector",
79 BooleanValue(false),
80 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
81 MakeBooleanChecker())
82 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
83 CcnxNameComponentsValue (),
84 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
85 MakeCcnxNameComponentsChecker ())
Alexander Afanasyev781ea812011-12-15 22:42:09 -080086
Alexander Afanasyev781ea812011-12-15 22:42:09 -080087 .AddAttribute ("RetxTimer",
88 "Timeout defining how frequent retransmission timeouts should be checked",
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080089 StringValue ("1ms"),
Alexander Afanasyev781ea812011-12-15 22:42:09 -080090 MakeTimeAccessor (&CcnxConsumer::GetRetxTimer, &CcnxConsumer::SetRetxTimer),
91 MakeTimeChecker ())
92
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080093 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
94 MakeTraceSourceAccessor (&CcnxConsumer::m_transmittedInterests))
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -080095
96 .AddTraceSource ("PathWeightsTrace", "PathWeightsTrace",
97 MakeTraceSourceAccessor (&CcnxConsumer::m_pathWeightsTrace))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070099
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800100 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700101}
102
103CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800104 : m_rand (0, std::numeric_limits<uint32_t>::max ())
105 , m_seq (0)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800106 , m_seqMax (0) // don't request anything
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700107{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800109
Alexander Afanasyev94cebd02012-01-16 12:22:34 -0800110 m_rtt = CreateObject<RttMeanDeviation> ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700111}
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800112
113void
114CcnxConsumer::SetRetxTimer (Time retxTimer)
115{
116 m_retxTimer = retxTimer;
117 if (m_retxEvent.IsRunning ())
118 m_retxEvent.Cancel (); // cancel any scheduled cleanup events
119
120 // schedule even with new timeout
121 m_retxEvent = Simulator::Schedule (m_retxTimer,
122 &CcnxConsumer::CheckRetxTimeout, this);
123}
124
125Time
126CcnxConsumer::GetRetxTimer () const
127{
128 return m_retxTimer;
129}
130
131void
132CcnxConsumer::CheckRetxTimeout ()
133{
134 Time now = Simulator::Now ();
135
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800136 Time rto = m_rtt->RetransmitTimeout ();
137
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800138 while (!m_seqTimeouts.empty ())
139 {
140 SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
141 m_seqTimeouts.get<i_timestamp> ().begin ();
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800142 if (entry->time + rto <= now) // timeout expired?
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800143 {
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800144 m_seqTimeouts.get<i_timestamp> ().erase (entry);
145 OnTimeout (entry->seq);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800146 }
147 else
148 break; // nothing else to do. All later packets need not be retransmitted
149 }
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800150
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800151 m_retxEvent = Simulator::Schedule (m_retxTimer,
152 &CcnxConsumer::CheckRetxTimeout, this);
153}
154
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700155// Application Methods
156void
157CcnxConsumer::StartApplication () // Called at time specified by Start
158{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800159 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700160
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800161 // do base stuff
162 CcnxApp::StartApplication ();
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800163
164 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700165}
166
167void
168CcnxConsumer::StopApplication () // Called at time specified by Stop
169{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800170 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700171
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800172 // cancel periodic packet generation
173 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800175 // cleanup base stuff
176 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700177}
178
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700179void
180CcnxConsumer::SendPacket ()
181{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800182 if (!m_active) return;
183
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800184 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800185
186 uint32_t seq;
187
188 if (m_retxSeqs.size () != 0)
189 {
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800190 // for (RetxSeqsContainer::const_iterator i=m_retxSeqs.begin (); i!=m_retxSeqs.end (); i++)
191 // {
192 // std::cout << *i << " ";
193 // }
194 // std::cout << "\n";
195
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800196 seq = *m_retxSeqs.begin ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800197 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800198 m_retxSeqs.erase (m_retxSeqs.begin ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800199 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800200 }
201 else
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800202 {
203 if (m_seqMax > 0)
204 {
205 if (m_seq >= m_seqMax)
206 {
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800207 return; // we are totally done
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -0800208 }
209 }
210
211 seq = m_seq++;
212 }
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800213
214 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800215
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800216 //
217 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800218 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800219 //
220
221 CcnxInterestHeader interestHeader;
222 interestHeader.SetNonce (m_rand.GetValue ());
223 interestHeader.SetName (nameWithSequence);
224 interestHeader.SetInterestLifetime (m_interestLifeTime);
225 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800226 if (m_exclude.size ()>0)
227 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800228 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800229 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800230 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
231 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700232
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800233 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
234 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700235
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800236 Ptr<Packet> packet = Create<Packet> ();
237 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800238
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800239 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800240
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800241 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
242
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800243 m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800244 m_transmittedInterests (&interestHeader, this, m_face);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800245
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800246 m_rtt->SentSeq (SequenceNumber32 (seq), 1);
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800247 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700248}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800250///////////////////////////////////////////////////
251// Process incoming packets //
252///////////////////////////////////////////////////
253
254
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700255void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700256CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800257 Ptr<Packet> payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700258{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800259 if (!m_active) return;
260
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800261 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
262
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800263 NS_LOG_FUNCTION (this << contentObject << payload);
264
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800265 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
266
267 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
268 NS_LOG_INFO ("< DATA for " << seq);
269
Alexander Afanasyev8dcfa5d2012-01-05 14:22:25 -0700270 // SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800271
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800272 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
273 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800274
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800275 // if (entry != m_seqTimeouts.end ())
276 // m_seqTimeouts.erase (entry);
277
278 m_seqTimeouts.erase (seq);
279 m_retxSeqs.erase (seq);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800280
281 m_rtt->AckSeq (SequenceNumber32 (seq));
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -0800282
283 Ptr<const WeightsPathStretchTag> tag = payload->RemovePacketTag<WeightsPathStretchTag> ();
284 if (tag != 0)
285 {
286 // Notify trace about path weights vector (e.g., for path-stretch calculation)
287 m_pathWeightsTrace (GetNode (), tag->GetDestinationNode (), seq, tag->GetTotalWeight ());
288 // std::cout << boost::cref(*tag) << "\n";
289 }
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800290}
291
292void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800293CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> origPacket)
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800294{
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800295 if (!m_active) return;
296
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800297 CcnxApp::OnNack (interest, origPacket); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800298
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800299 NS_LOG_DEBUG ("Nack type: " << interest->GetNack ());
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800300
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800301 NS_LOG_FUNCTION (this << interest);
302
303 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
304 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
305 NS_LOG_INFO ("< NACK for " << seq);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800306 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << "NACK for " << seq << "\n";
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800307
308 // put in the queue of interests to be retransmitted
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800309 NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800310 m_retxSeqs.insert (seq);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800311 NS_LOG_INFO ("After: " << m_retxSeqs.size ());
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800312
313 ScheduleNextPacket ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700314}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700315
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800316void
317CcnxConsumer::OnTimeout (uint32_t sequenceNumber)
318{
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800319 std::cout << Simulator::Now () << ", TO: " << sequenceNumber << ", current RTO: " << m_rtt->RetransmitTimeout ().ToDouble (Time::S) << "s\n";
320
Alexander Afanasyev4d66de52012-01-13 00:06:01 -0800321 m_retxSeqs.insert (sequenceNumber);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800322 ScheduleNextPacket ();
323}
324
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800325} // namespace ns3