forked from named-data-ndnSIM/ndnSIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndn-consumer-window.cpp
221 lines (184 loc) · 6.29 KB
/
ndn-consumer-window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2011-2015 Regents of the University of California.
*
* This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
* contributors.
*
* ndnSIM is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#include "ndn-consumer-window.hpp"
#include "ns3/ptr.h"
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/packet.h"
#include "ns3/callback.h"
#include "ns3/string.h"
#include "ns3/uinteger.h"
#include "ns3/double.h"
NS_LOG_COMPONENT_DEFINE("ndn.ConsumerWindow");
namespace ns3 {
namespace ndn {
NS_OBJECT_ENSURE_REGISTERED(ConsumerWindow);
TypeId
ConsumerWindow::GetTypeId(void)
{
static TypeId tid =
TypeId("ns3::ndn::ConsumerWindow")
.SetGroupName("Ndn")
.SetParent<Consumer>()
.AddConstructor<ConsumerWindow>()
.AddAttribute("Window", "Initial size of the window", StringValue("1"),
MakeUintegerAccessor(&ConsumerWindow::GetWindow, &ConsumerWindow::SetWindow),
MakeUintegerChecker<uint32_t>())
.AddAttribute("PayloadSize",
"Average size of content object size (to calculate interest generation rate)",
UintegerValue(1040), MakeUintegerAccessor(&ConsumerWindow::GetPayloadSize,
&ConsumerWindow::SetPayloadSize),
MakeUintegerChecker<uint32_t>())
.AddAttribute("Size", "Amount of data in megabytes to request, relying on PayloadSize "
"parameter (alternative to MaxSeq attribute)",
DoubleValue(-1), // don't impose limit by default
MakeDoubleAccessor(&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize),
MakeDoubleChecker<double>())
.AddAttribute("MaxSeq", "Maximum sequence number to request (alternative to Size attribute, "
"would activate only if Size is -1). "
"The parameter is activated only if Size negative (not set)",
IntegerValue(std::numeric_limits<uint32_t>::max()),
MakeUintegerAccessor(&ConsumerWindow::GetSeqMax, &ConsumerWindow::SetSeqMax),
MakeUintegerChecker<uint32_t>())
.AddAttribute("InitialWindowOnTimeout", "Set window to initial value when timeout occurs",
BooleanValue(true),
MakeBooleanAccessor(&ConsumerWindow::m_setInitialWindowOnTimeout),
MakeBooleanChecker())
.AddTraceSource("WindowTrace",
"Window that controls how many outstanding interests are allowed",
MakeTraceSourceAccessor(&ConsumerWindow::m_window),
"ns3::ndn::ConsumerWindow::WindowTraceCallback")
.AddTraceSource("InFlight", "Current number of outstanding interests",
MakeTraceSourceAccessor(&ConsumerWindow::m_inFlight),
"ns3::ndn::ConsumerWindow::WindowTraceCallback");
return tid;
}
ConsumerWindow::ConsumerWindow()
: m_payloadSize(1040)
, m_inFlight(0)
{
}
void
ConsumerWindow::SetWindow(uint32_t window)
{
m_initialWindow = window;
m_window = m_initialWindow;
}
uint32_t
ConsumerWindow::GetWindow() const
{
return m_initialWindow;
}
uint32_t
ConsumerWindow::GetPayloadSize() const
{
return m_payloadSize;
}
void
ConsumerWindow::SetPayloadSize(uint32_t payload)
{
m_payloadSize = payload;
}
double
ConsumerWindow::GetMaxSize() const
{
if (m_seqMax == 0)
return -1.0;
return m_maxSize;
}
void
ConsumerWindow::SetMaxSize(double size)
{
m_maxSize = size;
if (m_maxSize < 0) {
m_seqMax = 0;
return;
}
m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize);
NS_LOG_DEBUG("MaxSeqNo: " << m_seqMax);
// std::cout << "MaxSeqNo: " << m_seqMax << "\n";
}
uint32_t
ConsumerWindow::GetSeqMax() const
{
return m_seqMax;
}
void
ConsumerWindow::SetSeqMax(uint32_t seqMax)
{
if (m_maxSize < 0)
m_seqMax = seqMax;
// ignore otherwise
}
void
ConsumerWindow::ScheduleNextPacket()
{
if (m_window == static_cast<uint32_t>(0)) {
Simulator::Remove(m_sendEvent);
NS_LOG_DEBUG(
"Next event in " << (std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S)))
<< " sec");
m_sendEvent =
Simulator::Schedule(Seconds(
std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))),
&Consumer::SendPacket, this);
}
else if (m_inFlight >= m_window) {
// simply do nothing
}
else {
if (m_sendEvent.IsRunning()) {
Simulator::Remove(m_sendEvent);
}
m_sendEvent = Simulator::ScheduleNow(&Consumer::SendPacket, this);
}
}
///////////////////////////////////////////////////
// Process incoming packets //
///////////////////////////////////////////////////
void
ConsumerWindow::OnData(shared_ptr<const Data> contentObject)
{
Consumer::OnData(contentObject);
m_window = m_window + 1;
if (m_inFlight > static_cast<uint32_t>(0))
m_inFlight--;
NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight);
ScheduleNextPacket();
}
void
ConsumerWindow::OnTimeout(uint32_t sequenceNumber)
{
if (m_inFlight > static_cast<uint32_t>(0))
m_inFlight--;
if (m_setInitialWindowOnTimeout) {
// m_window = std::max<uint32_t> (0, m_window - 1);
m_window = m_initialWindow;
}
NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight);
Consumer::OnTimeout(sequenceNumber);
}
void
ConsumerWindow::WillSendOutInterest(uint32_t sequenceNumber)
{
m_inFlight++;
Consumer::WillSendOutInterest(sequenceNumber);
}
} // namespace ndn
} // namespace ns3