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