common: Make InternalPeer (more) thread-safe
[quassel.git] / src / common / internalpeer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "internalpeer.h"
22
23 using namespace Protocol;
24
25 InternalPeer::InternalPeer(QObject *parent)
26     : Peer(nullptr, parent)
27 {
28     static bool registered = []() {
29         qRegisterMetaType<Protocol::SyncMessage>();
30         qRegisterMetaType<Protocol::RpcCall>();
31         qRegisterMetaType<Protocol::InitRequest>();
32         qRegisterMetaType<Protocol::InitData>();
33         return true;
34     }();
35     Q_UNUSED(registered)
36
37     setFeatures(Quassel::Features{});
38 }
39
40
41 InternalPeer::~InternalPeer()
42 {
43     if (_isOpen) {
44         emit disconnected();
45     }
46 }
47
48
49 QString InternalPeer::description() const
50 {
51     return tr("internal connection");
52 }
53
54
55 QString InternalPeer::address() const
56 {
57     return tr("internal connection");
58 }
59
60
61 quint16 InternalPeer::port() const
62 {
63     return 0;
64 }
65
66
67 bool InternalPeer::isOpen() const
68 {
69     return true;
70 }
71
72
73 bool InternalPeer::isSecure() const
74 {
75     return true;
76 }
77
78
79 bool InternalPeer::isLocal() const
80 {
81     return true;
82 }
83
84
85 void InternalPeer::close(const QString &reason)
86 {
87     // FIXME
88     Q_UNUSED(reason)
89     qWarning() << "closing not implemented!";
90 }
91
92
93 int InternalPeer::lag() const
94 {
95     return 0;
96 }
97
98
99 ::SignalProxy *InternalPeer::signalProxy() const
100 {
101     return _proxy;
102 }
103
104
105 void InternalPeer::setSignalProxy(::SignalProxy *proxy)
106 {
107     if (!proxy && _proxy) {
108         _proxy = nullptr;
109         if (_isOpen) {
110             _isOpen = false;
111             emit disconnected();
112         }
113         return;
114     }
115
116     if (proxy && !_proxy) {
117         _proxy = proxy;
118         return;
119     }
120
121     qWarning() << Q_FUNC_INFO << "Changing the SignalProxy is not supported!";
122 }
123
124
125 void InternalPeer::setPeer(InternalPeer *peer)
126 {
127     connect(peer, SIGNAL(dispatchMessage(Protocol::SyncMessage)), SLOT(handleMessage(Protocol::SyncMessage)));
128     connect(peer, SIGNAL(dispatchMessage(Protocol::RpcCall))    , SLOT(handleMessage(Protocol::RpcCall)));
129     connect(peer, SIGNAL(dispatchMessage(Protocol::InitRequest)), SLOT(handleMessage(Protocol::InitRequest)));
130     connect(peer, SIGNAL(dispatchMessage(Protocol::InitData))   , SLOT(handleMessage(Protocol::InitData)));
131
132     connect(peer, SIGNAL(disconnected()), SLOT(peerDisconnected()));
133
134     _isOpen = true;
135 }
136
137
138 void InternalPeer::peerDisconnected()
139 {
140     disconnect(sender(), nullptr, this, nullptr);
141     if (_isOpen) {
142         _isOpen = false;
143         emit disconnected();
144     }
145 }
146
147
148 void InternalPeer::dispatch(const SyncMessage &msg)
149 {
150     emit dispatchMessage(msg);
151 }
152
153
154 void InternalPeer::dispatch(const RpcCall &msg)
155 {
156     emit dispatchMessage(msg);
157 }
158
159
160 void InternalPeer::dispatch(const InitRequest &msg)
161 {
162     emit dispatchMessage(msg);
163 }
164
165
166 void InternalPeer::dispatch(const InitData &msg)
167 {
168     emit dispatchMessage(msg);
169 }
170
171
172 void InternalPeer::handleMessage(const Protocol::SyncMessage &msg)
173 {
174     handle(msg);
175 }
176
177
178 void InternalPeer::handleMessage(const Protocol::RpcCall &msg)
179 {
180     handle(msg);
181 }
182
183
184 void InternalPeer::handleMessage(const Protocol::InitRequest &msg)
185 {
186     handle(msg);
187 }
188
189
190 void InternalPeer::handleMessage(const Protocol::InitData &msg)
191 {
192     handle(msg);
193 }
194
195
196 template<class T>
197 void InternalPeer::handle(const T &msg)
198 {
199     static auto setSourcePeer = [](Peer *peer) {
200         auto p = SignalProxy::current();
201         if (p) {
202             p->setSourcePeer(peer);
203         }
204     };
205
206     setSourcePeer(this);
207     Peer::handle(msg);
208     setSourcePeer(nullptr);
209 }