A Discrete-Event Network Simulator
API
 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 {
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  return true;
56  }
57  return false;
58 }
59 
60 Time
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
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 
93 {
94  bool operator() (const Neighbors::Neighbor & nb) const
95  {
96  return ((nb.m_expireTime < Simulator::Now ()) || nb.close);
97  }
98 };
99 
100 void
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
125 {
126  m_ntimer.Cancel ();
127  m_ntimer.Schedule ();
128 }
129 
130 void
132 {
133  m_arp.push_back (a);
134 }
135 
136 void
138 {
139  m_arp.erase (std::remove (m_arp.begin (), m_arp.end (), a), m_arp.end ());
140 }
141 
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
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 
Callback< void, WifiMacHeader const & > m_txErrorCallback
TX error callback.
Definition: aodv-neighbor.h:97
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Timer m_ntimer
Timer for neighbor's list. Schedule Purge().
Definition: aodv-neighbor.h:99
NS_LOG_COMPONENT_DEFINE("AodvNeighbors")
a simple Timer class
Definition: timer.h:45
void DelArpCache(Ptr< ArpCache >)
Don't use given ARP cache any more (interface is down)
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:286
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:152
void SetFunction(FN fn)
Definition: timer.h:254
bool IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
bool IsExpired(void) const
Definition: arp-cache.cc:383
void AddArpCache(Ptr< ArpCache >)
Add ARP cache to be used to allow layer 2 notifications processing.
void SetDelay(const Time &delay)
Definition: timer.cc:69
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Address GetMacAddress(void) const
Definition: arp-cache.cc:347
static Mac48Address ConvertFrom(const Address &address)
std::vector< Neighbor > m_nb
vector of entries
bool operator()(const Neighbors::Neighbor &nb) const
an EUI-48 address
Definition: mac48-address.h:41
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:160
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 time".
Definition: simulator.cc:180
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:38
void ProcessTxError(WifiMacHeader const &)
Process layer 2 TX error notification.
Mac48Address LookupMacAddress(Ipv4Address)
Find MAC address by IP using list of ARP caches.
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:103
Mac48Address GetAddr1(void) const
Return the address in the Address 1 field.
Implements the IEEE 802.11 MAC header.
void ScheduleTimer()
Schedule m_ntimer.