From 08e35aad0b53364fb82e830f1b2e2ceb60658ea5 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 1 Apr 2015 01:40:54 +0200 Subject: Refactored resourcefactory further. --- dummyresource/resourcefactory.cpp | 61 ++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 30 deletions(-) (limited to 'dummyresource/resourcefactory.cpp') diff --git a/dummyresource/resourcefactory.cpp b/dummyresource/resourcefactory.cpp index c0395b7..4e79f4c 100644 --- a/dummyresource/resourcefactory.cpp +++ b/dummyresource/resourcefactory.cpp @@ -29,6 +29,7 @@ #include "commands.h" #include "clientapi.h" #include "index.h" +#include "log.h" #include #include @@ -143,39 +144,22 @@ private slots: }).exec(); } - void processCommand(const Akonadi2::QueuedCommand *queuedCommand, std::function callback) + Async::Job processQueuedCommand(const Akonadi2::QueuedCommand *queuedCommand) { - qDebug() << "Dequeued: " << queuedCommand->commandId(); //Throw command into appropriate pipeline switch (queuedCommand->commandId()) { case Akonadi2::Commands::DeleteEntityCommand: //mPipeline->removedEntity - break; + return Async::null(); case Akonadi2::Commands::ModifyEntityCommand: //mPipeline->modifiedEntity - break; - case Akonadi2::Commands::CreateEntityCommand: { - //TODO JOBAPI: job lifetime management - //Right now we're just leaking jobs. In this case we'd like jobs that are heap allocated and delete - //themselves once done. In other cases we'd like jobs that only live as long as their handle though. - //FIXME this job is stack allocated and thus simply dies.... - //FIXME get rid of waitForFinished, it's a workaround for the missing lifetime management - mPipeline->newEntity(queuedCommand->command()->Data(), queuedCommand->command()->size()).then([callback]() { - callback(true); - }, - [this, callback](int errorCode, const QString &errorMessage) { - qWarning() << "Error while creating entity: " << errorCode << errorMessage; - emit error(errorCode, errorMessage); - callback(false); - }).exec().waitForFinished(); - } - break; + return Async::null(); + case Akonadi2::Commands::CreateEntityCommand: + return mPipeline->newEntity(queuedCommand->command()->Data(), queuedCommand->command()->size()); default: - //Unhandled command - qWarning() << "Unhandled command"; - callback(false); - break; + return Async::error(-1, "Unhandled command"); } + return Async::null(); } //Process all messages of this queue @@ -183,19 +167,36 @@ private slots: { auto job = Async::start([this, queue](Async::Future &future) { asyncWhile([&, queue](std::function whileCallback) { + // auto job = Async::start(void *ptr, int size) + //TODO use something like: + //Async::foreach("pass iterator here").each("process value here").join(); + //Async::foreach("pass iterator here").parallel("process value here").join(); queue->dequeue([this, whileCallback](void *ptr, int size, std::function messageQueueCallback) { + auto callback = [messageQueueCallback, whileCallback](bool success) { + messageQueueCallback(success); + whileCallback(!success); + }; + flatbuffers::Verifier verifyer(reinterpret_cast(ptr), size); if (!Akonadi2::VerifyQueuedCommandBuffer(verifyer)) { qWarning() << "invalid buffer"; - messageQueueCallback(false); - whileCallback(true); + callback(false); return; } auto queuedCommand = Akonadi2::GetQueuedCommand(ptr); - processCommand(queuedCommand, [whileCallback, messageQueueCallback](bool success) { - messageQueueCallback(success); - whileCallback(!success); - }); + qDebug() << "Dequeued: " << queuedCommand->commandId(); + //TODO JOBAPI: job lifetime management + //Right now we're just leaking jobs. In this case we'd like jobs that are heap allocated and delete + //themselves once done. In other cases we'd like jobs that only live as long as their handle though. + //FIXME this job is stack allocated and thus simply dies.... + //FIXME get rid of waitForFinished, it's a workaround for the missing lifetime management + processQueuedCommand(queuedCommand).then([callback]() { + callback(true); + }, + [callback](int errorCode, QString errorMessage) { + Warning() << errorMessage; + callback(false); + }).exec().waitForFinished(); }, [whileCallback](const MessageQueue::Error &error) { whileCallback(true); -- cgit v1.2.3