management: Add FaceManagementOptions
Change-Id: If360efb77a93436d06ad04c5b0f536a51c9a6743
diff --git a/src/encoding/tlv-nfd-control.hpp b/src/encoding/tlv-nfd-control.hpp
index 09bfef3..7c07a31 100644
--- a/src/encoding/tlv-nfd-control.hpp
+++ b/src/encoding/tlv-nfd-control.hpp
@@ -27,7 +27,8 @@
Strategy = 107,
// Face Management Protocol
- FaceManagementOptions = 108
+ FaceManagementOptions = 108,
+ Uri = 114
};
diff --git a/src/management/nfd-face-management-options.hpp b/src/management/nfd-face-management-options.hpp
new file mode 100644
index 0000000..20640df
--- /dev/null
+++ b/src/management/nfd-face-management-options.hpp
@@ -0,0 +1,168 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_MANAGEMENT_NFD_FACE_MANAGEMENT_OPTIONS_HPP
+#define NDN_MANAGEMENT_NFD_FACE_MANAGEMENT_OPTIONS_HPP
+
+#include "../encoding/encoding-buffer.hpp"
+#include "../encoding/tlv-nfd-control.hpp"
+
+namespace ndn {
+namespace nfd {
+
+class FaceManagementOptions {
+public:
+ struct Error : public Tlv::Error { Error(const std::string &what) : Tlv::Error(what) {} };
+
+ FaceManagementOptions ()
+ : m_faceId (INVALID_FACE_ID)
+ {
+ }
+
+ FaceManagementOptions(const Block& block)
+ {
+ wireDecode(block);
+ }
+
+ uint64_t
+ getFaceId () const
+ {
+ return m_faceId;
+ }
+
+ FaceManagementOptions&
+ setFaceId (uint64_t faceId)
+ {
+ m_faceId = faceId;
+ wire_.reset ();
+ return *this;
+ }
+
+ const std::string&
+ getUri () const
+ {
+ return m_uri;
+ }
+
+ FaceManagementOptions&
+ setUri (const std::string& uri)
+ {
+ m_uri = uri;
+ wire_.reset ();
+ return *this;
+ }
+
+ template<bool T>
+ size_t
+ wireEncode(EncodingImpl<T> &block) const;
+
+ const Block&
+ wireEncode () const;
+
+ void
+ wireDecode (const Block &wire);
+
+private:
+ static const uint64_t INVALID_FACE_ID;
+
+ uint64_t m_faceId;
+ std::string m_uri;
+
+ mutable Block wire_;
+};
+
+const uint64_t FaceManagementOptions::INVALID_FACE_ID = std::numeric_limits<uint64_t>::max();
+
+template<bool T>
+inline size_t
+FaceManagementOptions::wireEncode(EncodingImpl<T>& blk) const
+{
+ size_t total_len = 0;
+
+ if (!m_uri.empty())
+ {
+ size_t var_len = blk.prependByteArray (reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
+ total_len += var_len;
+ total_len += blk.prependVarNumber (var_len);
+ total_len += blk.prependVarNumber (tlv::nfd_control::Uri);
+ }
+
+ if (m_faceId != INVALID_FACE_ID)
+ {
+ size_t var_len = blk.prependNonNegativeInteger (m_faceId);
+ total_len += var_len;
+ total_len += blk.prependVarNumber (var_len);
+ total_len += blk.prependVarNumber (tlv::nfd_control::FaceId);
+ }
+
+ total_len += blk.prependVarNumber (total_len);
+ total_len += blk.prependVarNumber (tlv::nfd_control::FaceManagementOptions);
+ return total_len;
+}
+
+inline const Block&
+FaceManagementOptions::wireEncode () const
+{
+ if (wire_.hasWire ())
+ return wire_;
+
+ EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ wire_ = buffer.block();
+ return wire_;
+}
+
+inline void
+FaceManagementOptions::wireDecode (const Block &wire)
+{
+ m_uri.clear ();
+ m_faceId = INVALID_FACE_ID;
+
+ wire_ = wire;
+
+ if (wire_.type() != tlv::nfd_control::FaceManagementOptions)
+ throw Error("Requested decoding of FaceManagementOptions, but Block is of different type");
+
+ wire_.parse ();
+
+ // FaceID
+ Block::element_const_iterator val = wire_.find(tlv::nfd_control::FaceId);
+ if (val != wire_.elements_end())
+ {
+ m_faceId = readNonNegativeInteger(*val);
+ }
+
+ // Uri
+ val = wire_.find(tlv::nfd_control::Uri);
+ if (val != wire_.elements_end())
+ {
+ m_uri.append(reinterpret_cast<const char*>(val->value()), val->value_size());
+ }
+}
+
+inline std::ostream&
+operator << (std::ostream &os, const FaceManagementOptions &option)
+{
+ os << "ForwardingEntry(";
+
+ // FaceID
+ os << "FaceID: " << option.getFaceId () << ", ";
+
+ // Uri
+ os << "Uri: " << option.getUri ();
+
+ os << ")";
+ return os;
+}
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_FACE_MANAGEMENT_OPTIONS_HPP
diff --git a/tests/management/test-nfd-control.cpp b/tests/management/test-nfd-control.cpp
index 8ba18cc..735ba18 100644
--- a/tests/management/test-nfd-control.cpp
+++ b/tests/management/test-nfd-control.cpp
@@ -6,6 +6,7 @@
#include "management/nfd-control-response.hpp"
#include "management/nfd-fib-management-options.hpp"
+#include "management/nfd-face-management-options.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>
@@ -25,6 +26,12 @@
0x73, 0x74, 0x69, 0x01, 0x00, 0x6a, 0x01, 0x00
};
+const uint8_t TestFaceManagementOptions[] = {
+ 0x6c, 0x1e, 0x69, 0x01, 0x0a, 0x72, 0x19, 0x74, 0x63, 0x70, 0x3a, 0x2f,
+ 0x2f, 0x31, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x31, 0x2f, 0x68, 0x65, 0x6c,
+ 0x6c, 0x6f, 0x2f, 0x77, 0x6f, 0x72, 0x6c, 0x64
+};
+
// ControlResponse
BOOST_AUTO_TEST_CASE (ControlResponseEncode)
@@ -103,6 +110,30 @@
BOOST_CHECK_EQUAL (opt.getCost (), 0);
}
+BOOST_AUTO_TEST_CASE (FaceManagementOptionsFastEncoding)
+{
+ FaceManagementOptions opt;
+
+ opt.setFaceId (10);
+ opt.setUri ("tcp://1.1.1.1/hello/world");
+
+ BOOST_REQUIRE_NO_THROW (opt.wireEncode ());
+
+ BOOST_REQUIRE_EQUAL_COLLECTIONS (TestFaceManagementOptions,
+ TestFaceManagementOptions + sizeof (TestFibManagementOptions),
+ opt.wireEncode ().begin (), opt.wireEncode ().end ());
+}
+
+BOOST_AUTO_TEST_CASE (FaceManagementOptionsDecoding)
+{
+ Block blk (TestFaceManagementOptions, sizeof (TestFaceManagementOptions));
+ FaceManagementOptions opt;
+
+ BOOST_REQUIRE_NO_THROW (opt.wireDecode (blk));
+
+ BOOST_CHECK_EQUAL (opt.getFaceId(), 10);
+ BOOST_CHECK_EQUAL (opt.getUri(), "tcp://1.1.1.1/hello/world");
+}
BOOST_AUTO_TEST_SUITE_END()