blob: 57cea97753a03a5b936e95870849ffb29c9d2736 [file] [log] [blame]
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventof35c4272017-07-14 11:13:34 -04002/*
Davide Pesavento7e780642018-11-24 15:51:34 -05003 * Copyright (c) 2013-2018 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
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080022#define BOOST_TEST_MODULE ndn-cxx Integrated Tests (Network Monitor)
Davide Pesavento7e780642018-11-24 15:51:34 -050023#include "tests/boost-test.hpp"
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/net/network-monitor.hpp"
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "ndn-cxx/net/network-address.hpp"
28#include "ndn-cxx/net/network-interface.hpp"
Junxiao Shi24c5a002018-12-12 04:47:15 +000029#include "ndn-cxx/net/impl/link-type-helper.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050030#include "ndn-cxx/util/string-helper.hpp"
31#include "ndn-cxx/util/time.hpp"
Davide Pesavento537dc3a2016-02-18 19:35:26 +010032
33#include <boost/asio/io_service.hpp>
34#include <iostream>
35
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080036namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000037namespace net {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040038namespace tests {
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080039
Davide Pesavento2bf35a62017-04-02 00:41:06 -040040BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080041
Davide Pesavento2bf35a62017-04-02 00:41:06 -040042static std::ostream&
Junxiao Shi2dc416d2017-07-03 04:46:16 +000043logEvent(const shared_ptr<const NetworkInterface>& ni = nullptr, std::ostream& os = std::cout)
Davide Pesavento2bf35a62017-04-02 00:41:06 -040044{
45 os << '[' << time::toIsoString(time::system_clock::now()) << "] ";
46 if (ni != nullptr)
47 os << ni->getName() << ": ";
48 return os;
49}
50
51BOOST_AUTO_TEST_CASE(Signals)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080052{
53 boost::asio::io_service io;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080054 NetworkMonitor monitor(io);
55
Davide Pesaventof35c4272017-07-14 11:13:34 -040056 std::cout << "capabilities=" << AsHex{monitor.getCapabilities()} << std::endl;
Junxiao Shif4c1eb32017-05-30 17:05:10 +000057
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080058 monitor.onNetworkStateChanged.connect([] {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040059 logEvent() << "onNetworkStateChanged" << std::endl;
60 });
61
62 monitor.onEnumerationCompleted.connect([&monitor] {
63 logEvent() << "onEnumerationCompleted" << std::endl;
64 for (const auto& ni : monitor.listNetworkInterfaces()) {
65 std::cout << *ni;
66 }
67 });
68
Junxiao Shi2dc416d2017-07-03 04:46:16 +000069 monitor.onInterfaceAdded.connect([] (const shared_ptr<const NetworkInterface>& ni) {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040070 logEvent(ni) << "onInterfaceAdded\n" << *ni;
Davide Pesaventof35c4272017-07-14 11:13:34 -040071 logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << std::endl;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040072
73 ni->onAddressAdded.connect([ni] (const NetworkAddress& address) {
74 logEvent(ni) << "onAddressAdded " << address << std::endl;
Junxiao Shi2dc416d2017-07-03 04:46:16 +000075 });
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080076
Davide Pesavento2bf35a62017-04-02 00:41:06 -040077 ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) {
78 logEvent(ni) << "onAddressRemoved " << address << std::endl;
79 });
80
81 ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) {
82 logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl;
Davide Pesaventof35c4272017-07-14 11:13:34 -040083 logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << std::endl;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040084 });
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
Junxiao Shi2dc416d2017-07-03 04:46:16 +000091 monitor.onInterfaceRemoved.connect([] (const shared_ptr<const NetworkInterface>& ni) {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040092 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
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080099
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400100} // namespace tests
Junxiao Shi25467942017-06-30 02:53:14 +0000101} // namespace net
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800102} // namespace ndn