Fix discovery behaviour if the resouce is not available

This commit is contained in:
Simon Stürz 2022-05-03 14:30:01 +02:00
parent 1d04da6205
commit 17fc204122

View File

@ -84,8 +84,11 @@ NetworkDeviceDiscoveryImpl::NetworkDeviceDiscoveryImpl(QObject *parent) :
m_cacheSettings = new QSettings(NymeaSettings::cachePath() + "/network-device-discovery.cache", QSettings::IniFormat); m_cacheSettings = new QSettings(NymeaSettings::cachePath() + "/network-device-discovery.cache", QSettings::IniFormat);
loadNetworkDeviceCache(); loadNetworkDeviceCache();
// Start the monitor timer in any case, we do also the cache cleanup there... // Start the timer only if the resource is available
m_monitorTimer->start(); if (available()) {
// Start the monitor timer in any case, we do also the cache cleanup there...
m_monitorTimer->start();
}
} }
NetworkDeviceDiscoveryImpl::~NetworkDeviceDiscoveryImpl() NetworkDeviceDiscoveryImpl::~NetworkDeviceDiscoveryImpl()
@ -100,9 +103,17 @@ NetworkDeviceDiscoveryReply *NetworkDeviceDiscoveryImpl::discover()
return m_currentReply; return m_currentReply;
} }
qCInfo(dcNetworkDeviceDiscovery()) << "Starting network device discovery ...";
m_currentReply = new NetworkDeviceDiscoveryReplyImpl(this); m_currentReply = new NetworkDeviceDiscoveryReplyImpl(this);
if (!available()) {
qCWarning(dcNetworkDeviceDiscovery()) << "The network discovery is not available. Please make sure the binary has the required capability (CAP_NET_RAW) or start the application as root.";
// Finish the discovery in the next event loop so anny connections after the creation will work as expected
QTimer::singleShot(0, this, &NetworkDeviceDiscoveryImpl::finishDiscovery);
return m_currentReply;
}
connect(m_currentReply, &NetworkDeviceDiscoveryReplyImpl::networkDeviceInfoAdded, this, &NetworkDeviceDiscoveryImpl::updateCache); connect(m_currentReply, &NetworkDeviceDiscoveryReplyImpl::networkDeviceInfoAdded, this, &NetworkDeviceDiscoveryImpl::updateCache);
qCInfo(dcNetworkDeviceDiscovery()) << "Starting network device discovery ...";
if (m_ping->available()) if (m_ping->available())
pingAllNetworkDevices(); pingAllNetworkDevices();
@ -136,15 +147,16 @@ bool NetworkDeviceDiscoveryImpl::running() const
NetworkDeviceMonitor *NetworkDeviceDiscoveryImpl::registerMonitor(const MacAddress &macAddress) NetworkDeviceMonitor *NetworkDeviceDiscoveryImpl::registerMonitor(const MacAddress &macAddress)
{ {
qCInfo(dcNetworkDeviceDiscovery()) << "Register new network device monitor for" << macAddress; qCInfo(dcNetworkDeviceDiscovery()) << "Register new network device monitor for" << macAddress;
// Make sure we creare only one monitor per mac // Make sure we create only one monitor per MAC
if (m_monitors.contains(macAddress)) if (m_monitors.contains(macAddress))
return m_monitors.value(macAddress); return m_monitors.value(macAddress);
if (macAddress.isNull()) { if (macAddress.isNull()) {
qCWarning(dcNetworkDeviceDiscovery()) << "Could not register monitor for invalid" << macAddress.toString(); qCWarning(dcNetworkDeviceDiscovery()) << "Could not register monitor for invalid" << macAddress;
return nullptr; return nullptr;
} }
// Fill in cached information // Fill in cached information
NetworkDeviceInfo info; NetworkDeviceInfo info;
if (m_networkInfoCache.contains(macAddress)) { if (m_networkInfoCache.contains(macAddress)) {
@ -155,9 +167,13 @@ NetworkDeviceMonitor *NetworkDeviceDiscoveryImpl::registerMonitor(const MacAddre
NetworkDeviceMonitorImpl *monitor = new NetworkDeviceMonitorImpl(macAddress, this); NetworkDeviceMonitorImpl *monitor = new NetworkDeviceMonitorImpl(macAddress, this);
monitor->setNetworkDeviceInfo(info); monitor->setNetworkDeviceInfo(info);
m_monitors.insert(macAddress, monitor); m_monitors.insert(macAddress, monitor);
if (!available()) {
qCWarning(dcNetworkDeviceDiscovery()) << "Registered monitor but the hardware resource is not available. The monitor will not work as expected" << monitor;
return monitor;
}
// Restart the monitor timer since we evaluate this one now // Restart the monitor timer since we evaluate this one now
m_monitorTimer->start(); m_monitorTimer->start();