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 "aodv-neighbor.h"
30 #include "ns3/log.h"
31 #include <algorithm>
32 
33 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE ("AodvNeighbors");
38 
39 namespace aodv
40 {
42  m_ntimer (Timer::CANCEL_ON_DESTROY)
43 {
44  m_ntimer.SetDelay (delay);
47 }
48 
49 bool
51 {
52  Purge ();
53  for (std::vector<Neighbor>::const_iterator i = m_nb.begin ();
54  i != m_nb.end (); ++i)
55  {
56  if (i->m_neighborAddress == addr)
57  return true;
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  return (i->m_expireTime - Simulator::Now ());
71  }
72  return Seconds (0);
73 }
74 
75 void
77 {
78  for (std::vector<Neighbor>::iterator i = m_nb.begin (); i != m_nb.end (); ++i)
79  if (i->m_neighborAddress == addr)
80  {
81  i->m_expireTime
82  = std::max (expire + Simulator::Now (), i->m_expireTime);
83  if (i->m_hardwareAddress == Mac48Address ())
84  i->m_hardwareAddress = LookupMacAddress (i->m_neighborAddress);
85  return;
86  }
87 
88  NS_LOG_LOGIC ("Open link to " << addr);
89  Neighbor neighbor (addr, LookupMacAddress (addr), expire + Simulator::Now ());
90  m_nb.push_back (neighbor);
91  Purge ();
92 }
93 
95 {
96  bool operator() (const Neighbors::Neighbor & nb) const
97  {
98  return ((nb.m_expireTime < Simulator::Now ()) || nb.close);
99  }
100 };
101 
102 void
104 {
105  if (m_nb.empty ())
106  return;
107 
108  CloseNeighbor pred;
109  if (!m_handleLinkFailure.IsNull ())
110  {
111  for (std::vector<Neighbor>::iterator j = m_nb.begin (); j != m_nb.end (); ++j)
112  {
113  if (pred (*j))
114  {
115  NS_LOG_LOGIC ("Close link to " << j->m_neighborAddress);
116  m_handleLinkFailure (j->m_neighborAddress);
117  }
118  }
119  }
120  m_nb.erase (std::remove_if (m_nb.begin (), m_nb.end (), pred), m_nb.end ());
121  m_ntimer.Cancel ();
122  m_ntimer.Schedule ();
123 }
124 
125 void
127 {
128  m_ntimer.Cancel ();
129  m_ntimer.Schedule ();
130 }
131 
132 void
134 {
135  m_arp.push_back (a);
136 }
137 
138 void
140 {
141  m_arp.erase (std::remove (m_arp.begin (), m_arp.end (), a), m_arp.end ());
142 }
143 
146 {
147  Mac48Address hwaddr;
148  for (std::vector<Ptr<ArpCache> >::const_iterator i = m_arp.begin ();
149  i != m_arp.end (); ++i)
150  {
151  ArpCache::Entry * entry = (*i)->Lookup (addr);
152  if (entry != 0 && (entry->IsAlive () || entry->IsPermanent ()) && !entry->IsExpired ())
153  {
154  hwaddr = Mac48Address::ConvertFrom (entry->GetMacAddress ());
155  break;
156  }
157  }
158  return hwaddr;
159 }
160 
161 void
163 {
164  Mac48Address addr = hdr.GetAddr1 ();
165 
166  for (std::vector<Neighbor>::iterator i = m_nb.begin (); i != m_nb.end (); ++i)
167  {
168  if (i->m_hardwareAddress == addr)
169  i->close = true;
170  }
171  Purge ();
172 }
173 }
174 }
175 
Callback< void, WifiMacHeader const & > m_txErrorCallback
TX error callback.
Definition: aodv-neighbor.h:97
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Timer m_ntimer
Timer for neighbor's list. Schedule Purge().
Definition: aodv-neighbor.h:99
A simple Timer class.
Definition: timer.h:73
bool IsPermanent(void)
Definition: arp-cache.cc:387
void DelArpCache(Ptr< ArpCache >)
Don't use given ARP cache any more (interface is down)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Neighbors(Time delay)
c-tor
void Purge()
Remove all expired entries.
std::vector< Ptr< ArpCache > > m_arp
list of ARP cached to be used for layer 2 notifications processing
bool IsAlive(void)
Definition: arp-cache.cc:375
#define max(a, b)
Definition: 80211b.c:45
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void SetFunction(FN fn)
Definition: timer.h:309
bool IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
bool IsExpired(void) const
Definition: arp-cache.cc:497
void AddArpCache(Ptr< ArpCache >)
Add ARP cache to be used to allow layer 2 notifications processing.
void SetDelay(const Time &delay)
Definition: timer.cc:75
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Address GetMacAddress(void) const
Definition: arp-cache.cc:454
static Mac48Address ConvertFrom(const Address &address)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::vector< Neighbor > m_nb
vector of entries
bool operator()(const Neighbors::Neighbor &nb) const
an EUI-48 address
Definition: mac48-address.h:43
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:186
Neighbor description.
Definition: aodv-neighbor.h:55
void Update(Ipv4Address addr, Time expire)
Update expire time for entry with address addr, if it exists, else add new entry. ...
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
Callback< void, Ipv4Address > m_handleLinkFailure
link failure callback
Definition: aodv-neighbor.h:95
Time GetExpireTime(Ipv4Address addr)
Return expire time for neighbor node with address addr, if exists, else return 0. ...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void ProcessTxError(WifiMacHeader const &)
Process layer 2 TX error notification.
Mac48Address LookupMacAddress(Ipv4Address)
Find MAC address by IP using list of ARP caches.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
Mac48Address GetAddr1(void) const
Return the address in the Address 1 field.
Implements the IEEE 802.11 MAC header.
void ScheduleTimer()
Schedule m_ntimer.