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 
30 NS_LOG_COMPONENT_DEFINE("ndn.ConsumerWindow");
31 
32 namespace ns3 {
33 namespace ndn {
34 
36 
37 TypeId
39 {
40  static TypeId tid =
41  TypeId("ns3::ndn::ConsumerWindow")
42  .SetGroupName("Ndn")
43  .SetParent<Consumer>()
44  .AddConstructor<ConsumerWindow>()
45 
46  .AddAttribute("Window", "Initial size of the window", StringValue("1"),
47  MakeUintegerAccessor(&ConsumerWindow::GetWindow, &ConsumerWindow::SetWindow),
48  MakeUintegerChecker<uint32_t>())
49 
50  .AddAttribute("PayloadSize",
51  "Average size of content object size (to calculate interest generation rate)",
52  UintegerValue(1040), MakeUintegerAccessor(&ConsumerWindow::GetPayloadSize,
53  &ConsumerWindow::SetPayloadSize),
54  MakeUintegerChecker<uint32_t>())
55 
56  .AddAttribute("Size", "Amount of data in megabytes to request, relying on PayloadSize "
57  "parameter (alternative to MaxSeq attribute)",
58  DoubleValue(-1), // don't impose limit by default
59  MakeDoubleAccessor(&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize),
60  MakeDoubleChecker<double>())
61 
62  .AddAttribute("MaxSeq", "Maximum sequence number to request (alternative to Size attribute, "
63  "would activate only if Size is -1). "
64  "The parameter is activated only if Size negative (not set)",
65  IntegerValue(std::numeric_limits<uint32_t>::max()),
66  MakeUintegerAccessor(&ConsumerWindow::GetSeqMax, &ConsumerWindow::SetSeqMax),
67  MakeUintegerChecker<uint32_t>())
68 
69  .AddAttribute("InitialWindowOnTimeout", "Set window to initial value when timeout occurs",
70  BooleanValue(true),
71  MakeBooleanAccessor(&ConsumerWindow::m_setInitialWindowOnTimeout),
72  MakeBooleanChecker())
73 
74  .AddTraceSource("WindowTrace",
75  "Window that controls how many outstanding interests are allowed",
76  MakeTraceSourceAccessor(&ConsumerWindow::m_window),
77  "ns3::ndn::ConsumerWindow::WindowTraceCallback")
78  .AddTraceSource("InFlight", "Current number of outstanding interests",
79  MakeTraceSourceAccessor(&ConsumerWindow::m_inFlight),
80  "ns3::ndn::ConsumerWindow::WindowTraceCallback");
81 
82  return tid;
83 }
84 
86  : m_payloadSize(1040)
87  , m_inFlight(0)
88 {
89 }
90 
91 void
92 ConsumerWindow::SetWindow(uint32_t window)
93 {
94  m_initialWindow = window;
95  m_window = m_initialWindow;
96 }
97 
98 uint32_t
99 ConsumerWindow::GetWindow() const
100 {
101  return m_initialWindow;
102 }
103 
104 uint32_t
105 ConsumerWindow::GetPayloadSize() const
106 {
107  return m_payloadSize;
108 }
109 
110 void
111 ConsumerWindow::SetPayloadSize(uint32_t payload)
112 {
113  m_payloadSize = payload;
114 }
115 
116 double
117 ConsumerWindow::GetMaxSize() const
118 {
119  if (m_seqMax == 0)
120  return -1.0;
121 
122  return m_maxSize;
123 }
124 
125 void
126 ConsumerWindow::SetMaxSize(double size)
127 {
128  m_maxSize = size;
129  if (m_maxSize < 0) {
130  m_seqMax = 0;
131  return;
132  }
133 
134  m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize);
135  NS_LOG_DEBUG("MaxSeqNo: " << m_seqMax);
136  // std::cout << "MaxSeqNo: " << m_seqMax << "\n";
137 }
138 
139 uint32_t
140 ConsumerWindow::GetSeqMax() const
141 {
142  return m_seqMax;
143 }
144 
145 void
146 ConsumerWindow::SetSeqMax(uint32_t seqMax)
147 {
148  if (m_maxSize < 0)
149  m_seqMax = seqMax;
150 
151  // ignore otherwise
152 }
153 
154 void
156 {
157  if (m_window == static_cast<uint32_t>(0)) {
158  Simulator::Remove(m_sendEvent);
159 
160  NS_LOG_DEBUG(
161  "Next event in " << (std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S)))
162  << " sec");
163  m_sendEvent =
164  Simulator::Schedule(Seconds(
165  std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))),
166  &Consumer::SendPacket, this);
167  }
168  else if (m_inFlight >= m_window) {
169  // simply do nothing
170  }
171  else {
172  if (m_sendEvent.IsRunning()) {
173  Simulator::Remove(m_sendEvent);
174  }
175 
176  m_sendEvent = Simulator::ScheduleNow(&Consumer::SendPacket, this);
177  }
178 }
179 
181 // Process incoming packets //
183 
184 void
185 ConsumerWindow::OnData(shared_ptr<const Data> contentObject)
186 {
187  Consumer::OnData(contentObject);
188 
189  m_window = m_window + 1;
190 
191  if (m_inFlight > static_cast<uint32_t>(0))
192  m_inFlight--;
193  NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight);
194 
196 }
197 
198 void
199 ConsumerWindow::OnTimeout(uint32_t sequenceNumber)
200 {
201  if (m_inFlight > static_cast<uint32_t>(0))
202  m_inFlight--;
203 
204  if (m_setInitialWindowOnTimeout) {
205  // m_window = std::max<uint32_t> (0, m_window - 1);
206  m_window = m_initialWindow;
207  }
208 
209  NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight);
210  Consumer::OnTimeout(sequenceNumber);
211 }
212 
213 void
214 ConsumerWindow::WillSendOutInterest(uint32_t sequenceNumber)
215 {
216  m_inFlight++;
217  Consumer::WillSendOutInterest(sequenceNumber);
218 }
219 
220 } // namespace ndn
221 } // 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.
NS_OBJECT_ENSURE_REGISTERED(ContentStore)
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.
virtual void OnData(shared_ptr< const Data > contentObject)
Method that will be called every time new Data arrives.
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) ...