blob: b48d5e9514e3459df64d27c3d6ab754b520369d8 [file] [log] [blame]
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento2bf35a62017-04-02 00:41:06 -04003 * Copyright (c) 2013-2017 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
25#ifndef NDN_UTIL_NETWORK_MONITOR_HPP
26#define NDN_UTIL_NETWORK_MONITOR_HPP
27
28#include "signal.hpp"
29
Davide Pesavento2bf35a62017-04-02 00:41:06 -040030#include <vector>
31
Davide Pesavento537dc3a2016-02-18 19:35:26 +010032// forward declaration
33namespace boost {
34namespace asio {
35class io_service;
36} // namespace asio
37} // namespace boost
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080038
39namespace ndn {
40namespace util {
41
Davide Pesavento2bf35a62017-04-02 00:41:06 -040042class NetworkInterface;
43
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080044/**
Davide Pesavento2bf35a62017-04-02 00:41:06 -040045 * @brief Network interfaces monitor
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080046 *
Davide Pesavento2bf35a62017-04-02 00:41:06 -040047 * Maintains an up-to-date view of every system network interface and notifies when an interface
48 * is added or removed.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080049 *
50 * @note Implementation of this class is platform dependent and not all supported platforms
51 * are supported:
Davide Pesavento2bf35a62017-04-02 00:41:06 -040052 * - OS X: CFNotificationCenterAddObserver (incomplete)
Alexander Afanasyev7b3080f2015-01-28 21:21:01 -080053 * - Linux: rtnetlink notifications
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080054 *
Davide Pesavento2bf35a62017-04-02 00:41:06 -040055 * @todo macOS implementation needs to be updated to emit the new signals and keep track of
56 * interfaces (links) and addresses
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080057 */
Davide Pesavento2bf35a62017-04-02 00:41:06 -040058class NetworkMonitor : noncopyable
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080059{
60public:
61 class Error : public std::runtime_error
62 {
63 public:
64 explicit
65 Error(const std::string& what)
66 : std::runtime_error(what)
67 {
68 }
69 };
70
Davide Pesavento2bf35a62017-04-02 00:41:06 -040071 class Impl;
72
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080073 /**
Davide Pesavento2bf35a62017-04-02 00:41:06 -040074 * @brief Construct instance, request enumeration of all network interfaces, and start
75 * monitoring for network state changes
76 *
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080077 * @param io io_service thread that will dispatch events
78 * @throw Error when network monitoring is not supported or there is an error starting monitoring
79 */
80 explicit
81 NetworkMonitor(boost::asio::io_service& io);
82
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080083 ~NetworkMonitor();
84
Davide Pesavento2bf35a62017-04-02 00:41:06 -040085 shared_ptr<NetworkInterface>
86 getNetworkInterface(const std::string& ifname) const;
87
88 std::vector<shared_ptr<NetworkInterface>>
89 listNetworkInterfaces() const;
90
91public: // signals
92 /** @brief Fires when network interfaces enumeration is complete
93 */
94 Signal<NetworkMonitor> onEnumerationCompleted;
95
96 /** @brief Fires when a new interface is added
97 */
98 Signal<NetworkMonitor, shared_ptr<NetworkInterface>> onInterfaceAdded;
99
100 /**
101 * @brief Fires when an interface is removed
102 * @note The NetworkInterface object is no longer present in the network
103 * interfaces map when the signal is emitted
104 */
105 Signal<NetworkMonitor, shared_ptr<NetworkInterface>> onInterfaceRemoved;
106
107 // only for backward compatibility
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800108 Signal<NetworkMonitor> onNetworkStateChanged;
109
110private:
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800111 std::unique_ptr<Impl> m_impl;
112};
113
114} // namespace util
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400115} // namespace ndn
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800116
117#endif // NDN_UTIL_NETWORK_MONITOR_HPP