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-2023 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::net {
33 
34 class NetworkMonitorImplStub final : public NetworkMonitorImpl
35 {
36 public:
37  explicit
38  NetworkMonitorImplStub(uint32_t capabilities)
39  : m_capabilities(capabilities)
40  {
41  }
42 
43  uint32_t
44  getCapabilities() const final
45  {
46  return m_capabilities;
47  }
48 
49  shared_ptr<const NetworkInterface>
50  getNetworkInterface(const std::string& ifname) const final
51  {
52  auto i = m_interfaces.find(ifname);
53  return i == m_interfaces.end() ? nullptr : i->second;
54  }
55 
56  std::vector<shared_ptr<const NetworkInterface>>
57  listNetworkInterfaces() const final
58  {
59  std::vector<shared_ptr<const NetworkInterface>> v;
60  boost::copy(m_interfaces | boost::adaptors::map_values, std::back_inserter(v));
61  return v;
62  }
63 
64 public: // internal
66 
67  void
68  addInterface(shared_ptr<NetworkInterface> netif)
69  {
70  BOOST_ASSERT(netif != nullptr);
71  bool isNew = m_interfaces.try_emplace(netif->getName(), netif).second;
72  if (!isNew) {
73  NDN_THROW(std::invalid_argument("duplicate ifname"));
74  }
75  this->emitSignal(onInterfaceAdded, netif);
76  }
77 
78  void
79  removeInterface(const std::string& ifname)
80  {
81  auto i = m_interfaces.find(ifname);
82  if (i == m_interfaces.end()) {
83  return;
84  }
85  shared_ptr<NetworkInterface> netif = i->second;
86  m_interfaces.erase(i);
87  this->emitSignal(onInterfaceRemoved, netif);
88  }
89 
90  void
91  emitEnumerationCompleted()
92  {
93  this->emitSignal(onEnumerationCompleted);
94  }
95 
96 private:
97  uint32_t m_capabilities;
98  std::unordered_map<std::string, shared_ptr<NetworkInterface>> m_interfaces;
99 };
100 
102  : NetworkMonitor(make_unique<NetworkMonitorImplStub>(capabilities))
103 {
104 }
105 
106 NetworkMonitorImplStub&
107 NetworkMonitorStub::getImpl()
108 {
109  return static_cast<NetworkMonitorImplStub&>(this->NetworkMonitor::getImpl());
110 }
111 
112 shared_ptr<NetworkInterface>
114 {
115  return NetworkMonitorImplStub::makeNetworkInterface();
116 }
117 
118 void
119 NetworkMonitorStub::addInterface(shared_ptr<NetworkInterface> netif)
120 {
121  this->getImpl().addInterface(std::move(netif));
122 }
123 
124 void
125 NetworkMonitorStub::removeInterface(const std::string& ifname)
126 {
127  this->getImpl().removeInterface(ifname);
128 }
129 
130 void
132 {
133  this->getImpl().emitEnumerationCompleted();
134 }
135 
136 } // namespace ndn::net
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:72
#define NDN_THROW(e)
Definition: exception.hpp:56