network-monitor-stub.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  *
21  * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu>
22  * @author Davide Pesavento <davide.pesavento@lip6.fr>
23  */
24 
26 
27 #include <unordered_map>
28 
29 #include <boost/range/adaptor/map.hpp>
30 #include <boost/range/algorithm/copy.hpp>
31 
32 namespace ndn {
33 namespace net {
34 
35 class NetworkMonitorImplStub : public NetworkMonitorImpl
36 {
37 public:
38  explicit
39  NetworkMonitorImplStub(uint32_t capabilities)
40  : m_capabilities(capabilities)
41  {
42  }
43 
44  uint32_t
45  getCapabilities() const final
46  {
47  return m_capabilities;
48  }
49 
50  shared_ptr<const NetworkInterface>
51  getNetworkInterface(const std::string& ifname) const final
52  {
53  auto i = m_interfaces.find(ifname);
54  return i == m_interfaces.end() ? nullptr : i->second;
55  }
56 
57  std::vector<shared_ptr<const NetworkInterface>>
58  listNetworkInterfaces() const final
59  {
60  std::vector<shared_ptr<const NetworkInterface>> v;
61  boost::copy(m_interfaces | boost::adaptors::map_values, std::back_inserter(v));
62  return v;
63  }
64 
65 public: // internal
67 
68  void
69  addInterface(shared_ptr<NetworkInterface> netif)
70  {
71  BOOST_ASSERT(netif != nullptr);
72  bool isNew = m_interfaces.emplace(netif->getName(), netif).second;
73  if (!isNew) {
74  NDN_THROW(std::invalid_argument("duplicate ifname"));
75  }
76  this->emitSignal(onInterfaceAdded, netif);
77  }
78 
79  void
80  removeInterface(const std::string& ifname)
81  {
82  auto i = m_interfaces.find(ifname);
83  if (i == m_interfaces.end()) {
84  return;
85  }
86  shared_ptr<NetworkInterface> netif = i->second;
87  m_interfaces.erase(i);
88  this->emitSignal(onInterfaceRemoved, netif);
89  }
90 
91  void
92  emitEnumerationCompleted()
93  {
94  this->emitSignal(onEnumerationCompleted);
95  }
96 
97 private:
98  uint32_t m_capabilities;
99  std::unordered_map<std::string, shared_ptr<NetworkInterface>> m_interfaces;
100 };
101 
103  : NetworkMonitor(make_unique<NetworkMonitorImplStub>(capabilities))
104 {
105 }
106 
107 NetworkMonitorImplStub&
108 NetworkMonitorStub::getImpl()
109 {
110  return static_cast<NetworkMonitorImplStub&>(this->NetworkMonitor::getImpl());
111 }
112 
113 shared_ptr<NetworkInterface>
115 {
116  return NetworkMonitorImplStub::makeNetworkInterface();
117 }
118 
119 void
120 NetworkMonitorStub::addInterface(shared_ptr<NetworkInterface> netif)
121 {
122  this->getImpl().addInterface(std::move(netif));
123 }
124 
125 void
126 NetworkMonitorStub::removeInterface(const std::string& ifname)
127 {
128  this->getImpl().removeInterface(ifname);
129 }
130 
131 void
133 {
134  this->getImpl().emitEnumerationCompleted();
135 }
136 
137 } // namespace net
138 } // namespace ndn
static shared_ptr< NetworkInterface > makeNetworkInterface()
NetworkMonitorStub(uint32_t capabilities)
Constructor.
void removeInterface(const std::string &ifname)
Emit the onInterfaceRemoved signal and remove netif internally.
void emitEnumerationCompleted()
Emit the onEnumerationCompleted signal.
static shared_ptr< NetworkInterface > makeNetworkInterface()
Create a NetworkInterface instance.
void addInterface(shared_ptr< NetworkInterface > netif)
Emit the onInterfaceAdded signal and add netif internally.
Network interface monitor.
NetworkMonitorImpl & getImpl()
#define emitSignal(...)
(implementation detail)
Definition: emit.hpp:76
#define NDN_THROW(e)
Definition: exception.hpp:61
Definition: data.cpp:25