1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
24 #include <QAbstractSocket>
25 #include <QDataStream>
28 #include "authhandler.h"
30 #include "signalproxy.h"
32 class Peer : public QObject
37 Peer(AuthHandler *authHandler, QObject *parent = 0);
39 virtual Protocol::Type protocol() const = 0;
40 virtual QString description() const = 0;
42 virtual SignalProxy *signalProxy() const = 0;
43 virtual void setSignalProxy(SignalProxy *proxy) = 0;
45 AuthHandler *authHandler() const;
47 virtual bool isOpen() const = 0;
48 virtual bool isSecure() const = 0;
49 virtual bool isLocal() const = 0;
51 virtual int lag() const = 0;
56 QString _clientVersion;
59 /* Handshake messages */
60 virtual void dispatch(const Protocol::RegisterClient &) = 0;
61 virtual void dispatch(const Protocol::ClientDenied &) = 0;
62 virtual void dispatch(const Protocol::ClientRegistered &) = 0;
63 virtual void dispatch(const Protocol::SetupData &) = 0;
64 virtual void dispatch(const Protocol::SetupFailed &) = 0;
65 virtual void dispatch(const Protocol::SetupDone &) = 0;
66 virtual void dispatch(const Protocol::Login &) = 0;
67 virtual void dispatch(const Protocol::LoginFailed &) = 0;
68 virtual void dispatch(const Protocol::LoginSuccess &) = 0;
69 virtual void dispatch(const Protocol::SessionState &) = 0;
71 /* Sigproxy messages */
72 virtual void dispatch(const Protocol::SyncMessage &) = 0;
73 virtual void dispatch(const Protocol::RpcCall &) = 0;
74 virtual void dispatch(const Protocol::InitRequest &) = 0;
75 virtual void dispatch(const Protocol::InitData &) = 0;
77 virtual void close(const QString &reason = QString()) = 0;
81 void secureStateChanged(bool secure = true);
82 void lagUpdated(int msecs);
86 void handle(const T &protoMessage);
89 QPointer<AuthHandler> _authHandler;
92 // We need to special-case Peer* in attached signals/slots, so typedef it for the meta type system
93 typedef Peer * PeerPtr;
94 Q_DECLARE_METATYPE(PeerPtr)
96 QDataStream &operator<<(QDataStream &out, PeerPtr ptr);
97 QDataStream &operator>>(QDataStream &in, PeerPtr &ptr);
100 // Template method needed in the header
101 template<typename T> inline
102 void Peer::handle(const T &protoMessage)
104 switch(protoMessage.handler()) {
105 case Protocol::SignalProxy:
106 if (!signalProxy()) {
107 qWarning() << Q_FUNC_INFO << "Cannot handle message without a SignalProxy!";
110 signalProxy()->handle(this, protoMessage);
113 case Protocol::AuthHandler:
114 if (!authHandler()) {
115 qWarning() << Q_FUNC_INFO << "Cannot handle auth messages without an active AuthHandler!";
118 authHandler()->handle(protoMessage);
122 qWarning() << Q_FUNC_INFO << "Unknown handler for protocol message!";