Refactor SignalProxy, network and protocol code
[quassel.git] / src / common / internalconnection.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 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 #include <QCoreApplication>
22 #include <QThread>
23
24 #include "internalconnection.h"
25
26 template<class T>
27 class PeerMessageEvent : public QEvent
28 {
29 public:
30     PeerMessageEvent(InternalConnection *sender, InternalConnection::EventType eventType, const T &message)
31     : QEvent(QEvent::Type(eventType)), sender(sender), message(message) {}
32     InternalConnection *sender;
33     T message;
34 };
35
36
37 InternalConnection::InternalConnection(QObject *parent)
38     : SignalProxy::AbstractPeer(parent),
39     _proxy(0),
40     _peer(0),
41     _isOpen(true)
42 {
43
44 }
45
46
47 InternalConnection::~InternalConnection()
48 {
49     if (_isOpen)
50         emit disconnected();
51 }
52
53
54 QString InternalConnection::description() const
55 {
56     return tr("internal connection");
57 }
58
59
60 bool InternalConnection::isOpen() const
61 {
62     return true;
63 }
64
65
66 bool InternalConnection::isSecure() const
67 {
68     return true;
69 }
70
71
72 bool InternalConnection::isLocal() const
73 {
74     return true;
75 }
76
77
78 void InternalConnection::close(const QString &reason)
79 {
80     // FIXME
81     Q_UNUSED(reason)
82     qWarning() << "closing not implemented!";
83 }
84
85
86 int InternalConnection::lag() const
87 {
88     return 0;
89 }
90
91
92 void InternalConnection::setSignalProxy(SignalProxy *proxy)
93 {
94     if (!proxy && _proxy) {
95         _proxy = 0;
96         if (_isOpen) {
97             _isOpen = false;
98             emit disconnected();
99         }
100         return;
101     }
102
103     if (proxy && !_proxy) {
104         _proxy = proxy;
105         return;
106     }
107
108     qWarning() << Q_FUNC_INFO << "Changing the SignalProxy is not supported!";
109 }
110
111
112 void InternalConnection::setPeer(InternalConnection *peer)
113 {
114     if (_peer) {
115         qWarning() << Q_FUNC_INFO << "Peer already set, ignoring!";
116         return;
117     }
118     _peer = peer;
119     connect(peer, SIGNAL(disconnected()), SLOT(peerDisconnected()));
120 }
121
122
123 void InternalConnection::peerDisconnected()
124 {
125     disconnect(_peer, 0, this, 0);
126     _peer = 0;
127     if (_isOpen) {
128         _isOpen = false;
129         emit disconnected();
130     }
131 }
132
133
134 void InternalConnection::dispatch(const SignalProxy::SyncMessage &msg)
135 {
136     dispatch(SyncMessageEvent, msg);
137 }
138
139
140 void InternalConnection::dispatch(const SignalProxy::RpcCall &msg)
141 {
142     dispatch(RpcCallEvent, msg);
143 }
144
145
146 void InternalConnection::dispatch(const SignalProxy::InitRequest &msg)
147 {
148     dispatch(InitRequestEvent, msg);
149 }
150
151
152 void InternalConnection::dispatch(const SignalProxy::InitData &msg)
153 {
154     dispatch(InitDataEvent, msg);
155 }
156
157
158 template<class T>
159 void InternalConnection::dispatch(EventType eventType, const T &msg)
160 {
161     if (!_peer) {
162         qWarning() << Q_FUNC_INFO << "Cannot dispatch a message without a peer!";
163         return;
164     }
165
166     if(QThread::currentThread() == _peer->thread())
167         _peer->handle(msg);
168     else
169         QCoreApplication::postEvent(_peer, new PeerMessageEvent<T>(this, eventType, msg));
170 }
171
172
173 template<class T>
174 void InternalConnection::handle(const T &msg)
175 {
176     if (!_proxy) {
177         qWarning() << Q_FUNC_INFO << "Cannot handle a message without having a signal proxy set!";
178         return;
179     }
180
181     _proxy->handle(this, msg);
182 }
183
184
185 void InternalConnection::customEvent(QEvent *event)
186 {
187     switch ((int)event->type()) {
188         case SyncMessageEvent: {
189             PeerMessageEvent<SignalProxy::SyncMessage> *e = static_cast<PeerMessageEvent<SignalProxy::SyncMessage> *>(event);
190             handle(e->message);
191             break;
192         }
193         case RpcCallEvent: {
194             PeerMessageEvent<SignalProxy::RpcCall> *e = static_cast<PeerMessageEvent<SignalProxy::RpcCall> *>(event);
195             handle(e->message);
196             break;
197         }
198         case InitRequestEvent: {
199             PeerMessageEvent<SignalProxy::InitRequest> *e = static_cast<PeerMessageEvent<SignalProxy::InitRequest> *>(event);
200             handle(e->message);
201             break;
202         }
203         case InitDataEvent: {
204             PeerMessageEvent<SignalProxy::InitData> *e = static_cast<PeerMessageEvent<SignalProxy::InitData> *>(event);
205             handle(e->message);
206             break;
207         }
208
209         default:
210             qWarning() << Q_FUNC_INFO << "Received unknown custom event:" << event->type();
211             return;
212     }
213
214     event->accept();
215 }