From: Jonas Heese Date: Wed, 27 Feb 2008 18:58:30 +0000 (+0000) Subject: make use of "use random server to connect"-setting and cycle through available server... X-Git-Tag: 0.2.0-alpha2~11 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=236e70030428e5b85309d6ddb97b772fa3efe61a make use of "use random server to connect"-setting and cycle through available servers if this setting is not present --- diff --git a/src/core/networkconnection.cpp b/src/core/networkconnection.cpp index 93568b04..fd7e2ba5 100644 --- a/src/core/networkconnection.cpp +++ b/src/core/networkconnection.cpp @@ -46,7 +46,8 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session) : Q _autoReconnectCount(0) { _autoReconnectTimer.setSingleShot(true); - + _previousConnectionAttemptFailed = false; + _lastUsedServerlistIndex = 0; // TODO make configurable _whoTimer.setInterval(60 * 1000); _whoTimer.setSingleShot(false); @@ -194,9 +195,20 @@ void NetworkConnection::connectToIrc(bool reconnecting) { qWarning() << "Invalid identity configures, ignoring connect request!"; return; } - // TODO implement cycling / random servers - QString host = serverList[0].toMap()["Host"].toString(); - quint16 port = serverList[0].toMap()["Port"].toUInt(); + // use a random server? + if(network()->useRandomServer()) { + _lastUsedServerlistIndex = qrand() % serverList.size(); + } else if(_previousConnectionAttemptFailed) { + // cycle to next server if previous connection attempt failed + displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Connection failed. Cycling to next Server")); + if(++_lastUsedServerlistIndex == serverList.size()) { + _lastUsedServerlistIndex = 0; + } + } + _previousConnectionAttemptFailed = false; + + QString host = serverList[_lastUsedServerlistIndex].toMap()["Host"].toString(); + quint16 port = serverList[_lastUsedServerlistIndex].toMap()["Port"].toUInt(); displayStatusMsg(tr("Connecting to %1:%2...").arg(host).arg(port)); displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Connecting to %1:%2...").arg(host).arg(port)); socket.connectToHost(host, port); @@ -266,6 +278,7 @@ void NetworkConnection::socketHasData() { } void NetworkConnection::socketError(QAbstractSocket::SocketError) { + _previousConnectionAttemptFailed = true; qDebug() << qPrintable(tr("Could not connect to %1 (%2)").arg(network()->networkName(), socket.errorString())); emit connectionError(socket.errorString()); emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Connection failure: %1").arg(socket.errorString())); @@ -274,6 +287,8 @@ void NetworkConnection::socketError(QAbstractSocket::SocketError) { setConnectionState(Network::Disconnected); socketDisconnected(); } + // mark last connection attempt as failed + //qDebug() << "exiting..."; //exit(1); } diff --git a/src/core/networkconnection.h b/src/core/networkconnection.h index d6d18306..649171db 100644 --- a/src/core/networkconnection.h +++ b/src/core/networkconnection.h @@ -148,6 +148,9 @@ private: QTimer _whoTimer; + bool _previousConnectionAttemptFailed; + int _lastUsedServerlistIndex; + class ParseError : public Exception { public: ParseError(QString cmd, QString prefix, QStringList params);