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