summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-08-12 00:35:49 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-08-12 00:35:49 +0200
commita8825608e1969acf4622fec3f6b6c284f1c66ea4 (patch)
treeb9f30d0228cedb6f1aa359c5237d399ec2b1e52e
parentffef7c0341ea42aba550fc6db432da3a334fd7f1 (diff)
downloadsink-a8825608e1969acf4622fec3f6b6c284f1c66ea4.tar.gz
sink-a8825608e1969acf4622fec3f6b6c284f1c66ea4.zip
Moved sendCommand implementations to ResourceAccess
So we have commands in one place, and not in a header file.
-rw-r--r--common/facade.h57
-rw-r--r--common/resourceaccess.cpp63
-rw-r--r--common/resourceaccess.h7
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 @@
19 19
20#pragma once 20#pragma once
21 21
22#include "clientapi.h"
23
24#include <QByteArray> 22#include <QByteArray>
25 23
26#include <Async/Async> 24#include <Async/Async>
27 25
28#include "resourceaccess.h" 26#include "resourceaccess.h"
29#include "commands.h" 27#include "commands.h"
30#include "createentity_generated.h"
31#include "modifyentity_generated.h"
32#include "deleteentity_generated.h"
33#include "domainadaptor.h" 28#include "domainadaptor.h"
34#include "entitybuffer.h"
35#include "log.h" 29#include "log.h"
36#include "resultset.h" 30#include "resultset.h"
37#include "entitystorage.h" 31#include "entitystorage.h"
@@ -67,7 +61,7 @@ public:
67 } 61 }
68 62
69 /** 63 /**
70 * 64 * Set the query to run
71 */ 65 */
72 void setQuery(const QueryFunction &query) 66 void setQuery(const QueryFunction &query)
73 { 67 {
@@ -141,7 +135,7 @@ public:
141 } 135 }
142 flatbuffers::FlatBufferBuilder entityFbb; 136 flatbuffers::FlatBufferBuilder entityFbb;
143 mDomainTypeAdaptorFactory->createBuffer(domainObject, entityFbb); 137 mDomainTypeAdaptorFactory->createBuffer(domainObject, entityFbb);
144 return sendCreateCommand(bufferTypeForDomainType(), QByteArray::fromRawData(reinterpret_cast<const char*>(entityFbb.GetBufferPointer()), entityFbb.GetSize())); 138 return mResourceAccess->sendCreateCommand(bufferTypeForDomainType(), QByteArray::fromRawData(reinterpret_cast<const char*>(entityFbb.GetBufferPointer()), entityFbb.GetSize()));
145 } 139 }
146 140
147 KAsync::Job<void> modify(const DomainType &domainObject) Q_DECL_OVERRIDE 141 KAsync::Job<void> modify(const DomainType &domainObject) Q_DECL_OVERRIDE
@@ -152,12 +146,12 @@ public:
152 } 146 }
153 flatbuffers::FlatBufferBuilder entityFbb; 147 flatbuffers::FlatBufferBuilder entityFbb;
154 mDomainTypeAdaptorFactory->createBuffer(domainObject, entityFbb); 148 mDomainTypeAdaptorFactory->createBuffer(domainObject, entityFbb);
155 return sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), QByteArray::fromRawData(reinterpret_cast<const char*>(entityFbb.GetBufferPointer()), entityFbb.GetSize())); 149 return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), QByteArray::fromRawData(reinterpret_cast<const char*>(entityFbb.GetBufferPointer()), entityFbb.GetSize()));
156 } 150 }
157 151
158 KAsync::Job<void> remove(const DomainType &domainObject) Q_DECL_OVERRIDE 152 KAsync::Job<void> remove(const DomainType &domainObject) Q_DECL_OVERRIDE
159 { 153 {
160 return sendDeleteCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType()); 154 return mResourceAccess->sendDeleteCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType());
161 } 155 }
162 156
163 //TODO JOBAPI return job from sync continuation to execute it as subjob? 157 //TODO JOBAPI return job from sync continuation to execute it as subjob?
@@ -202,55 +196,13 @@ public:
202 } 196 }
203 197
204protected: 198protected:
205 KAsync::Job<void> sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer)
206 {
207 flatbuffers::FlatBufferBuilder fbb;
208 //This is the resource buffer type and not the domain type
209 auto type = fbb.CreateString(resourceBufferType.constData());
210 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size());
211 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta);
212 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);
213 mResourceAccess->open();
214 return mResourceAccess->sendCommand(Akonadi2::Commands::CreateEntityCommand, fbb);
215 }
216
217 KAsync::Job<void> sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer)
218 {
219 flatbuffers::FlatBufferBuilder fbb;
220 auto entityId = fbb.CreateString(uid.constData());
221 //This is the resource buffer type and not the domain type
222 auto type = fbb.CreateString(resourceBufferType.constData());
223 //FIXME
224 auto deletions = 0;
225 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size());
226 auto location = Akonadi2::Commands::CreateModifyEntity(fbb, revision, entityId, deletions, type, delta);
227 Akonadi2::Commands::FinishModifyEntityBuffer(fbb, location);
228 mResourceAccess->open();
229 return mResourceAccess->sendCommand(Akonadi2::Commands::ModifyEntityCommand, fbb);
230 }
231
232 KAsync::Job<void> sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType)
233 {
234 flatbuffers::FlatBufferBuilder fbb;
235 auto entityId = fbb.CreateString(uid.constData());
236 //This is the resource buffer type and not the domain type
237 auto type = fbb.CreateString(resourceBufferType.constData());
238 auto location = Akonadi2::Commands::CreateDeleteEntity(fbb, revision, entityId, type);
239 Akonadi2::Commands::FinishDeleteEntityBuffer(fbb, location);
240 mResourceAccess->open();
241 return mResourceAccess->sendCommand(Akonadi2::Commands::DeleteEntityCommand, fbb);
242 }
243
244 KAsync::Job<void> synchronizeResource(bool sync, bool processAll) 199 KAsync::Job<void> synchronizeResource(bool sync, bool processAll)
245 { 200 {
246 //TODO check if a sync is necessary 201 //TODO check if a sync is necessary
247 //TODO Only sync what was requested 202 //TODO Only sync what was requested
248 //TODO timeout 203 //TODO timeout
249 //TODO the synchronization should normally not be necessary: We just return what is already available.
250
251 if (sync || processAll) { 204 if (sync || processAll) {
252 return KAsync::start<void>([=](KAsync::Future<void> &future) { 205 return KAsync::start<void>([=](KAsync::Future<void> &future) {
253 mResourceAccess->open();
254 mResourceAccess->synchronizeResource(sync, processAll).then<void>([&future]() { 206 mResourceAccess->synchronizeResource(sync, processAll).then<void>([&future]() {
255 future.setFinished(); 207 future.setFinished();
256 }).exec(); 208 }).exec();
@@ -259,7 +211,6 @@ protected:
259 return KAsync::null<void>(); 211 return KAsync::null<void>();
260 } 212 }
261 213
262
263private: 214private:
264 virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) 215 virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision)
265 { 216 {
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 @@
26#include "common/revisionupdate_generated.h" 26#include "common/revisionupdate_generated.h"
27#include "common/synchronize_generated.h" 27#include "common/synchronize_generated.h"
28#include "common/notification_generated.h" 28#include "common/notification_generated.h"
29#include "common/createentity_generated.h"
30#include "common/modifyentity_generated.h"
31#include "common/deleteentity_generated.h"
32#include "common/entitybuffer.h"
29#include "log.h" 33#include "log.h"
30 34
31#include <QCoreApplication> 35#include <QCoreApplication>
@@ -79,7 +83,6 @@ public:
79 QByteArray resourceInstanceIdentifier; 83 QByteArray resourceInstanceIdentifier;
80 QSharedPointer<QLocalSocket> socket; 84 QSharedPointer<QLocalSocket> socket;
81 QByteArray partialMessageBuffer; 85 QByteArray partialMessageBuffer;
82 flatbuffers::FlatBufferBuilder fbb;
83 QVector<QSharedPointer<QueuedCommand>> commandQueue; 86 QVector<QSharedPointer<QueuedCommand>> commandQueue;
84 QMap<uint, QSharedPointer<QueuedCommand>> pendingCommands; 87 QMap<uint, QSharedPointer<QueuedCommand>> pendingCommands;
85 QMultiMap<uint, std::function<void(int error, const QString &errorMessage)> > resultHandler; 88 QMultiMap<uint, std::function<void(int error, const QString &errorMessage)> > resultHandler;
@@ -276,10 +279,50 @@ KAsync::Job<void> ResourceAccess::sendCommand(int commandId, flatbuffers::FlatB
276 279
277KAsync::Job<void> ResourceAccess::synchronizeResource(bool sourceSync, bool localSync) 280KAsync::Job<void> ResourceAccess::synchronizeResource(bool sourceSync, bool localSync)
278{ 281{
279 auto command = Akonadi2::CreateSynchronize(d->fbb, sourceSync, localSync); 282 flatbuffers::FlatBufferBuilder fbb;
280 Akonadi2::FinishSynchronizeBuffer(d->fbb, command); 283 auto command = Akonadi2::CreateSynchronize(fbb, sourceSync, localSync);
281 return sendCommand(Commands::SynchronizeCommand, d->fbb); 284 Akonadi2::FinishSynchronizeBuffer(fbb, command);
282 d->fbb.Clear(); 285 open();
286 return sendCommand(Commands::SynchronizeCommand, fbb);
287}
288
289KAsync::Job<void> ResourceAccess::sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer)
290{
291 flatbuffers::FlatBufferBuilder fbb;
292 //This is the resource buffer type and not the domain type
293 auto type = fbb.CreateString(resourceBufferType.constData());
294 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size());
295 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta);
296 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);
297 open();
298 return sendCommand(Akonadi2::Commands::CreateEntityCommand, fbb);
299}
300
301KAsync::Job<void> ResourceAccess::sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer)
302{
303 flatbuffers::FlatBufferBuilder fbb;
304 auto entityId = fbb.CreateString(uid.constData());
305 //This is the resource buffer type and not the domain type
306 auto type = fbb.CreateString(resourceBufferType.constData());
307 //FIXME
308 auto deletions = 0;
309 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size());
310 auto location = Akonadi2::Commands::CreateModifyEntity(fbb, revision, entityId, deletions, type, delta);
311 Akonadi2::Commands::FinishModifyEntityBuffer(fbb, location);
312 open();
313 return sendCommand(Akonadi2::Commands::ModifyEntityCommand, fbb);
314}
315
316KAsync::Job<void> ResourceAccess::sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType)
317{
318 flatbuffers::FlatBufferBuilder fbb;
319 auto entityId = fbb.CreateString(uid.constData());
320 //This is the resource buffer type and not the domain type
321 auto type = fbb.CreateString(resourceBufferType.constData());
322 auto location = Akonadi2::Commands::CreateDeleteEntity(fbb, revision, entityId, type);
323 Akonadi2::Commands::FinishDeleteEntityBuffer(fbb, location);
324 open();
325 return sendCommand(Akonadi2::Commands::DeleteEntityCommand, fbb);
283} 326}
284 327
285void ResourceAccess::open() 328void ResourceAccess::open()
@@ -346,11 +389,11 @@ void ResourceAccess::connected()
346 log(QString("Connected: %1").arg(d->socket->fullServerName())); 389 log(QString("Connected: %1").arg(d->socket->fullServerName()));
347 390
348 { 391 {
349 auto name = d->fbb.CreateString(QString::number(QCoreApplication::applicationPid()).toLatin1()); 392 flatbuffers::FlatBufferBuilder fbb;
350 auto command = Akonadi2::CreateHandshake(d->fbb, name); 393 auto name = fbb.CreateString(QString::number(QCoreApplication::applicationPid()).toLatin1());
351 Akonadi2::FinishHandshakeBuffer(d->fbb, command); 394 auto command = Akonadi2::CreateHandshake(fbb, name);
352 Commands::write(d->socket.data(), ++d->messageId, Commands::HandshakeCommand, d->fbb); 395 Akonadi2::FinishHandshakeBuffer(fbb, command);
353 d->fbb.Clear(); 396 Commands::write(d->socket.data(), ++d->messageId, Commands::HandshakeCommand, fbb);
354 } 397 }
355 398
356 processCommandQueue(); 399 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:
43 virtual KAsync::Job<void> sendCommand(int commandId, flatbuffers::FlatBufferBuilder &fbb) = 0; 43 virtual KAsync::Job<void> sendCommand(int commandId, flatbuffers::FlatBufferBuilder &fbb) = 0;
44 virtual KAsync::Job<void> synchronizeResource(bool remoteSync, bool localSync) = 0; 44 virtual KAsync::Job<void> synchronizeResource(bool remoteSync, bool localSync) = 0;
45 45
46 virtual KAsync::Job<void> sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer) { return KAsync::null<void>(); };
47 virtual KAsync::Job<void> sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer) { return KAsync::null<void>(); };
48 virtual KAsync::Job<void> sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType) { return KAsync::null<void>(); };
49
46Q_SIGNALS: 50Q_SIGNALS:
47 void ready(bool isReady); 51 void ready(bool isReady);
48 void revisionChanged(unsigned long long revision); 52 void revisionChanged(unsigned long long revision);
@@ -65,6 +69,9 @@ public:
65 KAsync::Job<void> sendCommand(int commandId) Q_DECL_OVERRIDE; 69 KAsync::Job<void> sendCommand(int commandId) Q_DECL_OVERRIDE;
66 KAsync::Job<void> sendCommand(int commandId, flatbuffers::FlatBufferBuilder &fbb) Q_DECL_OVERRIDE; 70 KAsync::Job<void> sendCommand(int commandId, flatbuffers::FlatBufferBuilder &fbb) Q_DECL_OVERRIDE;
67 KAsync::Job<void> synchronizeResource(bool remoteSync, bool localSync) Q_DECL_OVERRIDE; 71 KAsync::Job<void> synchronizeResource(bool remoteSync, bool localSync) Q_DECL_OVERRIDE;
72 KAsync::Job<void> sendCreateCommand(const QByteArray &resourceBufferType, const QByteArray &buffer);
73 KAsync::Job<void> sendModifyCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType, const QByteArrayList &deletedProperties, const QByteArray &buffer);
74 KAsync::Job<void> sendDeleteCommand(const QByteArray &uid, qint64 revision, const QByteArray &resourceBufferType);
68 /** 75 /**
69 * Tries to connect to server, and returns a connected socket on success. 76 * Tries to connect to server, and returns a connected socket on success.
70 */ 77 */