dsr: Remove redundant flow controls, and rework if-else to avoid duplicate code and reduce indentation

This commit is contained in:
Eduardo Almeida
2023-06-01 19:40:03 +01:00
parent b7a4eaaac8
commit f0996725dd
4 changed files with 243 additions and 288 deletions

View File

@@ -186,23 +186,22 @@ DsrOptions::SearchNextHop(Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec
nextHop = vec[1];
return nextHop;
}
else
if (ipv4Address == vec.back())
{
if (ipv4Address == vec.back())
NS_LOG_DEBUG("We have reached to the final destination " << ipv4Address << " "
<< vec.back());
return ipv4Address;
}
for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
{
if (ipv4Address == (*i))
{
NS_LOG_DEBUG("We have reached to the final destination " << ipv4Address << " "
<< vec.back());
return ipv4Address;
}
for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
{
if (ipv4Address == (*i))
{
nextHop = *(++i);
return nextHop;
}
nextHop = *(++i);
return nextHop;
}
}
NS_LOG_DEBUG("next hop address not found, route corrupted");
Ipv4Address none = "0.0.0.0";
return none;
@@ -219,17 +218,16 @@ DsrOptions::ReverseSearchNextHop(Ipv4Address ipv4Address, std::vector<Ipv4Addres
nextHop = vec[0];
return nextHop;
}
else
for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin(); ri != vec.rend(); ++ri)
{
for (std::vector<Ipv4Address>::reverse_iterator ri = vec.rbegin(); ri != vec.rend(); ++ri)
if (ipv4Address == (*ri))
{
if (ipv4Address == (*ri))
{
nextHop = *(++ri);
return nextHop;
}
nextHop = *(++ri);
return nextHop;
}
}
NS_LOG_DEBUG("next hop address not found, route corrupted");
Ipv4Address none = "0.0.0.0";
return none;
@@ -288,10 +286,6 @@ DsrOptions::IfDuplicates(std::vector<Ipv4Address>& vec, std::vector<Ipv4Address>
{
return true;
}
else
{
continue;
}
}
}
return false;
@@ -307,10 +301,6 @@ DsrOptions::CheckDuplicates(Ipv4Address ipv4Address, std::vector<Ipv4Address>& v
{
return true;
}
else
{
continue;
}
}
return false;
}
@@ -331,31 +321,22 @@ DsrOptions::RemoveDuplicates(std::vector<Ipv4Address>& vec)
vec.push_back(*i);
continue;
}
else
for (std::vector<Ipv4Address>::iterator j = vec.begin(); j != vec.end(); ++j)
{
for (std::vector<Ipv4Address>::iterator j = vec.begin(); j != vec.end(); ++j)
if ((*i) == (*j))
{
if ((*i) == (*j))
if ((j + 1) != vec.end())
{
if ((j + 1) != vec.end())
{
vec.erase(j + 1, vec.end()); // Automatic shorten the route
break;
}
else
{
break;
}
}
else if (j == (vec.end() - 1))
{
vec.push_back(*i);
break;
}
else
{
continue;
vec.erase(j + 1, vec.end()); // Automatic shorten the route
}
break;
}
else if (j == (vec.end() - 1))
{
vec.push_back(*i);
break;
}
}
}

View File

@@ -219,89 +219,84 @@ DsrRouteCache::LookupRoute(Ipv4Address id, DsrRouteCacheEntry& rt)
{
return LookupRoute_Link(id, rt);
}
else
{
Purge(); // Purge first to remove expired entries
if (m_sortedRoutes.empty())
{
NS_LOG_LOGIC("Route to " << id << " not found; m_sortedRoutes is empty");
return false;
}
std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator i =
m_sortedRoutes.find(id);
if (i == m_sortedRoutes.end())
{
NS_LOG_LOGIC("No Direct Route to " << id << " found");
for (std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator j =
m_sortedRoutes.begin();
j != m_sortedRoutes.end();
++j)
{
std::list<DsrRouteCacheEntry> rtVector =
j->second; // The route cache vector linked with destination address
/*
* Loop through the possibly multiple routes within the route vector
*/
for (std::list<DsrRouteCacheEntry>::const_iterator k = rtVector.begin();
k != rtVector.end();
++k)
{
// return the first route in the route vector
DsrRouteCacheEntry::IP_VECTOR routeVector = k->GetVector();
DsrRouteCacheEntry::IP_VECTOR changeVector;
for (DsrRouteCacheEntry::IP_VECTOR::iterator l = routeVector.begin();
l != routeVector.end();
++l)
Purge(); // Purge first to remove expired entries
if (m_sortedRoutes.empty())
{
NS_LOG_LOGIC("Route to " << id << " not found; m_sortedRoutes is empty");
return false;
}
std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator i =
m_sortedRoutes.find(id);
if (i == m_sortedRoutes.end())
{
NS_LOG_LOGIC("No Direct Route to " << id << " found");
for (std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator j =
m_sortedRoutes.begin();
j != m_sortedRoutes.end();
++j)
{
std::list<DsrRouteCacheEntry> rtVector =
j->second; // The route cache vector linked with destination address
/*
* Loop through the possibly multiple routes within the route vector
*/
for (std::list<DsrRouteCacheEntry>::const_iterator k = rtVector.begin();
k != rtVector.end();
++k)
{
// return the first route in the route vector
DsrRouteCacheEntry::IP_VECTOR routeVector = k->GetVector();
DsrRouteCacheEntry::IP_VECTOR changeVector;
for (DsrRouteCacheEntry::IP_VECTOR::iterator l = routeVector.begin();
l != routeVector.end();
++l)
{
changeVector.push_back(*l);
if (*l == id)
{
if (*l != id)
{
changeVector.push_back(*l);
}
else
{
changeVector.push_back(*l);
break;
}
}
/*
* When the changed vector is smaller in size and larger than 1, which means we
* have found a route with the destination address we are looking for
*/
if ((changeVector.size() < routeVector.size()) && (changeVector.size() > 1))
{
DsrRouteCacheEntry changeEntry; // Create the route entry
changeEntry.SetVector(changeVector);
changeEntry.SetDestination(id);
// Use the expire time from original route entry
changeEntry.SetExpireTime(k->GetExpireTime());
// We need to add new route entry here
std::list<DsrRouteCacheEntry> newVector;
newVector.push_back(changeEntry);
newVector.sort(CompareRoutesExpire); // sort the route vector first
m_sortedRoutes[id] =
newVector; // Only get the first sub route and add it in route cache
NS_LOG_INFO("We have a sub-route to " << id << " add it in route cache");
break;
}
}
/*
* When the changed vector is smaller in size and larger than 1, which means we
* have found a route with the destination address we are looking for
*/
if ((changeVector.size() < routeVector.size()) && (changeVector.size() > 1))
{
DsrRouteCacheEntry changeEntry; // Create the route entry
changeEntry.SetVector(changeVector);
changeEntry.SetDestination(id);
// Use the expire time from original route entry
changeEntry.SetExpireTime(k->GetExpireTime());
// We need to add new route entry here
std::list<DsrRouteCacheEntry> newVector;
newVector.push_back(changeEntry);
newVector.sort(CompareRoutesExpire); // sort the route vector first
m_sortedRoutes[id] =
newVector; // Only get the first sub route and add it in route cache
NS_LOG_INFO("We have a sub-route to " << id << " add it in route cache");
}
}
}
NS_LOG_INFO("Here we check the route cache again after updated the sub routes");
std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator m =
m_sortedRoutes.find(id);
if (m == m_sortedRoutes.end())
{
NS_LOG_LOGIC("No updated route till last time");
return false;
}
/*
* We have a direct route to the destination address
*/
std::list<DsrRouteCacheEntry> rtVector = m->second;
rt = rtVector.front(); // use the first entry in the route vector
NS_LOG_LOGIC("Route to " << id << " with route size " << rtVector.size());
return true;
}
NS_LOG_INFO("Here we check the route cache again after updated the sub routes");
std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator m =
m_sortedRoutes.find(id);
if (m == m_sortedRoutes.end())
{
NS_LOG_LOGIC("No updated route till last time");
return false;
}
/*
* We have a direct route to the destination address
*/
std::list<DsrRouteCacheEntry> rtVector = m->second;
rt = rtVector.front(); // use the first entry in the route vector
NS_LOG_LOGIC("Route to " << id << " with route size " << rtVector.size());
return true;
}
void
@@ -468,24 +463,22 @@ DsrRouteCache::LookupRoute_Link(Ipv4Address id, DsrRouteCacheEntry& rt)
NS_LOG_INFO("No route find to " << id);
return false;
}
else
{
if (i->second.size() < 2)
{
NS_LOG_LOGIC("Route to " << id << " error");
return false;
}
DsrRouteCacheEntry newEntry; // Create the route entry
newEntry.SetVector(i->second);
newEntry.SetDestination(id);
newEntry.SetExpireTime(RouteCacheTimeout);
NS_LOG_INFO("Route to " << id << " found with the length " << i->second.size());
rt = newEntry;
std::vector<Ipv4Address> path = rt.GetVector();
PrintVector(path);
return true;
if (i->second.size() < 2)
{
NS_LOG_LOGIC("Route to " << id << " error");
return false;
}
DsrRouteCacheEntry newEntry; // Create the route entry
newEntry.SetVector(i->second);
newEntry.SetDestination(id);
newEntry.SetExpireTime(RouteCacheTimeout);
NS_LOG_INFO("Route to " << id << " found with the length " << i->second.size());
rt = newEntry;
std::vector<Ipv4Address> path = rt.GetVector();
PrintVector(path);
return true;
}
void
@@ -709,55 +702,54 @@ DsrRouteCache::AddRoute(DsrRouteCacheEntry& rt)
m_sortedRoutes.insert(std::make_pair(dst, rtVector));
return result.second;
}
rtVector = i->second;
NS_LOG_DEBUG("The existing route size " << rtVector.size() << " for destination address "
<< dst);
/**
* \brief Drop the most aged packet when buffer reaches to max
*/
if (rtVector.size() >= m_maxEntriesEachDst)
{
RemoveLastEntry(rtVector); // Drop the last entry for the sorted route cache, the route
// has already been sorted
}
if (FindSameRoute(rt, rtVector))
{
NS_LOG_DEBUG(
"Find same vector, the FindSameRoute function will update the route expire time");
return true;
}
else
{
rtVector = i->second;
NS_LOG_DEBUG("The existing route size " << rtVector.size() << " for destination address "
<< dst);
/**
* \brief Drop the most aged packet when buffer reaches to max
*/
if (rtVector.size() >= m_maxEntriesEachDst)
// Check if the expire time for the new route has expired or not
if (rt.GetExpireTime() > Time(0))
{
RemoveLastEntry(rtVector); // Drop the last entry for the sorted route cache, the route
// has already been sorted
}
if (FindSameRoute(rt, rtVector))
{
NS_LOG_DEBUG(
"Find same vector, the FindSameRoute function will update the route expire time");
return true;
rtVector.push_back(rt);
// This sort function will sort the route cache entries based on the size of route
// in each of the route entries
rtVector.sort(CompareRoutesExpire);
NS_LOG_DEBUG("The first time" << rtVector.front().GetExpireTime().As(Time::S)
<< " The second time "
<< rtVector.back().GetExpireTime().As(Time::S));
NS_LOG_DEBUG("The first hop" << rtVector.front().GetVector().size()
<< " The second hop "
<< rtVector.back().GetVector().size());
m_sortedRoutes.erase(dst); // erase the route entries for dst first
/**
* Save the new route cache along with the destination address in map
*/
std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator, bool> result =
m_sortedRoutes.insert(std::make_pair(dst, rtVector));
return result.second;
}
else
{
// Check if the expire time for the new route has expired or not
if (rt.GetExpireTime() > Time(0))
{
rtVector.push_back(rt);
// This sort function will sort the route cache entries based on the size of route
// in each of the route entries
rtVector.sort(CompareRoutesExpire);
NS_LOG_DEBUG("The first time" << rtVector.front().GetExpireTime().As(Time::S)
<< " The second time "
<< rtVector.back().GetExpireTime().As(Time::S));
NS_LOG_DEBUG("The first hop" << rtVector.front().GetVector().size()
<< " The second hop "
<< rtVector.back().GetVector().size());
m_sortedRoutes.erase(dst); // erase the route entries for dst first
/**
* Save the new route cache along with the destination address in map
*/
std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator, bool>
result = m_sortedRoutes.insert(std::make_pair(dst, rtVector));
return result.second;
}
else
{
NS_LOG_INFO("The newly found route is already expired");
}
NS_LOG_INFO("The newly found route is already expired");
}
}
return false;
}
@@ -896,15 +888,12 @@ DsrRouteCache::DeleteAllRoutesIncludeLink(Ipv4Address errorSrc,
}
else
{
changeVector.push_back(*i);
if (*(i + 1) == unreachNode)
{
changeVector.push_back(*i);
break;
}
else
{
changeVector.push_back(*i);
}
}
}
/*
@@ -1108,14 +1097,12 @@ DsrRouteCache::CheckUniqueAckId(Ipv4Address nextHop)
m_ackIdCache[nextHop] = 1;
return 1;
}
else
{
uint16_t ackId = m_ackIdCache[nextHop];
NS_LOG_LOGIC("Ack id for " << nextHop << " found in the cache has value " << ackId);
ackId++;
m_ackIdCache[nextHop] = ackId;
return ackId;
}
uint16_t ackId = m_ackIdCache[nextHop];
NS_LOG_LOGIC("Ack id for " << nextHop << " found in the cache has value " << ackId);
ackId++;
m_ackIdCache[nextHop] = ackId;
return ackId;
}
uint16_t

View File

@@ -767,23 +767,22 @@ DsrRouting::SearchNextHop(Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec
nextHop = vec[1];
return nextHop;
}
else
if (ipv4Address == vec.back())
{
if (ipv4Address == vec.back())
NS_LOG_DEBUG("We have reached to the final destination " << ipv4Address << " "
<< vec.back());
return ipv4Address;
}
for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
{
if (ipv4Address == (*i))
{
NS_LOG_DEBUG("We have reached to the final destination " << ipv4Address << " "
<< vec.back());
return ipv4Address;
}
for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
{
if (ipv4Address == (*i))
{
nextHop = *(++i);
return nextHop;
}
nextHop = *(++i);
return nextHop;
}
}
NS_LOG_DEBUG("Next hop address not found");
Ipv4Address none = "0.0.0.0";
return none;
@@ -831,12 +830,10 @@ DsrRouting::GetIPfromID(uint16_t id)
NS_LOG_DEBUG("Exceed the node range");
return "0.0.0.0";
}
else
{
Ptr<Node> node = NodeList::GetNode(uint32_t(id));
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
return ipv4->GetAddress(1, 0).GetLocal();
}
Ptr<Node> node = NodeList::GetNode(uint32_t(id));
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
return ipv4->GetAddress(1, 0).GetLocal();
}
uint32_t
@@ -3033,7 +3030,6 @@ DsrRouting::SendErrorRequest(DsrOptionRerrUnreachHeader& rerr, uint8_t protocol)
SendPacketFromBuffer(sourceRoute, nextHop, protocol);
}
NS_LOG_LOGIC("Route to " << dst << " found");
return;
}
else
{
@@ -3672,34 +3668,32 @@ DsrRouting::Receive(Ptr<Packet> p, const Ipv4Header& ip, Ptr<Ipv4Interface> inco
uint8_t nextHeader = dsrRoutingHeader.GetNextHeader();
Ptr<Ipv4L3Protocol> l3proto = m_node->GetObject<Ipv4L3Protocol>();
Ptr<IpL4Protocol> nextProto = l3proto->GetProtocol(nextHeader);
if (nextProto)
{
// we need to make a copy in the unlikely event we hit the
// RX_ENDPOINT_UNREACH code path
// Here we can use the packet that has been get off whole DSR header
IpL4Protocol::RxStatus status = nextProto->Receive(copy, ip, incomingInterface);
NS_LOG_DEBUG("The receive status " << status);
switch (status)
{
case IpL4Protocol::RX_OK:
// fall through
case IpL4Protocol::RX_ENDPOINT_CLOSED:
// fall through
case IpL4Protocol::RX_CSUM_FAILED:
break;
case IpL4Protocol::RX_ENDPOINT_UNREACH:
if (ip.GetDestination().IsBroadcast() || ip.GetDestination().IsMulticast())
{
break; // Do not reply to broadcast or multicast
}
// Another case to suppress ICMP is a subnet-directed broadcast
}
return status;
}
else
if (!nextProto)
{
NS_FATAL_ERROR("Should not have 0 next protocol value");
}
// we need to make a copy in the unlikely event we hit the
// RX_ENDPOINT_UNREACH code path
// Here we can use the packet that has been get off whole DSR header
IpL4Protocol::RxStatus status = nextProto->Receive(copy, ip, incomingInterface);
NS_LOG_DEBUG("The receive status " << status);
switch (status)
{
case IpL4Protocol::RX_OK:
// fall through
case IpL4Protocol::RX_ENDPOINT_CLOSED:
// fall through
case IpL4Protocol::RX_CSUM_FAILED:
break;
case IpL4Protocol::RX_ENDPOINT_UNREACH:
if (ip.GetDestination().IsBroadcast() || ip.GetDestination().IsMulticast())
{
break; // Do not reply to broadcast or multicast
}
// Another case to suppress ICMP is a subnet-directed broadcast
}
return status;
}
else
{

View File

@@ -145,11 +145,9 @@ DsrRreqTable::GetRreqCnt(Ipv4Address dst)
NS_LOG_LOGIC("Request table entry not found");
return 0;
}
else
{
RreqTableEntry rreqTableEntry = i->second;
return rreqTableEntry.m_reqNo;
}
RreqTableEntry rreqTableEntry = i->second;
return rreqTableEntry.m_reqNo;
}
// ----------------------------------------------------------------------------------------------------------
@@ -168,25 +166,23 @@ DsrRreqTable::CheckUniqueRreqId(Ipv4Address dst)
m_rreqIdCache[dst] = 0;
return 0;
}
NS_LOG_LOGIC("Request id for " << dst << " found in the cache");
uint32_t rreqId = m_rreqIdCache[dst];
if (rreqId >= m_maxRreqId)
{
NS_LOG_DEBUG("The request id increase past the max value, " << m_maxRreqId
<< " so reset it to 0");
rreqId = 0;
m_rreqIdCache[dst] = rreqId;
}
else
{
NS_LOG_LOGIC("Request id for " << dst << " found in the cache");
uint32_t rreqId = m_rreqIdCache[dst];
if (rreqId >= m_maxRreqId)
{
NS_LOG_DEBUG("The request id increase past the max value, " << m_maxRreqId
<< " so reset it to 0");
rreqId = 0;
m_rreqIdCache[dst] = rreqId;
}
else
{
rreqId++;
m_rreqIdCache[dst] = rreqId;
}
NS_LOG_INFO("The Request id for " << dst << " is " << rreqId);
return rreqId;
rreqId++;
m_rreqIdCache[dst] = rreqId;
}
NS_LOG_INFO("The Request id for " << dst << " is " << rreqId);
return rreqId;
}
uint32_t
@@ -276,35 +272,32 @@ DsrRreqTable::FindSourceEntry(Ipv4Address src, Ipv4Address dst, uint16_t id)
m_sourceRreqMap[src] = receivedRreqEntryList;
return false;
}
else
{
NS_LOG_LOGIC("Find the request table entry for " << src
<< ", check if it is exact duplicate");
/*
* Drop the most aged packet when buffer reaches to max
*/
receivedRreqEntryList = i->second;
if (receivedRreqEntryList.size() >= m_requestIdSize)
{
receivedRreqEntryList.pop_front();
}
// We loop the receive rreq entry to find duplicate
for (std::list<DsrReceivedRreqEntry>::const_iterator j = receivedRreqEntryList.begin();
j != receivedRreqEntryList.end();
++j)
{
if (*j == rreqEntry) /// Check if we have found one duplication entry or not
{
return true;
}
}
/// if this entry is not found, we need to save the entry in the cache, and then return
/// false for the check
receivedRreqEntryList.push_back(rreqEntry);
m_sourceRreqMap[src] = receivedRreqEntryList;
return false;
NS_LOG_LOGIC("Find the request table entry for " << src << ", check if it is exact duplicate");
/*
* Drop the most aged packet when buffer reaches to max
*/
receivedRreqEntryList = i->second;
if (receivedRreqEntryList.size() >= m_requestIdSize)
{
receivedRreqEntryList.pop_front();
}
// We loop the receive rreq entry to find duplicate
for (std::list<DsrReceivedRreqEntry>::const_iterator j = receivedRreqEntryList.begin();
j != receivedRreqEntryList.end();
++j)
{
if (*j == rreqEntry) /// Check if we have found one duplication entry or not
{
return true;
}
}
/// if this entry is not found, we need to save the entry in the cache, and then return
/// false for the check
receivedRreqEntryList.push_back(rreqEntry);
m_sourceRreqMap[src] = receivedRreqEntryList;
return false;
}
} // namespace dsr