Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F67748643
D301.1776443235.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
10 KB
Referenced Files
None
Subscribers
None
D301.1776443235.diff
View Options
diff --git a/src/client/client.hpp b/src/client/client.hpp
--- a/src/client/client.hpp
+++ b/src/client/client.hpp
@@ -514,6 +514,9 @@
* the sync (posting filters and uploading identity keys,
* if needed) is successful, or until stopSyncing() is called.
*
+ * This function internally calls ensureReadyForSyncing() and
+ * doOneSyncLoop().
+ *
* @return A Promise that resolves when the Client is syncing
* (more exactly, when syncing() contains true), or when there
* is an error in the preparation of the sync.
@@ -529,6 +532,27 @@
*/
PromiseT stopSyncing() const;
+ /**
+ * Ensure the Client can start syncing using doOneSyncCycle().
+ *
+ * @return A Promise that resolves when the application can
+ * call doOneSyncCycle() or when there is an error. When there is
+ * an error, the Promise contains the standard error format.
+ */
+ PromiseT ensureReadyForSyncing() const;
+
+ /**
+ * Call the sync endpoint once and process the response.
+ *
+ * @return A Promise that resolves when the sync cycle is finished,
+ * or when there is an error. If the Promise is successful, the
+ * application can call doOneSyncCycle() again. If the Promise is
+ * not successful, the application should wait some time before calling
+ * doOneSyncCycle() again, and exponentially increase the wait time
+ * between retries (or retry when the user manually requests it).
+ */
+ PromiseT doOneSyncCycle() const;
+
/**
* Get the info of all devices of user `userId` that supports encryption.
*
diff --git a/src/client/client.cpp b/src/client/client.cpp
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -320,27 +320,7 @@
return m_ctx.createResolvedPromise(true);
}
- auto p1 = m_ctx.createResolvedPromise(true)
- .then([that=toEventLoop()](auto) {
- // post filters, if filters are incomplete
- if ((+that.clientCursor()[&ClientModel::initialSyncFilterId]).empty()
- || (+that.clientCursor()[&ClientModel::incrementalSyncFilterId]).empty()) {
- return that.m_ctx.dispatch(PostInitialFiltersAction{});
- }
- return that.m_ctx.createResolvedPromise(true);
- })
- .then([that=toEventLoop()](auto stat) {
- if (! stat.success()) {
- return that.m_ctx.createResolvedPromise(stat);
- }
- // Upload identity keys if we need to
- if (+that.clientCursor()[&ClientModel::crypto]
- && ! +that.clientCursor()[&ClientModel::identityKeysUploaded]) {
- return that.m_ctx.dispatch(UploadIdentityKeysAction{});
- } else {
- return that.m_ctx.createResolvedPromise(true);
- }
- });
+ auto p1 = ensureReadyForSyncing();
p1
.then([m_ctx=m_ctx](auto stat) {
@@ -516,60 +496,13 @@
// assert (m_deps);
using namespace CursorOp;
- bool isInitialSync = ! (+clientCursor()[&ClientModel::syncToken]).has_value();
-
bool shouldSync = +clientCursor()[&ClientModel::shouldSync];
if (! shouldSync) {
return;
}
- //
-
- auto syncRes = m_ctx.dispatch(SyncAction{});
-
- auto uploadOneTimeKeysRes = syncRes
- .then([that=toEventLoop()](auto stat) {
- if (! stat.success()) {
- return that.m_ctx.createResolvedPromise(stat);
- }
- auto &rg = lager::get<RandomInterface &>(that.m_deps.value());
- bool hasCrypto{+that.clientCursor()[&ClientModel::crypto]};
- if (! hasCrypto) {
- return that.m_ctx.createResolvedPromise(true);
- }
- auto numKeysToGenerate = (+that.clientCursor()).numOneTimeKeysNeeded();
- return that.m_ctx.dispatch(GenerateAndUploadOneTimeKeysAction{
- numKeysToGenerate,
- rg.generateRange<RandomData>(GenerateAndUploadOneTimeKeysAction::randomSize(numKeysToGenerate))
- });
- });
-
- auto queryKeysRes = syncRes
- .then([that=toEventLoop(), isInitialSync](auto stat) {
- if (! stat.success()) {
- return that.m_ctx.createResolvedPromise(stat);
- }
- bool hasCrypto{+that.clientCursor()[&ClientModel::crypto]};
- return hasCrypto
- ? that.m_ctx.dispatch(QueryKeysAction{isInitialSync})
- : that.m_ctx.createResolvedPromise(true);
- });
- auto processVerificationEventsRes = syncRes
- .then([that=toEventLoop()](EffectStatus stat) {
- if (!stat.success() || !that.m_deps ||
- !that.clientCursor().map([](const auto &c) {
- return c.crypto.has_value();
- }).make().get()) {
- return that.m_ctx.createResolvedPromise(stat);
- }
- kzo.client.dbg() << "processVerificationEvents: " << stat.data().get().dump() << std::endl;
- EventList ves = stat.data().get().at("verificationEvents").at("toDevice").template get<EventList>();
- return that.processVerificationEventsFromSync(ves);
- });
-
- m_ctx.promiseInterface()
- .all(std::vector<PromiseT>{uploadOneTimeKeysRes, queryKeysRes, processVerificationEventsRes})
+ doOneSyncCycle()
.then([that=toEventLoop(), retryTime](auto stat) {
if (stat.success()) {
that.syncForever(); // reset retry time
@@ -594,6 +527,88 @@
return m_ctx.dispatch(SetShouldSyncAction{false});
}
+ auto Client::ensureReadyForSyncing() const -> PromiseT
+ {
+ KAZV_VERIFY_THREAD_ID();
+ using namespace Kazv::CursorOp;
+
+ return m_ctx.createResolvedPromise(true)
+ .then([that=toEventLoop()](auto) {
+ // post filters, if filters are incomplete
+ if ((+that.clientCursor()[&ClientModel::initialSyncFilterId]).empty()
+ || (+that.clientCursor()[&ClientModel::incrementalSyncFilterId]).empty()) {
+ return that.m_ctx.dispatch(PostInitialFiltersAction{});
+ }
+ return that.m_ctx.createResolvedPromise(true);
+ })
+ .then([that=toEventLoop()](auto stat) {
+ if (! stat.success()) {
+ return that.m_ctx.createResolvedPromise(stat);
+ }
+ // Upload identity keys if we need to
+ if (+that.clientCursor()[&ClientModel::crypto]
+ && ! +that.clientCursor()[&ClientModel::identityKeysUploaded]) {
+ return that.m_ctx.dispatch(UploadIdentityKeysAction{});
+ } else {
+ return that.m_ctx.createResolvedPromise(true);
+ }
+ });
+ }
+
+ auto Client::doOneSyncCycle() const -> PromiseT
+ {
+ KAZV_VERIFY_THREAD_ID();
+ using namespace CursorOp;
+
+ bool isInitialSync = !(+clientCursor()[&ClientModel::syncToken]).has_value();
+
+ auto syncRes = m_ctx.dispatch(SyncAction{});
+
+ auto uploadOneTimeKeysRes = syncRes
+ .then([that=toEventLoop()](auto stat) {
+ if (! stat.success()) {
+ return that.m_ctx.createResolvedPromise(stat);
+ }
+ auto &rg = lager::get<RandomInterface &>(that.m_deps.value());
+ bool hasCrypto{+that.clientCursor()[&ClientModel::crypto]};
+ if (! hasCrypto) {
+ return that.m_ctx.createResolvedPromise(true);
+ }
+ auto numKeysToGenerate = (+that.clientCursor()).numOneTimeKeysNeeded();
+ return that.m_ctx.dispatch(GenerateAndUploadOneTimeKeysAction{
+ numKeysToGenerate,
+ rg.generateRange<RandomData>(GenerateAndUploadOneTimeKeysAction::randomSize(numKeysToGenerate))
+ });
+ });
+
+ auto queryKeysRes = syncRes
+ .then([that=toEventLoop(), isInitialSync](auto stat) {
+ if (! stat.success()) {
+ return that.m_ctx.createResolvedPromise(stat);
+ }
+ bool hasCrypto{+that.clientCursor()[&ClientModel::crypto]};
+ return hasCrypto
+ ? that.m_ctx.dispatch(QueryKeysAction{isInitialSync})
+ : that.m_ctx.createResolvedPromise(true);
+ });
+
+ auto processVerificationEventsRes = syncRes
+ .then([that=toEventLoop()](EffectStatus stat) {
+ if (!stat.success() || !that.m_deps ||
+ !that.clientCursor().map([](const auto &c) {
+ return c.crypto.has_value();
+ }).make().get()) {
+ return that.m_ctx.createResolvedPromise(stat);
+ }
+ kzo.client.dbg() << "processVerificationEvents: " << stat.data().get().dump() << std::endl;
+ EventList ves = stat.data().get().at("verificationEvents").at("toDevice").template get<EventList>();
+ return that.processVerificationEventsFromSync(ves);
+ });
+
+ return m_ctx.promiseInterface()
+ .all(std::vector<PromiseT>{uploadOneTimeKeysRes, queryKeysRes, processVerificationEventsRes});
+ }
+
lager::reader<ClientModel> Client::clientCursor() const
{
KAZV_VERIFY_THREAD_ID();
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Apr 17, 9:27 AM (7 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1337898
Default Alt Text
D301.1776443235.diff (10 KB)
Attached To
Mode
D301: Extract sync preparation and single sync cycle into own functions
Attached
Detach File
Event Timeline
Log In to Comment