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