blob: 4bd4d4c9e8491c1237b9f1eb6eacfdfb30a3c686 [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>())
60
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080061 .AddAttribute ("OffTime", "Time interval between packets",
62 StringValue ("100ms"),
63 MakeTimeAccessor (&CcnxConsumer::m_offTime),
64 MakeTimeChecker ())
65 .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)",
66 StringValue ("/"),
67 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
68 MakeCcnxNameComponentsChecker ())
69 .AddAttribute ("LifeTime", "LifeTime fo interest packet",
70 StringValue ("2s"),
71 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
72 MakeTimeChecker ())
73 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
74 IntegerValue(-1),
75 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
76 MakeIntegerChecker<int32_t>())
77 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
78 IntegerValue(-1),
79 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
80 MakeIntegerChecker<int32_t>())
81 .AddAttribute ("ChildSelector", "ChildSelector",
82 BooleanValue(false),
83 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
84 MakeBooleanChecker())
85 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
86 CcnxNameComponentsValue (),
87 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
88 MakeCcnxNameComponentsChecker ())
Alexander Afanasyev781ea812011-12-15 22:42:09 -080089
90 .AddAttribute ("RTO",
91 "Initial retransmission timeout",
92 StringValue ("1s"),
93 MakeTimeAccessor (&CcnxConsumer::m_rto),
94 MakeTimeChecker ())
95 .AddAttribute ("RetxTimer",
96 "Timeout defining how frequent retransmission timeouts should be checked",
97 StringValue ("1s"),
98 MakeTimeAccessor (&CcnxConsumer::GetRetxTimer, &CcnxConsumer::SetRetxTimer),
99 MakeTimeChecker ())
100
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800101 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
102 MakeTraceSourceAccessor (&CcnxConsumer::m_transmittedInterests))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700106}
107
108CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 : m_rand (0, std::numeric_limits<uint32_t>::max ())
110 , m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700111{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700113}
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800114
115void
116CcnxConsumer::SetRetxTimer (Time retxTimer)
117{
118 m_retxTimer = retxTimer;
119 if (m_retxEvent.IsRunning ())
120 m_retxEvent.Cancel (); // cancel any scheduled cleanup events
121
122 // schedule even with new timeout
123 m_retxEvent = Simulator::Schedule (m_retxTimer,
124 &CcnxConsumer::CheckRetxTimeout, this);
125}
126
127Time
128CcnxConsumer::GetRetxTimer () const
129{
130 return m_retxTimer;
131}
132
133void
134CcnxConsumer::CheckRetxTimeout ()
135{
136 Time now = Simulator::Now ();
137
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800138 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
139
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800140 while (!m_seqTimeouts.empty ())
141 {
142 SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
143 m_seqTimeouts.get<i_timestamp> ().begin ();
144 if (entry->time + m_rto <= now) // timeout expired?
145 {
146 m_retxSeqs.insert (entry->seq);
147 m_seqTimeouts.get<i_timestamp> ().modify (entry,
148 ll::bind(&SeqTimeout::time, ll::_1) = now);
149 }
150 else
151 break; // nothing else to do. All later packets need not be retransmitted
152 }
153
154 m_retxEvent = Simulator::Schedule (m_retxTimer,
155 &CcnxConsumer::CheckRetxTimeout, this);
156}
157
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700158// Application Methods
159void
160CcnxConsumer::StartApplication () // Called at time specified by Start
161{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700163
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164 // do base stuff
165 CcnxApp::StartApplication ();
166
167 // schedule periodic packet generation
168 m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700169}
170
171void
172CcnxConsumer::StopApplication () // Called at time specified by Stop
173{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800174 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700175
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800176 // cancel periodic packet generation
177 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800179 // cleanup base stuff
180 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700181}
182
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700183void
184CcnxConsumer::SendPacket ()
185{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800187
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800188 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
189
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800190 uint32_t seq;
191
192 if (m_retxSeqs.size () != 0)
193 {
194 seq = *m_retxSeqs.begin ();
195 m_retxSeqs.erase (m_retxSeqs.begin ());
196 }
197 else
198 seq = m_seq++;
199
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800200 //
201 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800202 (*nameWithSequence) (seq);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800203 //
204
205 CcnxInterestHeader interestHeader;
206 interestHeader.SetNonce (m_rand.GetValue ());
207 interestHeader.SetName (nameWithSequence);
208 interestHeader.SetInterestLifetime (m_interestLifeTime);
209 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800210 if (m_exclude.size ()>0)
211 {
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800212 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800213 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800214 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
215 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700216
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800217 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
218 NS_LOG_INFO ("> Interest for " << seq);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700219
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800220 Ptr<Packet> packet = Create<Packet> ();
221 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800222
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800223 m_protocolHandler (packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800224
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800225 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
226
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800227 std::pair<SeqTimeoutsContainer::iterator, bool>
228 res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800229
Alexander Afanasyeva845f722011-12-16 13:11:02 -0800230 if (!res.second)
231 m_seqTimeouts.modify (res.first,
232 ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800233
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800234 m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800235
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800236 m_transmittedInterests (&interestHeader, this, m_face);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700237}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700238
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700239void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700240CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
241 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700242{
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800243 CcnxApp::OnContentObject (contentObject, payload); // tracing inside
244
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800245 NS_LOG_FUNCTION (this << contentObject << payload);
246
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800247 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
248
249 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
250 NS_LOG_INFO ("< DATA for " << seq);
251
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800252 boost::mutex::scoped_lock (m_seqTimeoutsGuard);
253
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800254 SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
255
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800256 // NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
257 // "Comment out this assert, if it causes problems");
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800258
259 if (entry != m_seqTimeouts.end ())
260 m_seqTimeouts.erase (entry);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800261}
262
263void
264CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
265{
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800266 CcnxApp::OnNack (interest); // tracing inside
267
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800268 NS_LOG_FUNCTION (this << interest);
269
270 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
271 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
272 NS_LOG_INFO ("< NACK for " << seq);
273
274 // put in the queue of interests to be retransmitted
275 m_retxSeqs.insert (seq);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700276}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700277
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800278} // namespace ns3