Merge PR #493: Fix UPnP when multiple hosts reply and add more debug

This commit is contained in:
Jenkins nymea 2021-01-02 13:30:19 +01:00
commit 1f0b565c2d

View File

@ -135,51 +135,47 @@ void UpnpDiscovery::readData()
quint16 port; quint16 port;
QHostAddress hostAddress; QHostAddress hostAddress;
// read the answere from the multicast // read the answers from the multicast
while (socket->hasPendingDatagrams()) { while (socket->hasPendingDatagrams()) {
data.resize(socket->pendingDatagramSize()); data.resize(socket->pendingDatagramSize());
socket->readDatagram(data.data(), data.size(), &hostAddress, &port); socket->readDatagram(data.data(), data.size(), &hostAddress, &port);
}
if (!discovering()) { qDebug() << "Received UPnP datagram:" << data;
return;
}
// qDebug() << "upnp packet" << data; // if the data contains the HTTP OK header...
if (data.contains("HTTP/1.1 200 OK")) {
QUrl location;
bool isNymea = false;
// if the data contains the HTTP OK header... const QStringList lines = QString(data).split("\r\n");
if (data.contains("HTTP/1.1 200 OK")) { foreach (const QString& line, lines) {
QUrl location; int separatorIndex = line.indexOf(':');
bool isNymea = false; QString key = line.left(separatorIndex).toUpper();
QString value = line.mid(separatorIndex+1).trimmed();
const QStringList lines = QString(data).split("\r\n");
foreach (const QString& line, lines) {
int separatorIndex = line.indexOf(':');
QString key = line.left(separatorIndex).toUpper();
QString value = line.mid(separatorIndex+1).trimmed();
if (key.contains("Server") || key.contains("SERVER")) { if (key.contains("Server") || key.contains("SERVER")) {
if (value.contains("nymea")) { if (value.contains("nymea")) {
isNymea = true; isNymea = true;
}
}
// get location
if (key.contains("LOCATION") || key.contains("Location")) {
location = QUrl(value);
} }
} }
// get location if (!m_foundDevices.contains(location) && isNymea) {
if (key.contains("LOCATION") || key.contains("Location")) { m_foundDevices.append(location);
location = QUrl(value); // qDebug() << "Getting server data from:" << location;
QNetworkReply *reply = m_networkAccessManager->get(QNetworkRequest(location));
connect(reply, &QNetworkReply::sslErrors, [reply](const QList<QSslError> &errors){
reply->ignoreSslErrors(errors);
});
m_runningReplies.insert(reply, hostAddress);
} }
} }
if (!m_foundDevices.contains(location) && isNymea) {
m_foundDevices.append(location);
// qDebug() << "Getting server data from:" << location;
QNetworkReply *reply = m_networkAccessManager->get(QNetworkRequest(location));
connect(reply, &QNetworkReply::sslErrors, [reply](const QList<QSslError> &errors){
reply->ignoreSslErrors(errors);
});
m_runningReplies.insert(reply, hostAddress);
}
} }
} }