1 /***************************************************************************
2 * Copyright (C) 2005-2012 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 "remoteconnection.h"
30 RemoteConnection::RemoteConnection(QTcpSocket *socket, QObject *parent)
31 : SignalProxy::AbstractPeer(parent),
34 _heartBeatTimer(new QTimer(this)),
38 socket->setParent(this);
39 connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
40 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SIGNAL(error(QAbstractSocket::SocketError)));
43 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
45 connect(sslSocket, SIGNAL(encrypted()), SIGNAL(secureStateChanged()));
48 connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
52 QString RemoteConnection::description() const
55 return socket()->peerAddress().toString();
61 SignalProxy *RemoteConnection::signalProxy() const
67 void RemoteConnection::setSignalProxy(SignalProxy *proxy)
69 if (proxy == _signalProxy)
73 _heartBeatTimer->stop();
74 disconnect(signalProxy(), 0, this, 0);
81 qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
85 connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
86 _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
87 _heartBeatTimer->start();
92 void RemoteConnection::changeHeartBeatInterval(int secs)
95 _heartBeatTimer->stop();
97 _heartBeatTimer->setInterval(secs * 1000);
98 _heartBeatTimer->start();
103 int RemoteConnection::lag() const
109 QTcpSocket *RemoteConnection::socket() const
115 bool RemoteConnection::isSecure() const
121 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
122 if (sslSocket && sslSocket->isEncrypted())
130 bool RemoteConnection::isLocal() const
133 if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
140 bool RemoteConnection::isOpen() const
142 return socket() && socket()->state() == QTcpSocket::ConnectedState;
146 void RemoteConnection::close(const QString &reason)
148 if (!reason.isEmpty()) {
149 qWarning() << "Disconnecting:" << reason;
152 if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
153 socket()->disconnectFromHost();
158 void RemoteConnection::handle(const HeartBeat &heartBeat)
160 dispatch(HeartBeatReply(heartBeat.timestamp()));
164 void RemoteConnection::handle(const HeartBeatReply &heartBeatReply)
167 emit lagUpdated(heartBeatReply.timestamp().msecsTo(QDateTime::currentDateTimeUtc()));
171 void RemoteConnection::sendHeartBeat()
173 if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
174 qWarning() << "Disconnecting peer:" << description()
175 << "(didn't receive a heartbeat for over" << _heartBeatCount *_heartBeatTimer->interval() / 1000 << "seconds)";
177 _heartBeatTimer->stop();
181 if (_heartBeatCount > 0) {
182 _lag = _heartBeatCount * _heartBeatTimer->interval();
183 emit lagUpdated(_lag);
186 dispatch(HeartBeat(QDateTime::currentDateTimeUtc()));