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