management: NFD Status

refs #1199

Change-Id: I65ca4be685d3a9249488025813e00b9212e6abcc
diff --git a/src/encoding/tlv-nfd.hpp b/src/encoding/tlv-nfd.hpp
index 0f199ff..cb89709 100644
--- a/src/encoding/tlv-nfd.hpp
+++ b/src/encoding/tlv-nfd.hpp
@@ -10,6 +10,8 @@
 #ifndef NDN_TLV_NFD_HPP
 #define NDN_TLV_NFD_HPP
 
+#include "../common.hpp"
+
 namespace ndn {
 namespace tlv {
 namespace nfd {
@@ -32,7 +34,7 @@
   // Face Management Protocol
   FaceManagementOptions = 108,
   Uri                   = 114,
-  
+
   // Strategy Choice Protocol
   StrategyChoiceOptions = 115,
   Strategy              = 107,
@@ -46,6 +48,19 @@
   FaceEvent                    = 192,
   FaceEventKind                = 193,
 
+  // Forwarder status
+  NfdVersion           = 128,
+  StartTimestamp       = 129,
+  CurrentTimestamp     = 130,
+  NNameTreeEntries     = 131,
+  NFibEntries          = 132,
+  NPitEntries          = 133,
+  NMeasurementsEntries = 134,
+  NCsEntries           = 135,
+  NInInterests         = TotalIncomingInterestCounter,
+  NOutInterests        = TotalOutgoingInterestCounter,
+  NInDatas             = TotalIncomingDataCounter,
+  NOutDatas            = TotalOutgoingDataCounter
 };
 
 enum {
diff --git a/src/management/nfd-status.hpp b/src/management/nfd-status.hpp
new file mode 100644
index 0000000..fa7f884
--- /dev/null
+++ b/src/management/nfd-status.hpp
@@ -0,0 +1,348 @@
+/* -*- 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_STATUS_HPP
+#define NDN_MANAGEMENT_NFD_STATUS_HPP
+
+#include "../encoding/tlv-nfd.hpp"
+#include "../encoding/tlv.hpp"
+#include "../encoding/encoding-buffer.hpp"
+#include <boost/chrono/include.hpp>
+
+namespace ndn {
+namespace nfd {
+
+class Status {
+public:
+  struct Error : public Tlv::Error
+  {
+    Error(const std::string& what)
+      : Tlv::Error(what)
+    {
+    }
+  };
+
+  Status();
+
+  explicit
+  Status(const Block& payload)
+  {
+    this->wireDecode(payload);
+  }
+
+  // Status is encoded as a series of blocks within Data Payload, not a single block.
+  // Thus there is no "const Block& wireEncode() const" method.
+
+  template<bool T>
+  size_t
+  wireEncode(EncodingImpl<T>& encoder) const;
+
+  void
+  wireDecode(const Block& payload);
+
+public:
+  typedef boost::chrono::time_point<boost::chrono::system_clock, boost::chrono::seconds> Timestamp;
+
+  int
+  getNfdVersion() const
+  {
+    return m_nfdVersion;
+  }
+
+  void
+  setNfdVersion(int nfdVersion)
+  {
+    m_nfdVersion = nfdVersion;
+  }
+
+  Timestamp
+  getStartTimestamp() const
+  {
+    return m_startTimestamp;
+  }
+
+  void
+  setStartTimestamp(Timestamp startTimestamp)
+  {
+    m_startTimestamp = startTimestamp;
+  }
+
+  Timestamp
+  getCurrentTimestamp() const
+  {
+    return m_currentTimestamp;
+  }
+
+  void
+  setCurrentTimestamp(Timestamp currentTimestamp)
+  {
+    m_currentTimestamp = currentTimestamp;
+  }
+
+  size_t
+  getNNameTreeEntries() const
+  {
+    return m_nNameTreeEntries;
+  }
+
+  void
+  setNNameTreeEntries(size_t nNameTreeEntries)
+  {
+    m_nNameTreeEntries = nNameTreeEntries;
+  }
+
+  size_t
+  getNFibEntries() const
+  {
+    return m_nFibEntries;
+  }
+
+  void
+  setNFibEntries(size_t nFibEntries)
+  {
+    m_nFibEntries = nFibEntries;
+  }
+
+  size_t
+  getNPitEntries() const
+  {
+    return m_nPitEntries;
+  }
+
+  void
+  setNPitEntries(size_t nPitEntries)
+  {
+    m_nPitEntries = nPitEntries;
+  }
+
+  size_t
+  getNMeasurementsEntries() const
+  {
+    return m_nMeasurementsEntries;
+  }
+
+  void
+  setNMeasurementsEntries(size_t nMeasurementsEntries)
+  {
+    m_nMeasurementsEntries = nMeasurementsEntries;
+  }
+
+  size_t
+  getNCsEntries() const
+  {
+    return m_nCsEntries;
+  }
+
+  void
+  setNCsEntries(size_t nCsEntries)
+  {
+    m_nCsEntries = nCsEntries;
+  }
+
+  int
+  getNInInterests() const
+  {
+    return m_nInInterests;
+  }
+
+  void
+  setNInInterests(int nInInterests)
+  {
+    m_nInInterests = nInInterests;
+  }
+
+  int
+  getNOutInterests() const
+  {
+    return m_nOutInterests;
+  }
+
+  void
+  setNOutInterests(int nOutInterests)
+  {
+    m_nOutInterests = nOutInterests;
+  }
+
+  int
+  getNInDatas() const
+  {
+    return m_nInDatas;
+  }
+
+  void
+  setNInDatas(int nInDatas)
+  {
+    m_nInDatas = nInDatas;
+  }
+
+  int
+  getNOutDatas() const
+  {
+    return m_nOutDatas;
+  }
+
+  void
+  setNOutDatas(int nOutDatas)
+  {
+    m_nOutDatas = nOutDatas;
+  }
+
+private:
+  int       m_nfdVersion;
+  Timestamp m_startTimestamp;
+  Timestamp m_currentTimestamp;
+  size_t    m_nNameTreeEntries;
+  size_t    m_nFibEntries;
+  size_t    m_nPitEntries;
+  size_t    m_nMeasurementsEntries;
+  size_t    m_nCsEntries;
+  int       m_nInInterests;
+  int       m_nOutInterests;
+  int       m_nInDatas;
+  int       m_nOutDatas;
+};
+
+BOOST_STATIC_ASSERT((boost::is_same<Status::Timestamp::period, boost::ratio<1> >::value));
+
+inline
+Status::Status()
+{
+  m_nfdVersion = 0;
+  m_startTimestamp = Timestamp::min();
+  m_currentTimestamp = Timestamp::min();
+  m_nNameTreeEntries = 0;
+  m_nFibEntries = 0;
+  m_nPitEntries = 0;
+  m_nMeasurementsEntries = 0;
+  m_nCsEntries = 0;
+  m_nInInterests = 0;
+  m_nOutInterests = 0;
+  m_nInDatas = 0;
+  m_nOutDatas = 0;
+}
+
+template<bool T>
+inline size_t
+Status::wireEncode(EncodingImpl<T>& encoder) const
+{
+  size_t total_len = 0;
+
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutDatas,
+                                              m_nOutDatas);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInDatas,
+                                              m_nInDatas);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests,
+                                              m_nOutInterests);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests,
+                                              m_nInInterests);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries,
+                                              m_nCsEntries);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries,
+                                              m_nMeasurementsEntries);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries,
+                                              m_nPitEntries);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries,
+                                              m_nFibEntries);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries,
+                                              m_nNameTreeEntries);
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::CurrentTimestamp,
+               m_currentTimestamp.time_since_epoch().count());
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp,
+               m_startTimestamp.time_since_epoch().count());
+  total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NfdVersion,
+                                              m_nfdVersion);
+
+  return total_len;
+}
+
+inline void
+Status::wireDecode(const Block& payload)
+{
+  m_nfdVersion = 0;
+  m_startTimestamp = Timestamp::min();
+  m_currentTimestamp = Timestamp::min();
+  m_nNameTreeEntries = 0;
+  m_nFibEntries = 0;
+  m_nPitEntries = 0;
+  m_nMeasurementsEntries = 0;
+  m_nCsEntries = 0;
+  m_nInInterests = 0;
+  m_nOutInterests = 0;
+  m_nInDatas = 0;
+  m_nOutDatas = 0;
+
+  if (payload.type() != Tlv::Content) {
+    throw Error("Requested decoding of Status Payload, but block is not Content");
+  }
+  payload.parse();
+
+  Block::element_const_iterator val;
+
+  val = payload.find(tlv::nfd::NfdVersion);
+  if (val != payload.elements_end()) {
+    m_nfdVersion = static_cast<int>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::StartTimestamp);
+  if (val != payload.elements_end()) {
+    m_startTimestamp = Timestamp(boost::chrono::seconds(readNonNegativeInteger(*val)));
+  }
+
+  val = payload.find(tlv::nfd::CurrentTimestamp);
+  if (val != payload.elements_end()) {
+    m_currentTimestamp = Timestamp(boost::chrono::seconds(readNonNegativeInteger(*val)));
+  }
+
+  val = payload.find(tlv::nfd::NNameTreeEntries);
+  if (val != payload.elements_end()) {
+    m_nNameTreeEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NFibEntries);
+  if (val != payload.elements_end()) {
+    m_nFibEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NPitEntries);
+  if (val != payload.elements_end()) {
+    m_nPitEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NMeasurementsEntries);
+  if (val != payload.elements_end()) {
+    m_nMeasurementsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NCsEntries);
+  if (val != payload.elements_end()) {
+    m_nCsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NInInterests);
+  if (val != payload.elements_end()) {
+    m_nInInterests = static_cast<int>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NOutInterests);
+  if (val != payload.elements_end()) {
+    m_nOutInterests = static_cast<int>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NInDatas);
+  if (val != payload.elements_end()) {
+    m_nInDatas = static_cast<int>(readNonNegativeInteger(*val));
+  }
+
+  val = payload.find(tlv::nfd::NOutDatas);
+  if (val != payload.elements_end()) {
+    m_nOutDatas = static_cast<int>(readNonNegativeInteger(*val));
+  }
+}
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_STATUS_HPP
diff --git a/tests/management/nfd-status.cpp b/tests/management/nfd-status.cpp
new file mode 100644
index 0000000..b5d408c
--- /dev/null
+++ b/tests/management/nfd-status.cpp
@@ -0,0 +1,57 @@
+/* -*- 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.
+ */
+
+#include "management/nfd-status.hpp"
+#include "data.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace ndn {
+namespace nfd {
+
+BOOST_AUTO_TEST_SUITE(NfdStatus)
+
+BOOST_AUTO_TEST_CASE(StatusEncode)
+{
+  Status status1;
+  status1.setNfdVersion(1014210635);
+  status1.setStartTimestamp(Status::Timestamp(boost::chrono::seconds(375193249)));
+  status1.setCurrentTimestamp(Status::Timestamp(boost::chrono::seconds(1886109034)));
+  status1.setNNameTreeEntries(1849943160);
+  status1.setNFibEntries(621739748);
+  status1.setNPitEntries(482129741);
+  status1.setNMeasurementsEntries(1771725298);
+  status1.setNCsEntries(1264968688);
+  status1.setNInInterests(612811615);
+  status1.setNOutInterests(952144445);
+  status1.setNInDatas(1843576050);
+  status1.setNOutDatas(138198826);
+
+  EncodingBuffer buffer;
+  status1.wireEncode(buffer);
+
+  Data data;
+  data.setContent(buffer.buf(), buffer.size());
+
+  Status status2(data.getContent());
+  BOOST_CHECK_EQUAL(status1.getNfdVersion(), status2.getNfdVersion());
+  BOOST_CHECK_EQUAL(status1.getStartTimestamp(), status2.getStartTimestamp());
+  BOOST_CHECK_EQUAL(status1.getCurrentTimestamp(), status2.getCurrentTimestamp());
+  BOOST_CHECK_EQUAL(status1.getNNameTreeEntries(), status2.getNNameTreeEntries());
+  BOOST_CHECK_EQUAL(status1.getNFibEntries(), status2.getNFibEntries());
+  BOOST_CHECK_EQUAL(status1.getNPitEntries(), status2.getNPitEntries());
+  BOOST_CHECK_EQUAL(status1.getNMeasurementsEntries(), status2.getNMeasurementsEntries());
+  BOOST_CHECK_EQUAL(status1.getNCsEntries(), status2.getNCsEntries());
+  BOOST_CHECK_EQUAL(status1.getNInInterests(), status2.getNInInterests());
+  BOOST_CHECK_EQUAL(status1.getNOutInterests(), status2.getNOutInterests());
+  BOOST_CHECK_EQUAL(status1.getNInDatas(), status2.getNInDatas());
+  BOOST_CHECK_EQUAL(status1.getNOutDatas(), status2.getNOutDatas());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace nfd
+} // namespace ndn