mgmt: StatusServer

refs #1199

Change-Id: Idf2181dde857970f026e8147b60d09dea45007a1
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index 62612f2..c3898c6 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -60,6 +60,9 @@
   void
   onData(Face& face, const Data& data);
 
+  NameTree&
+  getNameTree();
+
   Fib&
   getFib();
 
@@ -192,6 +195,12 @@
   this->onIncomingData(face, data);
 }
 
+inline NameTree&
+Forwarder::getNameTree()
+{
+  return m_nameTree;
+}
+
 inline Fib&
 Forwarder::getFib()
 {
diff --git a/daemon/main.cpp b/daemon/main.cpp
index f80a4bd..8abdba6 100644
--- a/daemon/main.cpp
+++ b/daemon/main.cpp
@@ -12,6 +12,7 @@
 #include "mgmt/face-manager.hpp"
 #include "mgmt/local-control-header-manager.hpp"
 #include "mgmt/strategy-choice-manager.hpp"
+#include "mgmt/status-server.hpp"
 #include "mgmt/config-file.hpp"
 
 #include <boost/filesystem.hpp>
@@ -32,6 +33,7 @@
 static FaceManager* g_faceManager;
 static LocalControlHeaderManager* g_localControlHeaderManager;
 static StrategyChoiceManager* g_strategyChoiceManager;
+static StatusServer* g_statusServer;
 static shared_ptr<InternalFace> g_internalFace;
 
 
@@ -42,7 +44,7 @@
     "%s --help\n\tshow this help and exit\n"
     "%s "
        "[--config /path/to/nfd.conf]\n"
-      "\trun forwarding daemon\n"
+       "\trun forwarding daemon\n"
        "\t--config <configuration file>]: path to configuration file\n"
     "\n",
     programName, programName
@@ -55,12 +57,12 @@
   g_options.m_showUsage = false;
   g_options.m_config = DEFAULT_CONFIG_FILE;
 
-  while (1) {
+  while (true) {
     int option_index = 0;
     static ::option long_options[] = {
-      { "help"          , no_argument      , 0, 0 },
-      { "config"        , required_argument, 0, 0 },
-      { 0               , 0                , 0, 0 }
+      { "help"   , no_argument      , 0, 0 },
+      { "config" , required_argument, 0, 0 },
+      { 0        , 0                , 0, 0 }
     };
     int c = getopt_long_only(argc, argv, "", long_options, &option_index);
     if (c == -1) break;
@@ -88,10 +90,8 @@
   ConfigFile config;
 
   g_internalFace = make_shared<InternalFace>();
-  g_forwarder->addFace(g_internalFace);
-
   g_internalFace->getValidator().setConfigFile(config);
-
+  g_forwarder->addFace(g_internalFace);
 
   g_fibManager = new FibManager(g_forwarder->getFib(),
                                 bind(&Forwarder::getFace, g_forwarder, _1),
@@ -107,6 +107,8 @@
   g_strategyChoiceManager = new StrategyChoiceManager(g_forwarder->getStrategyChoice(),
                                                       g_internalFace);
 
+  g_statusServer = new StatusServer(g_internalFace, *g_forwarder);
+
   config.parse(g_options.m_config, true);
   config.parse(g_options.m_config, false);
 
@@ -117,17 +119,17 @@
 int
 main(int argc, char** argv)
 {
-  try {
-    bool isCommandLineValid = parseCommandLine(argc, argv);
-    if (!isCommandLineValid) {
-      usage(argv[0]);
-      return 1;
-    }
-    if (g_options.m_showUsage) {
-      usage(argv[0]);
-      return 0;
-    }
+  bool isCommandLineValid = parseCommandLine(argc, argv);
+  if (!isCommandLineValid) {
+    usage(argv[0]);
+    return 1;
+  }
+  if (g_options.m_showUsage) {
+    usage(argv[0]);
+    return 0;
+  }
 
+  try {
     g_forwarder = new Forwarder();
     initializeMgmt();
 
@@ -138,15 +140,18 @@
   //   NFD_LOG_ERROR("Error: " << error.what());
   //   NFD_LOG_ERROR("You should either specify --config option or copy sample configuration into "
   //                 << DEFAULT_CONFIG_FILE);
-  } catch(boost::filesystem::filesystem_error& error) {
-    if (error.code() == boost::system::errc::permission_denied) {
-      NFD_LOG_ERROR("Error: Permissions denied for " << error.path1());
+  }
+  catch (boost::filesystem::filesystem_error& e) {
+    if (e.code() == boost::system::errc::permission_denied) {
+      NFD_LOG_ERROR("Error: Permissions denied for " << e.path1());
       NFD_LOG_ERROR(argv[0] << " should be run as superuser");
-    } else {
-      NFD_LOG_ERROR("Error: " << error.what());
     }
-  } catch(std::exception& exception) {
-    NFD_LOG_ERROR("Error: " << exception.what());
+    else {
+      NFD_LOG_ERROR("Error: " << e.what());
+    }
+  }
+  catch (std::exception& e) {
+    NFD_LOG_ERROR("Error: " << e.what());
     return 1;
   }
 
@@ -160,3 +165,4 @@
 {
   return nfd::main(argc, argv);
 }
+
diff --git a/daemon/mgmt/status-server.cpp b/daemon/mgmt/status-server.cpp
new file mode 100644
index 0000000..9635488
--- /dev/null
+++ b/daemon/mgmt/status-server.cpp
@@ -0,0 +1,87 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "status-server.hpp"
+#include "fw/forwarder.hpp"
+#include "core/version.hpp"
+
+namespace nfd {
+
+const Name StatusServer::DATASET_PREFIX = "ndn:/localhost/nfd/status";
+const ndn::Milliseconds StatusServer::RESPONSE_FRESHNESS = 5000;
+
+static inline ndn::nfd::Status::Timestamp
+now()
+{
+  // make sure boost::chrono::system_clock's epoch is UNIX epoch
+  BOOST_ASSERT(static_cast<std::time_t>(0) == boost::chrono::system_clock::to_time_t(
+    boost::chrono::system_clock::time_point(
+      boost::chrono::duration_cast<boost::chrono::system_clock::duration>(
+        ndn::nfd::Status::Timestamp::duration::zero()
+      )
+    )
+  ));
+
+  return ndn::nfd::Status::Timestamp(
+    boost::chrono::duration_cast<ndn::nfd::Status::Timestamp::duration>(
+      boost::chrono::system_clock::now().time_since_epoch()
+    )
+  );
+}
+
+StatusServer::StatusServer(shared_ptr<AppFace> face, Forwarder& forwarder)
+  : m_face(face)
+  , m_forwarder(forwarder)
+  , m_startTimestamp(now())
+{
+  m_face->setInterestFilter(DATASET_PREFIX, bind(&StatusServer::onInterest, this, _2));
+}
+
+void
+StatusServer::onInterest(const Interest& interest) const
+{
+  Name name(DATASET_PREFIX);
+  // TODO use NumberComponent
+  name.append(ndn::Name::Component::fromNumberWithMarker(ndn::ndn_getNowMilliseconds(), 0x00));
+  name.append(ndn::Name::Component::fromNumberWithMarker(0, 0x00));
+
+  shared_ptr<Data> data = make_shared<Data>(name);
+  data->setFreshnessPeriod(RESPONSE_FRESHNESS);
+
+  shared_ptr<ndn::nfd::Status> payload = this->collectStatus();
+  ndn::EncodingBuffer payloadBuffer;
+  payload->wireEncode(payloadBuffer);
+  data->setContent(payloadBuffer.buf(), payloadBuffer.size());
+
+  m_face->sign(*data);
+  m_face->put(*data);
+}
+
+shared_ptr<ndn::nfd::Status>
+StatusServer::collectStatus() const
+{
+  shared_ptr<ndn::nfd::Status> status = make_shared<ndn::nfd::Status>();
+
+  status->setNfdVersion(NFD_VERSION);
+  status->setStartTimestamp(m_startTimestamp);
+  status->setCurrentTimestamp(now());
+
+  status->setNNameTreeEntries(m_forwarder.getNameTree().size());
+  status->setNFibEntries(m_forwarder.getFib().size());
+  status->setNPitEntries(m_forwarder.getPit().size());
+  status->setNMeasurementsEntries(m_forwarder.getMeasurements().size());
+  status->setNCsEntries(m_forwarder.getCs().size());
+
+  const ForwarderCounters& counters = m_forwarder.getCounters();
+  status->setNInInterests(counters.getInInterest());
+  status->setNOutInterests(counters.getOutInterest());
+  status->setNInDatas(counters.getInData());
+  status->setNOutDatas(counters.getOutData());
+
+  return status;
+}
+
+} // namespace nfd
diff --git a/daemon/mgmt/status-server.hpp b/daemon/mgmt/status-server.hpp
new file mode 100644
index 0000000..94c8e82
--- /dev/null
+++ b/daemon/mgmt/status-server.hpp
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_MGMT_STATUS_SERVER_HPP
+#define NFD_MGMT_STATUS_SERVER_HPP
+
+#include "mgmt/app-face.hpp"
+#include <ndn-cpp-dev/management/nfd-status.hpp>
+
+namespace nfd {
+
+class Forwarder;
+
+class StatusServer : noncopyable
+{
+public:
+  StatusServer(shared_ptr<AppFace> face, Forwarder& forwarder);
+
+private:
+  void
+  onInterest(const Interest& interest) const;
+
+  shared_ptr<ndn::nfd::Status>
+  collectStatus() const;
+
+private:
+  static const Name DATASET_PREFIX;
+  static const ndn::Milliseconds RESPONSE_FRESHNESS;
+
+  shared_ptr<AppFace> m_face;
+  Forwarder& m_forwarder;
+  ndn::nfd::Status::Timestamp m_startTimestamp;
+};
+
+} // namespace nfd
+
+#endif // NFD_MGMT_STATUS_SERVER_HPP
diff --git a/tests/mgmt/status-server.cpp b/tests/mgmt/status-server.cpp
new file mode 100644
index 0000000..3d8442b
--- /dev/null
+++ b/tests/mgmt/status-server.cpp
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "mgmt/status-server.hpp"
+#include "fw/forwarder.hpp"
+#include "core/version.hpp"
+#include "mgmt/internal-face.hpp"
+
+#include "tests/test-common.hpp"
+#include "tests/face/dummy-face.hpp"
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(MgmtStatusServer, BaseFixture)
+
+static inline ndn::nfd::Status::Timestamp
+now()
+{
+  return ndn::nfd::Status::Timestamp(
+    boost::chrono::duration_cast<ndn::nfd::Status::Timestamp::duration>(
+      boost::chrono::system_clock::now().time_since_epoch()
+    )
+  );
+}
+
+shared_ptr<const Data> g_response;
+
+void
+interceptResponse(const Data& data)
+{
+  g_response = data.shared_from_this();
+}
+
+BOOST_AUTO_TEST_CASE(Status)
+{
+  // initialize
+  ndn::nfd::Status::Timestamp t1 = now();
+  Forwarder forwarder;
+  shared_ptr<InternalFace> internalFace = make_shared<InternalFace>();
+  internalFace->onReceiveData += &interceptResponse;
+  StatusServer statusServer(internalFace, boost::ref(forwarder));
+  ndn::nfd::Status::Timestamp t2 = now();
+  
+  // populate tables
+  forwarder.getFib().insert("ndn:/fib1");
+  forwarder.getPit().insert(*makeInterest("ndn:/pit1"));
+  forwarder.getPit().insert(*makeInterest("ndn:/pit2"));
+  forwarder.getPit().insert(*makeInterest("ndn:/pit3"));
+  forwarder.getPit().insert(*makeInterest("ndn:/pit4"));
+  forwarder.getMeasurements().get("ndn:/measurements1");
+  forwarder.getMeasurements().get("ndn:/measurements2");
+  forwarder.getMeasurements().get("ndn:/measurements3");
+  BOOST_CHECK_GE(forwarder.getFib().size(), 1);
+  BOOST_CHECK_GE(forwarder.getPit().size(), 4);
+  BOOST_CHECK_GE(forwarder.getMeasurements().size(), 3);
+  
+  // request
+  shared_ptr<Interest> request = makeInterest("ndn:/localhost/nfd/status");
+  request->setMustBeFresh(true);
+  request->setChildSelector(1);
+  
+  g_response.reset();
+  ndn::nfd::Status::Timestamp t3 = now();
+  internalFace->sendInterest(*request);
+  ndn::nfd::Status::Timestamp t4 = now();
+  BOOST_REQUIRE(static_cast<bool>(g_response));
+  
+  // verify
+  ndn::nfd::Status status;
+  BOOST_REQUIRE_NO_THROW(status.wireDecode(g_response->getContent()));
+  
+  BOOST_CHECK_EQUAL(status.getNfdVersion(), NFD_VERSION);
+  BOOST_CHECK_GE(status.getStartTimestamp(), t1);
+  BOOST_CHECK_LE(status.getStartTimestamp(), t2);
+  BOOST_CHECK_GE(status.getCurrentTimestamp(), t3);
+  BOOST_CHECK_LE(status.getCurrentTimestamp(), t4);
+  
+  // StatusServer under test isn't added to Forwarder,
+  // so request and response won't affect table size
+  BOOST_CHECK_EQUAL(status.getNNameTreeEntries(), forwarder.getNameTree().size());
+  BOOST_CHECK_EQUAL(status.getNFibEntries(), forwarder.getFib().size());
+  BOOST_CHECK_EQUAL(status.getNPitEntries(), forwarder.getPit().size());
+  BOOST_CHECK_EQUAL(status.getNMeasurementsEntries(), forwarder.getMeasurements().size());
+  BOOST_CHECK_EQUAL(status.getNCsEntries(), forwarder.getCs().size());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/wscript b/wscript
index ea5ef35..0a8652d 100644
--- a/wscript
+++ b/wscript
@@ -22,20 +22,20 @@
         pass
 
     if conf.options.debug:
-        conf.define ('_DEBUG', 1)
-        conf.add_supported_cxxflags (cxxflags = ['-O0',
-                                                 '-Wall',
-                                                 '-Wno-unused-variable',
-                                                 '-g3',
-                                                 '-Wno-unused-private-field', # only clang supports
-                                                 '-fcolor-diagnostics',       # only clang supports
-                                                 '-Qunused-arguments',        # only clang supports
-                                                 '-Wno-tautological-compare', # suppress warnings from CryptoPP
-                                                 '-Wno-unused-function',      # suppress warnings from CryptoPP
-                                                 '-fno-inline',
-                                                 ])
+        conf.define('_DEBUG', 1)
+        conf.add_supported_cxxflags(cxxflags = ['-O0',
+                                                '-Wall',
+                                                '-Wno-unused-variable',
+                                                '-g3',
+                                                '-Wno-unused-private-field', # only clang supports
+                                                '-fcolor-diagnostics',       # only clang supports
+                                                '-Qunused-arguments',        # only clang supports
+                                                '-Wno-tautological-compare', # suppress warnings from CryptoPP
+                                                '-Wno-unused-function',      # suppress warnings from CryptoPP
+                                                '-fno-inline',
+                                                ])
     else:
-        conf.add_supported_cxxflags (cxxflags = ['-O3', '-g', '-Wno-tautological-compare', '-Wno-unused-function'])
+        conf.add_supported_cxxflags(cxxflags = ['-O3', '-g', '-Wno-tautological-compare', '-Wno-unused-function'])
 
     if not conf.options.ndn_cpp_dir:
         conf.check_cfg(package='libndn-cpp-dev', args=['--cflags', '--libs'], uselib_store='NDN_CPP', mandatory=True)
@@ -45,7 +45,7 @@
                        linkflags="-L%s/lib" % conf.options.ndn_cpp_dir,
                        mandatory=True)
 
-    boost_libs='system'
+    boost_libs = 'system chrono'
     if conf.options.with_tests:
         conf.env['WITH_TESTS'] = 1
         conf.define('WITH_TESTS', 1);
@@ -54,9 +54,9 @@
     conf.check_boost(lib=boost_libs)
 
     if conf.env.BOOST_VERSION_NUMBER < 104800:
-        Logs.error ("Minimum required boost version is 1.48.0")
-        Logs.error ("Please upgrade your distribution or install custom boost libraries" +
-                    " (http://redmine.named-data.net/projects/nfd/wiki/Boost_FAQ)")
+        Logs.error("Minimum required boost version is 1.48.0")
+        Logs.error("Please upgrade your distribution or install custom boost libraries" +
+                   " (http://redmine.named-data.net/projects/nfd/wiki/Boost_FAQ)")
         return
 
     conf.load('unix-socket')
@@ -106,7 +106,7 @@
 
     # Unit tests
     if bld.env['WITH_TESTS']:
-        unit_tests = unittests = bld.program(
+        unit_tests = bld.program(
             target="unit-tests",
             features = "cxx cxxprogram",
             source = bld.path.ant_glob(['tests/**/*.cpp'],