Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 50b9226 | 2018-07-11 12:28:31 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 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. |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 22 | * @author Davide Pesavento <davide.pesavento@lip6.fr> |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 23 | */ |
| 24 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 25 | #include "network-monitor.hpp" |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 26 | #include "ndn-cxx-config.hpp" |
Davide Pesavento | 755f8a8 | 2018-08-24 00:06:45 -0400 | [diff] [blame] | 27 | #include "../util/logger.hpp" |
| 28 | |
| 29 | #include "detail/network-monitor-impl-noop.hpp" |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 31 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 32 | #include "detail/network-monitor-impl-osx.hpp" |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 33 | #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplOsx |
Davide Pesavento | 474c3b2 | 2018-08-25 16:24:43 -0400 | [diff] [blame] | 34 | #elif defined(NDN_CXX_HAVE_NETLINK) |
Davide Pesavento | 50b9226 | 2018-07-11 12:28:31 -0400 | [diff] [blame] | 35 | #include "detail/network-monitor-impl-netlink.hpp" |
| 36 | #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNetlink |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 37 | #else |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 38 | #define NETWORK_MONITOR_IMPL_TYPE NetworkMonitorImplNoop |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 39 | #endif |
| 40 | |
Davide Pesavento | 755f8a8 | 2018-08-24 00:06:45 -0400 | [diff] [blame] | 41 | NDN_LOG_INIT(ndn.NetworkMonitor); |
| 42 | |
Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 43 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 44 | namespace net { |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame] | 45 | |
Davide Pesavento | 755f8a8 | 2018-08-24 00:06:45 -0400 | [diff] [blame] | 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 | |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame] | 59 | NetworkMonitor::NetworkMonitor(boost::asio::io_service& io) |
Davide Pesavento | 755f8a8 | 2018-08-24 00:06:45 -0400 | [diff] [blame] | 60 | : NetworkMonitor(makeNetworkMonitorImpl(io)) |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame] | 61 | { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 64 | NetworkMonitor::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 Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 72 | |
Junxiao Shi | f4c1eb3 | 2017-05-30 17:05:10 +0000 | [diff] [blame] | 73 | uint32_t |
| 74 | NetworkMonitor::getCapabilities() const |
| 75 | { |
| 76 | return m_impl->getCapabilities(); |
| 77 | } |
| 78 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 79 | shared_ptr<const NetworkInterface> |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 80 | NetworkMonitor::getNetworkInterface(const std::string& ifname) const |
| 81 | { |
| 82 | return m_impl->getNetworkInterface(ifname); |
| 83 | } |
| 84 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 85 | std::vector<shared_ptr<const NetworkInterface>> |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 86 | NetworkMonitor::listNetworkInterfaces() const |
| 87 | { |
| 88 | return m_impl->listNetworkInterfaces(); |
| 89 | } |
| 90 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 91 | shared_ptr<NetworkInterface> |
| 92 | NetworkMonitorImpl::makeNetworkInterface() |
| 93 | { |
| 94 | // cannot use make_shared because NetworkInterface constructor is private |
| 95 | return shared_ptr<NetworkInterface>(new NetworkInterface); |
| 96 | } |
| 97 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 98 | } // namespace net |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 99 | } // namespace ndn |