Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 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 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 26 | #include "net/network-monitor.hpp" |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 27 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 28 | #include "net/network-address.hpp" |
| 29 | #include "net/network-interface.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 30 | #include "net/detail/link-type-helper.hpp" |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 31 | #include "util/string-helper.hpp" |
| 32 | #include "util/time.hpp" |
Alexander Afanasyev | 5bcee10 | 2017-03-27 22:28:29 -0500 | [diff] [blame] | 33 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 34 | #include "boost-test.hpp" |
| 35 | |
| 36 | #include <boost/asio/io_service.hpp> |
| 37 | #include <iostream> |
| 38 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 39 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 40 | namespace net { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 41 | namespace tests { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 42 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 43 | BOOST_AUTO_TEST_SUITE(TestNetworkMonitor) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 44 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 45 | static std::ostream& |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 46 | logEvent(const shared_ptr<const NetworkInterface>& ni = nullptr, std::ostream& os = std::cout) |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 47 | { |
| 48 | os << '[' << time::toIsoString(time::system_clock::now()) << "] "; |
| 49 | if (ni != nullptr) |
| 50 | os << ni->getName() << ": "; |
| 51 | return os; |
| 52 | } |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(Signals) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 55 | { |
| 56 | boost::asio::io_service io; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 57 | NetworkMonitor monitor(io); |
| 58 | |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 59 | std::cout << "capabilities=" << AsHex{monitor.getCapabilities()} << std::endl; |
Junxiao Shi | f4c1eb3 | 2017-05-30 17:05:10 +0000 | [diff] [blame] | 60 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 61 | monitor.onNetworkStateChanged.connect([] { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 62 | logEvent() << "onNetworkStateChanged" << std::endl; |
| 63 | }); |
| 64 | |
| 65 | monitor.onEnumerationCompleted.connect([&monitor] { |
| 66 | logEvent() << "onEnumerationCompleted" << std::endl; |
| 67 | for (const auto& ni : monitor.listNetworkInterfaces()) { |
| 68 | std::cout << *ni; |
| 69 | } |
| 70 | }); |
| 71 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 72 | monitor.onInterfaceAdded.connect([] (const shared_ptr<const NetworkInterface>& ni) { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 73 | logEvent(ni) << "onInterfaceAdded\n" << *ni; |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 74 | logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << std::endl; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 75 | |
| 76 | ni->onAddressAdded.connect([ni] (const NetworkAddress& address) { |
| 77 | logEvent(ni) << "onAddressAdded " << address << std::endl; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 78 | }); |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 79 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 80 | ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) { |
| 81 | logEvent(ni) << "onAddressRemoved " << address << std::endl; |
| 82 | }); |
| 83 | |
| 84 | ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) { |
| 85 | logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl; |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 86 | logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << std::endl; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 87 | }); |
| 88 | |
| 89 | ni->onMtuChanged.connect([ni] (uint32_t oldMtu, uint32_t newMtu) { |
| 90 | logEvent(ni) << "onMtuChanged " << oldMtu << " -> " << newMtu << std::endl; |
| 91 | }); |
| 92 | }); // monitor.onInterfaceAdded.connect |
| 93 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 94 | monitor.onInterfaceRemoved.connect([] (const shared_ptr<const NetworkInterface>& ni) { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 95 | logEvent(ni) << "onInterfaceRemoved" << std::endl; |
| 96 | }); |
| 97 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 98 | io.run(); |
| 99 | } |
| 100 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 101 | BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 102 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 103 | } // namespace tests |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 104 | } // namespace net |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 105 | } // namespace ndn |