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