02eb3c0cde3813bb20ee4a4f1d783262b0800fae
[quassel.git] / src / common / peer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 PEER_H
22 #define PEER_H
23
24 #include <QAbstractSocket>
25 #include <QPointer>
26
27 #include "authhandler.h"
28 #include "protocol.h"
29 #include "signalproxy.h"
30
31 class Peer : public QObject
32 {
33     Q_OBJECT
34
35 public:
36     Peer(AuthHandler *authHandler, QObject *parent = 0);
37
38     virtual Protocol::Type protocol() const = 0;
39     virtual QString description() const = 0;
40
41     virtual SignalProxy *signalProxy() const = 0;
42     virtual void setSignalProxy(SignalProxy *proxy) = 0;
43
44     AuthHandler *authHandler() const;
45
46     virtual bool isOpen() const = 0;
47     virtual bool isSecure() const = 0;
48     virtual bool isLocal() const = 0;
49
50     virtual int lag() const = 0;
51
52 public slots:
53     /* Handshake messages */
54     virtual void dispatch(const Protocol::RegisterClient &) = 0;
55     virtual void dispatch(const Protocol::ClientDenied &) = 0;
56     virtual void dispatch(const Protocol::ClientRegistered &) = 0;
57     virtual void dispatch(const Protocol::SetupData &) = 0;
58     virtual void dispatch(const Protocol::SetupFailed &) = 0;
59     virtual void dispatch(const Protocol::SetupDone &) = 0;
60     virtual void dispatch(const Protocol::Login &) = 0;
61     virtual void dispatch(const Protocol::LoginFailed &) = 0;
62     virtual void dispatch(const Protocol::LoginSuccess &) = 0;
63     virtual void dispatch(const Protocol::SessionState &) = 0;
64
65     /* Sigproxy messages */
66     virtual void dispatch(const Protocol::SyncMessage &) = 0;
67     virtual void dispatch(const Protocol::RpcCall &) = 0;
68     virtual void dispatch(const Protocol::InitRequest &) = 0;
69     virtual void dispatch(const Protocol::InitData &) = 0;
70
71     virtual void close(const QString &reason = QString()) = 0;
72
73 signals:
74     void disconnected();
75     void secureStateChanged(bool secure = true);
76     void lagUpdated(int msecs);
77
78 protected:
79     template<typename T>
80     void handle(const T &protoMessage);
81
82 private:
83     QPointer<AuthHandler> _authHandler;
84 };
85
86 // We need to special-case Peer* in attached signals/slots, so typedef it for the meta type system
87 typedef Peer * PeerPtr;
88 Q_DECLARE_METATYPE(PeerPtr)
89
90 QDataStream &operator<<(QDataStream &out, PeerPtr ptr);
91 QDataStream &operator>>(QDataStream &in, PeerPtr &ptr);
92
93
94 // Template method needed in the header
95 template<typename T> inline
96 void Peer::handle(const T &protoMessage)
97 {
98     switch(protoMessage.handler()) {
99         case Protocol::SignalProxy:
100             if (!signalProxy()) {
101                 qWarning() << Q_FUNC_INFO << "Cannot handle message without a SignalProxy!";
102                 return;
103             }
104             signalProxy()->handle(this, protoMessage);
105             break;
106
107         case Protocol::AuthHandler:
108             if (!authHandler()) {
109                 qWarning() << Q_FUNC_INFO << "Cannot handle auth messages without an active AuthHandler!";
110                 return;
111             }
112             authHandler()->handle(protoMessage);
113             break;
114
115         default:
116             qWarning() << Q_FUNC_INFO << "Unknown handler for protocol message!";
117             return;
118     }
119 }
120
121 #endif