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