mgmt: FaceEventNotification equality operators and formatted output
Change-Id: I92aca47a2e3823592d1c0bef07a42dbba5c6cb91
Refs: #3903
diff --git a/src/mgmt/nfd/face-event-notification.cpp b/src/mgmt/nfd/face-event-notification.cpp
index f3ea655..81ef384 100644
--- a/src/mgmt/nfd/face-event-notification.cpp
+++ b/src/mgmt/nfd/face-event-notification.cpp
@@ -25,14 +25,12 @@
#include "encoding/tlv-nfd.hpp"
#include "util/concepts.hpp"
+#include <iomanip>
+
namespace ndn {
namespace nfd {
-//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceEventNotification>));
-BOOST_CONCEPT_ASSERT((WireEncodable<FaceEventNotification>));
-BOOST_CONCEPT_ASSERT((WireDecodable<FaceEventNotification>));
-static_assert(std::is_base_of<tlv::Error, FaceEventNotification::Error>::value,
- "FaceEventNotification::Error must inherit from tlv::Error");
+BOOST_CONCEPT_ASSERT((NotificationStreamItem<FaceEventNotification>));
FaceEventNotification::FaceEventNotification()
: m_kind(FACE_EVENT_NONE)
@@ -59,9 +57,9 @@
totalLength += prependNonNegativeIntegerBlock(encoder,
tlv::nfd::FaceScope, m_faceScope);
totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
- reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
+ reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
- reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
+ reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
totalLength += prependNonNegativeIntegerBlock(encoder,
tlv::nfd::FaceId, m_faceId);
totalLength += prependNonNegativeIntegerBlock(encoder,
@@ -72,6 +70,12 @@
return totalLength;
}
+template size_t
+FaceEventNotification::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
+
+template size_t
+FaceEventNotification::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
+
const Block&
FaceEventNotification::wireEncode() const
{
@@ -156,6 +160,7 @@
if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
m_flags = readNonNegativeInteger(*val);
+ ++val;
}
else {
BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
@@ -170,42 +175,55 @@
return *this;
}
+bool
+operator==(const FaceEventNotification& a, const FaceEventNotification& b)
+{
+ return a.getFaceId() == b.getFaceId() &&
+ a.getRemoteUri() == b.getRemoteUri() &&
+ a.getLocalUri() == b.getLocalUri() &&
+ a.getFaceScope() == b.getFaceScope() &&
+ a.getFacePersistency() == b.getFacePersistency() &&
+ a.getLinkType() == b.getLinkType() &&
+ a.getFlags() == b.getFlags() &&
+ a.getKind() == b.getKind();
+}
+
+std::ostream&
+operator<<(std::ostream& os, FaceEventKind kind)
+{
+ switch (kind) {
+ case FACE_EVENT_NONE:
+ return os << "none";
+ case FACE_EVENT_CREATED:
+ return os << "created";
+ case FACE_EVENT_DESTROYED:
+ return os << "destroyed";
+ case FACE_EVENT_UP:
+ return os << "up";
+ case FACE_EVENT_DOWN:
+ return os << "down";
+ }
+ return os << static_cast<unsigned>(kind);
+}
+
std::ostream&
operator<<(std::ostream& os, const FaceEventNotification& notification)
{
- os << "FaceEventNotification(";
-
- switch (notification.getKind()) {
- case FACE_EVENT_NONE:
- os << "Kind: none, ";
- break;
- case FACE_EVENT_CREATED:
- os << "Kind: created, ";
- break;
- case FACE_EVENT_DESTROYED:
- os << "Kind: destroyed, ";
- break;
- case FACE_EVENT_UP:
- os << "Kind: up, ";
- break;
- case FACE_EVENT_DOWN:
- os << "Kind: down, ";
- break;
- }
-
- os << "FaceID: " << notification.getFaceId() << ", "
- << "RemoteUri: " << notification.getRemoteUri() << ", "
- << "LocalUri: " << notification.getLocalUri() << ", "
- << "FaceScope: " << notification.getFaceScope() << ", "
- << "FacePersistency: " << notification.getFacePersistency() << ", "
- << "LinkType: " << notification.getLinkType() << ", ";
+ os << "FaceEvent(Kind: " << notification.getKind() << ",\n"
+ << " FaceId: " << notification.getFaceId() << ",\n"
+ << " RemoteUri: " << notification.getRemoteUri() << ",\n"
+ << " LocalUri: " << notification.getLocalUri() << ",\n"
+ << " FaceScope: " << notification.getFaceScope() << ",\n"
+ << " FacePersistency: " << notification.getFacePersistency() << ",\n"
+ << " LinkType: " << notification.getLinkType() << ",\n";
auto osFlags = os.flags();
- os << "Flags: " << std::showbase << std::hex << notification.getFlags();
+ // std::showbase doesn't work with number 0
+ os << " Flags: 0x" << std::noshowbase << std::noshowpos << std::nouppercase
+ << std::hex << notification.getFlags() << "\n";
os.flags(osFlags);
- os << ")";
- return os;
+ return os << " )";
}
} // namespace nfd
diff --git a/src/mgmt/nfd/face-event-notification.hpp b/src/mgmt/nfd/face-event-notification.hpp
index 6abfee1..06836e7 100644
--- a/src/mgmt/nfd/face-event-notification.hpp
+++ b/src/mgmt/nfd/face-event-notification.hpp
@@ -38,6 +38,9 @@
FACE_EVENT_DOWN = 4 ///< face went DOWN (from UP state)
};
+std::ostream&
+operator<<(std::ostream& os, FaceEventKind kind);
+
/**
* \ingroup management
* \brief represents a Face status change notification
@@ -81,6 +84,15 @@
FaceEventKind m_kind;
};
+bool
+operator==(const FaceEventNotification& a, const FaceEventNotification& b);
+
+inline bool
+operator!=(const FaceEventNotification& a, const FaceEventNotification& b)
+{
+ return !(a == b);
+}
+
std::ostream&
operator<<(std::ostream& os, const FaceEventNotification& notification);
diff --git a/src/util/concepts.hpp b/src/util/concepts.hpp
index 20d857e..bb8c744 100644
--- a/src/util/concepts.hpp
+++ b/src/util/concepts.hpp
@@ -106,16 +106,15 @@
}
};
-/** \brief a concept check for a Status Dataset item
- * \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset
- */
+namespace detail {
+
template<class X>
-class StatusDatasetItem : public WireEncodable<X>
- , public WireEncodableWithEncodingBuffer<X>
- , public WireDecodable<X>
+class NfdMgmtProtocolStruct : public WireEncodable<X>
+ , public WireEncodableWithEncodingBuffer<X>
+ , public WireDecodable<X>
{
public:
- BOOST_CONCEPT_USAGE(StatusDatasetItem)
+ BOOST_CONCEPT_USAGE(NfdMgmtProtocolStruct)
{
static_assert(std::is_default_constructible<X>::value, "");
static_assert(boost::has_equal_to<X, X, bool>::value, "");
@@ -125,6 +124,24 @@
}
};
+} // namespace detail
+
+/** \brief concept check for an item in a Status Dataset
+ * \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ */
+template<class X>
+class StatusDatasetItem : public detail::NfdMgmtProtocolStruct<X>
+{
+};
+
+/** \brief concept check for an item in a Notification Stream
+ * \sa https://redmine.named-data.net/projects/nfd/wiki/Notification
+ */
+template<class X>
+class NotificationStreamItem : public detail::NfdMgmtProtocolStruct<X>
+{
+};
+
// NDN_CXX_ASSERT_DEFAULT_CONSTRUCTIBLE and NDN_CXX_ASSERT_FORWARD_ITERATOR
// originally written as part of NFD codebase