blob: 498b9709df6cb59ca5e5e3a25022804906829318 [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
Alexander Afanasyev5bcee102017-03-27 22:28:29 -050032#include "util/detail/link-type-helper.hpp"
33
Davide Pesavento537dc3a2016-02-18 19:35:26 +010034#include "boost-test.hpp"
35
36#include <boost/asio/io_service.hpp>
37#include <iostream>
38
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080039namespace ndn {
40namespace util {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040041namespace tests {
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080042
Davide Pesavento2bf35a62017-04-02 00:41:06 -040043BOOST_AUTO_TEST_SUITE(Util)
44BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080045
Davide Pesavento2bf35a62017-04-02 00:41:06 -040046static std::ostream&
47logEvent(const shared_ptr<NetworkInterface>& ni = nullptr, std::ostream& os = std::cout)
48{
49 os << '[' << time::toIsoString(time::system_clock::now()) << "] ";
50 if (ni != nullptr)
51 os << ni->getName() << ": ";
52 return os;
53}
54
55BOOST_AUTO_TEST_CASE(Signals)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080056{
57 boost::asio::io_service io;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080058 NetworkMonitor monitor(io);
59
Junxiao Shif4c1eb32017-05-30 17:05:10 +000060 std::cout << "capabilities=" << monitor.getCapabilities() << std::endl;
61
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080062 monitor.onNetworkStateChanged.connect([] {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040063 logEvent() << "onNetworkStateChanged" << std::endl;
64 });
65
66 monitor.onEnumerationCompleted.connect([&monitor] {
67 logEvent() << "onEnumerationCompleted" << std::endl;
68 for (const auto& ni : monitor.listNetworkInterfaces()) {
69 std::cout << *ni;
70 }
71 });
72
73 monitor.onInterfaceAdded.connect([] (const shared_ptr<NetworkInterface>& ni) {
74 logEvent(ni) << "onInterfaceAdded\n" << *ni;
Alexander Afanasyev5bcee102017-03-27 22:28:29 -050075 logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << "\n";
Davide Pesavento2bf35a62017-04-02 00:41:06 -040076
77 ni->onAddressAdded.connect([ni] (const NetworkAddress& address) {
78 logEvent(ni) << "onAddressAdded " << address << std::endl;
Alexander Afanasyev5bcee102017-03-27 22:28:29 -050079 });
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080080
Davide Pesavento2bf35a62017-04-02 00:41:06 -040081 ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) {
82 logEvent(ni) << "onAddressRemoved " << address << std::endl;
83 });
84
85 ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) {
86 logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl;
Alexander Afanasyev5bcee102017-03-27 22:28:29 -050087 logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << "\n";
Davide Pesavento2bf35a62017-04-02 00:41:06 -040088 });
89
90 ni->onMtuChanged.connect([ni] (uint32_t oldMtu, uint32_t newMtu) {
91 logEvent(ni) << "onMtuChanged " << oldMtu << " -> " << newMtu << std::endl;
92 });
93 }); // monitor.onInterfaceAdded.connect
94
95 monitor.onInterfaceRemoved.connect([] (const shared_ptr<NetworkInterface>& ni) {
96 logEvent(ni) << "onInterfaceRemoved" << std::endl;
97 });
98
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080099 io.run();
100}
101
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400102BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor
103BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800104
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400105} // namespace tests
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800106} // namespace util
107} // namespace ndn