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