Refactor SignalProxy, network and protocol code
[quassel.git] / src / common / remoteconnection.h
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 #ifndef REMOTECONNECTION_H
22 #define REMOTECONNECTION_H
23
24 #include <QDateTime>
25 #include <QTcpSocket>
26
27 #include "signalproxy.h"
28
29 class QTimer;
30
31 class RemoteConnection : public SignalProxy::AbstractPeer
32 {
33     Q_OBJECT
34
35 public:
36     RemoteConnection(QTcpSocket *socket, QObject *parent = 0);
37     virtual ~RemoteConnection() {};
38
39     void setSignalProxy(SignalProxy *proxy);
40
41     QString description() const;
42
43     bool isOpen() const;
44     bool isSecure() const;
45     bool isLocal() const;
46
47     int lag() const;
48
49     bool compressionEnabled() const;
50     void setCompressionEnabled(bool enabled);
51
52     QTcpSocket *socket() const;
53
54     // this is only used for the auth phase and should be replaced by something more generic
55     virtual void writeSocketData(const QVariant &item) = 0;
56
57 public slots:
58     void close(const QString &reason = QString());
59
60 signals:
61     // this is only used for the auth phase and should be replaced by something more generic
62     void dataReceived(const QVariant &item);
63
64     void disconnected();
65     void error(QAbstractSocket::SocketError);
66
67     void transferProgress(int current, int max);
68
69 protected:
70     class HeartBeat;
71     class HeartBeatReply;
72
73     SignalProxy *signalProxy() const;
74
75     template<class T>
76     void handle(const T &protoMessage);
77
78     void handle(const HeartBeat &heartBeat);
79     void handle(const HeartBeatReply &heartBeatReply);
80
81     virtual void dispatch(const HeartBeat &msg) = 0;
82     virtual void dispatch(const HeartBeatReply &msg) = 0;
83
84 private slots:
85     void sendHeartBeat();
86     void changeHeartBeatInterval(int secs);
87
88 private:
89     QTcpSocket *_socket;
90     SignalProxy *_signalProxy;
91     QTimer *_heartBeatTimer;
92     int _heartBeatCount;
93     int _lag;
94 };
95
96
97 // These protocol messages get handled internally and won't reach SignalProxy
98
99 class RemoteConnection::HeartBeat
100 {
101 public:
102     inline HeartBeat(const QDateTime &timestamp) : _timestamp(timestamp) {}
103
104     inline QDateTime timestamp() const { return _timestamp; }
105
106 private:
107     QDateTime _timestamp;
108 };
109
110
111 class RemoteConnection::HeartBeatReply
112 {
113 public:
114     inline HeartBeatReply(const QDateTime &timestamp) : _timestamp(timestamp) {}
115
116     inline QDateTime timestamp() const { return _timestamp; }
117
118 private:
119     QDateTime _timestamp;
120 };
121
122
123 // Template methods we need in the header
124 template<class T> inline
125 void RemoteConnection::handle(const T &protoMessage)
126 {
127     if (!signalProxy()) {
128         qWarning() << Q_FUNC_INFO << "Cannot handle messages without a SignalProxy!";
129         return;
130     }
131
132     // _heartBeatCount = 0;
133
134     signalProxy()->handle(this, protoMessage);
135 }
136
137
138 #endif