1 /***************************************************************************
2 * Copyright (C) 2005-2013 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>
28 #include "remotepeer.h"
30 using namespace Protocol;
32 RemotePeer::RemotePeer(QTcpSocket *socket, QObject *parent)
33 : SignalProxy::AbstractPeer(parent),
36 _heartBeatTimer(new QTimer(this)),
40 socket->setParent(this);
41 connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
42 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SIGNAL(error(QAbstractSocket::SocketError)));
45 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
47 connect(sslSocket, SIGNAL(encrypted()), SIGNAL(secureStateChanged()));
50 connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
54 QString RemotePeer::description() const
57 return socket()->peerAddress().toString();
63 ::SignalProxy *RemotePeer::signalProxy() const
69 void RemotePeer::setSignalProxy(::SignalProxy *proxy)
71 if (proxy == _signalProxy)
75 _heartBeatTimer->stop();
76 disconnect(signalProxy(), 0, this, 0);
83 qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
87 connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
88 _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
89 _heartBeatTimer->start();
94 void RemotePeer::changeHeartBeatInterval(int secs)
97 _heartBeatTimer->stop();
99 _heartBeatTimer->setInterval(secs * 1000);
100 _heartBeatTimer->start();
105 int RemotePeer::lag() const
111 QTcpSocket *RemotePeer::socket() const
117 bool RemotePeer::isSecure() const
123 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
124 if (sslSocket && sslSocket->isEncrypted())
132 bool RemotePeer::isLocal() const
135 if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
142 bool RemotePeer::isOpen() const
144 return socket() && socket()->state() == QTcpSocket::ConnectedState;
148 void RemotePeer::close(const QString &reason)
150 if (!reason.isEmpty()) {
151 qWarning() << "Disconnecting:" << reason;
154 if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
155 socket()->disconnectFromHost();
160 void RemotePeer::handle(const HeartBeat &heartBeat)
162 dispatch(HeartBeatReply(heartBeat.timestamp()));
166 void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
169 #if QT_VERSION < 0x040700
170 emit lagUpdated(heartBeatReply.timestamp().time().msecsTo(QTime::currentTime()) / 2);
172 emit lagUpdated(heartBeatReply.timestamp().msecsTo(QDateTime::currentDateTime()) / 2);
177 void RemotePeer::sendHeartBeat()
179 if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
180 qWarning() << "Disconnecting peer:" << description()
181 << "(didn't receive a heartbeat for over" << _heartBeatCount *_heartBeatTimer->interval() / 1000 << "seconds)";
183 _heartBeatTimer->stop();
187 if (_heartBeatCount > 0) {
188 _lag = _heartBeatCount * _heartBeatTimer->interval();
189 emit lagUpdated(_lag);
192 dispatch(HeartBeat(QDateTime::currentDateTime()));