NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-consumer-window.cc
1 /* -*- 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19  */
20 
21 #include "ndn-consumer-window.h"
22 #include "ns3/ptr.h"
23 #include "ns3/log.h"
24 #include "ns3/simulator.h"
25 #include "ns3/packet.h"
26 #include "ns3/callback.h"
27 #include "ns3/string.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/double.h"
30 #include "ns3/ndn-data.h"
31 #include "ns3/ndn-interest.h"
32 
33 NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerWindow");
34 
35 namespace ns3 {
36 namespace ndn {
37 
38 NS_OBJECT_ENSURE_REGISTERED (ConsumerWindow);
39 
40 TypeId
41 ConsumerWindow::GetTypeId (void)
42 {
43  static TypeId tid = TypeId ("ns3::ndn::ConsumerWindow")
44  .SetGroupName ("Ndn")
45  .SetParent<Consumer> ()
46  .AddConstructor<ConsumerWindow> ()
47 
48  .AddAttribute ("Window", "Initial size of the window",
49  StringValue ("1"),
50  MakeUintegerAccessor (&ConsumerWindow::GetWindow, &ConsumerWindow::SetWindow),
51  MakeUintegerChecker<uint32_t> ())
52 
53  .AddAttribute ("PayloadSize", "Average size of content object size (to calculate interest generation rate)",
54  UintegerValue (1040),
55  MakeUintegerAccessor (&ConsumerWindow::GetPayloadSize, &ConsumerWindow::SetPayloadSize),
56  MakeUintegerChecker<uint32_t>())
57 
58  .AddAttribute ("Size",
59  "Amount of data in megabytes to request, relying on PayloadSize parameter (alternative to MaxSeq attribute)",
60  DoubleValue (-1), // don't impose limit by default
61  MakeDoubleAccessor (&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize),
62  MakeDoubleChecker<double> ())
63 
64  .AddAttribute ("MaxSeq",
65  "Maximum sequence number to request (alternative to Size attribute, would activate only if Size is -1). "
66  "The parameter is activated only if Size negative (not set)",
67  IntegerValue (std::numeric_limits<uint32_t>::max ()),
68  MakeUintegerAccessor (&ConsumerWindow::GetSeqMax, &ConsumerWindow::SetSeqMax),
69  MakeUintegerChecker<uint32_t> ())
70 
71  .AddAttribute ("InitialWindowOnTimeout", "Set window to initial value when timeout occurs",
72  BooleanValue (true),
73  MakeBooleanAccessor (&ConsumerWindow::m_setInitialWindowOnTimeout),
74  MakeBooleanChecker ())
75 
76  .AddTraceSource ("WindowTrace",
77  "Window that controls how many outstanding interests are allowed",
78  MakeTraceSourceAccessor (&ConsumerWindow::m_window))
79  .AddTraceSource ("InFlight",
80  "Current number of outstanding interests",
81  MakeTraceSourceAccessor (&ConsumerWindow::m_inFlight))
82  ;
83 
84  return tid;
85 }
86 
88  : m_payloadSize (1040)
89  , m_inFlight (0)
90 {
91 }
92 
93 void
94 ConsumerWindow::SetWindow (uint32_t window)
95 {
96  m_initialWindow = window;
97  m_window = m_initialWindow;
98 }
99 
100 uint32_t
101 ConsumerWindow::GetWindow () const
102 {
103  return m_initialWindow;
104 }
105 
106 uint32_t
107 ConsumerWindow::GetPayloadSize () const
108 {
109  return m_payloadSize;
110 }
111 
112 void
113 ConsumerWindow::SetPayloadSize (uint32_t payload)
114 {
115  m_payloadSize = payload;
116 }
117 
118 double
119 ConsumerWindow::GetMaxSize () const
120 {
121  if (m_seqMax == 0)
122  return -1.0;
123 
124  return m_maxSize;
125 }
126 
127 void
128 ConsumerWindow::SetMaxSize (double size)
129 {
130  m_maxSize = size;
131  if (m_maxSize < 0)
132  {
133  m_seqMax = 0;
134  return;
135  }
136 
137  m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize);
138  NS_LOG_DEBUG ("MaxSeqNo: " << m_seqMax);
139  // std::cout << "MaxSeqNo: " << m_seqMax << "\n";
140 }
141 
142 uint32_t
143 ConsumerWindow::GetSeqMax () const
144 {
145  return m_seqMax;
146 }
147 
148 void
149 ConsumerWindow::SetSeqMax (uint32_t seqMax)
150 {
151  if (m_maxSize < 0)
152  m_seqMax = seqMax;
153 
154  // ignore otherwise
155 }
156 
157 
158 void
160 {
161  if (m_window == static_cast<uint32_t> (0))
162  {
163  Simulator::Remove (m_sendEvent);
164 
165  NS_LOG_DEBUG ("Next event in " << (std::min<double> (0.5, m_rtt->RetransmitTimeout ().ToDouble (Time::S))) << " sec");
166  m_sendEvent = Simulator::Schedule (Seconds (std::min<double> (0.5, m_rtt->RetransmitTimeout ().ToDouble (Time::S))),
167  &Consumer::SendPacket, this);
168  }
169  else if (m_inFlight >= m_window)
170  {
171  // simply do nothing
172  }
173  else
174  {
175  if (m_sendEvent.IsRunning ())
176  {
177  Simulator::Remove (m_sendEvent);
178  }
179 
180  m_sendEvent = Simulator::ScheduleNow (&Consumer::SendPacket, this);
181  }
182 }
183 
185 // Process incoming packets //
187 
188 void
189 ConsumerWindow::OnData (Ptr<const Data> contentObject)
190 {
191  Consumer::OnData (contentObject);
192 
193  m_window = m_window + 1;
194 
195  if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
196  NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
197 
199 }
200 
201 void
202 ConsumerWindow::OnNack (Ptr<const Interest> interest)
203 {
204  Consumer::OnNack (interest);
205 
206  if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
207 
208  if (m_window > static_cast<uint32_t> (0))
209  {
210  // m_window = 0.5 * m_window;//m_window - 1;
211  m_window = std::max<uint32_t> (0, m_window - 1);
212  }
213 
214  NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
215 
217 }
218 
219 void
220 ConsumerWindow::OnTimeout (uint32_t sequenceNumber)
221 {
222  if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
223 
224  if (m_setInitialWindowOnTimeout)
225  {
226  // m_window = std::max<uint32_t> (0, m_window - 1);
227  m_window = m_initialWindow;
228  }
229 
230  NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
231  Consumer::OnTimeout (sequenceNumber);
232 }
233 
234 void
235 ConsumerWindow::WillSendOutInterest (uint32_t sequenceNumber)
236 {
237  m_inFlight ++;
238  Consumer::WillSendOutInterest (sequenceNumber);
239 }
240 
241 
242 } // namespace ndn
243 } // namespace ns3
virtual void OnData(Ptr< const Data > contentObject)
Method that will be called every time new Data arrives.
virtual void OnTimeout(uint32_t sequenceNumber)
Timeout event.
Consumer()
Default constructor Sets up randomizer function and packet sequence number.
Definition: ndn-consumer.cc:86
virtual void OnNack(Ptr< const Interest > interest)
Method that will be called every time new NACK arrives.
virtual void WillSendOutInterest(uint32_t sequenceNumber)
An event that is fired just before an Interest packet is actually send out (send is inevitable) ...
virtual void ScheduleNextPacket()
Constructs the Interest packet and sends it using a callback to the underlying NDN protocol...
EventId m_sendEvent
EventId of pending "send packet" event.
Definition: ndn-consumer.h:131
virtual void OnData(Ptr< const Data > contentObject)
Method that will be called every time new Data arrives.
virtual void OnTimeout(uint32_t sequenceNumber)
Timeout event.
Ptr< RttEstimator > m_rtt
RTT estimator.
Definition: ndn-consumer.h:135
uint32_t m_seqMax
maximum number of sequence number
Definition: ndn-consumer.h:130
void SendPacket()
Actually send packet.
virtual void WillSendOutInterest(uint32_t sequenceNumber)
An event that is fired just before an Interest packet is actually send out (send is inevitable) ...
virtual void OnNack(Ptr< const Interest > interest)
Method that will be called every time new NACK arrives.
ConsumerWindow()
Default constructor.