forked from named-data-ndnSIM/ndnSIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndn-consumer.cpp
288 lines (229 loc) · 8.38 KB
/
ndn-consumer.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*- 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.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/boolean.h"
#include "ns3/uinteger.h"
#include "ns3/integer.h"
#include "ns3/double.h"
#include "utils/ndn-ns3-packet-tag.hpp"
#include "model/ndn-app-face.hpp"
#include "utils/ndn-rtt-mean-deviation.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/ref.hpp>
NS_LOG_COMPONENT_DEFINE("ndn.Consumer");
namespace ns3 {
namespace ndn {
NS_OBJECT_ENSURE_REGISTERED(Consumer);
TypeId
Consumer::GetTypeId(void)
{
static TypeId tid =
TypeId("ns3::ndn::Consumer")
.SetGroupName("Ndn")
.SetParent<App>()
.AddAttribute("StartSeq", "Initial sequence number", IntegerValue(0),
MakeIntegerAccessor(&Consumer::m_seq), MakeIntegerChecker<int32_t>())
.AddAttribute("Prefix", "Name of the Interest", StringValue("/"),
MakeNameAccessor(&Consumer::m_interestName), MakeNameChecker())
.AddAttribute("LifeTime", "LifeTime for interest packet", StringValue("2s"),
MakeTimeAccessor(&Consumer::m_interestLifeTime), MakeTimeChecker())
.AddAttribute("RetxTimer",
"Timeout defining how frequent retransmission timeouts should be checked",
StringValue("50ms"),
MakeTimeAccessor(&Consumer::GetRetxTimer, &Consumer::SetRetxTimer),
MakeTimeChecker())
.AddTraceSource("LastRetransmittedInterestDataDelay",
"Delay between last retransmitted Interest and received Data",
MakeTraceSourceAccessor(&Consumer::m_lastRetransmittedInterestDataDelay),
"ns3::ndn::Consumer::LastRetransmittedInterestDataDelayCallback")
.AddTraceSource("FirstInterestDataDelay",
"Delay between first transmitted Interest and received Data",
MakeTraceSourceAccessor(&Consumer::m_firstInterestDataDelay),
"ns3::ndn::Consumer::FirstInterestDataDelayCallback");
return tid;
}
Consumer::Consumer()
: m_rand(CreateObject<UniformRandomVariable>())
, m_seq(0)
, m_seqMax(0) // don't request anything
{
NS_LOG_FUNCTION_NOARGS();
m_rtt = CreateObject<RttMeanDeviation>();
}
void
Consumer::SetRetxTimer(Time retxTimer)
{
m_retxTimer = retxTimer;
if (m_retxEvent.IsRunning()) {
// m_retxEvent.Cancel (); // cancel any scheduled cleanup events
Simulator::Remove(m_retxEvent); // slower, but better for memory
}
// schedule even with new timeout
m_retxEvent = Simulator::Schedule(m_retxTimer, &Consumer::CheckRetxTimeout, this);
}
Time
Consumer::GetRetxTimer() const
{
return m_retxTimer;
}
void
Consumer::CheckRetxTimeout()
{
Time now = Simulator::Now();
Time rto = m_rtt->RetransmitTimeout();
// NS_LOG_DEBUG ("Current RTO: " << rto.ToDouble (Time::S) << "s");
while (!m_seqTimeouts.empty()) {
SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
m_seqTimeouts.get<i_timestamp>().begin();
if (entry->time + rto <= now) // timeout expired?
{
uint32_t seqNo = entry->seq;
m_seqTimeouts.get<i_timestamp>().erase(entry);
OnTimeout(seqNo);
}
else
break; // nothing else to do. All later packets need not be retransmitted
}
m_retxEvent = Simulator::Schedule(m_retxTimer, &Consumer::CheckRetxTimeout, this);
}
// Application Methods
void
Consumer::StartApplication() // Called at time specified by Start
{
NS_LOG_FUNCTION_NOARGS();
// do base stuff
App::StartApplication();
ScheduleNextPacket();
}
void
Consumer::StopApplication() // Called at time specified by Stop
{
NS_LOG_FUNCTION_NOARGS();
// cancel periodic packet generation
Simulator::Cancel(m_sendEvent);
// cleanup base stuff
App::StopApplication();
}
void
Consumer::SendPacket()
{
if (!m_active)
return;
NS_LOG_FUNCTION_NOARGS();
uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
while (m_retxSeqs.size()) {
seq = *m_retxSeqs.begin();
m_retxSeqs.erase(m_retxSeqs.begin());
break;
}
if (seq == std::numeric_limits<uint32_t>::max()) {
if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
if (m_seq >= m_seqMax) {
return; // we are totally done
}
}
seq = m_seq++;
}
//
shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
nameWithSequence->appendSequenceNumber(seq);
//
// shared_ptr<Interest> interest = make_shared<Interest> ();
shared_ptr<Interest> interest = make_shared<Interest>();
interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
interest->setName(*nameWithSequence);
time::milliseconds interestLifeTime(m_interestLifeTime.GetMilliSeconds());
interest->setInterestLifetime(interestLifeTime);
// NS_LOG_INFO ("Requesting Interest: \n" << *interest);
NS_LOG_INFO("> Interest for " << seq);
WillSendOutInterest(seq);
m_transmittedInterests(interest, this, m_face);
m_face->onReceiveInterest(*interest);
ScheduleNextPacket();
}
///////////////////////////////////////////////////
// Process incoming packets //
///////////////////////////////////////////////////
void
Consumer::OnData(shared_ptr<const Data> data)
{
if (!m_active)
return;
App::OnData(data); // tracing inside
NS_LOG_FUNCTION(this << data);
// NS_LOG_INFO ("Received content object: " << boost::cref(*data));
// This could be a problem......
uint32_t seq = data->getName().at(-1).toSequenceNumber();
NS_LOG_INFO("< DATA for " << seq);
int hopCount = 0;
auto ns3PacketTag = data->getTag<Ns3PacketTag>();
if (ns3PacketTag != nullptr) { // e.g., packet came from local node's cache
FwHopCountTag hopCountTag;
if (ns3PacketTag->getPacket()->PeekPacketTag(hopCountTag)) {
hopCount = hopCountTag.Get();
NS_LOG_DEBUG("Hop count: " << hopCount);
}
}
SeqTimeoutsContainer::iterator entry = m_seqLastDelay.find(seq);
if (entry != m_seqLastDelay.end()) {
m_lastRetransmittedInterestDataDelay(this, seq, Simulator::Now() - entry->time, hopCount);
}
entry = m_seqFullDelay.find(seq);
if (entry != m_seqFullDelay.end()) {
m_firstInterestDataDelay(this, seq, Simulator::Now() - entry->time, m_seqRetxCounts[seq], hopCount);
}
m_seqRetxCounts.erase(seq);
m_seqFullDelay.erase(seq);
m_seqLastDelay.erase(seq);
m_seqTimeouts.erase(seq);
m_retxSeqs.erase(seq);
m_rtt->AckSeq(SequenceNumber32(seq));
}
void
Consumer::OnTimeout(uint32_t sequenceNumber)
{
NS_LOG_FUNCTION(sequenceNumber);
// std::cout << Simulator::Now () << ", TO: " << sequenceNumber << ", current RTO: " <<
// m_rtt->RetransmitTimeout ().ToDouble (Time::S) << "s\n";
m_rtt->IncreaseMultiplier(); // Double the next RTO
m_rtt->SentSeq(SequenceNumber32(sequenceNumber),
1); // make sure to disable RTT calculation for this sample
m_retxSeqs.insert(sequenceNumber);
ScheduleNextPacket();
}
void
Consumer::WillSendOutInterest(uint32_t sequenceNumber)
{
NS_LOG_DEBUG("Trying to add " << sequenceNumber << " with " << Simulator::Now() << ". already "
<< m_seqTimeouts.size() << " items");
m_seqTimeouts.insert(SeqTimeout(sequenceNumber, Simulator::Now()));
m_seqFullDelay.insert(SeqTimeout(sequenceNumber, Simulator::Now()));
m_seqLastDelay.erase(sequenceNumber);
m_seqLastDelay.insert(SeqTimeout(sequenceNumber, Simulator::Now()));
m_seqRetxCounts[sequenceNumber]++;
m_rtt->SentSeq(SequenceNumber32(sequenceNumber), 1);
}
} // namespace ndn
} // namespace ns3