network-monitor-stub.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 #include "network-monitor-stub.hpp"
26 #include <unordered_map>
27 #include <boost/range/adaptor/map.hpp>
28 #include <boost/range/algorithm/copy.hpp>
29 
30 namespace ndn {
31 namespace net {
32 
33 class NetworkMonitorImplStub : public NetworkMonitorImpl
34 {
35 public:
36  explicit
37  NetworkMonitorImplStub(uint32_t capabilities)
38  : m_capabilities(capabilities)
39  {
40  }
41 
42  uint32_t
43  getCapabilities() const final
44  {
45  return m_capabilities;
46  }
47 
48  shared_ptr<const NetworkInterface>
49  getNetworkInterface(const std::string& ifname) const final
50  {
51  auto i = m_interfaces.find(ifname);
52  return i == m_interfaces.end() ? nullptr : i->second;
53  }
54 
55  std::vector<shared_ptr<const NetworkInterface>>
56  listNetworkInterfaces() const final
57  {
58  std::vector<shared_ptr<const NetworkInterface>> v;
59  boost::copy(m_interfaces | boost::adaptors::map_values, std::back_inserter(v));
60  return v;
61  }
62 
63 public: // internal
65 
66  void
67  addInterface(shared_ptr<NetworkInterface> netif)
68  {
69  BOOST_ASSERT(netif != nullptr);
70  bool isNew = m_interfaces.emplace(netif->getName(), netif).second;
71  if (!isNew) {
72  BOOST_THROW_EXCEPTION(std::invalid_argument("duplicate ifname"));
73  }
74  this->emitSignal(onInterfaceAdded, netif);
75  }
76 
77  void
78  removeInterface(const std::string& ifname)
79  {
80  auto i = m_interfaces.find(ifname);
81  if (i == m_interfaces.end()) {
82  return;
83  }
84  shared_ptr<NetworkInterface> netif = i->second;
85  m_interfaces.erase(i);
86  this->emitSignal(onInterfaceRemoved, netif);
87  }
88 
89  void
90  emitEnumerationCompleted()
91  {
92  this->emitSignal(onEnumerationCompleted);
93  }
94 
95 private:
96  uint32_t m_capabilities;
97  std::unordered_map<std::string, shared_ptr<NetworkInterface>> m_interfaces;
98 };
99 
101  : NetworkMonitor(make_unique<NetworkMonitorImplStub>(capabilities))
102 {
103 }
104 
105 NetworkMonitorImplStub&
106 NetworkMonitorStub::getImpl()
107 {
108  return static_cast<NetworkMonitorImplStub&>(this->NetworkMonitor::getImpl());
109 }
110 
111 shared_ptr<NetworkInterface>
113 {
114  return NetworkMonitorImplStub::makeNetworkInterface();
115 }
116 
117 void
118 NetworkMonitorStub::addInterface(shared_ptr<NetworkInterface> netif)
119 {
120  this->getImpl().addInterface(std::move(netif));
121 }
122 
123 void
124 NetworkMonitorStub::removeInterface(const std::string& ifname)
125 {
126  this->getImpl().removeInterface(ifname);
127 }
128 
129 void
131 {
132  this->getImpl().emitEnumerationCompleted();
133 }
134 
135 } // namespace net
136 } // namespace ndn
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
void addInterface(shared_ptr< NetworkInterface > netif)
emit the onInterfaceAdded signal and add netif internally
static shared_ptr< NetworkInterface > makeNetworkInterface()
static shared_ptr< NetworkInterface > makeNetworkInterface()
create a NetworkInterface instance
void emitEnumerationCompleted()
emit the onEnumerationCompleted signal
unique_ptr< T > make_unique(Args &&...args)
Definition: backports.hpp:73
#define emitSignal(...)
(implementation detail)
Definition: emit.hpp:76
void removeInterface(const std::string &ifname)
emit the onInterfaceRemoved signal and remove netif internally
Network interface monitor.
NetworkMonitorStub(uint32_t capabilities)
constructor
NetworkMonitorImpl & getImpl()