util: NetworkMonitor: fine-grained signals on interface/address changes
Change-Id: I60d4cc6d673b920ba81d57502977f0340be0da48
Refs: #3353
diff --git a/tests/integrated/network-monitor.cpp b/tests/integrated/network-monitor.cpp
index cbc0e8a..3b4a516 100644
--- a/tests/integrated/network-monitor.cpp
+++ b/tests/integrated/network-monitor.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -25,6 +25,8 @@
#include "util/network-monitor.hpp"
+#include "util/network-address.hpp"
+#include "util/network-interface.hpp"
#include "util/time.hpp"
#include "boost-test.hpp"
@@ -34,25 +36,66 @@
namespace ndn {
namespace util {
+namespace tests {
-BOOST_AUTO_TEST_SUITE(UtilNetworkMonitor)
+BOOST_AUTO_TEST_SUITE(Util)
+BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
-BOOST_AUTO_TEST_CASE(Basic)
+static std::ostream&
+logEvent(const shared_ptr<NetworkInterface>& ni = nullptr, std::ostream& os = std::cout)
+{
+ os << '[' << time::toIsoString(time::system_clock::now()) << "] ";
+ if (ni != nullptr)
+ os << ni->getName() << ": ";
+ return os;
+}
+
+BOOST_AUTO_TEST_CASE(Signals)
{
boost::asio::io_service io;
- BOOST_REQUIRE_NO_THROW((NetworkMonitor(io)));
-
NetworkMonitor monitor(io);
monitor.onNetworkStateChanged.connect([] {
- std::cout << time::toString(time::system_clock::now())
- << "\tReceived network state change event" << std::endl;
+ logEvent() << "onNetworkStateChanged" << std::endl;
+ });
+
+ monitor.onEnumerationCompleted.connect([&monitor] {
+ logEvent() << "onEnumerationCompleted" << std::endl;
+ for (const auto& ni : monitor.listNetworkInterfaces()) {
+ std::cout << *ni;
+ }
+ });
+
+ monitor.onInterfaceAdded.connect([] (const shared_ptr<NetworkInterface>& ni) {
+ logEvent(ni) << "onInterfaceAdded\n" << *ni;
+
+ ni->onAddressAdded.connect([ni] (const NetworkAddress& address) {
+ logEvent(ni) << "onAddressAdded " << address << std::endl;
});
+ ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) {
+ logEvent(ni) << "onAddressRemoved " << address << std::endl;
+ });
+
+ ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) {
+ logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl;
+ });
+
+ ni->onMtuChanged.connect([ni] (uint32_t oldMtu, uint32_t newMtu) {
+ logEvent(ni) << "onMtuChanged " << oldMtu << " -> " << newMtu << std::endl;
+ });
+ }); // monitor.onInterfaceAdded.connect
+
+ monitor.onInterfaceRemoved.connect([] (const shared_ptr<NetworkInterface>& ni) {
+ logEvent(ni) << "onInterfaceRemoved" << std::endl;
+ });
+
io.run();
}
-BOOST_AUTO_TEST_SUITE_END()
+BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor
+BOOST_AUTO_TEST_SUITE_END() // Util
+} // namespace tests
} // namespace util
} // namespace ndn