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
ns3::ndn::ConsumerWindow::ScheduleNextPacket
virtual void ScheduleNextPacket()
Constructs the Interest packet and sends it using a callback to the underlying NDN protocol.
Definition: ndn-consumer-window.cpp:156
ns3::ndn::ConsumerWindow::GetTypeId
static TypeId GetTypeId()
Definition: ndn-consumer-window.cpp:39
ns3::ndn::ConsumerWindow::m_initialWindow
uint32_t m_initialWindow
Definition: ndn-consumer-window.hpp:99
ns3::ndn::ConsumerWindow::m_payloadSize
uint32_t m_payloadSize
Definition: ndn-consumer-window.hpp:96
ns3
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-app-link-service.cpp:32
ns3::ndn::ConsumerWindow::m_setInitialWindowOnTimeout
bool m_setInitialWindowOnTimeout
Definition: ndn-consumer-window.hpp:100
ns3::ndn::Consumer::m_sendEvent
EventId m_sendEvent
EventId of pending "send packet" event.
Definition: ndn-consumer.hpp:139
ns3::ndn::ConsumerWindow::ConsumerWindow
ConsumerWindow()
Default constructor.
Definition: ndn-consumer-window.cpp:86
ns3::ndn::NS_OBJECT_ENSURE_REGISTERED
NS_OBJECT_ENSURE_REGISTERED(GlobalRouter)
ns3::ndn::Consumer::WillSendOutInterest
virtual void WillSendOutInterest(uint32_t sequenceNumber)
An event that is fired just before an Interest packet is actually send out (send is inevitable)
Definition: ndn-consumer.cpp:280
ns3::ndn::ConsumerWindow::WillSendOutInterest
virtual void WillSendOutInterest(uint32_t sequenceNumber)
An event that is fired just before an Interest packet is actually send out (send is inevitable)
Definition: ndn-consumer-window.cpp:215
ns3::ndn::Consumer::m_seqMax
uint32_t m_seqMax
maximum number of sequence number
Definition: ndn-consumer.hpp:138
ns3::ndn::ConsumerWindow::m_maxSize
double m_maxSize
Definition: ndn-consumer-window.hpp:97
ns3::ndn::Consumer
NDN application for sending out Interest packets.
Definition: ndn-consumer.hpp:49
ns3::ndn::Consumer::OnTimeout
virtual void OnTimeout(uint32_t sequenceNumber)
Timeout event.
Definition: ndn-consumer.cpp:266
ns3::ndn::ConsumerWindow::m_inFlight
TracedValue< uint32_t > m_inFlight
Definition: ndn-consumer-window.hpp:103
ndn-consumer-window.hpp
ns3::ndn::ConsumerWindow::OnTimeout
virtual void OnTimeout(uint32_t sequenceNumber)
Timeout event.
Definition: ndn-consumer-window.cpp:200
ns3::ndn::ConsumerWindow::m_window
TracedValue< double > m_window
Definition: ndn-consumer-window.hpp:102
ns3::ndn::Consumer::OnData
virtual void OnData(shared_ptr< const Data > contentObject)
Method that will be called every time new Data arrives.
Definition: ndn-consumer.cpp:213
ns3::ndn::ConsumerWindow
Ndn application for sending out Interest packets (window-based)
Definition: ndn-consumer-window.hpp:39
ns3::ndn::Consumer::m_rtt
Ptr< RttEstimator > m_rtt
RTT estimator.
Definition: ndn-consumer.hpp:143
ns3::ndn::Consumer::SendPacket
void SendPacket()
Actually send packet.
Definition: ndn-consumer.cpp:159
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ns3::ndn::ConsumerWindow::OnData
virtual void OnData(shared_ptr< const Data > contentObject)
Method that will be called every time new Data arrives.
Definition: ndn-consumer-window.cpp:186