Encapsulate socket state in AuthHandler, properly handle disconnects
[quassel.git] / src / common / remotepeer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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(::AuthHandler *authHandler, QTcpSocket *socket, QObject *parent)
35     : Peer(authHandler, 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)), SLOT(onSocketError(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 void RemotePeer::onSocketStateChanged(QAbstractSocket::SocketState state)
57 {
58     if (state == QAbstractSocket::ClosingState) {
59         emit statusMessage(tr("Disconnecting..."));
60     }
61 }
62
63
64 void RemotePeer::onSocketError(QAbstractSocket::SocketError error)
65 {
66     emit socketError(error, socket()->errorString());
67 }
68
69
70 QString RemotePeer::description() const
71 {
72     if (socket())
73         return socket()->peerAddress().toString();
74
75     return QString();
76 }
77
78
79 ::SignalProxy *RemotePeer::signalProxy() const
80 {
81     return _signalProxy;
82 }
83
84
85 void RemotePeer::setSignalProxy(::SignalProxy *proxy)
86 {
87     if (proxy == _signalProxy)
88         return;
89
90     if (!proxy) {
91         _heartBeatTimer->stop();
92         disconnect(signalProxy(), 0, this, 0);
93         _signalProxy = 0;
94         if (isOpen())
95             close();
96     }
97     else {
98         if (signalProxy()) {
99             qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
100             return;
101         }
102         _signalProxy = proxy;
103         connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
104         _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
105         _heartBeatTimer->start();
106     }
107 }
108
109
110 void RemotePeer::changeHeartBeatInterval(int secs)
111 {
112     if(secs <= 0)
113         _heartBeatTimer->stop();
114     else {
115         _heartBeatTimer->setInterval(secs * 1000);
116         _heartBeatTimer->start();
117     }
118 }
119
120
121 int RemotePeer::lag() const
122 {
123     return _lag;
124 }
125
126
127 QTcpSocket *RemotePeer::socket() const
128 {
129     return _socket;
130 }
131
132
133 bool RemotePeer::isSecure() const
134 {
135     if (socket()) {
136         if (isLocal())
137             return true;
138 #ifdef HAVE_SSL
139         QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
140         if (sslSocket && sslSocket->isEncrypted())
141             return true;
142 #endif
143     }
144     return false;
145 }
146
147
148 bool RemotePeer::isLocal() const
149 {
150     if (socket()) {
151         if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
152             return true;
153     }
154     return false;
155 }
156
157
158 bool RemotePeer::isOpen() const
159 {
160     return socket() && socket()->state() == QTcpSocket::ConnectedState;
161 }
162
163
164 void RemotePeer::close(const QString &reason)
165 {
166     if (!reason.isEmpty()) {
167         qWarning() << "Disconnecting:" << reason;
168     }
169
170     if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
171         socket()->disconnectFromHost();
172     }
173 }
174
175
176 void RemotePeer::handle(const HeartBeat &heartBeat)
177 {
178     dispatch(HeartBeatReply(heartBeat.timestamp));
179 }
180
181
182 void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
183 {
184     _heartBeatCount = 0;
185 #if QT_VERSION >= 0x040900
186     emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
187 #else
188     emit lagUpdated(heartBeatReply.timestamp.time().msecsTo(QDateTime::currentDateTime().toUTC().time()) / 2);
189 #endif
190 }
191
192
193 void RemotePeer::sendHeartBeat()
194 {
195     if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
196         qWarning() << "Disconnecting peer:" << description()
197                    << "(didn't receive a heartbeat for over" << _heartBeatCount *_heartBeatTimer->interval() / 1000 << "seconds)";
198         socket()->close();
199         _heartBeatTimer->stop();
200         return;
201     }
202
203     if (_heartBeatCount > 0) {
204         _lag = _heartBeatCount * _heartBeatTimer->interval();
205         emit lagUpdated(_lag);
206     }
207
208     dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));
209     ++_heartBeatCount;
210 }