1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
25 #include "coresession.h"
26 #include "internalpeer.h"
27 #include "remotepeer.h"
28 #include "sessionthread.h"
29 #include "signalproxy.h"
33 class Worker : public QObject
38 Worker(UserId userId, bool restoreState, bool strictIdentEnabled)
40 , _restoreState{restoreState}
41 , _strictIdentEnabled{strictIdentEnabled}
48 _session = new CoreSession{_userId, _restoreState, _strictIdentEnabled, this};
49 connect(_session, &QObject::destroyed, QThread::currentThread(), &QThread::quit);
50 connect(_session, &CoreSession::sessionStateReceived, Core::instance(), &Core::sessionStateReceived);
61 void addClient(Peer *peer)
64 qWarning() << "Session not initialized!";
68 auto remotePeer = qobject_cast<RemotePeer*>(peer);
70 _session->addClient(remotePeer);
73 auto internalPeer = qobject_cast<InternalPeer*>(peer);
75 _session->addClient(internalPeer);
79 qWarning() << "SessionThread::addClient() received invalid peer!" << peer;
88 bool _strictIdentEnabled; ///< Whether or not strict ident mode is enabled, locking users' idents to Quassel username
89 QPointer<CoreSession> _session;
94 SessionThread::SessionThread(UserId uid, bool restoreState, bool strictIdentEnabled, QObject *parent)
97 auto worker = new Worker(uid, restoreState, strictIdentEnabled);
98 worker->moveToThread(&_sessionThread);
99 connect(&_sessionThread, &QThread::started, worker, &Worker::initialize);
100 connect(&_sessionThread, &QThread::finished, worker, &QObject::deleteLater);
101 connect(worker, &Worker::initialized, this, &SessionThread::onSessionInitialized);
102 connect(worker, &QObject::destroyed, this, &SessionThread::onSessionDestroyed);
104 connect(this, &SessionThread::addClientToWorker, worker, &Worker::addClient);
105 connect(this, &SessionThread::shutdownSession, worker, &Worker::shutdown);
107 // Defer thread start through the event loop, so the SessionThread instance is fully constructed before
108 QTimer::singleShot(0, &_sessionThread, SLOT(start()));
112 SessionThread::~SessionThread()
114 // shut down thread gracefully
115 _sessionThread.quit();
116 _sessionThread.wait(30000);
120 void SessionThread::shutdown()
122 emit shutdownSession();
126 void SessionThread::onSessionInitialized()
128 _sessionInitialized = true;
129 for (auto &&peer : _clientQueue) {
130 peer->setParent(nullptr);
131 peer->moveToThread(&_sessionThread);
132 emit addClientToWorker(peer);
134 _clientQueue.clear();
138 void SessionThread::onSessionDestroyed()
140 emit shutdownComplete(this);
143 void SessionThread::addClient(Peer *peer)
145 if (_sessionInitialized) {
146 peer->setParent(nullptr);
147 peer->moveToThread(&_sessionThread);
148 emit addClientToWorker(peer);
151 _clientQueue.push_back(peer);
155 #include "sessionthread.moc"