blob: ac74862e9b2b06f9626377cf7a172cd62306775d [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.
20 */
21
22#define BOOST_TEST_MAIN 1
23#define BOOST_TEST_DYN_LINK 1
24#define BOOST_TEST_MODULE ndn-cxx Integrated Tests (Network Monitor)
25
26#include "util/network-monitor.hpp"
27
Davide Pesavento2bf35a62017-04-02 00:41:06 -040028#include "util/network-address.hpp"
29#include "util/network-interface.hpp"
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080030#include "util/time.hpp"
31
Davide Pesavento537dc3a2016-02-18 19:35:26 +010032#include "boost-test.hpp"
33
34#include <boost/asio/io_service.hpp>
35#include <iostream>
36
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080037namespace ndn {
38namespace util {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040039namespace tests {
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080040
Davide Pesavento2bf35a62017-04-02 00:41:06 -040041BOOST_AUTO_TEST_SUITE(Util)
42BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080043
Davide Pesavento2bf35a62017-04-02 00:41:06 -040044static std::ostream&
45logEvent(const shared_ptr<NetworkInterface>& ni = nullptr, std::ostream& os = std::cout)
46{
47 os << '[' << time::toIsoString(time::system_clock::now()) << "] ";
48 if (ni != nullptr)
49 os << ni->getName() << ": ";
50 return os;
51}
52
53BOOST_AUTO_TEST_CASE(Signals)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080054{
55 boost::asio::io_service io;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080056 NetworkMonitor monitor(io);
57
Junxiao Shif4c1eb32017-05-30 17:05:10 +000058 std::cout << "capabilities=" << monitor.getCapabilities() << std::endl;
59
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080060 monitor.onNetworkStateChanged.connect([] {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040061 logEvent() << "onNetworkStateChanged" << std::endl;
62 });
63
64 monitor.onEnumerationCompleted.connect([&monitor] {
65 logEvent() << "onEnumerationCompleted" << std::endl;
66 for (const auto& ni : monitor.listNetworkInterfaces()) {
67 std::cout << *ni;
68 }
69 });
70
71 monitor.onInterfaceAdded.connect([] (const shared_ptr<NetworkInterface>& ni) {
72 logEvent(ni) << "onInterfaceAdded\n" << *ni;
73
74 ni->onAddressAdded.connect([ni] (const NetworkAddress& address) {
75 logEvent(ni) << "onAddressAdded " << address << std::endl;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080076 });
77
Davide Pesavento2bf35a62017-04-02 00:41:06 -040078 ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) {
79 logEvent(ni) << "onAddressRemoved " << address << std::endl;
80 });
81
82 ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) {
83 logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl;
84 });
85
86 ni->onMtuChanged.connect([ni] (uint32_t oldMtu, uint32_t newMtu) {
87 logEvent(ni) << "onMtuChanged " << oldMtu << " -> " << newMtu << std::endl;
88 });
89 }); // monitor.onInterfaceAdded.connect
90
91 monitor.onInterfaceRemoved.connect([] (const shared_ptr<NetworkInterface>& ni) {
92 logEvent(ni) << "onInterfaceRemoved" << std::endl;
93 });
94
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080095 io.run();
96}
97
Davide Pesavento2bf35a62017-04-02 00:41:06 -040098BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor
99BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800100
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400101} // namespace tests
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800102} // namespace util
103} // namespace ndn