face, mgmt: add channel status dataset publisher

Expose channel maps in protocol factories

Change-Id: I057b1bba638bfc9372867318653d71ba7d0e9888
Refs: #1631, #1653
diff --git a/daemon/mgmt/channel-status-publisher.cpp b/daemon/mgmt/channel-status-publisher.cpp
new file mode 100644
index 0000000..42e4f3a
--- /dev/null
+++ b/daemon/mgmt/channel-status-publisher.cpp
@@ -0,0 +1,87 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "channel-status-publisher.hpp"
+
+#include "face-flags.hpp"
+#include "core/logger.hpp"
+#include "face/protocol-factory.hpp"
+#include "face/channel.hpp"
+
+#include <ndn-cxx/management/nfd-channel-status.hpp>
+
+namespace nfd {
+
+NFD_LOG_INIT("ChannelStatusPublisher");
+
+
+ChannelStatusPublisher::ChannelStatusPublisher(const FactoryMap& factories,
+                                               shared_ptr<AppFace> face,
+                                               const Name& prefix)
+  : SegmentPublisher(face, prefix)
+  , m_factories(factories)
+{
+
+}
+
+
+ChannelStatusPublisher::~ChannelStatusPublisher()
+{
+
+}
+
+size_t
+ChannelStatusPublisher::generate(ndn::EncodingBuffer& outBuffer)
+{
+  size_t totalLength = 0;
+  std::set<shared_ptr<ProtocolFactory> > seenFactories;
+
+  for (FactoryMap::const_iterator i = m_factories.begin();
+       i != m_factories.end(); ++i)
+    {
+      const shared_ptr<ProtocolFactory>& factory = i->second;
+
+      if (seenFactories.find(factory) != seenFactories.end())
+        {
+          continue;
+        }
+      seenFactories.insert(factory);
+
+      std::list<shared_ptr<const Channel> > channels = factory->getChannels();
+
+      for (std::list<shared_ptr<const Channel> >::const_iterator j = channels.begin();
+           j != channels.end(); ++j)
+        {
+          ndn::nfd::ChannelStatus entry;
+          entry.setLocalUri((*j)->getUri().toString());
+
+          totalLength += entry.wireEncode(outBuffer);
+        }
+    }
+
+  return totalLength;
+}
+
+} // namespace nfd
diff --git a/daemon/mgmt/channel-status-publisher.hpp b/daemon/mgmt/channel-status-publisher.hpp
new file mode 100644
index 0000000..b204258
--- /dev/null
+++ b/daemon/mgmt/channel-status-publisher.hpp
@@ -0,0 +1,58 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_MGMT_CHANNEL_STATUS_PUBLISHER_HPP
+#define NFD_DAEMON_MGMT_CHANNEL_STATUS_PUBLISHER_HPP
+
+#include "mgmt/segment-publisher.hpp"
+
+namespace nfd {
+
+class ProtocolFactory;
+
+class ChannelStatusPublisher : public SegmentPublisher
+{
+public:
+  typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
+
+  ChannelStatusPublisher(const FactoryMap& factories,
+                         shared_ptr<AppFace> face,
+                         const Name& prefix);
+
+  virtual
+  ~ChannelStatusPublisher();
+
+protected:
+
+  virtual size_t
+  generate(ndn::EncodingBuffer& outBuffer);
+
+private:
+  const FactoryMap& m_factories;
+};
+
+} // namespace nfd
+
+#endif // NFD_DAEMON_MGMT_CHANNEL_STATUS_PUBLISHER_HPP
diff --git a/daemon/mgmt/face-manager.cpp b/daemon/mgmt/face-manager.cpp
index e33bbe7..0c299f2 100644
--- a/daemon/mgmt/face-manager.cpp
+++ b/daemon/mgmt/face-manager.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "face-manager.hpp"
 
@@ -96,19 +97,28 @@
                              Name::Component("events"),
                              &FaceManager::ignoreUnsignedVerb
                              ),
+
+    UnsignedVerbAndProcessor(
+                             Name::Component("channels"),
+                             &FaceManager::listChannels
+                             ),
   };
 
-const Name FaceManager::LIST_COMMAND_PREFIX("/localhost/nfd/faces/list");
-const size_t FaceManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
+const Name FaceManager::FACES_LIST_DATASET_PREFIX("/localhost/nfd/faces/list");
+const size_t FaceManager::FACES_LIST_DATASET_NCOMPS = FACES_LIST_DATASET_PREFIX.size();
 
-const Name FaceManager::EVENTS_COMMAND_PREFIX("/localhost/nfd/faces/events");
+const Name FaceManager::FACE_EVENTS_PREFIX("/localhost/nfd/faces/events");
+
+const Name FaceManager::CHANNELS_LIST_DATASET_PREFIX("/localhost/nfd/faces/channels");
+const size_t FaceManager::CHANNELS_LIST_DATASET_NCOMPS = CHANNELS_LIST_DATASET_PREFIX.size();
 
 FaceManager::FaceManager(FaceTable& faceTable,
                          shared_ptr<InternalFace> face)
   : ManagerBase(face, FACE_MANAGER_PRIVILEGE)
   , m_faceTable(faceTable)
-  , m_statusPublisher(m_faceTable, m_face, LIST_COMMAND_PREFIX)
-  , m_notificationStream(m_face, EVENTS_COMMAND_PREFIX)
+  , m_faceStatusPublisher(m_faceTable, m_face, FACES_LIST_DATASET_PREFIX)
+  , m_channelStatusPublisher(m_factories, m_face, CHANNELS_LIST_DATASET_PREFIX)
+  , m_notificationStream(m_face, FACE_EVENTS_PREFIX)
   , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
                          SIGNED_COMMAND_VERBS +
                          (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
@@ -818,11 +828,10 @@
 
       shared_ptr<WebSocketFactory> factory = ndn::make_shared<WebSocketFactory>(port);
       m_factories.insert(std::make_pair("websocket", factory));
-      uint16_t portNo = boost::lexical_cast<uint16_t>(port);
 
       if (enableV6 && enableV4)
         {
-          shared_ptr<WebSocketChannel> ip46Channel = factory->createChannel("::", portNo);
+          shared_ptr<WebSocketChannel> ip46Channel = factory->createChannel("::", port);
           if (needToListen)
             {
               ip46Channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
@@ -832,7 +841,7 @@
         }
       else if (enableV4)
         {
-          shared_ptr<WebSocketChannel> ipv4Channel = factory->createChannel("0.0.0.0", portNo);
+          shared_ptr<WebSocketChannel> ipv4Channel = factory->createChannel("0.0.0.0", port);
           if (needToListen)
             {
               ipv4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
@@ -1116,15 +1125,34 @@
   const Name& command = request.getName();
   const size_t commandNComps = command.size();
 
-  if (commandNComps < LIST_COMMAND_NCOMPS ||
-      !LIST_COMMAND_PREFIX.isPrefixOf(command))
+  if (commandNComps < FACES_LIST_DATASET_NCOMPS ||
+      !FACES_LIST_DATASET_PREFIX.isPrefixOf(command))
     {
       NFD_LOG_DEBUG("command result: malformed");
       sendResponse(command, 400, "Malformed command");
       return;
     }
 
-  m_statusPublisher.publish();
+  m_faceStatusPublisher.publish();
+}
+
+void
+FaceManager::listChannels(const Interest& request)
+{
+  NFD_LOG_DEBUG("in listChannels");
+  const Name& command = request.getName();
+  const size_t commandNComps = command.size();
+
+  if (commandNComps < CHANNELS_LIST_DATASET_NCOMPS ||
+      !CHANNELS_LIST_DATASET_PREFIX.isPrefixOf(command))
+    {
+      NFD_LOG_DEBUG("command result: malformed");
+      sendResponse(command, 400, "Malformed command");
+      return;
+    }
+
+  NFD_LOG_DEBUG("publishing");
+  m_channelStatusPublisher.publish();
 }
 
 shared_ptr<ProtocolFactory>
diff --git a/daemon/mgmt/face-manager.hpp b/daemon/mgmt/face-manager.hpp
index 1040970..0a5579e 100644
--- a/daemon/mgmt/face-manager.hpp
+++ b/daemon/mgmt/face-manager.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_DAEMON_MGMT_FACE_MANAGER_HPP
 #define NFD_DAEMON_MGMT_FACE_MANAGER_HPP
@@ -29,6 +30,7 @@
 #include "face/local-face.hpp"
 #include "mgmt/manager-base.hpp"
 #include "mgmt/face-status-publisher.hpp"
+#include "mgmt/channel-status-publisher.hpp"
 #include "mgmt/notification-stream.hpp"
 
 #include <ndn-cxx/management/nfd-control-parameters.hpp>
@@ -76,6 +78,9 @@
   void
   listFaces(const Interest& request);
 
+  void
+  listChannels(const Interest& request);
+
   shared_ptr<ProtocolFactory>
   findFactory(const std::string& protocol);
 
@@ -159,11 +164,15 @@
              const std::string& optionName,
              const std::string& sectionName);
 
-private:
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
+
   FactoryMap m_factories;
+
+private:
   FaceTable& m_faceTable;
-  FaceStatusPublisher m_statusPublisher;
+  FaceStatusPublisher m_faceStatusPublisher;
+  ChannelStatusPublisher m_channelStatusPublisher;
   NotificationStream m_notificationStream;
 
   typedef function<void(FaceManager*,
@@ -195,10 +204,13 @@
   static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
   static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
 
-  static const Name LIST_COMMAND_PREFIX;
-  static const size_t LIST_COMMAND_NCOMPS;
+  static const Name FACES_LIST_DATASET_PREFIX;
+  static const size_t FACES_LIST_DATASET_NCOMPS;
 
-  static const Name EVENTS_COMMAND_PREFIX;
+  static const Name CHANNELS_LIST_DATASET_PREFIX;
+  static const size_t CHANNELS_LIST_DATASET_NCOMPS;
+
+  static const Name FACE_EVENTS_PREFIX;
 };
 
 inline bool