blob: 57ef7f1963835592ae6867f5133b9e10dba104e7 [file] [log] [blame]
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento50b92262018-07-11 12:28:31 -04002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08004 *
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.
Davide Pesavento2bf35a62017-04-02 00:41:06 -040020 *
21 * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu>
22 * @author Davide Pesavento <davide.pesavento@lip6.fr>
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080023 */
24
Davide Pesavento9a8bae52016-02-24 20:33:08 +010025#include "network-monitor.hpp"
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080026#include "ndn-cxx-config.hpp"
Davide Pesavento755f8a82018-08-24 00:06:45 -040027#include "../util/logger.hpp"
28
29#include "detail/network-monitor-impl-noop.hpp"
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080030
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -050031#if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS)
Davide Pesavento9a8bae52016-02-24 20:33:08 +010032#include "detail/network-monitor-impl-osx.hpp"
Junxiao Shi2dc416d2017-07-03 04:46:16 +000033#define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplOsx
Davide Pesavento474c3b22018-08-25 16:24:43 -040034#elif defined(NDN_CXX_HAVE_NETLINK)
Davide Pesavento50b92262018-07-11 12:28:31 -040035#include "detail/network-monitor-impl-netlink.hpp"
36#define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNetlink
Davide Pesavento9a8bae52016-02-24 20:33:08 +010037#else
Junxiao Shi2dc416d2017-07-03 04:46:16 +000038#define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNoop
Davide Pesavento9a8bae52016-02-24 20:33:08 +010039#endif
40
Davide Pesavento755f8a82018-08-24 00:06:45 -040041NDN_LOG_INIT(ndn.NetworkMonitor);
42
Davide Pesavento9a8bae52016-02-24 20:33:08 +010043namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000044namespace net {
Alexander Afanasyev7b3080f2015-01-28 21:21:01 -080045
Davide Pesavento755f8a82018-08-24 00:06:45 -040046static unique_ptr<NetworkMonitorImpl>
47makeNetworkMonitorImpl(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
Alexander Afanasyev7b3080f2015-01-28 21:21:01 -080059NetworkMonitor::NetworkMonitor(boost::asio::io_service& io)
Davide Pesavento755f8a82018-08-24 00:06:45 -040060 : NetworkMonitor(makeNetworkMonitorImpl(io))
Alexander Afanasyev7b3080f2015-01-28 21:21:01 -080061{
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080062}
63
Junxiao Shi2dc416d2017-07-03 04:46:16 +000064NetworkMonitor::NetworkMonitor(unique_ptr<NetworkMonitorImpl> impl)
65 : m_impl(std::move(impl))
66 , onEnumerationCompleted(m_impl->onEnumerationCompleted)
67 , onInterfaceAdded(m_impl->onInterfaceAdded)
68 , onInterfaceRemoved(m_impl->onInterfaceRemoved)
69 , onNetworkStateChanged(m_impl->onNetworkStateChanged)
70{
71}
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080072
Junxiao Shif4c1eb32017-05-30 17:05:10 +000073uint32_t
74NetworkMonitor::getCapabilities() const
75{
76 return m_impl->getCapabilities();
77}
78
Junxiao Shi2dc416d2017-07-03 04:46:16 +000079shared_ptr<const NetworkInterface>
Davide Pesavento2bf35a62017-04-02 00:41:06 -040080NetworkMonitor::getNetworkInterface(const std::string& ifname) const
81{
82 return m_impl->getNetworkInterface(ifname);
83}
84
Junxiao Shi2dc416d2017-07-03 04:46:16 +000085std::vector<shared_ptr<const NetworkInterface>>
Davide Pesavento2bf35a62017-04-02 00:41:06 -040086NetworkMonitor::listNetworkInterfaces() const
87{
88 return m_impl->listNetworkInterfaces();
89}
90
Junxiao Shi2dc416d2017-07-03 04:46:16 +000091shared_ptr<NetworkInterface>
92NetworkMonitorImpl::makeNetworkInterface()
93{
94 // cannot use make_shared because NetworkInterface constructor is private
95 return shared_ptr<NetworkInterface>(new NetworkInterface);
96}
97
Junxiao Shi25467942017-06-30 02:53:14 +000098} // namespace net
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080099} // namespace ndn