[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2018 Simon Redman <simon@ergotech.com>
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "requestconversationworker.h"
|
|
|
|
|
|
|
|
#include "conversationsdbusinterface.h"
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
RequestConversationWorker::RequestConversationWorker(const qint64 &conversationID, int start, int end, ConversationsDbusInterface *interface)
|
|
|
|
: // QObject(interface)
|
|
|
|
conversationID(conversationID)
|
|
|
|
, start(start)
|
|
|
|
, parent(interface)
|
|
|
|
, m_thread(new QThread)
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
{
|
2019-01-23 01:27:10 +00:00
|
|
|
Q_ASSERT(end >= start && "Not allowed to have a negative-length range");
|
|
|
|
howMany = end - start;
|
|
|
|
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
this->moveToThread(m_thread);
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(m_thread, &QThread::started, this, &RequestConversationWorker::handleRequestConversation);
|
|
|
|
connect(m_thread, &QThread::finished, m_thread, &QObject::deleteLater);
|
|
|
|
connect(this, &RequestConversationWorker::finished, m_thread, &QThread::quit);
|
|
|
|
connect(this, &RequestConversationWorker::finished, this, &QObject::deleteLater);
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RequestConversationWorker::handleRequestConversation()
|
|
|
|
{
|
|
|
|
auto messagesList = parent->getConversation(conversationID);
|
|
|
|
|
|
|
|
if (messagesList.isEmpty()) {
|
|
|
|
// Since there are no messages in the conversation, it's likely that it is a junk ID, but go ahead anyway
|
|
|
|
qCWarning(KDECONNECT_CONVERSATIONS) << "Got a conversationID for a conversation with no messages!" << conversationID;
|
|
|
|
}
|
|
|
|
|
2018-12-14 16:59:33 +00:00
|
|
|
// In case the remote takes awhile to respond, we should go ahead and do anything we can from the cache
|
2019-01-23 01:27:10 +00:00
|
|
|
size_t numHandled = replyForConversation(messagesList, start, howMany);
|
2018-12-14 16:59:33 +00:00
|
|
|
|
2019-01-23 01:27:10 +00:00
|
|
|
if (numHandled < howMany) {
|
2020-11-02 16:40:58 +00:00
|
|
|
// In this case, the cache wasn't able to satisfy the request fully. Get more.
|
|
|
|
|
2019-01-23 01:27:10 +00:00
|
|
|
size_t numRemaining = howMany - numHandled;
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
parent->updateConversation(conversationID);
|
|
|
|
messagesList = parent->getConversation(conversationID);
|
2022-09-10 22:23:52 +01:00
|
|
|
// ConversationsDbusInterface::updateConversation blocks until it sees new messages in the requested conversation
|
2019-01-23 01:27:10 +00:00
|
|
|
replyForConversation(messagesList, start + numHandled, numRemaining);
|
2020-11-02 16:40:58 +00:00
|
|
|
} else {
|
|
|
|
// The cache was able to fully satisfy the request but we need to check that it isn't running dry
|
|
|
|
|
|
|
|
size_t numCachedMessages = messagesList.count();
|
|
|
|
size_t requestEnd = start + numHandled;
|
|
|
|
size_t numRemainingMessages = numCachedMessages - requestEnd;
|
2022-09-10 22:23:52 +01:00
|
|
|
double percentRemaining = ((double)numRemainingMessages / numCachedMessages) * 100;
|
2020-11-02 16:40:58 +00:00
|
|
|
|
|
|
|
if (percentRemaining < CACHE_LOW_WATER_MARK_PERCENT || numRemainingMessages < MIN_NUMBER_TO_REQUEST) {
|
|
|
|
parent->updateConversation(conversationID);
|
|
|
|
}
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 01:27:10 +00:00
|
|
|
Q_EMIT finished();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
size_t RequestConversationWorker::replyForConversation(const QList<ConversationMessage> &conversation, int start, size_t howMany)
|
|
|
|
{
|
2019-01-23 01:27:10 +00:00
|
|
|
Q_ASSERT(start >= 0);
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
// Messages are sorted in ascending order of keys, meaning the front of the list has the oldest
|
|
|
|
// messages (smallest timestamp number)
|
|
|
|
// Therefore, return the end of the list first (most recent messages)
|
2019-03-07 18:48:06 +00:00
|
|
|
size_t i = 0;
|
2022-09-10 22:23:52 +01:00
|
|
|
for (auto it = conversation.crbegin() + start; it != conversation.crend(); ++it) {
|
2019-01-23 01:27:10 +00:00
|
|
|
if (i >= howMany) {
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-07-19 18:33:15 +01:00
|
|
|
Q_EMIT conversationMessageRead(QDBusVariant(QVariant::fromValue(*it)));
|
2019-01-23 01:27:10 +00:00
|
|
|
i++;
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 01:27:10 +00:00
|
|
|
return i;
|
[SMS App] Make requestMoreMessages asynchronous, blocking, and caching
Summary:
The most serious change from this patch is to move the asynchronous replying to a request from the app for more messages to a newly-spawned, self-destructing thread. Within that thread, we block until the remote device replies with the requested messages.
All gotten messages are cached in the ConversationDbusInterface, so all future requests are fast and don't hit the remote device.
Test Plan: After applying this diff, the messaging app should show 10 messages every time it is opened
Reviewers: #kde_connect, nicolasfella, albertvaka
Reviewed By: #kde_connect, albertvaka
Subscribers: albertvaka, apol, nicolasfella, kdeconnect
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D16475
2018-12-13 05:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RequestConversationWorker::work()
|
|
|
|
{
|
|
|
|
m_thread->start();
|
|
|
|
}
|