A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
dsr-rreq-table.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2011 Yufei Cheng
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Yufei Cheng <yfcheng@ittc.ku.edu>
19
*
20
* James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21
* ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22
* Information and Telecommunication Technology Center (ITTC)
23
* and Department of Electrical Engineering and Computer Science
24
* The University of Kansas Lawrence, KS USA.
25
*
26
* Work supported in part by NSF FIND (Future Internet Design) Program
27
* under grant CNS-0626918 (Postmodern Internet Architecture),
28
* NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29
* US Department of Defense (DoD), and ITTC at The University of Kansas.
30
*/
31
32
#include "
dsr-rreq-table.h
"
33
#include "ns3/log.h"
34
#include <algorithm>
35
#include <iostream>
36
37
NS_LOG_COMPONENT_DEFINE
(
"RreqTable"
);
38
39
namespace
ns3 {
40
namespace
dsr {
41
42
NS_OBJECT_ENSURE_REGISTERED
(RreqTable);
43
44
TypeId
RreqTable::GetTypeId
()
45
{
46
static
TypeId
tid =
TypeId
(
"ns3::dsr::RreqTable"
)
47
.
SetParent
<
Object
> ()
48
.AddConstructor<RreqTable> ()
49
;
50
return
tid;
51
}
52
53
RreqTable::RreqTable
()
54
: m_linkStates (
PROBABLE
)
55
{
56
}
57
58
RreqTable::~RreqTable
()
59
{
60
NS_LOG_FUNCTION_NOARGS
();
61
}
62
63
void
64
RreqTable::RemoveLeastExpire
(std::map<Ipv4Address, RreqTableEntry > & rreqDstMap)
65
{
66
NS_LOG_FUNCTION
(
this
);
67
Ipv4Address
firstExpire;
68
Time
max =
Seconds
(0.0);
69
for
(std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
70
rreqDstMap.begin (); i != rreqDstMap.end (); ++i)
71
{
72
Ipv4Address
dst = i->first;
73
RreqTableEntry
rreqTableEntry = i->second;
74
if
(rreqTableEntry.
m_expire
> max)
75
{
76
max = rreqTableEntry.
m_expire
;
77
firstExpire = dst;
78
}
79
}
80
rreqDstMap.erase (firstExpire);
81
}
82
83
void
84
RreqTable::FindAndUpdate
(
Ipv4Address
dst)
85
{
86
NS_LOG_FUNCTION
(
this
<< dst);
87
std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
88
m_rreqDstMap
.find (dst);
89
if
(i ==
m_rreqDstMap
.end ())
90
{
91
NS_LOG_DEBUG
(
"The request table entry for "
<< dst <<
" not found"
);
92
/*
93
* Drop the most aged packet when buffer reaches to max
94
*/
95
if
(
m_rreqDstMap
.size () >=
m_requestTableSize
)
96
{
97
RemoveLeastExpire
(
m_rreqDstMap
);
98
NS_LOG_DEBUG
(
"The request table size after erase "
<< (uint32_t)
m_rreqDstMap
.size ());
99
}
100
RreqTableEntry
rreqTableEntry;
101
rreqTableEntry.
m_reqNo
= 1;
102
rreqTableEntry.
m_expire
=
Simulator::Now
();
103
m_rreqDstMap
[dst] = rreqTableEntry;
104
}
105
else
106
{
107
NS_LOG_INFO
(
"Find the request table entry for "
<< dst <<
", increment the request count"
);
108
Ipv4Address
dst = i->first;
109
RreqTableEntry
rreqTableEntry = i->second;
110
NS_LOG_DEBUG
(
"The request count before incrementing "
<< rreqTableEntry.
m_reqNo
);
111
rreqTableEntry.
m_reqNo
= (rreqTableEntry.
m_reqNo
+ 1);
112
rreqTableEntry.
m_expire
=
Simulator::Now
();
113
m_rreqDstMap
[dst] = rreqTableEntry;
114
}
115
}
116
117
void
118
RreqTable::RemoveRreqEntry
(
Ipv4Address
dst)
119
{
120
NS_LOG_FUNCTION
(
this
<< dst);
121
NS_LOG_DEBUG
(
"Remove rreq entry with index dst"
);
122
std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
123
m_rreqDstMap
.find (dst);
124
if
(i ==
m_rreqDstMap
.end ())
125
{
126
NS_LOG_DEBUG
(
"The request table entry not found"
);
127
}
128
else
129
{
130
// erase the request entry
131
m_rreqDstMap
.erase (dst);
132
}
133
}
134
135
uint32_t
136
RreqTable::GetRreqCnt
(
Ipv4Address
dst)
137
{
138
NS_LOG_FUNCTION
(
this
<< dst);
139
std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
140
m_rreqDstMap
.find (dst);
141
if
(i ==
m_rreqDstMap
.end ())
142
{
143
NS_LOG_DEBUG
(
"The request table entry not found"
);
144
return
0;
145
}
146
else
147
{
148
RreqTableEntry
rreqTableEntry = i->second;
149
NS_LOG_DEBUG
(
"Find the request count for "
<< dst <<
" "
<< rreqTableEntry.
m_reqNo
);
150
return
rreqTableEntry.
m_reqNo
;
151
}
152
}
153
154
// ----------------------------------------------------------------------------------------------------------
159
uint32_t
160
RreqTable::CheckUniqueRreqId
(
Ipv4Address
dst)
161
{
162
NS_LOG_DEBUG
(
"The size of id cache "
<<
m_rreqIdCache
.size ());
163
std::map<Ipv4Address, uint32_t>::const_iterator i =
164
m_rreqIdCache
.find (dst);
165
if
(i ==
m_rreqIdCache
.end ())
166
{
167
NS_LOG_LOGIC
(
"No Request id for "
<< dst <<
" found"
);
168
m_rreqIdCache
[dst] = 0;
169
return
0;
170
}
171
else
172
{
173
NS_LOG_LOGIC
(
"Request id for "
<< dst <<
" found in the cache"
);
174
uint32_t rreqId =
m_rreqIdCache
[dst];
175
if
(rreqId >=
m_maxRreqId
)
176
{
177
NS_LOG_DEBUG
(
"The request id increase past the max value, "
<<
m_maxRreqId
<<
" so reset it to 0"
);
178
rreqId = 0;
179
m_rreqIdCache
[dst] = rreqId;
180
}
181
else
182
{
183
rreqId++;
184
m_rreqIdCache
[dst] = rreqId;
185
}
186
NS_LOG_DEBUG
(
"The Request id for "
<< dst <<
" is "
<< rreqId);
187
return
rreqId;
188
}
189
}
190
191
uint32_t
192
RreqTable::GetRreqSize
()
193
{
194
return
m_rreqIdCache
.size ();
195
}
196
197
// ----------------------------------------------------------------------------------------------------------
202
void
203
RreqTable::Invalidate
()
204
{
205
if
(
m_linkStates
==
QUESTIONABLE
)
206
{
207
return
;
208
}
209
m_linkStates
=
QUESTIONABLE
;
210
}
211
212
BlackList
*
213
RreqTable::FindUnidirectional
(
Ipv4Address
neighbor)
214
{
215
PurgeNeighbor
();
// purge the neighbor cache
216
for
(std::vector<BlackList>::iterator i =
m_blackList
.begin ();
217
i !=
m_blackList
.end (); ++i)
218
{
219
if
(i->m_neighborAddress == neighbor)
220
{
221
return
&(*i);
222
}
223
}
224
return
NULL;
225
}
226
227
bool
228
RreqTable::MarkLinkAsUnidirectional
(
Ipv4Address
neighbor,
Time
blacklistTimeout)
229
{
230
NS_LOG_LOGIC
(
"Add neighbor address in blacklist "
<<
m_blackList
.size ());
231
for
(std::vector<BlackList>::iterator i =
m_blackList
.begin (); i !=
m_blackList
.end (); ++i)
232
{
233
if
(i->m_neighborAddress == neighbor)
234
{
235
NS_LOG_DEBUG
(
"Update the blacklist list timeout if found the blacklist entry"
);
236
i->m_expireTime = std::max (blacklistTimeout +
Simulator::Now
(), i->m_expireTime);
237
}
238
BlackList
blackList (neighbor, blacklistTimeout +
Simulator::Now
());
239
m_blackList
.push_back (blackList);
240
PurgeNeighbor
();
241
return
true
;
242
}
243
return
false
;
244
}
245
246
void
247
RreqTable::PurgeNeighbor
()
248
{
249
/*
250
* Purge the expired blacklist entries
251
*/
252
m_blackList
.erase (remove_if (
m_blackList
.begin (),
m_blackList
.end (),
253
IsExpired
()),
m_blackList
.end ());
254
}
255
256
}
// namespace dsr
257
}
// namespace ns3
src
dsr
model
dsr-rreq-table.cc
Generated on Tue Oct 9 2012 16:45:37 for ns-3 by
1.8.1.2