network-monitor.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 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 #include "ndn-cxx/util/logger.hpp"
27 
28 #include "ndn-cxx/detail/config.hpp"
29 #include "ndn-cxx/net/impl/network-monitor-impl-noop.hpp"
30 #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS)
31 #include "ndn-cxx/net/impl/network-monitor-impl-osx.hpp"
32 #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplOsx
33 #elif defined(NDN_CXX_HAVE_NETLINK)
34 #include "ndn-cxx/net/impl/network-monitor-impl-netlink.hpp"
35 #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNetlink
36 #else
37 #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNoop
38 #endif
39 
40 NDN_LOG_INIT(ndn.NetworkMonitor);
41 
42 namespace ndn {
43 namespace net {
44 
45 static unique_ptr<NetworkMonitorImpl>
46 makeNetworkMonitorImpl(boost::asio::io_service& io)
47 {
48  try {
49  return make_unique<NETWORK_MONITOR_IMPL_TYPE>(io);
50  }
51  catch (const std::runtime_error& e) {
52  NDN_LOG_WARN("failed to initialize " BOOST_STRINGIZE(NETWORK_MONITOR_IMPL_TYPE) ": " << e.what());
53  // fallback to dummy implementation
54  return make_unique<NetworkMonitorImplNoop>(io);
55  }
56 }
57 
58 NetworkMonitor::NetworkMonitor(boost::asio::io_service& io)
59  : NetworkMonitor(makeNetworkMonitorImpl(io))
60 {
61 }
62 
63 NetworkMonitor::NetworkMonitor(unique_ptr<NetworkMonitorImpl> impl)
64  : m_impl(std::move(impl))
65  , onEnumerationCompleted(m_impl->onEnumerationCompleted)
66  , onInterfaceAdded(m_impl->onInterfaceAdded)
67  , onInterfaceRemoved(m_impl->onInterfaceRemoved)
68  , onNetworkStateChanged(m_impl->onNetworkStateChanged)
69 {
70 }
71 
72 uint32_t
74 {
75  return m_impl->getCapabilities();
76 }
77 
78 shared_ptr<const NetworkInterface>
79 NetworkMonitor::getNetworkInterface(const std::string& ifname) const
80 {
81  return m_impl->getNetworkInterface(ifname);
82 }
83 
84 std::vector<shared_ptr<const NetworkInterface>>
86 {
87  return m_impl->listNetworkInterfaces();
88 }
89 
90 shared_ptr<NetworkInterface>
92 {
93  // cannot use make_shared because NetworkInterface constructor is private
94  return shared_ptr<NetworkInterface>(new NetworkInterface);
95 }
96 
97 } // namespace net
98 } // namespace ndn
Represents one network interface attached to the host.
static shared_ptr< NetworkInterface > makeNetworkInterface()
Network interface monitor.
NetworkMonitor(boost::asio::io_service &io)
Construct instance, request enumeration of all network interfaces, and start monitoring for network s...
uint32_t getCapabilities() const
Returns a bitwise OR'ed set of Capability flags supported on the current platform.
std::vector< shared_ptr< const NetworkInterface > > listNetworkInterfaces() const
Lists all network interfaces currently available on the system.
shared_ptr< const NetworkInterface > getNetworkInterface(const std::string &ifname) const
Returns the NetworkInterface with the given name, or nullptr if it does not exist.
#define NDN_LOG_WARN(expression)
Log at WARN level.
Definition: logger.hpp:264
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:163
Definition: data.cpp:25
#define NETWORK_MONITOR_IMPL_TYPE