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 | #ifndef REMOTEPEER_H |
22 | #define REMOTEPEER_H |
23 | |
24 | #include <QDateTime> |
25 | |
26 | #include "compressor.h" |
27 | #include "peer.h" |
28 | #include "protocol.h" |
29 | #include "signalproxy.h" |
30 | |
31 | class QTimer; |
32 | |
33 | class AuthHandler; |
34 | |
35 | class RemotePeer : public Peer |
36 | { |
37 | Q_OBJECT |
38 | |
39 | public: |
40 | // import the virtuals from the baseclass |
41 | using Peer::handle; |
42 | using Peer::dispatch; |
43 | |
44 | RemotePeer(AuthHandler *authHandler, QTcpSocket *socket, Compressor::CompressionLevel level, QObject *parent = 0); |
45 | |
46 | void setSignalProxy(SignalProxy *proxy); |
47 | |
48 | virtual Protocol::Type protocol() const = 0; |
49 | virtual QString protocolName() const = 0; |
50 | virtual QString description() const; |
51 | virtual quint16 enabledFeatures() const { return 0; } |
52 | |
53 | bool isOpen() const; |
54 | bool isSecure() const; |
55 | bool isLocal() const; |
56 | |
57 | int lag() const; |
58 | |
59 | bool compressionEnabled() const; |
60 | void setCompressionEnabled(bool enabled); |
61 | |
62 | QTcpSocket *socket() const; |
63 | |
64 | public slots: |
65 | void close(const QString &reason = QString()); |
66 | |
67 | signals: |
68 | void transferProgress(int current, int max); |
69 | void socketError(QAbstractSocket::SocketError error, const QString &errorString); |
70 | void statusMessage(const QString &msg); |
71 | |
72 | protected: |
73 | SignalProxy *signalProxy() const; |
74 | |
75 | void writeMessage(const QByteArray &msg); |
76 | virtual void processMessage(const QByteArray &msg) = 0; |
77 | |
78 | // These protocol messages get handled internally and won't reach SignalProxy |
79 | void handle(const Protocol::HeartBeat &heartBeat); |
80 | void handle(const Protocol::HeartBeatReply &heartBeatReply); |
81 | virtual void dispatch(const Protocol::HeartBeat &msg) = 0; |
82 | virtual void dispatch(const Protocol::HeartBeatReply &msg) = 0; |
83 | |
84 | protected slots: |
85 | virtual void onSocketStateChanged(QAbstractSocket::SocketState state); |
86 | virtual void onSocketError(QAbstractSocket::SocketError error); |
87 | |
88 | private slots: |
89 | void onReadyRead(); |
90 | void onCompressionError(Compressor::Error error); |
91 | |
92 | void sendHeartBeat(); |
93 | void changeHeartBeatInterval(int secs); |
94 | |
95 | private: |
96 | bool readMessage(QByteArray &msg); |
97 | |
98 | private: |
99 | QTcpSocket *_socket; |
100 | Compressor *_compressor; |
101 | SignalProxy *_signalProxy; |
102 | QTimer *_heartBeatTimer; |
103 | int _heartBeatCount; |
104 | int _lag; |
105 | quint32 _msgSize; |
106 | }; |
107 | |
108 | #endif |
109 | |