From a8825608e1969acf4622fec3f6b6c284f1c66ea4 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 12 Aug 2015 00:35:49 +0200 Subject: Moved sendCommand implementations to ResourceAccess So we have commands in one place, and not in a header file. --- common/facade.h | 57 +++--------------------------------------- common/resourceaccess.cpp | 63 +++++++++++++++++++++++++++++++++++++++-------- common/resourceaccess.h | 7 ++++++ 3 files changed, 64 insertions(+), 63 deletions(-) diff --git a/common/facade.h b/common/facade.h index 52b2134..1715a7f 100644 --- a/common/facade.h +++ b/common/facade.h @@ -19,19 +19,13 @@ #pragma once -#include "clientapi.h" - #include #include #include "resourceaccess.h" #include "commands.h" -#include "createentity_generated.h" -#include "modifyentity_generated.h" -#include "deleteentity_generated.h" #include "domainadaptor.h" -#include "entitybuffer.h" #include "log.h" #include "resultset.h" #include "entitystorage.h" @@ -67,7 +61,7 @@ public: } /** - * + * Set the query to run */ void setQuery(const QueryFunction &query) { @@ -141,7 +135,7 @@ public: } flatbuffers::FlatBufferBuilder entityFbb; mDomainTypeAdaptorFactory->createBuffer(domainObject, entityFbb); - return sendCreateCommand(bufferTypeForDomainType(), QByteArray::fromRawData(reinterpret_cast(entityFbb.GetBufferPointer()), entityFbb.GetSize())); + return mResourceAccess->sendCreateCommand(bufferTypeForDomainType(), QByteArray::fromRawData(reinterpret_cast(entityFbb.GetBufferPointer()), entityFbb.GetSize())); } KAsync::Job modify(const DomainType &domainObject) Q_DECL_OVERRIDE @@ -152,12 +146,12 @@ public: } flatbuffers::FlatBufferBuilder entityFbb; mDomainTypeAdaptorFactory->createBuffer(domainObject, entityFbb); - return sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), QByteArray::fromRawData(reinterpret_cast(entityFbb.GetBufferPointer()), entityFbb.GetSize())); + return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), QByteArray::fromRawData(reinterpret_cast(entityFbb.GetBufferPointer()), entityFbb.GetSize())); } KAsync::Job remove(const DomainType &domainObject) Q_DECL_OVERRIDE { - return sendDeleteCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType()); + return mResourceAccess->sendDeleteCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType()); } //TODO JOBAPI return job from sync continuation to execute it as subjob? @@ -202,55 +196,13 @@ public: } protected: - KAsync::Job sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer) - { - flatbuffers::FlatBufferBuilder fbb; - //This is the resource buffer type and not the domain type - auto type = fbb.CreateString(resourceBufferType.constData()); - auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size()); - auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); - Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); - mResourceAccess->open(); - return mResourceAccess->sendCommand(Akonadi2::Commands::CreateEntityCommand, fbb); - } - - KAsync::Job sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer) - { - flatbuffers::FlatBufferBuilder fbb; - auto entityId = fbb.CreateString(uid.constData()); - //This is the resource buffer type and not the domain type - auto type = fbb.CreateString(resourceBufferType.constData()); - //FIXME - auto deletions = 0; - auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size()); - auto location = Akonadi2::Commands::CreateModifyEntity(fbb, revision, entityId, deletions, type, delta); - Akonadi2::Commands::FinishModifyEntityBuffer(fbb, location); - mResourceAccess->open(); - return mResourceAccess->sendCommand(Akonadi2::Commands::ModifyEntityCommand, fbb); - } - - KAsync::Job sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType) - { - flatbuffers::FlatBufferBuilder fbb; - auto entityId = fbb.CreateString(uid.constData()); - //This is the resource buffer type and not the domain type - auto type = fbb.CreateString(resourceBufferType.constData()); - auto location = Akonadi2::Commands::CreateDeleteEntity(fbb, revision, entityId, type); - Akonadi2::Commands::FinishDeleteEntityBuffer(fbb, location); - mResourceAccess->open(); - return mResourceAccess->sendCommand(Akonadi2::Commands::DeleteEntityCommand, fbb); - } - KAsync::Job synchronizeResource(bool sync, bool processAll) { //TODO check if a sync is necessary //TODO Only sync what was requested //TODO timeout - //TODO the synchronization should normally not be necessary: We just return what is already available. - if (sync || processAll) { return KAsync::start([=](KAsync::Future &future) { - mResourceAccess->open(); mResourceAccess->synchronizeResource(sync, processAll).then([&future]() { future.setFinished(); }).exec(); @@ -259,7 +211,6 @@ protected: return KAsync::null(); } - private: virtual KAsync::Job load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider, qint64 oldRevision, qint64 newRevision) { diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index a4a5795..ef4e64c 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp @@ -26,6 +26,10 @@ #include "common/revisionupdate_generated.h" #include "common/synchronize_generated.h" #include "common/notification_generated.h" +#include "common/createentity_generated.h" +#include "common/modifyentity_generated.h" +#include "common/deleteentity_generated.h" +#include "common/entitybuffer.h" #include "log.h" #include @@ -79,7 +83,6 @@ public: QByteArray resourceInstanceIdentifier; QSharedPointer socket; QByteArray partialMessageBuffer; - flatbuffers::FlatBufferBuilder fbb; QVector> commandQueue; QMap> pendingCommands; QMultiMap > resultHandler; @@ -276,10 +279,50 @@ KAsync::Job ResourceAccess::sendCommand(int commandId, flatbuffers::FlatB KAsync::Job ResourceAccess::synchronizeResource(bool sourceSync, bool localSync) { - auto command = Akonadi2::CreateSynchronize(d->fbb, sourceSync, localSync); - Akonadi2::FinishSynchronizeBuffer(d->fbb, command); - return sendCommand(Commands::SynchronizeCommand, d->fbb); - d->fbb.Clear(); + flatbuffers::FlatBufferBuilder fbb; + auto command = Akonadi2::CreateSynchronize(fbb, sourceSync, localSync); + Akonadi2::FinishSynchronizeBuffer(fbb, command); + open(); + return sendCommand(Commands::SynchronizeCommand, fbb); +} + +KAsync::Job ResourceAccess::sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer) +{ + flatbuffers::FlatBufferBuilder fbb; + //This is the resource buffer type and not the domain type + auto type = fbb.CreateString(resourceBufferType.constData()); + auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size()); + auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); + Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); + open(); + return sendCommand(Akonadi2::Commands::CreateEntityCommand, fbb); +} + +KAsync::Job ResourceAccess::sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer) +{ + flatbuffers::FlatBufferBuilder fbb; + auto entityId = fbb.CreateString(uid.constData()); + //This is the resource buffer type and not the domain type + auto type = fbb.CreateString(resourceBufferType.constData()); + //FIXME + auto deletions = 0; + auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size()); + auto location = Akonadi2::Commands::CreateModifyEntity(fbb, revision, entityId, deletions, type, delta); + Akonadi2::Commands::FinishModifyEntityBuffer(fbb, location); + open(); + return sendCommand(Akonadi2::Commands::ModifyEntityCommand, fbb); +} + +KAsync::Job ResourceAccess::sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType) +{ + flatbuffers::FlatBufferBuilder fbb; + auto entityId = fbb.CreateString(uid.constData()); + //This is the resource buffer type and not the domain type + auto type = fbb.CreateString(resourceBufferType.constData()); + auto location = Akonadi2::Commands::CreateDeleteEntity(fbb, revision, entityId, type); + Akonadi2::Commands::FinishDeleteEntityBuffer(fbb, location); + open(); + return sendCommand(Akonadi2::Commands::DeleteEntityCommand, fbb); } void ResourceAccess::open() @@ -346,11 +389,11 @@ void ResourceAccess::connected() log(QString("Connected: %1").arg(d->socket->fullServerName())); { - auto name = d->fbb.CreateString(QString::number(QCoreApplication::applicationPid()).toLatin1()); - auto command = Akonadi2::CreateHandshake(d->fbb, name); - Akonadi2::FinishHandshakeBuffer(d->fbb, command); - Commands::write(d->socket.data(), ++d->messageId, Commands::HandshakeCommand, d->fbb); - d->fbb.Clear(); + flatbuffers::FlatBufferBuilder fbb; + auto name = fbb.CreateString(QString::number(QCoreApplication::applicationPid()).toLatin1()); + auto command = Akonadi2::CreateHandshake(fbb, name); + Akonadi2::FinishHandshakeBuffer(fbb, command); + Commands::write(d->socket.data(), ++d->messageId, Commands::HandshakeCommand, fbb); } processCommandQueue(); diff --git a/common/resourceaccess.h b/common/resourceaccess.h index 55379f3..e6b9d91 100644 --- a/common/resourceaccess.h +++ b/common/resourceaccess.h @@ -43,6 +43,10 @@ public: virtual KAsync::Job sendCommand(int commandId, flatbuffers::FlatBufferBuilder &fbb) = 0; virtual KAsync::Job synchronizeResource(bool remoteSync, bool localSync) = 0; + virtual KAsync::Job sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer) { return KAsync::null(); }; + virtual KAsync::Job sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer) { return KAsync::null(); }; + virtual KAsync::Job sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType) { return KAsync::null(); }; + Q_SIGNALS: void ready(bool isReady); void revisionChanged(unsigned long long revision); @@ -65,6 +69,9 @@ public: KAsync::Job sendCommand(int commandId) Q_DECL_OVERRIDE; KAsync::Job sendCommand(int commandId, flatbuffers::FlatBufferBuilder &fbb) Q_DECL_OVERRIDE; KAsync::Job synchronizeResource(bool remoteSync, bool localSync) Q_DECL_OVERRIDE; + KAsync::Job sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer); + KAsync::Job sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer); + KAsync::Job sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType); /** * Tries to connect to server, and returns a connected socket on success. */ -- cgit v1.2.3