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>
27 # include <QTcpSocket>
30 #include "remotepeer.h"
32 using namespace Protocol;
34 RemotePeer::RemotePeer(QTcpSocket *socket, QObject *parent)
38 _heartBeatTimer(new QTimer(this)),
42 socket->setParent(this);
43 connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
44 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SIGNAL(error(QAbstractSocket::SocketError)));
47 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
49 connect(sslSocket, SIGNAL(encrypted()), SIGNAL(secureStateChanged()));
52 connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
56 QString RemotePeer::description() const
59 return socket()->peerAddress().toString();
65 ::SignalProxy *RemotePeer::signalProxy() const
71 void RemotePeer::setSignalProxy(::SignalProxy *proxy)
73 if (proxy == _signalProxy)
77 _heartBeatTimer->stop();
78 disconnect(signalProxy(), 0, this, 0);
85 qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
89 connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
90 _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
91 _heartBeatTimer->start();
96 void RemotePeer::changeHeartBeatInterval(int secs)
99 _heartBeatTimer->stop();
101 _heartBeatTimer->setInterval(secs * 1000);
102 _heartBeatTimer->start();
107 int RemotePeer::lag() const
113 QTcpSocket *RemotePeer::socket() const
119 bool RemotePeer::isSecure() const
125 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
126 if (sslSocket && sslSocket->isEncrypted())
134 bool RemotePeer::isLocal() const
137 if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
144 bool RemotePeer::isOpen() const
146 return socket() && socket()->state() == QTcpSocket::ConnectedState;
150 void RemotePeer::close(const QString &reason)
152 if (!reason.isEmpty()) {
153 qWarning() << "Disconnecting:" << reason;
156 if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
157 socket()->disconnectFromHost();
162 void RemotePeer::handle(const HeartBeat &heartBeat)
164 dispatch(HeartBeatReply(heartBeat.timestamp()));
168 void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
171 #if QT_VERSION >= 0x040700
172 emit lagUpdated(heartBeatReply.timestamp().msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
174 emit lagUpdated(heartBeatReply.timestamp().time().msecsTo(QDateTime::currentDateTime().toUTC().time()) / 2);
179 void RemotePeer::sendHeartBeat()
181 if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
182 qWarning() << "Disconnecting peer:" << description()
183 << "(didn't receive a heartbeat for over" << _heartBeatCount *_heartBeatTimer->interval() / 1000 << "seconds)";
185 _heartBeatTimer->stop();
189 if (_heartBeatCount > 0) {
190 _lag = _heartBeatCount * _heartBeatTimer->interval();
191 emit lagUpdated(_lag);
194 dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));