A Discrete-Event Network Simulator
API
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 <algorithm>
30 #include "ns3/log.h"
31 #include "ns3/wifi-mac-header.h"
32 #include "aodv-neighbor.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("AodvNeighbors");
37 
38 namespace aodv {
40  : m_ntimer (Timer::CANCEL_ON_DESTROY)
41 {
42  m_ntimer.SetDelay (delay);
45 }
46 
47 bool
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  {
56  return true;
57  }
58  }
59  return false;
60 }
61 
62 Time
64 {
65  Purge ();
66  for (std::vector<Neighbor>::const_iterator i = m_nb.begin (); i
67  != m_nb.end (); ++i)
68  {
69  if (i->m_neighborAddress == addr)
70  {
71  return (i->m_expireTime - Simulator::Now ());
72  }
73  }
74  return Seconds (0);
75 }
76 
77 void
79 {
80  for (std::vector<Neighbor>::iterator i = m_nb.begin (); i != m_nb.end (); ++i)
81  {
82  if (i->m_neighborAddress == addr)
83  {
84  i->m_expireTime
85  = std::max (expire + Simulator::Now (), i->m_expireTime);
86  if (i->m_hardwareAddress == Mac48Address ())
87  {
88  i->m_hardwareAddress = LookupMacAddress (i->m_neighborAddress);
89  }
90  return;
91  }
92  }
93 
94  NS_LOG_LOGIC ("Open link to " << addr);
95  Neighbor neighbor (addr, LookupMacAddress (addr), expire + Simulator::Now ());
96  m_nb.push_back (neighbor);
97  Purge ();
98 }
99 
104 {
111  bool operator() (const Neighbors::Neighbor & nb) const
112  {
113  return ((nb.m_expireTime < Simulator::Now ()) || nb.close);
114  }
115 };
116 
117 void
119 {
120  if (m_nb.empty ())
121  {
122  return;
123  }
124 
125  CloseNeighbor pred;
126  if (!m_handleLinkFailure.IsNull ())
127  {
128  for (std::vector<Neighbor>::iterator j = m_nb.begin (); j != m_nb.end (); ++j)
129  {
130  if (pred (*j))
131  {
132  NS_LOG_LOGIC ("Close link to " << j->m_neighborAddress);
133  m_handleLinkFailure (j->m_neighborAddress);
134  }
135  }
136  }
137  m_nb.erase (std::remove_if (m_nb.begin (), m_nb.end (), pred), m_nb.end ());
138  m_ntimer.Cancel ();
139  m_ntimer.Schedule ();
140 }
141 
142 void
144 {
145  m_ntimer.Cancel ();
146  m_ntimer.Schedule ();
147 }
148 
149 void
151 {
152  m_arp.push_back (a);
153 }
154 
155 void
157 {
158  m_arp.erase (std::remove (m_arp.begin (), m_arp.end (), a), m_arp.end ());
159 }
160 
163 {
164  Mac48Address hwaddr;
165  for (std::vector<Ptr<ArpCache> >::const_iterator i = m_arp.begin ();
166  i != m_arp.end (); ++i)
167  {
168  ArpCache::Entry * entry = (*i)->Lookup (addr);
169  if (entry != 0 && (entry->IsAlive () || entry->IsPermanent ()) && !entry->IsExpired ())
170  {
171  hwaddr = Mac48Address::ConvertFrom (entry->GetMacAddress ());
172  break;
173  }
174  }
175  return hwaddr;
176 }
177 
178 void
180 {
181  Mac48Address addr = hdr.GetAddr1 ();
182 
183  for (std::vector<Neighbor>::iterator i = m_nb.begin (); i != m_nb.end (); ++i)
184  {
185  if (i->m_hardwareAddress == addr)
186  {
187  i->close = true;
188  }
189  }
190  Purge ();
191 }
192 
193 } // namespace aodv
194 } // namespace ns3
195 
#define max(a, b)
Definition: 80211b.c:43
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:178
bool IsAlive(void)
Definition: arp-cache.cc:375
bool IsPermanent(void)
Definition: arp-cache.cc:387
Address GetMacAddress(void) const
Definition: arp-cache.cc:454
bool IsExpired(void) const
Definition: arp-cache.cc:497
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
an EUI-48 address
Definition: mac48-address.h:44
static Mac48Address ConvertFrom(const Address &address)
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
A simple virtual Timer class.
Definition: timer.h:74
void SetDelay(const Time &delay)
Definition: timer.cc:75
void SetFunction(FN fn)
Definition: timer.h:278
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
Implements the IEEE 802.11 MAC header.
Mac48Address GetAddr1(void) const
Return the address in the Address 1 field.
Time GetExpireTime(Ipv4Address addr)
Return expire time for neighbor node with address addr, if exists, else return 0.
void ScheduleTimer()
Schedule m_ntimer.
Neighbors(Time delay)
constructor
void Purge()
Remove all expired entries.
std::vector< Ptr< ArpCache > > m_arp
list of ARP cached to be used for layer 2 notifications processing
Mac48Address LookupMacAddress(Ipv4Address addr)
Find MAC address by IP using list of ARP caches.
void ProcessTxError(WifiMacHeader const &hdr)
Process layer 2 TX error notification.
void Update(Ipv4Address addr, Time expire)
Update expire time for entry with address addr, if it exists, else add new entry.
std::vector< Neighbor > m_nb
vector of entries
Timer m_ntimer
Timer for neighbor's list. Schedule Purge().
Callback< void, Ipv4Address > m_handleLinkFailure
link failure callback
Callback< void, WifiMacHeader const & > m_txErrorCallback
TX error callback.
void DelArpCache(Ptr< ArpCache > a)
Don't use given ARP cache any more (interface is down)
bool IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor.
void AddArpCache(Ptr< ArpCache > a)
Add ARP cache to be used to allow layer 2 notifications processing.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
CloseNeighbor structure.
bool operator()(const Neighbors::Neighbor &nb) const
Check if the entry is expired.
Neighbor description.
Definition: aodv-neighbor.h:61
bool close
Neighbor close indicator.
Definition: aodv-neighbor.h:69
Time m_expireTime
Neighbor expire time.
Definition: aodv-neighbor.h:67