41 #include "ns3/simulator.h"    42 #include "ns3/ipv4-route.h"    43 #include "ns3/socket.h"    45 #include "ns3/address-utils.h"    46 #include "ns3/packet.h"    47 #include "ns3/wifi-mac-header.h"   104 typedef std::list<DsrRouteCacheEntry>::value_type 
route_pair;
   107   : m_ackTimer (
Timer::CANCEL_ON_DESTROY),
   112     m_blackListState (false),
   141     .SetGroupName (
"Dsr")
   149     m_maxEntriesEachDst (3),
   150     m_isLinkCache (false),
   151     m_ntimer (
Timer::CANCEL_ON_DESTROY),
   174   rtVector.pop_back ();
   181   std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::const_iterator i =
   185       NS_LOG_LOGIC (
"Failed to find the route entry for the destination " << dst);
   190       std::list<DsrRouteCacheEntry> rtVector = i->second;
   193       rtVector.pop_front ();
   194       rtVector.push_back (successEntry);
   200       std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator, 
bool> result =
   202       return result.second;
   220           NS_LOG_LOGIC (
"Route to " << 
id << 
" not found; m_sortedRoutes is empty");
   223       std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::const_iterator i = 
m_sortedRoutes.find (
id);
   227           for (std::map<
Ipv4Address, std::list<DsrRouteCacheEntry> >::const_iterator j =
   230               std::list<DsrRouteCacheEntry> rtVector = j->second; 
   234               for (std::list<DsrRouteCacheEntry>::const_iterator k = rtVector.begin (); k != rtVector.end (); ++k)
   244                           changeVector.push_back (*l);
   248                           changeVector.push_back (*l);
   256                   if ((changeVector.size () < 
routeVector.size ())  && (changeVector.size () > 1))
   264                       std::list<DsrRouteCacheEntry> newVector;
   265                       newVector.push_back (changeEntry);
   268                       NS_LOG_INFO (
"We have a sub-route to " << 
id << 
" add it in route cache");
   273       NS_LOG_INFO (
"Here we check the route cache again after updated the sub routes");
   274       std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::const_iterator m = 
m_sortedRoutes.find (
id);
   283       std::list<DsrRouteCacheEntry> rtVector = m->second;
   284       rt = rtVector.front ();  
   285       NS_LOG_LOGIC (
"Route to " << 
id << 
" with route size " << rtVector.size ());
   294   if (type == std::string (
"LinkCache"))
   298   else if (type == std::string (
"PathCache"))
   324   std::map<Ipv4Address, uint32_t> d;
   326   std::map<Ipv4Address, Ipv4Address> pre;
   329       if (i->second.find (source) != i->second.end ())
   331           d[i->first] = i->second[source];
   332           pre[i->first] = source;
   345   std::map<Ipv4Address, bool> s;
   348   for (uint32_t i = 0; i < 
m_netGraph.size (); i++)
   351       for (std::map<Ipv4Address,uint32_t>::const_iterator j = d.begin (); j != d.end (); ++j)
   354           if (s.find (ip) == s.end ())
   359               if (j->second <= temp)
   369           for (std::map<Ipv4Address, uint32_t>::const_iterator k = 
m_netGraph[tempip].begin (); k != 
m_netGraph[tempip].end (); ++k)
   371               if (s.find (k->first) == s.end () && d[k->first] > d[tempip] + k->second)
   373                   d[k->first] = d[tempip] + k->second;
   374                   pre[k->first] = tempip;
   382               else if (d[k->first] == d[tempip] + k->second)
   384                   std::map<Link, DsrLinkStab>::iterator oldlink = 
m_linkCache.find (
Link (k->first, pre[k->first]));
   385                   std::map<Link, DsrLinkStab>::iterator newlink = 
m_linkCache.find (
Link (k->first, tempip));
   388                       if (oldlink->second.GetLinkStability () < newlink->second.GetLinkStability ())
   390                           NS_LOG_INFO (
"Select the link with longest expected lifetime");
   391                           d[k->first] = d[tempip] + k->second;
   392                           pre[k->first] = tempip;
   405   for (std::map<Ipv4Address, Ipv4Address>::iterator i = pre.begin (); i != pre.end (); ++i)
   411       if (!i->second.IsBroadcast () && iptemp != source)
   413           while (iptemp != source)
   415               route.push_back (iptemp);
   416               iptemp = pre[iptemp];
   418           route.push_back (source);
   421           for (DsrRouteCacheEntry::IP_VECTOR::reverse_iterator j = route.rbegin (); j != route.rend (); ++j)
   423               reverseroute.push_back (*j);
   438   std::map<Ipv4Address, DsrRouteCacheEntry::IP_VECTOR>::const_iterator i = 
m_bestRoutesTable_link.find (
id);
   446       if (i->second.size () < 2)
   456       NS_LOG_INFO (
"Route to " << 
id << 
" found with the length " << i->second.size ());
   458       std::vector<Ipv4Address> path = rt.
GetVector ();
   470       NS_LOG_DEBUG (
"The link stability " << i->second.GetLinkStability ().GetSeconds ());
   471       std::map<Link, DsrLinkStab>::iterator itmp = i;
   472       if (i->second.GetLinkStability () <= 
Seconds (0))
   485       NS_LOG_DEBUG (
"The node stability " << i->second.GetNodeStability ().GetSeconds ());
   486       std::map<Ipv4Address, DsrNodeStab>::iterator itmp = i;
   487       if (i->second.GetNodeStability () <= 
Seconds (0))
   509       m_netGraph[i->first.m_low][i->first.m_high] = weight;
   510       m_netGraph[i->first.m_high][i->first.m_low] = weight;
   518   std::map<Ipv4Address, DsrNodeStab>::const_iterator i = 
m_nodeCache.find (node);
   529       NS_LOG_INFO (
"The node stability " << i->second.GetNodeStability ().GetSeconds ());
   542   std::map<Ipv4Address, DsrNodeStab>::const_iterator i = 
m_nodeCache.find (node);
   552       NS_LOG_INFO (
"The stability here " << i->second.GetNodeStability ().GetSeconds ());
   568   for (uint32_t i = 0; i < nodelist.size () - 1; i++)
   581       Link link (nodelist[i], nodelist[i + 1]);         
   621   for (DsrRouteCacheEntry::IP_VECTOR::iterator i = rt.begin (); i != rt.end () - 1; ++i)
   623       Link link (*i, *(i + 1));
   639   for (DsrRouteCacheEntry::IP_VECTOR::iterator i = rt.begin (); i != rt.end (); ++i)
   650               NS_LOG_INFO (
"The node stability has already been increased");
   661   std::list<DsrRouteCacheEntry> rtVector;   
   663   std::vector<Ipv4Address> route = rt.
GetVector ();
   666   std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::const_iterator i =
   671       rtVector.push_back (rt);
   676       std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator, 
bool> result =
   678       return result.second;
   682       rtVector = i->second;
   683       NS_LOG_DEBUG (
"The existing route size " << rtVector.size () << 
" for destination address " << dst);
   694           NS_LOG_DEBUG (
"Find same vector, the FindSameRoute function will update the route expire time");
   702               rtVector.push_back (rt);
   706               NS_LOG_DEBUG (
"The first time" << rtVector.front ().GetExpireTime ().GetSeconds () << 
" The second time "   707                                              << rtVector.back ().GetExpireTime ().GetSeconds ());
   708               NS_LOG_DEBUG (
"The first hop" << rtVector.front ().GetVector ().size () << 
" The second hop "   709                                             << rtVector.back ().GetVector ().size ());
   714               std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator, 
bool> result =
   716               return result.second;
   720               NS_LOG_INFO (
"The newly found route is already expired");
   730   for (std::list<DsrRouteCacheEntry>::iterator i = rtVector.begin (); i != rtVector.end (); ++i)
   738           NS_LOG_DEBUG (
"Found same routes in the route cache with the vector size "   741                                                      << 
" the original expire time " << i->GetExpireTime ().GetSeconds ());
   751           std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator, 
bool> result =
   753           return result.second;
   766       NS_LOG_LOGIC (
"Route deletion to " << dst << 
" successful");
   769   NS_LOG_LOGIC (
"Route deletion to " << dst << 
" not successful");
   785       Link link1 (errorSrc, unreachNode);
   786       Link link2 (unreachNode, errorSrc);
   795       std::map<Ipv4Address, DsrNodeStab>::iterator i = 
m_nodeCache.find (errorSrc);
   798           NS_LOG_LOGIC (
"Update the node stability unsuccessfully");
   807           NS_LOG_LOGIC (
"Update the node stability unsuccessfully");
   830       for (std::map<
Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator j =
   833           std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator jtmp = j;
   835           std::list<DsrRouteCacheEntry> rtVector = j->second;
   839           for (std::list<DsrRouteCacheEntry>::iterator k = rtVector.begin (); k != rtVector.end (); )
   851                       changeVector.push_back (*i);
   855                       if (*(i + 1) == unreachNode)
   857                           changeVector.push_back (*i);
   862                           changeVector.push_back (*i);
   871                   NS_LOG_DEBUG (
"The route does not contain the broken link");
   874               else if ((changeVector.size () < 
routeVector.size ()) && (changeVector.size () > 1))
   879                       Time expire = k->GetExpireTime ();
   883                       k = rtVector.erase (k);
   887                       NS_LOG_DEBUG (
"The destination of the newly formed route " << destination << 
" and the size of the route " << changeVector.size ());
   890                       rtVector.push_back (changeEntry);  
   891                       NS_LOG_DEBUG (
"We have a sub-route to " << destination);
   898                       k = rtVector.erase (k);
   903                   NS_LOG_LOGIC (
"Cut route unsuccessful and erase the route");
   907                   k = rtVector.erase (k);
   915           if (rtVector.size ())
   945       for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
   956   for (std::list<DsrRouteCacheEntry>::iterator i = route.begin (); i != route.end (); i++)
   958       std::vector<Ipv4Address> path = i->GetVector ();
   974   for (std::map<
Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator i =
   978       std::map<Ipv4Address, std::list<DsrRouteCacheEntry> >::iterator itmp = i;
   983       std::list<DsrRouteCacheEntry> rtVector = i->second;
   984       NS_LOG_DEBUG (
"The route vector size of 1 " << dst << 
" " << rtVector.size ());
   985       if (rtVector.size ())
   987           for (std::list<DsrRouteCacheEntry>::iterator j = rtVector.begin (); j != rtVector.end (); )
   989               NS_LOG_DEBUG (
"The expire time of every entry with expire time " << j->GetExpireTime ());
   993               if (j->GetExpireTime () <= 
Seconds (0))
   998                   NS_LOG_DEBUG (
"Erase the expired route for " << dst << 
" with expire time " << j->GetExpireTime ());
   999                   j = rtVector.erase (j);
  1006           NS_LOG_DEBUG (
"The route vector size of 2 " << dst << 
" " << rtVector.size ());
  1007           if (rtVector.size ())
  1036   os << 
"\nDSR Route Cache\n"  1037      << 
"Destination\tGateway\t\tInterface\tFlag\tExpire\tHops\n";
  1038   for (std::list<DsrRouteCacheEntry>::const_iterator i =
  1054   std::map<Ipv4Address, uint16_t>::const_iterator i =
  1058       NS_LOG_LOGIC (
"No Ack id for " << nextHop << 
" found and use id 1 for the first network ack id");
  1065       NS_LOG_LOGIC (
"Ack id for " << nextHop << 
" found in the cache has value " << ackId);
  1087   for (std::vector<Neighbor>::const_iterator i = 
m_nb.begin ();
  1088        i != 
m_nb.end (); ++i)
  1090       if (i->m_neighborAddress == addr)
  1103   for (std::vector<Neighbor>::const_iterator i = 
m_nb.begin (); i
  1104        != 
m_nb.end (); ++i)
  1106       if (i->m_neighborAddress == addr)
  1118   for (std::vector<Neighbor>::iterator i = 
m_nb.begin (); i != 
m_nb.end (); ++i)
  1120       for (std::vector<Ipv4Address>::iterator j = nodeList.begin (); j != nodeList.end (); ++j)
  1122           if (i->m_neighborAddress == (*j))
  1138   m_nb.push_back (neighbor);
  1145   NS_LOG_LOGIC (
"Add neighbor number " << nodeList.size ());
  1146   for (std::vector<Ipv4Address>::iterator j = nodeList.begin (); j != nodeList.end (); )
  1149       if (addr == ownAddress)
  1151           j = nodeList.erase (j);
  1152           NS_LOG_DEBUG (
"The node list size " << nodeList.size ());
  1159       m_nb.push_back (neighbor);
  1190       for (std::vector<Neighbor>::iterator j = 
m_nb.begin (); j != 
m_nb.end (); ++j)
  1194               NS_LOG_LOGIC (
"Close link to " << j->m_neighborAddress);
  1200   m_nb.erase (std::remove_if (
m_nb.begin (), 
m_nb.end (), pred), 
m_nb.end ());
  1215   m_arp.push_back (a);
  1229        i != 
m_arp.end (); ++i)
  1246   for (std::vector<Neighbor>::iterator i = 
m_nb.begin (); i != 
m_nb.end (); ++i)
  1248       if (i->m_hardwareAddress == addr)
 Time m_minLifeTime
minimum lifetime 
void ProcessTxError(WifiMacHeader const &hdr)
Process layer 2 TX error notification. 
Simulation virtual time values and global simulation resolution. 
DsrRouteCacheEntry class for entries in the route cache. 
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
DSR route request queue Since DSR is an on demand routing we queue requests while looking for route...
void SetDestination(Ipv4Address d)
Set destination address. 
uint16_t CheckUniqueAckId(Ipv4Address nextHop)
Check for duplicate ids and save new entries if the id is not present in the table. 
void AddNeighbor(std::vector< Ipv4Address > nodeList, Ipv4Address ownAddress, Time expire)
Add to the neighbor list. 
Control the scheduling of simulation events. 
Time m_expireTime
route expire time 
bool IsBroadcast(void) const
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system. 
void SetNodeStability(Time nodeStab)
Set node stability. 
std::vector< Ptr< ArpCache > > m_arp
list of ARP cached to be used for layer 2 notifications processing 
void UpdateNetGraph()
Update the Net Graph for the link and node cache has changed. 
uint32_t m_maxEntriesEachDst
number of entries for each destination 
void DeleteAllRoutesIncludeLink(Ipv4Address errorSrc, Ipv4Address unreachNode, Ipv4Address node)
Delete all the routes which includes the link from next hop address that has just been notified as un...
void Invalidate(Time badLinkLifetime)
Mark entry as "down" (i.e. 
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit. 
DsrNodeStab(Time nodeStab=Simulator::Now())
Constructor. 
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name. 
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit. 
std::vector< Neighbor > m_nb
vector of entries 
void PrintRouteVector(std::list< DsrRouteCacheEntry > route)
Print all the route vector elements from the route list. 
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO. 
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time. 
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function. 
Timer m_ntimer
Timer for neighbor's list. Schedule Purge(). 
routeEntryVector m_routeEntryVector
Define the route vector. 
void RebuildBestRouteTable(Ipv4Address source)
Rebuild the best route table. 
uint16_t GetAckSize()
Get the ack table size. 
void AddArpCache(Ptr< ArpCache >)
Add ARP cache to be used to allow layer 2 notifications processing. 
bool IsLinkCache()
is link cached 
Time RouteCacheTimeout
The maximum period of time that dsr is allowed to for an unused route. 
bool UpdateRouteEntry(Ipv4Address dst)
Update route cache entry if it has been recently used and successfully delivered the data packet...
Ipv4Address m_high
high IP address 
bool CompareRoutesBoth(const DsrRouteCacheEntry &a, const DsrRouteCacheEntry &b)
Time m_expire
Expire time for queue entry. 
Time m_delay
This timeout deals with the passive ack. 
bool m_isLinkCache
Check if the route is using path cache or link cache. 
Time m_initStability
initial stability 
std::map< Ipv4Address, DsrNodeStab > m_nodeCache
The data structure to store node info. 
IP_VECTOR GetVector() const
Get the IP vector. 
std::list< DsrRouteCacheEntry::IP_VECTOR > routeVector
Define the vector of route entries. 
Address GetMacAddress(void) const
static TypeId GetTypeId()
Get the type ID. 
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments. 
Ipv4Address GetDestination() const
Get destination address. 
std::map< Link, DsrLinkStab > m_linkCache
The data structure to store link info. 
bool CompareRoutesHops(const DsrRouteCacheEntry &a, const DsrRouteCacheEntry &b)
bool IncStability(Ipv4Address node)
increase the stability of the node 
void ScheduleTimer()
Schedule m_ntimer. 
Time GetExpireTime(Ipv4Address addr)
Return expire time for neighbor node with address addr, if exists, else return 0. ...
The route cache structure. 
DsrLinkStab(Time linkStab=Simulator::Now())
Constructor. 
uint32_t m_stabilityDecrFactor
stability decrease factor 
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
bool IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor. 
bool LookupRoute_Link(Ipv4Address id, DsrRouteCacheEntry &rt)
used by LookupRoute when LinkCache 
Mac48Address LookupMacAddress(Ipv4Address addr)
Find MAC address by IP using list of ARP caches. 
void Print() const
Print function. 
std::vector< Ipv4Address > IP_VECTOR
Define the vector to hold Ip address. 
std::map< Ipv4Address, std::map< Ipv4Address, uint32_t > > m_netGraph
Current network graph state for this node, double is weight, which is calculated by the node informat...
Time m_useExtends
use extend 
void SetDelay(const Time &delay)
bool LookupRoute(Ipv4Address id, DsrRouteCacheEntry &rt)
Lookup route cache entry with destination address dst. 
Time GetExpireTime() const
Get expire time. 
std::map< Ipv4Address, uint16_t > m_ackIdCache
The id cache to ensure all the ids are unique. 
static Mac48Address ConvertFrom(const Address &address)
DsrLinkStab class (DSR link stability) 
bool AddRoute_Link(DsrRouteCacheEntry::IP_VECTOR nodelist, Ipv4Address node)
dd route link to cache 
Every class exported by the ns3 library is enclosed in the ns3 namespace. 
void SetLinkStability(Time linkStab)
set the link stability 
bool FindSameRoute(DsrRouteCacheEntry &rt, std::list< DsrRouteCacheEntry > &rtVector)
Find the same route in the route cache. 
void Purge()
Delete all outdated entries and invalidate valid entry if Lifetime is expired. 
Ipv4Address m_dst
The destination Ip address. 
bool DeleteRoute(Ipv4Address dst)
Delete the route with certain destination address. 
bool IsExpired(void) const
void Print(std::ostream &os) const
Print necessary fields. 
void UpdateNeighbor(std::vector< Ipv4Address > nodeList, Time expire)
Update expire time for entry with address addr, if it exists, else add new entry. ...
Time GetLinkStability() const
get the link stability 
virtual ~DsrLinkStab()
Destructor. 
A record that that holds information about an ArpCache entry. 
static Time Now(void)
Return the current simulation virtual time. 
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
void PurgeLinkNode()
Purge from the cache if the stability time expired. 
Structure to manage neighbor state. 
std::map< Ipv4Address, DsrRouteCacheEntry::IP_VECTOR > m_bestRoutesTable_link
for link route cache 
void RemoveLastEntry(std::list< DsrRouteCacheEntry > &rtVector)
Remove the aged route cache entries when the route cache is full. 
std::map< Ipv4Address, routeEntryVector > m_sortedRoutes
Map the ipv4Address to route entry vector. 
Ipv4 addresses are stored in host order in this class. 
virtual ~DsrRouteCacheEntry()
Callback< void, WifiMacHeader const & > m_txErrorCallback
TX error callback. 
void DelArpCache(Ptr< ArpCache >)
Don't use the provided ARP cache any more (interface is down) 
#define MAXWEIGHT
The link cache to update all the link status, bi-link is two link for link is a struct when the weigh...
void SetVector(IP_VECTOR v)
Sets the IP vector. 
bool DecStability(Ipv4Address node)
decrease the stability of the node 
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG. 
void PurgeMac()
Remove all expired mac entries. 
Time Seconds(double value)
Construct a Time in the indicated unit. 
void Cancel(void)
Cancel the currently-running event if there is one. 
void Print(std::ostream &os)
Print route cache. 
void SetExpireTime(Time exp)
Set expire time. 
uint32_t m_stabilityIncrFactor
stability increase factor 
void SetCacheType(std::string type)
Dijsktra algorithm to get the best route from m_netGraph and update the m_bestRoutesTable_link when c...
bool AddRoute(DsrRouteCacheEntry &rt)
Add route cache entry if it doesn't yet exist in route cache. 
Time Now(void)
create an ns3::Time instance which contains the current simulation time. 
DsrRouteCacheEntry(IP_VECTOR const &ip=IP_VECTOR(), Ipv4Address dst=Ipv4Address(), Time exp=Simulator::Now())
Constructor. 
A base class which provides memory management and object aggregation. 
bool operator()(const DsrRouteCache::Neighbor &nb) const
Check if the entry is expired. 
void Print() const
Print function. 
Callback< void, Ipv4Address, uint8_t > m_handleLinkFailure
The following code handles link-layer acks. 
void UseExtends(DsrRouteCacheEntry::IP_VECTOR rt)
When a link from the Route Cache is used in routing a packet originated or salvaged by that node...
bool close
is route active 
DsrNodeStab class (DSR node stability) 
std::list< DsrRouteCacheEntry >::value_type route_pair
Ipv4Address m_low
low IP address 
a unique identifier for an interface. 
TypeId SetParent(TypeId tid)
Set the parent TypeId. 
void PrintVector(std::vector< Ipv4Address > &vec)
Print the route vector elements. 
uint8_t m_reqCount
Number of route requests. 
bool m_subRoute
Check if save the sub route entries or not. 
bool CompareRoutesExpire(const DsrRouteCacheEntry &a, const DsrRouteCacheEntry &b)