net: NetworkMonitor: use an enum to track the interface enumeration phase
Change-Id: Idf8e53025f9648f346c65bbf90007028a4f1b586
diff --git a/ndn-cxx/net/impl/network-monitor-impl-netlink.cpp b/ndn-cxx/net/impl/network-monitor-impl-netlink.cpp
index 40dcccd..4190717 100644
--- a/ndn-cxx/net/impl/network-monitor-impl-netlink.cpp
+++ b/ndn-cxx/net/impl/network-monitor-impl-netlink.cpp
@@ -43,8 +43,6 @@
NetworkMonitorImplNetlink::NetworkMonitorImplNetlink(boost::asio::io_service& io)
: m_rtnlSocket(io)
, m_genlSocket(io)
- , m_isEnumeratingLinks(false)
- , m_isEnumeratingAddresses(false)
{
m_rtnlSocket.open();
@@ -55,10 +53,10 @@
}
m_rtnlSocket.registerNotificationCallback([this] (const auto& msg) { this->parseRtnlMessage(msg); });
+ m_phase = ENUMERATING_LINKS;
NDN_LOG_TRACE("enumerating links");
m_rtnlSocket.sendDumpRequest(RTM_GETLINK,
[this] (const auto& msg) { this->parseRtnlMessage(msg); });
- m_isEnumeratingLinks = true;
}
shared_ptr<const NetworkInterface>
@@ -80,12 +78,6 @@
return v;
}
-bool
-NetworkMonitorImplNetlink::isEnumerating() const
-{
- return m_isEnumeratingLinks || m_isEnumeratingAddresses;
-}
-
void
NetworkMonitorImplNetlink::parseRtnlMessage(const NetlinkMessage& nlmsg)
{
@@ -93,21 +85,21 @@
case RTM_NEWLINK:
case RTM_DELLINK:
parseLinkMessage(nlmsg);
- if (!isEnumerating())
+ if (m_phase == ENUMERATION_COMPLETE)
this->emitSignal(onNetworkStateChanged); // backward compat
break;
case RTM_NEWADDR:
case RTM_DELADDR:
parseAddressMessage(nlmsg);
- if (!isEnumerating())
+ if (m_phase == ENUMERATION_COMPLETE)
this->emitSignal(onNetworkStateChanged); // backward compat
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
parseRouteMessage(nlmsg);
- if (!isEnumerating())
+ if (m_phase == ENUMERATION_COMPLETE)
this->emitSignal(onNetworkStateChanged); // backward compat
break;
@@ -353,20 +345,22 @@
#endif // NDN_CXX_HAVE_NETLINK_EXT_ACK
}
- if (m_isEnumeratingLinks) {
+ switch (m_phase) {
+ case ENUMERATING_LINKS:
// links enumeration complete, now request all the addresses
- m_isEnumeratingLinks = false;
+ m_phase = ENUMERATING_ADDRS;
NDN_LOG_TRACE("enumerating addresses");
m_rtnlSocket.sendDumpRequest(RTM_GETADDR,
[this] (const auto& msg) { this->parseRtnlMessage(msg); });
- m_isEnumeratingAddresses = true;
- }
- else if (m_isEnumeratingAddresses) {
+ break;
+ case ENUMERATING_ADDRS:
// links and addresses enumeration complete
- m_isEnumeratingAddresses = false;
- // TODO: enumerate routes
+ m_phase = ENUMERATION_COMPLETE; // TODO: enumerate routes
NDN_LOG_DEBUG("enumeration complete");
this->emitSignal(onEnumerationCompleted);
+ break;
+ default:
+ break;
}
}
diff --git a/ndn-cxx/net/impl/network-monitor-impl-netlink.hpp b/ndn-cxx/net/impl/network-monitor-impl-netlink.hpp
index 559271f..c7388a4 100644
--- a/ndn-cxx/net/impl/network-monitor-impl-netlink.hpp
+++ b/ndn-cxx/net/impl/network-monitor-impl-netlink.hpp
@@ -63,9 +63,6 @@
listNetworkInterfaces() const final;
private:
- bool
- isEnumerating() const;
-
void
parseRtnlMessage(const NetlinkMessage& nlmsg);
@@ -88,8 +85,14 @@
std::map<int, shared_ptr<NetworkInterface>> m_interfaces; ///< ifindex => interface
RtnlSocket m_rtnlSocket; ///< rtnetlink socket
GenlSocket m_genlSocket; ///< generic netlink socket to communicate with nl80211
- bool m_isEnumeratingLinks; ///< true if a dump of all links is in progress
- bool m_isEnumeratingAddresses; ///< true if a dump of all addresses is in progress
+
+ enum {
+ ENUMERATION_NOT_STARTED,
+ ENUMERATING_LINKS, ///< a dump of all links (RTM_GETLINK) is in progress
+ ENUMERATING_ADDRS, ///< a dump of all addresses (RTM_GETADDR) is in progress
+ ENUMERATING_ROUTES, ///< a dump of all routes (RTM_GETROUTE) is in progress (unimplemented)
+ ENUMERATION_COMPLETE,
+ } m_phase = ENUMERATION_NOT_STARTED;
};
} // namespace net
diff --git a/ndn-cxx/net/network-monitor.hpp b/ndn-cxx/net/network-monitor.hpp
index 480a798..a28bee2 100644
--- a/ndn-cxx/net/network-monitor.hpp
+++ b/ndn-cxx/net/network-monitor.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -111,16 +111,16 @@
// be assigned to references below.
public: // signals
- /// Fires when network interfaces enumeration is complete
+ /// Fires when the enumeration of all network interfaces on the system is complete.
util::Signal<NetworkMonitorImpl>& onEnumerationCompleted;
- /// Fires when a new interface is added
+ /// Fires whenever a new interface is detected on the system.
util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceAdded;
/**
- * @brief Fires when an interface is removed
- * @note The NetworkInterface object is no longer present in the internal list of
- * network interfaces when this signal is emitted.
+ * @brief Fires whenever an interface disappears from the system.
+ * @note The NetworkInterface object has already been removed from the list
+ * returned by listNetworkInterfaces() when this signal is emitted.
*/
util::Signal<NetworkMonitorImpl, shared_ptr<const NetworkInterface>>& onInterfaceRemoved;