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