olsr: Declare willingness variables as enum Willingness

This commit is contained in:
Eduardo Almeida
2023-06-01 21:07:42 +01:00
parent 390f3d26b2
commit 60b982e500
8 changed files with 52 additions and 20 deletions

View File

@@ -399,7 +399,7 @@ void
MessageHeader::Hello::Print(std::ostream& os) const
{
os << " Interval: " << +hTime << " (" << EmfToSeconds(hTime) << "s)";
os << " Willingness: " << +willingness;
os << " Willingness: " << willingness;
for (const auto& ilinkMessage : linkMessages)
{
@@ -430,7 +430,7 @@ MessageHeader::Hello::Serialize(Buffer::Iterator start) const
i.WriteU16(0); // Reserved
i.WriteU8(this->hTime);
i.WriteU8(this->willingness);
i.WriteU8(static_cast<uint8_t>(this->willingness));
for (std::vector<LinkMessage>::const_iterator iter = this->linkMessages.begin();
iter != this->linkMessages.end();
@@ -470,7 +470,7 @@ MessageHeader::Hello::Deserialize(Buffer::Iterator start, uint32_t messageSize)
i.ReadNtohU16(); // Reserved
this->hTime = i.ReadU8();
this->willingness = i.ReadU8();
this->willingness = Willingness(i.ReadU8());
helloSizeLeft -= 4;

View File

@@ -20,6 +20,8 @@
#ifndef OLSR_HEADER_H
#define OLSR_HEADER_H
#include "olsr-repositories.h"
#include "ns3/header.h"
#include "ns3/ipv4-address.h"
#include "ns3/nstime.h"
@@ -412,8 +414,8 @@ class MessageHeader : public Header
return Seconds(EmfToSeconds(this->hTime));
}
uint8_t willingness; //!< The willingness of a node to carry and forward traffic for other
//!< nodes.
Willingness willingness; //!< The willingness of a node to carry and forward traffic for
//!< other nodes.
std::vector<LinkMessage> linkMessages; //!< Link messages container.
/**

View File

@@ -25,6 +25,7 @@
#include "ns3/ipv4-address.h"
#include "ns3/nstime.h"
#include <iostream>
#include <set>
#include <vector>
@@ -51,6 +52,34 @@ enum Willingness : uint8_t
ALWAYS = 7,
};
/**
* Stream insertion operator for OLSR willingness.
*
* \param os Output stream.
* \param willingness Willingness.
* \return A reference to the output stream.
*/
inline std::ostream&
operator<<(std::ostream& os, Willingness willingness)
{
switch (willingness)
{
case Willingness::NEVER:
return (os << "NEVER");
case Willingness::LOW:
return (os << "LOW");
case Willingness::DEFAULT:
return (os << "DEFAULT");
case Willingness::HIGH:
return (os << "HIGH");
case Willingness::ALWAYS:
return (os << "ALWAYS");
default:
return (os << static_cast<uint32_t>(willingness)); // Cast to uint32_t to print correctly
}
return os;
}
/// \ingroup olsr
/// An Interface Association Tuple.
struct IfaceAssocTuple
@@ -127,7 +156,7 @@ struct NeighborTuple
/// A value between 0 and 7 specifying the node's willingness to carry traffic on behalf of
/// other nodes.
uint8_t willingness;
Willingness willingness;
};
static inline bool
@@ -142,7 +171,7 @@ operator<<(std::ostream& os, const NeighborTuple& tuple)
{
os << "NeighborTuple(neighborMainAddr=" << tuple.neighborMainAddr
<< ", status=" << (tuple.status == NeighborTuple::STATUS_SYM ? "SYM" : "NOT_SYM")
<< ", willingness=" << (int)tuple.willingness << ")";
<< ", willingness=" << tuple.willingness << ")";
return os;
}

View File

@@ -2414,7 +2414,7 @@ RoutingProtocol::RemoveDuplicateTuple(const DuplicateTuple& tuple)
}
void
RoutingProtocol::LinkTupleAdded(const LinkTuple& tuple, uint8_t willingness)
RoutingProtocol::LinkTupleAdded(const LinkTuple& tuple, Willingness willingness)
{
// Creates associated neighbor tuple
NeighborTuple nb_tuple;
@@ -2444,7 +2444,7 @@ RoutingProtocol::RemoveLinkTuple(const LinkTuple& tuple)
}
void
RoutingProtocol::LinkTupleUpdated(const LinkTuple& tuple, uint8_t willingness)
RoutingProtocol::LinkTupleUpdated(const LinkTuple& tuple, Willingness willingness)
{
// Each time a link tuple changes, the associated neighbor tuple must be recomputed

View File

@@ -256,11 +256,11 @@ class RoutingProtocol : public Ipv4RoutingProtocol
uint16_t m_messageSequenceNumber; //!< Messages sequence number counter.
uint16_t m_ansn; //!< Advertised Neighbor Set sequence number.
Time m_helloInterval; //!< HELLO messages' emission interval.
Time m_tcInterval; //!< TC messages' emission interval.
Time m_midInterval; //!< MID messages' emission interval.
Time m_hnaInterval; //!< HNA messages' emission interval.
uint8_t m_willingness; //!< Willingness for forwarding packets on behalf of other nodes.
Time m_helloInterval; //!< HELLO messages' emission interval.
Time m_tcInterval; //!< TC messages' emission interval.
Time m_midInterval; //!< MID messages' emission interval.
Time m_hnaInterval; //!< HNA messages' emission interval.
Willingness m_willingness; //!< Willingness for forwarding packets on behalf of other nodes.
OlsrState m_state; //!< Internal state with all needed data structs.
Ptr<Ipv4> m_ipv4; //!< IPv4 object the routing is linked to.
@@ -614,7 +614,7 @@ class RoutingProtocol : public Ipv4RoutingProtocol
* \param tuple The tuple to be added.
* \param willingness The tuple willingness.
*/
void LinkTupleAdded(const LinkTuple& tuple, uint8_t willingness);
void LinkTupleAdded(const LinkTuple& tuple, Willingness willingness);
/**
* \brief Removes a link tuple from the Link Set.
@@ -630,7 +630,7 @@ class RoutingProtocol : public Ipv4RoutingProtocol
* \param tuple The link tuple which has been updated.
* \param willingness The tuple willingness.
*/
void LinkTupleUpdated(const LinkTuple& tuple, uint8_t willingness);
void LinkTupleUpdated(const LinkTuple& tuple, Willingness willingness);
/**
* \brief Adds a neighbor tuple to the Neighbor Set.

View File

@@ -132,7 +132,7 @@ OlsrState::FindSymNeighborTuple(const Ipv4Address& mainAddr) const
}
NeighborTuple*
OlsrState::FindNeighborTuple(const Ipv4Address& mainAddr, uint8_t willingness)
OlsrState::FindNeighborTuple(const Ipv4Address& mainAddr, Willingness willingness)
{
for (NeighborSet::iterator it = m_neighborSet.begin(); it != m_neighborSet.end(); it++)
{

View File

@@ -137,7 +137,7 @@ class OlsrState
* \param willingness The neighbor willingness.
* \returns The neighbor tuple, if found. Else it returns a null pointer.
*/
NeighborTuple* FindNeighborTuple(const Ipv4Address& mainAddr, uint8_t willingness);
NeighborTuple* FindNeighborTuple(const Ipv4Address& mainAddr, Willingness willingness);
/**
* Erases a neighbor tuple.

View File

@@ -18,6 +18,7 @@
*/
#include "ns3/olsr-header.h"
#include "ns3/olsr-repositories.h"
#include "ns3/packet.h"
#include "ns3/test.h"
@@ -191,7 +192,7 @@ OlsrHelloTestCase::DoRun()
olsr::MessageHeader::Hello& helloIn = msgIn.GetHello();
helloIn.SetHTime(Seconds(7));
helloIn.willingness = 66;
helloIn.willingness = olsr::Willingness::HIGH;
{
olsr::MessageHeader::Hello::LinkMessage lm1;
@@ -214,7 +215,7 @@ OlsrHelloTestCase::DoRun()
olsr::MessageHeader::Hello& helloOut = msgOut.GetHello();
NS_TEST_ASSERT_MSG_EQ(helloOut.GetHTime(), Seconds(7), "300");
NS_TEST_ASSERT_MSG_EQ(helloOut.willingness, 66, "301");
NS_TEST_ASSERT_MSG_EQ(helloOut.willingness, olsr::Willingness::HIGH, "301");
NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages.size(), 2, "302");
NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[0].linkCode, 2, "303");