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-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 #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 namespace ndn::net {
41 
42 NDN_LOG_INIT(ndn.NetworkMonitor);
43 
44 static unique_ptr<NetworkMonitorImpl>
45 makeNetworkMonitorImpl(boost::asio::io_context& io)
46 {
47  try {
48  return make_unique<NETWORK_MONITOR_IMPL_TYPE>(io);
49  }
50  catch (const std::runtime_error& e) {
51  NDN_LOG_WARN("failed to initialize " BOOST_STRINGIZE(NETWORK_MONITOR_IMPL_TYPE) ": " << e.what());
52  // fallback to dummy implementation
53  return make_unique<NetworkMonitorImplNoop>(io);
54  }
55 }
56 
57 NetworkMonitor::NetworkMonitor(boost::asio::io_context& io)
58  : NetworkMonitor(makeNetworkMonitorImpl(io))
59 {
60 }
61 
62 NetworkMonitor::NetworkMonitor(unique_ptr<NetworkMonitorImpl> impl)
63  : m_impl(std::move(impl))
64  , onEnumerationCompleted(m_impl->onEnumerationCompleted)
65  , onInterfaceAdded(m_impl->onInterfaceAdded)
66  , onInterfaceRemoved(m_impl->onInterfaceRemoved)
67  , onNetworkStateChanged(m_impl->onNetworkStateChanged)
68 {
69 }
70 
71 uint32_t
73 {
74  return m_impl->getCapabilities();
75 }
76 
77 shared_ptr<const NetworkInterface>
78 NetworkMonitor::getNetworkInterface(const std::string& ifname) const
79 {
80  return m_impl->getNetworkInterface(ifname);
81 }
82 
83 std::vector<shared_ptr<const NetworkInterface>>
85 {
86  return m_impl->listNetworkInterfaces();
87 }
88 
89 shared_ptr<NetworkInterface>
91 {
92  // cannot use make_shared because NetworkInterface constructor is private
93  return shared_ptr<NetworkInterface>(new NetworkInterface);
94 }
95 
96 } // namespace ndn::net
Represents one network interface attached to the host.
static shared_ptr< NetworkInterface > makeNetworkInterface()
Network interface monitor.
NetworkMonitor(boost::asio::io_context &ioCtx)
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:270
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:169
Definition: data.cpp:25
#define NETWORK_MONITOR_IMPL_TYPE