diff --git a/src/olsr/model/olsr-header.cc b/src/olsr/model/olsr-header.cc index 67e347b31..b7c865563 100644 --- a/src/olsr/model/olsr-header.cc +++ b/src/olsr/model/olsr-header.cc @@ -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(this->willingness)); for (std::vector::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; diff --git a/src/olsr/model/olsr-header.h b/src/olsr/model/olsr-header.h index 5040d1685..b3c65cefc 100644 --- a/src/olsr/model/olsr-header.h +++ b/src/olsr/model/olsr-header.h @@ -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 linkMessages; //!< Link messages container. /** diff --git a/src/olsr/model/olsr-repositories.h b/src/olsr/model/olsr-repositories.h index 854a9999b..c3c9f04ae 100644 --- a/src/olsr/model/olsr-repositories.h +++ b/src/olsr/model/olsr-repositories.h @@ -25,6 +25,7 @@ #include "ns3/ipv4-address.h" #include "ns3/nstime.h" +#include #include #include @@ -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(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; } diff --git a/src/olsr/model/olsr-routing-protocol.cc b/src/olsr/model/olsr-routing-protocol.cc index 9f9047730..e50015987 100644 --- a/src/olsr/model/olsr-routing-protocol.cc +++ b/src/olsr/model/olsr-routing-protocol.cc @@ -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 diff --git a/src/olsr/model/olsr-routing-protocol.h b/src/olsr/model/olsr-routing-protocol.h index cd11c7112..bddcecf76 100644 --- a/src/olsr/model/olsr-routing-protocol.h +++ b/src/olsr/model/olsr-routing-protocol.h @@ -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 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. diff --git a/src/olsr/model/olsr-state.cc b/src/olsr/model/olsr-state.cc index 0478d60c2..ebc318796 100644 --- a/src/olsr/model/olsr-state.cc +++ b/src/olsr/model/olsr-state.cc @@ -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++) { diff --git a/src/olsr/model/olsr-state.h b/src/olsr/model/olsr-state.h index a1f4b0fdd..03ef17f1d 100644 --- a/src/olsr/model/olsr-state.h +++ b/src/olsr/model/olsr-state.h @@ -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. diff --git a/src/olsr/test/olsr-header-test-suite.cc b/src/olsr/test/olsr-header-test-suite.cc index 851e23d97..b83cfa745 100644 --- a/src/olsr/test/olsr-header-test-suite.cc +++ b/src/olsr/test/olsr-header-test-suite.cc @@ -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");