Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 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 | #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 Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 28 | #include "util/network-address.hpp" |
| 29 | #include "util/network-interface.hpp" |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 30 | #include "util/time.hpp" |
| 31 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 32 | #include "boost-test.hpp" |
| 33 | |
| 34 | #include <boost/asio/io_service.hpp> |
| 35 | #include <iostream> |
| 36 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 37 | namespace ndn { |
| 38 | namespace util { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 39 | namespace tests { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 40 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(Util) |
| 42 | BOOST_AUTO_TEST_SUITE(TestNetworkMonitor) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 43 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 44 | static std::ostream& |
| 45 | logEvent(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 | |
| 53 | BOOST_AUTO_TEST_CASE(Signals) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 54 | { |
| 55 | boost::asio::io_service io; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 56 | NetworkMonitor monitor(io); |
| 57 | |
| 58 | monitor.onNetworkStateChanged.connect([] { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 59 | 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 | |
| 69 | monitor.onInterfaceAdded.connect([] (const shared_ptr<NetworkInterface>& ni) { |
| 70 | logEvent(ni) << "onInterfaceAdded\n" << *ni; |
| 71 | |
| 72 | ni->onAddressAdded.connect([ni] (const NetworkAddress& address) { |
| 73 | logEvent(ni) << "onAddressAdded " << address << std::endl; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 74 | }); |
| 75 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 76 | ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) { |
| 77 | logEvent(ni) << "onAddressRemoved " << address << std::endl; |
| 78 | }); |
| 79 | |
| 80 | ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) { |
| 81 | logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl; |
| 82 | }); |
| 83 | |
| 84 | ni->onMtuChanged.connect([ni] (uint32_t oldMtu, uint32_t newMtu) { |
| 85 | logEvent(ni) << "onMtuChanged " << oldMtu << " -> " << newMtu << std::endl; |
| 86 | }); |
| 87 | }); // monitor.onInterfaceAdded.connect |
| 88 | |
| 89 | monitor.onInterfaceRemoved.connect([] (const shared_ptr<NetworkInterface>& ni) { |
| 90 | logEvent(ni) << "onInterfaceRemoved" << std::endl; |
| 91 | }); |
| 92 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 93 | io.run(); |
| 94 | } |
| 95 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 96 | BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor |
| 97 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 98 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 99 | } // namespace tests |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 100 | } // namespace util |
| 101 | } // namespace ndn |