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