Build against Qt 4.6 again
[quassel.git] / src / common / remotepeer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QHostAddress>
22 #include <QTimer>
23
24 #ifdef HAVE_SSL
25 #  include <QSslSocket>
26 #else
27 #  include <QTcpSocket>
28 #endif
29
30 #include "remotepeer.h"
31
32 using namespace Protocol;
33
34 RemotePeer::RemotePeer(QTcpSocket *socket, QObject *parent)
35     : Peer(parent),
36     _socket(socket),
37     _signalProxy(0),
38     _heartBeatTimer(new QTimer(this)),
39     _heartBeatCount(0),
40     _lag(0)
41 {
42     socket->setParent(this);
43     connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
44     connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SIGNAL(error(QAbstractSocket::SocketError)));
45
46 #ifdef HAVE_SSL
47     QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
48     if (sslSocket)
49         connect(sslSocket, SIGNAL(encrypted()), SIGNAL(secureStateChanged()));
50 #endif
51
52     connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
53 }
54
55
56 QString RemotePeer::description() const
57 {
58     if (socket())
59         return socket()->peerAddress().toString();
60
61     return QString();
62 }
63
64
65 ::SignalProxy *RemotePeer::signalProxy() const
66 {
67     return _signalProxy;
68 }
69
70
71 void RemotePeer::setSignalProxy(::SignalProxy *proxy)
72 {
73     if (proxy == _signalProxy)
74         return;
75
76     if (!proxy) {
77         _heartBeatTimer->stop();
78         disconnect(signalProxy(), 0, this, 0);
79         _signalProxy = 0;
80         if (isOpen())
81             close();
82     }
83     else {
84         if (signalProxy()) {
85             qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
86             return;
87         }
88         _signalProxy = proxy;
89         connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
90         _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
91         _heartBeatTimer->start();
92     }
93 }
94
95
96 void RemotePeer::changeHeartBeatInterval(int secs)
97 {
98     if(secs <= 0)
99         _heartBeatTimer->stop();
100     else {
101         _heartBeatTimer->setInterval(secs * 1000);
102         _heartBeatTimer->start();
103     }
104 }
105
106
107 int RemotePeer::lag() const
108 {
109     return _lag;
110 }
111
112
113 QTcpSocket *RemotePeer::socket() const
114 {
115     return _socket;
116 }
117
118
119 bool RemotePeer::isSecure() const
120 {
121     if (socket()) {
122         if (isLocal())
123             return true;
124 #ifdef HAVE_SSL
125         QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
126         if (sslSocket && sslSocket->isEncrypted())
127             return true;
128 #endif
129     }
130     return false;
131 }
132
133
134 bool RemotePeer::isLocal() const
135 {
136     if (socket()) {
137         if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
138             return true;
139     }
140     return false;
141 }
142
143
144 bool RemotePeer::isOpen() const
145 {
146     return socket() && socket()->state() == QTcpSocket::ConnectedState;
147 }
148
149
150 void RemotePeer::close(const QString &reason)
151 {
152     if (!reason.isEmpty()) {
153         qWarning() << "Disconnecting:" << reason;
154     }
155
156     if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
157         socket()->disconnectFromHost();
158     }
159 }
160
161
162 void RemotePeer::handle(const HeartBeat &heartBeat)
163 {
164     dispatch(HeartBeatReply(heartBeat.timestamp()));
165 }
166
167
168 void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
169 {
170     _heartBeatCount = 0;
171 #if QT_VERSION >= 0x040900
172     emit lagUpdated(heartBeatReply.timestamp().msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
173 #else
174     emit lagUpdated(heartBeatReply.timestamp().time().msecsTo(QDateTime::currentDateTime().toUTC().time()) / 2);
175 #endif
176 }
177
178
179 void RemotePeer::sendHeartBeat()
180 {
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)";
184         socket()->close();
185         _heartBeatTimer->stop();
186         return;
187     }
188
189     if (_heartBeatCount > 0) {
190         _lag = _heartBeatCount * _heartBeatTimer->interval();
191         emit lagUpdated(_lag);
192     }
193
194     dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));
195     ++_heartBeatCount;
196 }