net: add NetworkMonitorStub

refs #4024

Change-Id: I20da6f3351f51667b045987dc07e88ba814a4456
diff --git a/src/net/network-monitor-stub.cpp b/src/net/network-monitor-stub.cpp
new file mode 100644
index 0000000..9c3425b
--- /dev/null
+++ b/src/net/network-monitor-stub.cpp
@@ -0,0 +1,136 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * @author Davide Pesavento <davide.pesavento@lip6.fr>
+ */
+
+#include "network-monitor-stub.hpp"
+#include <unordered_map>
+#include <boost/range/adaptor/map.hpp>
+#include <boost/range/algorithm/copy.hpp>
+
+namespace ndn {
+namespace net {
+
+class NetworkMonitorImplStub : public NetworkMonitorImpl
+{
+public:
+  explicit
+  NetworkMonitorImplStub(uint32_t capabilities)
+    : m_capabilities(capabilities)
+  {
+  }
+
+  uint32_t
+  getCapabilities() const final
+  {
+    return m_capabilities;
+  }
+
+  shared_ptr<const NetworkInterface>
+  getNetworkInterface(const std::string& ifname) const final
+  {
+    auto i = m_interfaces.find(ifname);
+    return i == m_interfaces.end() ? nullptr : i->second;
+  }
+
+  std::vector<shared_ptr<const NetworkInterface>>
+  listNetworkInterfaces() const final
+  {
+    std::vector<shared_ptr<const NetworkInterface>> v;
+    boost::copy(m_interfaces | boost::adaptors::map_values, std::back_inserter(v));
+    return v;
+  }
+
+public: // internal
+  using NetworkMonitorImpl::makeNetworkInterface;
+
+  void
+  addInterface(shared_ptr<NetworkInterface> netif)
+  {
+    BOOST_ASSERT(netif != nullptr);
+    bool isNew = m_interfaces.emplace(netif->getName(), netif).second;
+    if (!isNew) {
+      BOOST_THROW_EXCEPTION(std::invalid_argument("duplicate ifname"));
+    }
+    this->emitSignal(onInterfaceAdded, netif);
+  }
+
+  void
+  removeInterface(const std::string& ifname)
+  {
+    auto i = m_interfaces.find(ifname);
+    if (i == m_interfaces.end()) {
+      return;
+    }
+    shared_ptr<NetworkInterface> netif = i->second;
+    m_interfaces.erase(i);
+    this->emitSignal(onInterfaceRemoved, netif);
+  }
+
+  void
+  emitEnumerationCompleted()
+  {
+    this->emitSignal(onEnumerationCompleted);
+  }
+
+private:
+  uint32_t m_capabilities;
+  std::unordered_map<std::string, shared_ptr<NetworkInterface>> m_interfaces;
+};
+
+NetworkMonitorStub::NetworkMonitorStub(uint32_t capabilities)
+  : NetworkMonitor(make_unique<NetworkMonitorImplStub>(capabilities))
+{
+}
+
+NetworkMonitorImplStub&
+NetworkMonitorStub::getImpl()
+{
+  return static_cast<NetworkMonitorImplStub&>(this->NetworkMonitor::getImpl());
+}
+
+shared_ptr<NetworkInterface>
+NetworkMonitorStub::makeNetworkInterface()
+{
+  return NetworkMonitorImplStub::makeNetworkInterface();
+}
+
+void
+NetworkMonitorStub::addInterface(shared_ptr<NetworkInterface> netif)
+{
+  this->getImpl().addInterface(std::move(netif));
+}
+
+void
+NetworkMonitorStub::removeInterface(const std::string& ifname)
+{
+  this->getImpl().removeInterface(ifname);
+}
+
+void
+NetworkMonitorStub::emitEnumerationCompleted()
+{
+  this->getImpl().emitEnumerationCompleted();
+}
+
+} // namespace net
+} // namespace ndn