blob: 7e541ec01a43f609e0eb9e07aea44e5aadd3cf03 [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")
54 .SetParent<Application> ()
55 .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 Afanasyev19426ef2011-11-23 20:55:28 -0800101 // .AddTraceSource ("InterestTrace", "Interests that were sent",
102 // MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace))
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800103 // .AddTraceSource ("NackTrace", "NACKs received",
104 // MakeTraceSourceAccessor (&CcnxConsumer::m_nackTrace))
105 // .AddTraceSource ("ContentObjectTrace", "ContentObjects received",
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106 // MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800107 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700108
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700110}
111
112CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113 : m_rand (0, std::numeric_limits<uint32_t>::max ())
114 , m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700115{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700117}
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800118
119void
120CcnxConsumer::SetRetxTimer (Time retxTimer)
121{
122 m_retxTimer = retxTimer;
123 if (m_retxEvent.IsRunning ())
124 m_retxEvent.Cancel (); // cancel any scheduled cleanup events
125
126 // schedule even with new timeout
127 m_retxEvent = Simulator::Schedule (m_retxTimer,
128 &CcnxConsumer::CheckRetxTimeout, this);
129}
130
131Time
132CcnxConsumer::GetRetxTimer () const
133{
134 return m_retxTimer;
135}
136
137void
138CcnxConsumer::CheckRetxTimeout ()
139{
140 Time now = Simulator::Now ();
141
142 while (!m_seqTimeouts.empty ())
143 {
144 SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
145 m_seqTimeouts.get<i_timestamp> ().begin ();
146 if (entry->time + m_rto <= now) // timeout expired?
147 {
148 m_retxSeqs.insert (entry->seq);
149 m_seqTimeouts.get<i_timestamp> ().modify (entry,
150 ll::bind(&SeqTimeout::time, ll::_1) = now);
151 }
152 else
153 break; // nothing else to do. All later packets need not be retransmitted
154 }
155
156 m_retxEvent = Simulator::Schedule (m_retxTimer,
157 &CcnxConsumer::CheckRetxTimeout, this);
158}
159
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700160// Application Methods
161void
162CcnxConsumer::StartApplication () // Called at time specified by Start
163{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700165
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 // do base stuff
167 CcnxApp::StartApplication ();
168
169 // schedule periodic packet generation
170 m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700171}
172
173void
174CcnxConsumer::StopApplication () // Called at time specified by Stop
175{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800176 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700177
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800178 // cancel periodic packet generation
179 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700180
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800181 // cleanup base stuff
182 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700183}
184
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700185void
186CcnxConsumer::SendPacket ()
187{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800188 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800189
190 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 {
212 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
213 }
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
225 std::pair<SeqTimeoutsContainer::iterator, bool>
226 res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
227 if (!res.second)
228 m_seqTimeouts.modify (res.first,
229 ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
230
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800231 m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800232
233 // \todo Trace
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700234}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700235
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700236void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700237CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
238 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700239{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800240 NS_LOG_FUNCTION (this << contentObject << payload);
241
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800242 // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
243
244 uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
245 NS_LOG_INFO ("< DATA for " << seq);
246
247 SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq);
248
249 NS_ASSERT_MSG (entry != m_seqTimeouts.end (),
250 "Comment out this assert, if it causes problems");
251
252 if (entry != m_seqTimeouts.end ())
253 m_seqTimeouts.erase (entry);
254
255 // \todo Trace
256}
257
258void
259CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
260{
261 NS_LOG_FUNCTION (this << interest);
262
263 // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
264 uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
265 NS_LOG_INFO ("< NACK for " << seq);
266
267 // put in the queue of interests to be retransmitted
268 m_retxSeqs.insert (seq);
269
270 // \todo Trace?
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700271}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700272
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800273} // namespace ns3