Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_UTIL_NETWORK_MONITOR_HPP |
| 23 | #define NDN_UTIL_NETWORK_MONITOR_HPP |
| 24 | |
| 25 | #include "signal.hpp" |
| 26 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 27 | // forward declaration |
| 28 | namespace boost { |
| 29 | namespace asio { |
| 30 | class io_service; |
| 31 | } // namespace asio |
| 32 | } // namespace boost |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | namespace util { |
| 36 | |
| 37 | /** |
| 38 | * @brief Network state change monitor |
| 39 | * |
| 40 | * When network change is detected, onNetworkStateChanged signal will be fired. |
| 41 | * Monitoring is run for the lifetime of the NetworkMonitor instance. |
| 42 | * |
| 43 | * @note Implementation of this class is platform dependent and not all supported platforms |
| 44 | * are supported: |
| 45 | * - OS X: CFNotificationCenterAddObserver |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame] | 46 | * - Linux: rtnetlink notifications |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 47 | * |
| 48 | * Network state change detection is not guaranteed to be precise and (zero or more) |
| 49 | * notifications are expected to be fired for the following events: |
| 50 | * - any network interface going up or down |
| 51 | * - IPv4 or IPv6 address changes on any of the interfaces |
| 52 | */ |
| 53 | class NetworkMonitor : boost::noncopyable |
| 54 | { |
| 55 | public: |
| 56 | class Error : public std::runtime_error |
| 57 | { |
| 58 | public: |
| 59 | explicit |
| 60 | Error(const std::string& what) |
| 61 | : std::runtime_error(what) |
| 62 | { |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * @brief Construct instance and start monitoring for network state changes |
| 68 | * @param io io_service thread that will dispatch events |
| 69 | * @throw Error when network monitoring is not supported or there is an error starting monitoring |
| 70 | */ |
| 71 | explicit |
| 72 | NetworkMonitor(boost::asio::io_service& io); |
| 73 | |
| 74 | /** |
| 75 | * @brief Terminate network state monitoring |
| 76 | */ |
| 77 | ~NetworkMonitor(); |
| 78 | |
| 79 | Signal<NetworkMonitor> onNetworkStateChanged; |
| 80 | |
| 81 | private: |
| 82 | class Impl; |
| 83 | std::unique_ptr<Impl> m_impl; |
| 84 | }; |
| 85 | |
| 86 | } // namespace util |
| 87 | } // namespace autoconfig |
| 88 | |
| 89 | #endif // NDN_UTIL_NETWORK_MONITOR_HPP |