1 /***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <QHostAddress>
25 # include <QSslSocket>
27 # include <QTcpSocket>
30 #include "remotepeer.h"
32 using namespace Protocol;
34 RemotePeer::RemotePeer(::AuthHandler *authHandler, QTcpSocket *socket, QObject *parent)
35 : Peer(authHandler, parent),
38 _heartBeatTimer(new QTimer(this)),
42 socket->setParent(this);
43 connect(socket, SIGNAL(readyRead()), SLOT(onSocketDataAvailable()));
44 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
45 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketError(QAbstractSocket::SocketError)));
46 connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
49 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
51 connect(sslSocket, SIGNAL(encrypted()), SIGNAL(secureStateChanged()));
54 connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
56 // It's possible that more data has already arrived during the handshake, so readyRead() wouldn't be triggered.
57 // However, we can't call a virtual function from the ctor, so let's do it asynchronously.
58 if (socket->bytesAvailable())
59 QTimer::singleShot(0, this, SLOT(onSocketDataAvailable()));
63 void RemotePeer::onSocketStateChanged(QAbstractSocket::SocketState state)
65 if (state == QAbstractSocket::ClosingState) {
66 emit statusMessage(tr("Disconnecting..."));
71 void RemotePeer::onSocketError(QAbstractSocket::SocketError error)
73 emit socketError(error, socket()->errorString());
77 QString RemotePeer::description() const
80 return socket()->peerAddress().toString();
86 ::SignalProxy *RemotePeer::signalProxy() const
92 void RemotePeer::setSignalProxy(::SignalProxy *proxy)
94 if (proxy == _signalProxy)
98 _heartBeatTimer->stop();
99 disconnect(signalProxy(), 0, this, 0);
106 qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
109 _signalProxy = proxy;
110 connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
111 _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
112 _heartBeatTimer->start();
117 void RemotePeer::changeHeartBeatInterval(int secs)
120 _heartBeatTimer->stop();
122 _heartBeatTimer->setInterval(secs * 1000);
123 _heartBeatTimer->start();
128 int RemotePeer::lag() const
134 QTcpSocket *RemotePeer::socket() const
140 bool RemotePeer::isSecure() const
146 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
147 if (sslSocket && sslSocket->isEncrypted())
155 bool RemotePeer::isLocal() const
158 if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
165 bool RemotePeer::isOpen() const
167 return socket() && socket()->state() == QTcpSocket::ConnectedState;
171 void RemotePeer::close(const QString &reason)
173 if (!reason.isEmpty()) {
174 qWarning() << "Disconnecting:" << reason;
177 if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
178 socket()->disconnectFromHost();
183 void RemotePeer::handle(const HeartBeat &heartBeat)
185 dispatch(HeartBeatReply(heartBeat.timestamp));
189 void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
192 #if QT_VERSION >= 0x040900
193 emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
195 emit lagUpdated(heartBeatReply.timestamp.time().msecsTo(QDateTime::currentDateTime().toUTC().time()) / 2);
200 void RemotePeer::sendHeartBeat()
202 if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
203 qWarning() << "Disconnecting peer:" << description()
204 << "(didn't receive a heartbeat for over" << _heartBeatCount *_heartBeatTimer->interval() / 1000 << "seconds)";
206 _heartBeatTimer->stop();
210 if (_heartBeatCount > 0) {
211 _lag = _heartBeatCount * _heartBeatTimer->interval();
212 emit lagUpdated(_lag);
215 dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));