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 | 394977c | 2020-04-25 21:40:31 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
Davide Pesavento | 394977c | 2020-04-25 21:40:31 -0400 | [diff] [blame] | 22 | #define BOOST_TEST_MODULE ndn-cxx Integration (NetworkMonitor) |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/net/network-monitor.hpp" |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "ndn-cxx/net/network-address.hpp" |
| 28 | #include "ndn-cxx/net/network-interface.hpp" |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 29 | #include "ndn-cxx/net/impl/link-type-helper.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 30 | #include "ndn-cxx/util/string-helper.hpp" |
| 31 | #include "ndn-cxx/util/time.hpp" |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 32 | |
| 33 | #include <boost/asio/io_service.hpp> |
| 34 | #include <iostream> |
| 35 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 36 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 37 | namespace net { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 38 | namespace tests { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 39 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 40 | static std::ostream& |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 41 | 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] | 42 | { |
| 43 | os << '[' << time::toIsoString(time::system_clock::now()) << "] "; |
| 44 | if (ni != nullptr) |
| 45 | os << ni->getName() << ": "; |
| 46 | return os; |
| 47 | } |
| 48 | |
| 49 | BOOST_AUTO_TEST_CASE(Signals) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 50 | { |
| 51 | boost::asio::io_service io; |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 52 | NetworkMonitor monitor(io); |
| 53 | |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 54 | std::cout << "capabilities=" << AsHex{monitor.getCapabilities()} << std::endl; |
Junxiao Shi | f4c1eb3 | 2017-05-30 17:05:10 +0000 | [diff] [blame] | 55 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 56 | monitor.onNetworkStateChanged.connect([] { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 57 | logEvent() << "onNetworkStateChanged" << std::endl; |
| 58 | }); |
| 59 | |
| 60 | monitor.onEnumerationCompleted.connect([&monitor] { |
| 61 | logEvent() << "onEnumerationCompleted" << std::endl; |
| 62 | for (const auto& ni : monitor.listNetworkInterfaces()) { |
| 63 | std::cout << *ni; |
| 64 | } |
| 65 | }); |
| 66 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 67 | monitor.onInterfaceAdded.connect([] (const shared_ptr<const NetworkInterface>& ni) { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 68 | logEvent(ni) << "onInterfaceAdded\n" << *ni; |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 69 | logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << std::endl; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 70 | |
| 71 | ni->onAddressAdded.connect([ni] (const NetworkAddress& address) { |
| 72 | logEvent(ni) << "onAddressAdded " << address << std::endl; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 73 | }); |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 74 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 75 | ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) { |
| 76 | logEvent(ni) << "onAddressRemoved " << address << std::endl; |
| 77 | }); |
| 78 | |
| 79 | ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) { |
| 80 | logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl; |
Davide Pesavento | f35c427 | 2017-07-14 11:13:34 -0400 | [diff] [blame] | 81 | logEvent(ni) << "link-type: " << detail::getLinkType(ni->getName()) << std::endl; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 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 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 89 | monitor.onInterfaceRemoved.connect([] (const shared_ptr<const NetworkInterface>& ni) { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 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 | } // namespace tests |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 97 | } // namespace net |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 98 | } // namespace ndn |