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