diff options
Diffstat (limited to 'distfoldd/discoverer.cc')
-rw-r--r-- | distfoldd/discoverer.cc | 56 |
1 files changed, 13 insertions, 43 deletions
diff --git a/distfoldd/discoverer.cc b/distfoldd/discoverer.cc index 629a955..011f938 100644 --- a/distfoldd/discoverer.cc +++ b/distfoldd/discoverer.cc @@ -8,8 +8,7 @@ Discoverer::Discoverer(const QUuid &uuid, uint port, const QString &serviceName, _mtime(), _mtimeChanged(false), _receiver(new QUdpSocket(this)), _sender(new QUdpSocket(this)), _netConfig(new QNetworkConfigurationManager(this)), - _netInfo(new QSystemNetworkInfo(this)), - _timer(new QSystemAlignedTimer(this)), _fallbackTimer(new QTimer(this)) + _timer(new QTimer(this)) { connect(_receiver, SIGNAL(readyRead()), SLOT(handleDataAvailable())); if (!_receiver->bind(servicePort, QUdpSocket::ShareAddress)) { @@ -17,21 +16,13 @@ Discoverer::Discoverer(const QUuid &uuid, uint port, const QString &serviceName, } connect(_netConfig, SIGNAL(onlineStateChanged(bool)), SLOT(handleOnlineStateChanged(bool))); connect(_timer, SIGNAL(timeout()), SLOT(handleTimerTimeout())); - connect(_fallbackTimer, SIGNAL(timeout()), SLOT(handleTimerTimeout())); - connect(_timer, SIGNAL(error(QSystemAlignedTimer::AlignedTimerError)), SLOT(handleTimerError())); _timer->setSingleShot(true); - _fallbackTimer->setSingleShot(true); + _timer->setTimerType(Qt::VeryCoarseTimer); if (_netConfig->isOnline() || _netConfig->allConfigurations(QNetworkConfiguration::Defined).empty()) { // Either only, or _netConfig has no configs so it is useless. qDebug() << "Start timer"; - _timer->start(0, activeBroadcastInterval); - if (_timer && _timer->lastError() != QSystemAlignedTimer::NoError) { - qDebug() << "QSystemAlignedTimer broken at start"; - delete _timer; - _timer = 0; - _fallbackTimer->start(activeBroadcastInterval * 1000); - } + _timer->start(activeBroadcastInterval * 1000); } else { qDebug() << "I am NOT online"; } @@ -52,8 +43,8 @@ void Discoverer::setLastModifiedTime(const QDateTime& dateTime) QByteArray Discoverer::encodeMessage() const { QByteArray ba; - QByteArray hostUuid = _hostUuid.toString().toAscii(); - QByteArray folderUuid = _folderUuid.toString().toAscii(); + QByteArray hostUuid = _hostUuid.toString().toLatin1(); + QByteArray folderUuid = _folderUuid.toString().toLatin1(); QByteArray serviceNameUtf8 = _serviceName.toUtf8(); Message msg; @@ -74,13 +65,8 @@ QByteArray Discoverer::encodeMessage() const void Discoverer::switchToActiveMode() { - if (_timer) { - if (!_timer->isActive() || _timer->maximumInterval() > activeBroadcastInterval) { - if (_timer->isActive()) _timer->wokeUp(); - _timer->start(0, activeBroadcastInterval); - } - } else if (!_fallbackTimer->isActive() || _fallbackTimer->interval() > activeBroadcastInterval * 1000) { - _fallbackTimer->start(activeBroadcastInterval * 1000); + if (!_timer->isActive() || _timer->interval() > activeBroadcastInterval * 1000) { + _timer->start(activeBroadcastInterval * 1000); } } @@ -112,9 +98,9 @@ void Discoverer::handleBroadcastMessage(const QHostAddress& sender, const QByteA char uuidData[sizeof(msg->hostUuid)]; strncpy(uuidData, msg->hostUuid, sizeof(uuidData)); - QUuid hostUuid(QString::fromAscii(uuidData, sizeof(uuidData))); + QUuid hostUuid(QString::fromLatin1(uuidData, sizeof(uuidData))); strncpy(uuidData, msg->folderUuid, sizeof(uuidData)); - QUuid folderUuid(QString::fromAscii(uuidData, sizeof(uuidData))); + QUuid folderUuid(QString::fromLatin1(uuidData, sizeof(uuidData))); if (hostUuid.isNull() || folderUuid.isNull()) { qWarning() << "Null uuid"; @@ -151,7 +137,7 @@ void Discoverer::handleHostSeen(const QUuid &hostUuid) void Discoverer::handleTimerTimeout() { - qDebug() << "Timer"; + qDebug() << "Timer tick"; broadcastMessage(); QDateTime now = QDateTime::currentDateTime(); QHash<QUuid, HostInfo>::iterator i = _knownHosts.begin(); @@ -163,33 +149,17 @@ void Discoverer::handleTimerTimeout() } } qDebug() << "Known hosts" << _knownHosts.size(); - if (_timer) { - _timer->start(idleBroadcastInterval * 0.7f, idleBroadcastInterval * 1.3f); - } else { - _fallbackTimer->start(idleBroadcastInterval * 1000); - } -} - -void Discoverer::handleTimerError() -{ - qDebug() << "SystemAlignedTimer broken"; - _timer->deleteLater(); - _timer = 0; - _fallbackTimer->start(activeBroadcastInterval * 1000); + _timer->start(idleBroadcastInterval * 1000); } void Discoverer::handleOnlineStateChanged(bool online) { if (online) { qDebug() << "Online"; - if (_netInfo->currentMode() == QSystemNetworkInfo::UnknownMode || - _netInfo->currentMode() == QSystemNetworkInfo::WlanMode) { - switchToActiveMode(); - } + switchToActiveMode(); } else { qDebug() << "Offline"; _knownHosts.clear(); - _fallbackTimer->stop(); - if (_timer) _timer->stop(); + _timer->stop(); } } |