2012-01-06 00:22:51 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef WORKERPOOL_H
|
|
|
|
#define WORKERPOOL_H
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
#include <QAtomicInt>
|
2012-01-06 00:22:51 +01:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QLocalServer>
|
2012-01-06 22:27:02 +01:00
|
|
|
#include <QLocalSocket>
|
2012-01-22 19:41:26 +01:00
|
|
|
#include <QMutex>
|
2012-01-06 00:22:51 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QProcess>
|
2012-01-22 19:41:26 +01:00
|
|
|
#include <QQueue>
|
2012-01-06 00:22:51 +01:00
|
|
|
#include <QThread>
|
|
|
|
|
2021-04-09 07:58:26 +02:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
|
|
|
|
#include <QRandomGenerator>
|
|
|
|
#endif
|
|
|
|
|
2021-03-16 06:35:22 +01:00
|
|
|
#include "clementine-config.h"
|
2012-01-06 00:22:51 +01:00
|
|
|
#include "core/closure.h"
|
2012-01-06 22:27:02 +01:00
|
|
|
#include "core/logging.h"
|
2012-01-06 00:22:51 +01:00
|
|
|
|
|
|
|
// Base class containing signals and slots - required because moc doesn't do
|
|
|
|
// templated objects.
|
|
|
|
class _WorkerPoolBase : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2014-02-10 16:03:54 +01:00
|
|
|
_WorkerPoolBase(QObject* parent = nullptr);
|
2012-01-06 00:22:51 +01:00
|
|
|
|
|
|
|
signals:
|
|
|
|
// Emitted when a worker failed to start. This usually happens when the
|
|
|
|
// worker wasn't found, or couldn't be executed.
|
|
|
|
void WorkerFailedToStart();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected slots:
|
2012-01-06 22:27:02 +01:00
|
|
|
virtual void DoStart() {}
|
2012-01-06 00:22:51 +01:00
|
|
|
virtual void NewConnection() {}
|
|
|
|
virtual void ProcessError(QProcess::ProcessError) {}
|
2012-01-22 19:41:26 +01:00
|
|
|
virtual void SendQueuedMessages() {}
|
2012-01-06 00:22:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Manages a pool of one or more external processes. A local socket server is
|
|
|
|
// started for each process, and the address is passed to the process as
|
|
|
|
// argv[1]. The process is expected to connect back to the socket server, and
|
|
|
|
// when it does a HandlerType is created for it.
|
2012-01-22 19:41:26 +01:00
|
|
|
// Instances of HandlerType are created in the WorkerPool's thread.
|
2012-01-06 00:22:51 +01:00
|
|
|
template <typename HandlerType>
|
|
|
|
class WorkerPool : public _WorkerPoolBase {
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2014-02-10 16:03:54 +01:00
|
|
|
WorkerPool(QObject* parent = nullptr);
|
2012-01-08 17:34:34 +01:00
|
|
|
~WorkerPool();
|
2012-01-06 00:22:51 +01:00
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
typedef typename HandlerType::MessageType MessageType;
|
|
|
|
typedef typename HandlerType::ReplyType ReplyType;
|
|
|
|
|
2012-01-06 00:22:51 +01:00
|
|
|
// Sets the name of the worker executable. This is looked for first in the
|
|
|
|
// current directory, and then in $PATH. You must call this before calling
|
|
|
|
// Start().
|
|
|
|
void SetExecutableName(const QString& executable_name);
|
|
|
|
|
|
|
|
// Sets the number of worker process to use. Defaults to
|
|
|
|
// 1 <= (processors / 2) <= 2.
|
|
|
|
void SetWorkerCount(int count);
|
|
|
|
|
|
|
|
// Sets the prefix to use for the local server (on unix this is a named pipe
|
|
|
|
// in /tmp). Defaults to QApplication::applicationName(). A random number
|
|
|
|
// is appended to this name when creating each server.
|
|
|
|
void SetLocalServerName(const QString& local_server_name);
|
|
|
|
|
|
|
|
// Starts all workers.
|
|
|
|
void Start();
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
// Fills in the message's "id" field and creates a reply future. The message
|
|
|
|
// is queued and the WorkerPool's thread will send it to the next available
|
|
|
|
// worker. Can be called from any thread.
|
|
|
|
ReplyType* SendMessageWithReply(MessageType* message);
|
2012-01-06 00:22:51 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected:
|
2012-01-22 19:41:26 +01:00
|
|
|
// These are all reimplemented slots, they are called on the WorkerPool's
|
|
|
|
// thread.
|
2012-01-06 22:27:02 +01:00
|
|
|
void DoStart();
|
2012-01-06 00:22:51 +01:00
|
|
|
void NewConnection();
|
|
|
|
void ProcessError(QProcess::ProcessError error);
|
2012-01-22 19:41:26 +01:00
|
|
|
void SendQueuedMessages();
|
2012-01-06 00:22:51 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2012-01-06 00:22:51 +01:00
|
|
|
struct Worker {
|
2014-02-07 16:34:20 +01:00
|
|
|
Worker()
|
|
|
|
: local_server_(NULL),
|
|
|
|
local_socket_(NULL),
|
|
|
|
process_(NULL),
|
|
|
|
handler_(NULL) {}
|
2012-01-06 00:22:51 +01:00
|
|
|
|
|
|
|
QLocalServer* local_server_;
|
2012-01-08 17:34:34 +01:00
|
|
|
QLocalSocket* local_socket_;
|
2012-01-06 00:22:51 +01:00
|
|
|
QProcess* process_;
|
|
|
|
HandlerType* handler_;
|
|
|
|
};
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
// Must only ever be called on my thread.
|
2012-01-06 00:22:51 +01:00
|
|
|
void StartOneWorker(Worker* worker);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Worker* FindWorker(T Worker::*member, T value) {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (typename QList<Worker>::iterator it = workers_.begin();
|
|
|
|
it != workers_.end(); ++it) {
|
2012-01-06 22:27:02 +01:00
|
|
|
if ((*it).*member == value) {
|
|
|
|
return &(*it);
|
2012-01-06 00:22:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-06 22:27:02 +01:00
|
|
|
template <typename T>
|
|
|
|
void DeleteQObjectPointerLater(T** p) {
|
2012-01-06 00:22:51 +01:00
|
|
|
if (*p) {
|
|
|
|
(*p)->deleteLater();
|
|
|
|
*p = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
// Creates a new reply future for the request with the next sequential ID,
|
|
|
|
// and sets the request's ID to the ID of the reply. Can be called from any
|
|
|
|
// thread
|
|
|
|
ReplyType* NewReply(MessageType* message);
|
|
|
|
|
|
|
|
// Returns the next handler, or NULL if there isn't one. Must be called from
|
|
|
|
// my thread.
|
|
|
|
HandlerType* NextHandler() const;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2012-01-06 00:22:51 +01:00
|
|
|
QString local_server_name_;
|
|
|
|
QString executable_name_;
|
|
|
|
QString executable_path_;
|
|
|
|
|
|
|
|
int worker_count_;
|
2012-01-22 19:41:26 +01:00
|
|
|
mutable int next_worker_;
|
2012-01-06 00:22:51 +01:00
|
|
|
QList<Worker> workers_;
|
2012-01-22 19:41:26 +01:00
|
|
|
|
|
|
|
QAtomicInt next_id_;
|
|
|
|
|
|
|
|
QMutex message_queue_mutex_;
|
|
|
|
QQueue<ReplyType*> message_queue_;
|
2012-01-06 00:22:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
WorkerPool<HandlerType>::WorkerPool(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: _WorkerPoolBase(parent), next_worker_(0), next_id_(0) {
|
2012-01-06 00:22:51 +01:00
|
|
|
worker_count_ = qBound(1, QThread::idealThreadCount() / 2, 2);
|
|
|
|
local_server_name_ = qApp->applicationName().toLower();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (local_server_name_.isEmpty()) local_server_name_ = "workerpool";
|
2012-01-06 00:22:51 +01:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:34:34 +01:00
|
|
|
template <typename HandlerType>
|
|
|
|
WorkerPool<HandlerType>::~WorkerPool() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Worker& worker : workers_) {
|
2012-01-08 17:34:34 +01:00
|
|
|
if (worker.local_socket_ && worker.process_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
disconnect(worker.process_, SIGNAL(error(QProcess::ProcessError)), this,
|
|
|
|
SLOT(ProcessError(QProcess::ProcessError)));
|
2012-01-22 19:52:24 +01:00
|
|
|
|
2012-01-08 17:34:34 +01:00
|
|
|
// The worker is connected. Close his socket and wait for him to exit.
|
|
|
|
qLog(Debug) << "Closing worker socket";
|
|
|
|
worker.local_socket_->close();
|
|
|
|
worker.process_->waitForFinished(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker.process_ && worker.process_->state() == QProcess::Running) {
|
|
|
|
// The worker is still running - kill it.
|
|
|
|
qLog(Debug) << "Killing worker process";
|
|
|
|
worker.process_->terminate();
|
|
|
|
if (!worker.process_->waitForFinished(500)) {
|
|
|
|
worker.process_->kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-30 00:01:28 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (ReplyType* reply : message_queue_) {
|
|
|
|
reply->Abort();
|
|
|
|
}
|
2012-01-08 17:34:34 +01:00
|
|
|
}
|
|
|
|
|
2012-01-06 00:22:51 +01:00
|
|
|
template <typename HandlerType>
|
|
|
|
void WorkerPool<HandlerType>::SetWorkerCount(int count) {
|
|
|
|
Q_ASSERT(workers_.isEmpty());
|
|
|
|
worker_count_ = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
2014-02-07 16:34:20 +01:00
|
|
|
void WorkerPool<HandlerType>::SetLocalServerName(
|
|
|
|
const QString& local_server_name) {
|
2012-01-06 00:22:51 +01:00
|
|
|
Q_ASSERT(workers_.isEmpty());
|
|
|
|
local_server_name_ = local_server_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
2014-02-07 16:34:20 +01:00
|
|
|
void WorkerPool<HandlerType>::SetExecutableName(
|
|
|
|
const QString& executable_name) {
|
2012-01-06 00:22:51 +01:00
|
|
|
Q_ASSERT(workers_.isEmpty());
|
|
|
|
executable_name_ = executable_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
void WorkerPool<HandlerType>::Start() {
|
2012-01-06 22:27:02 +01:00
|
|
|
metaObject()->invokeMethod(this, "DoStart");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
void WorkerPool<HandlerType>::DoStart() {
|
2012-01-06 00:22:51 +01:00
|
|
|
Q_ASSERT(workers_.isEmpty());
|
2012-01-06 22:27:02 +01:00
|
|
|
Q_ASSERT(!executable_name_.isEmpty());
|
2012-01-22 19:41:26 +01:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
2012-01-06 00:22:51 +01:00
|
|
|
|
|
|
|
// Find the executable if we can, default to searching $PATH
|
|
|
|
executable_path_ = executable_name_;
|
|
|
|
|
|
|
|
QStringList search_path;
|
|
|
|
search_path << qApp->applicationDirPath();
|
2018-11-29 23:11:55 +01:00
|
|
|
#if defined(Q_OS_MAC) && defined(USE_BUNDLE)
|
2018-11-29 22:32:12 +01:00
|
|
|
search_path << qApp->applicationDirPath() + "/" + USE_BUNDLE_DIR;
|
2012-01-06 00:22:51 +01:00
|
|
|
#endif
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& path_prefix : search_path) {
|
2012-01-06 00:22:51 +01:00
|
|
|
const QString executable_path = path_prefix + "/" + executable_name_;
|
|
|
|
if (QFile::exists(executable_path)) {
|
|
|
|
executable_path_ = executable_path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start all the workers
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < worker_count_; ++i) {
|
2012-01-06 00:22:51 +01:00
|
|
|
Worker worker;
|
|
|
|
StartOneWorker(&worker);
|
|
|
|
|
|
|
|
workers_ << worker;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
void WorkerPool<HandlerType>::StartOneWorker(Worker* worker) {
|
2012-01-22 19:41:26 +01:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
|
|
|
|
2012-01-06 00:22:51 +01:00
|
|
|
DeleteQObjectPointerLater(&worker->local_server_);
|
2012-01-08 17:34:34 +01:00
|
|
|
DeleteQObjectPointerLater(&worker->local_socket_);
|
2012-01-06 00:22:51 +01:00
|
|
|
DeleteQObjectPointerLater(&worker->process_);
|
|
|
|
DeleteQObjectPointerLater(&worker->handler_);
|
|
|
|
|
|
|
|
worker->local_server_ = new QLocalServer(this);
|
|
|
|
worker->process_ = new QProcess(this);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(worker->local_server_, SIGNAL(newConnection()),
|
|
|
|
SLOT(NewConnection()));
|
2012-01-06 00:22:51 +01:00
|
|
|
connect(worker->process_, SIGNAL(error(QProcess::ProcessError)),
|
|
|
|
SLOT(ProcessError(QProcess::ProcessError)));
|
|
|
|
|
|
|
|
// Create a server, find an unused name and start listening
|
|
|
|
forever {
|
2021-04-09 07:58:26 +02:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
|
2012-01-06 22:27:02 +01:00
|
|
|
const int unique_number = qrand() ^ ((int)(quint64(this) & 0xFFFFFFFF));
|
2021-04-09 07:58:26 +02:00
|
|
|
#else
|
|
|
|
// The global generator is securely seeded.
|
2021-04-21 23:35:12 +02:00
|
|
|
const unsigned int unique_number = QRandomGenerator::global()->generate();
|
2021-04-09 07:58:26 +02:00
|
|
|
#endif
|
2014-02-07 16:34:20 +01:00
|
|
|
const QString name =
|
|
|
|
QString("%1_%2").arg(local_server_name_).arg(unique_number);
|
2012-01-06 00:22:51 +01:00
|
|
|
|
|
|
|
if (worker->local_server_->listen(name)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
qLog(Debug) << "Starting worker" << worker << executable_path_
|
2012-01-06 22:27:02 +01:00
|
|
|
<< worker->local_server_->fullServerName();
|
|
|
|
|
2012-01-06 00:22:51 +01:00
|
|
|
// Start the process
|
|
|
|
worker->process_->setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
worker->process_->start(executable_path_,
|
2014-02-07 16:34:20 +01:00
|
|
|
QStringList()
|
|
|
|
<< worker->local_server_->fullServerName());
|
2012-01-06 00:22:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
void WorkerPool<HandlerType>::NewConnection() {
|
2012-01-22 19:41:26 +01:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
|
|
|
|
2012-01-06 00:22:51 +01:00
|
|
|
QLocalServer* server = qobject_cast<QLocalServer*>(sender());
|
|
|
|
|
|
|
|
// Find the worker with this server.
|
|
|
|
Worker* worker = FindWorker(&Worker::local_server_, server);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!worker) return;
|
2012-01-06 00:22:51 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Worker" << worker << "connected to"
|
|
|
|
<< server->fullServerName();
|
2012-01-06 22:27:02 +01:00
|
|
|
|
2012-01-06 00:22:51 +01:00
|
|
|
// Accept the connection.
|
2012-01-08 17:34:34 +01:00
|
|
|
worker->local_socket_ = server->nextPendingConnection();
|
2012-01-06 00:22:51 +01:00
|
|
|
|
|
|
|
// We only ever accept one connection per worker, so destroy the server now.
|
2012-01-08 17:34:34 +01:00
|
|
|
worker->local_socket_->setParent(this);
|
2012-01-06 22:27:02 +01:00
|
|
|
worker->local_server_->deleteLater();
|
2012-01-06 00:22:51 +01:00
|
|
|
worker->local_server_ = NULL;
|
|
|
|
|
|
|
|
// Create the handler.
|
2012-01-08 17:34:34 +01:00
|
|
|
worker->handler_ = new HandlerType(worker->local_socket_, this);
|
2012-01-06 00:22:51 +01:00
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
SendQueuedMessages();
|
2012-01-06 00:22:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
void WorkerPool<HandlerType>::ProcessError(QProcess::ProcessError error) {
|
2012-01-22 19:41:26 +01:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
|
|
|
|
2012-01-06 00:22:51 +01:00
|
|
|
QProcess* process = qobject_cast<QProcess*>(sender());
|
|
|
|
|
|
|
|
// Find the worker with this process.
|
|
|
|
Worker* worker = FindWorker(&Worker::process_, process);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!worker) return;
|
2012-01-06 00:22:51 +01:00
|
|
|
|
|
|
|
switch (error) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QProcess::FailedToStart:
|
|
|
|
// Failed to start errors are bad - it usually means the worker isn't
|
|
|
|
// installed. Don't restart the process, but tell our owner, who will
|
|
|
|
// probably want to do something fatal.
|
|
|
|
qLog(Error) << "Worker failed to start";
|
|
|
|
emit WorkerFailedToStart();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// On any other error we just restart the process.
|
|
|
|
qLog(Debug) << "Worker" << worker << "failed with error" << error
|
|
|
|
<< "- restarting";
|
|
|
|
StartOneWorker(worker);
|
|
|
|
break;
|
2012-01-06 00:22:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
2014-02-07 16:34:20 +01:00
|
|
|
typename WorkerPool<HandlerType>::ReplyType* WorkerPool<HandlerType>::NewReply(
|
|
|
|
MessageType* message) {
|
2012-01-22 19:41:26 +01:00
|
|
|
const int id = next_id_.fetchAndAddOrdered(1);
|
|
|
|
message->set_id(id);
|
2012-01-06 00:22:51 +01:00
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
return new ReplyType(*message);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
typename WorkerPool<HandlerType>::ReplyType*
|
|
|
|
WorkerPool<HandlerType>::SendMessageWithReply(MessageType* message) {
|
|
|
|
ReplyType* reply = NewReply(message);
|
|
|
|
|
2012-11-06 23:18:12 +01:00
|
|
|
// Add the pending reply to the queue
|
2012-01-22 19:41:26 +01:00
|
|
|
{
|
|
|
|
QMutexLocker l(&message_queue_mutex_);
|
|
|
|
message_queue_.enqueue(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wake up the main thread
|
|
|
|
metaObject()->invokeMethod(this, "SendQueuedMessages", Qt::QueuedConnection);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
void WorkerPool<HandlerType>::SendQueuedMessages() {
|
|
|
|
QMutexLocker l(&message_queue_mutex_);
|
|
|
|
|
|
|
|
while (!message_queue_.isEmpty()) {
|
|
|
|
ReplyType* reply = message_queue_.dequeue();
|
|
|
|
|
|
|
|
// Find a worker for this message
|
|
|
|
HandlerType* handler = NextHandler();
|
|
|
|
if (!handler) {
|
|
|
|
// No available handlers - put the message on the front of the queue.
|
|
|
|
message_queue_.prepend(reply);
|
|
|
|
qLog(Debug) << "No available handlers to process request";
|
|
|
|
break;
|
2012-01-06 00:22:51 +01:00
|
|
|
}
|
2012-01-06 22:27:02 +01:00
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
handler->SendRequest(reply);
|
2012-01-06 00:22:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
template <typename HandlerType>
|
|
|
|
HandlerType* WorkerPool<HandlerType>::NextHandler() const {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < workers_.count(); ++i) {
|
2012-01-22 19:41:26 +01:00
|
|
|
const int worker_index = (next_worker_ + i) % workers_.count();
|
|
|
|
|
2012-01-22 19:42:40 +01:00
|
|
|
if (workers_[worker_index].handler_ &&
|
|
|
|
!workers_[worker_index].handler_->is_device_closed()) {
|
2012-01-22 19:41:26 +01:00
|
|
|
next_worker_ = (worker_index + 1) % workers_.count();
|
|
|
|
return workers_[worker_index].handler_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // WORKERPOOL_H
|