NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  .AddTraceSource("InFlight", "Current number of outstanding interests",
78  MakeTraceSourceAccessor(&ConsumerWindow::m_inFlight));
79 
80  return tid;
81 }
82 
84  : m_payloadSize(1040)
85  , m_inFlight(0)
86 {
87 }
88 
89 void
90 ConsumerWindow::SetWindow(uint32_t window)
91 {
92  m_initialWindow = window;
93  m_window = m_initialWindow;
94 }
95 
96 uint32_t
97 ConsumerWindow::GetWindow() const
98 {
99  return m_initialWindow;
100 }
101 
102 uint32_t
103 ConsumerWindow::GetPayloadSize() const
104 {
105  return m_payloadSize;
106 }
107 
108 void
109 ConsumerWindow::SetPayloadSize(uint32_t payload)
110 {
111  m_payloadSize = payload;
112 }
113 
114 double
115 ConsumerWindow::GetMaxSize() const
116 {
117  if (m_seqMax == 0)
118  return -1.0;
119 
120  return m_maxSize;
121 }
122 
123 void
124 ConsumerWindow::SetMaxSize(double size)
125 {
126  m_maxSize = size;
127  if (m_maxSize < 0) {
128  m_seqMax = 0;
129  return;
130  }
131 
132  m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize);
133  NS_LOG_DEBUG("MaxSeqNo: " << m_seqMax);
134  // std::cout << "MaxSeqNo: " << m_seqMax << "\n";
135 }
136 
137 uint32_t
138 ConsumerWindow::GetSeqMax() const
139 {
140  return m_seqMax;
141 }
142 
143 void
144 ConsumerWindow::SetSeqMax(uint32_t seqMax)
145 {
146  if (m_maxSize < 0)
147  m_seqMax = seqMax;
148 
149  // ignore otherwise
150 }
151 
152 void
154 {
155  if (m_window == static_cast<uint32_t>(0)) {
156  Simulator::Remove(m_sendEvent);
157 
158  NS_LOG_DEBUG(
159  "Next event in " << (std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S)))
160  << " sec");
161  m_sendEvent =
162  Simulator::Schedule(Seconds(
163  std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))),
164  &Consumer::SendPacket, this);
165  }
166  else if (m_inFlight >= m_window) {
167  // simply do nothing
168  }
169  else {
170  if (m_sendEvent.IsRunning()) {
171  Simulator::Remove(m_sendEvent);
172  }
173 
174  m_sendEvent = Simulator::ScheduleNow(&Consumer::SendPacket, this);
175  }
176 }
177 
179 // Process incoming packets //
181 
182 void
183 ConsumerWindow::OnData(shared_ptr<const Data> contentObject)
184 {
185  Consumer::OnData(contentObject);
186 
187  m_window = m_window + 1;
188 
189  if (m_inFlight > static_cast<uint32_t>(0))
190  m_inFlight--;
191  NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight);
192 
194 }
195 
196 void
197 ConsumerWindow::OnTimeout(uint32_t sequenceNumber)
198 {
199  if (m_inFlight > static_cast<uint32_t>(0))
200  m_inFlight--;
201 
202  if (m_setInitialWindowOnTimeout) {
203  // m_window = std::max<uint32_t> (0, m_window - 1);
204  m_window = m_initialWindow;
205  }
206 
207  NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight);
208  Consumer::OnTimeout(sequenceNumber);
209 }
210 
211 void
212 ConsumerWindow::WillSendOutInterest(uint32_t sequenceNumber)
213 {
214  m_inFlight++;
215  Consumer::WillSendOutInterest(sequenceNumber);
216 }
217 
218 } // namespace ndn
219 } // namespace ns3
virtual void OnTimeout(uint32_t sequenceNumber)
Timeout event.
uint32_t m_seqMax
maximum number of sequence number
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.
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) ...