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
aodv-neighbor.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2009 IITP RAS
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
* Based on
19
* NS-2 AODV model developed by the CMU/MONARCH group and optimized and
20
* tuned by Samir Das and Mahesh Marina, University of Cincinnati;
21
*
22
* AODV-UU implementation by Erik Nordström of Uppsala University
23
* http://core.it.uu.se/core/index.php/AODV-UU
24
*
25
* Authors: Elena Buchatskaia <borovkovaes@iitp.ru>
26
* Pavel Boyko <boyko@iitp.ru>
27
*/
28
29
#include "
aodv-neighbor.h
"
30
#include "ns3/log.h"
31
#include <algorithm>
32
33
NS_LOG_COMPONENT_DEFINE
(
"AodvNeighbors"
);
34
35
namespace
ns3
36
{
37
namespace
aodv
38
{
39
Neighbors::Neighbors
(
Time
delay) :
40
m_ntimer (
Timer
::CANCEL_ON_DESTROY)
41
{
42
m_ntimer
.
SetDelay
(delay);
43
m_ntimer
.
SetFunction
(&
Neighbors::Purge
,
this
);
44
m_txErrorCallback
=
MakeCallback
(&
Neighbors::ProcessTxError
,
this
);
45
}
46
47
bool
48
Neighbors::IsNeighbor
(
Ipv4Address
addr)
49
{
50
Purge
();
51
for
(std::vector<Neighbor>::const_iterator i =
m_nb
.begin ();
52
i !=
m_nb
.end (); ++i)
53
{
54
if
(i->m_neighborAddress == addr)
55
return
true
;
56
}
57
return
false
;
58
}
59
60
Time
61
Neighbors::GetExpireTime
(
Ipv4Address
addr)
62
{
63
Purge
();
64
for
(std::vector<Neighbor>::const_iterator i =
m_nb
.begin (); i
65
!=
m_nb
.end (); ++i)
66
{
67
if
(i->m_neighborAddress == addr)
68
return
(i->m_expireTime -
Simulator::Now
());
69
}
70
return
Seconds
(0);
71
}
72
73
void
74
Neighbors::Update
(
Ipv4Address
addr,
Time
expire)
75
{
76
for
(std::vector<Neighbor>::iterator i =
m_nb
.begin (); i !=
m_nb
.end (); ++i)
77
if
(i->m_neighborAddress == addr)
78
{
79
i->m_expireTime
80
= std::max (expire +
Simulator::Now
(), i->m_expireTime);
81
if
(i->m_hardwareAddress ==
Mac48Address
())
82
i->m_hardwareAddress =
LookupMacAddress
(i->m_neighborAddress);
83
return
;
84
}
85
86
NS_LOG_LOGIC
(
"Open link to "
<< addr);
87
Neighbor
neighbor (addr,
LookupMacAddress
(addr), expire +
Simulator::Now
());
88
m_nb
.push_back (neighbor);
89
Purge
();
90
}
91
92
struct
CloseNeighbor
93
{
94
bool
operator()
(
const
Neighbors::Neighbor
& nb)
const
95
{
96
return
((nb.
m_expireTime
<
Simulator::Now
()) || nb.
close
);
97
}
98
};
99
100
void
101
Neighbors::Purge
()
102
{
103
if
(
m_nb
.empty ())
104
return
;
105
106
CloseNeighbor
pred;
107
if
(!
m_handleLinkFailure
.
IsNull
())
108
{
109
for
(std::vector<Neighbor>::iterator j =
m_nb
.begin (); j !=
m_nb
.end (); ++j)
110
{
111
if
(pred (*j))
112
{
113
NS_LOG_LOGIC
(
"Close link to "
<< j->m_neighborAddress);
114
m_handleLinkFailure
(j->m_neighborAddress);
115
}
116
}
117
}
118
m_nb
.erase (std::remove_if (
m_nb
.begin (),
m_nb
.end (), pred),
m_nb
.end ());
119
m_ntimer
.
Cancel
();
120
m_ntimer
.
Schedule
();
121
}
122
123
void
124
Neighbors::ScheduleTimer
()
125
{
126
m_ntimer
.
Cancel
();
127
m_ntimer
.
Schedule
();
128
}
129
130
void
131
Neighbors::AddArpCache
(
Ptr<ArpCache>
a)
132
{
133
m_arp
.push_back (a);
134
}
135
136
void
137
Neighbors::DelArpCache
(
Ptr<ArpCache>
a)
138
{
139
m_arp
.erase (std::remove (
m_arp
.begin (),
m_arp
.end (), a),
m_arp
.end ());
140
}
141
142
Mac48Address
143
Neighbors::LookupMacAddress
(
Ipv4Address
addr)
144
{
145
Mac48Address
hwaddr;
146
for
(std::vector<
Ptr<ArpCache>
>::const_iterator i =
m_arp
.begin ();
147
i !=
m_arp
.end (); ++i)
148
{
149
ArpCache::Entry
* entry = (*i)->Lookup (addr);
150
if
(entry != 0 && entry->
IsAlive
() && !entry->
IsExpired
())
151
{
152
hwaddr =
Mac48Address::ConvertFrom
(entry->
GetMacAddress
());
153
break
;
154
}
155
}
156
return
hwaddr;
157
}
158
159
void
160
Neighbors::ProcessTxError
(
WifiMacHeader
const
& hdr)
161
{
162
Mac48Address
addr = hdr.
GetAddr1
();
163
164
for
(std::vector<Neighbor>::iterator i =
m_nb
.begin (); i !=
m_nb
.end (); ++i)
165
{
166
if
(i->m_hardwareAddress == addr)
167
i->close =
true
;
168
}
169
Purge
();
170
}
171
}
172
}
173
src
aodv
model
aodv-neighbor.cc
Generated on Tue May 14 2013 11:08:15 for ns-3 by
1.8.1.2