some ideas over delay manager

This commit is contained in:
Blue 2023-03-04 00:27:12 +03:00
parent 77dd28b600
commit 2d8f32c257
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
7 changed files with 276 additions and 31 deletions

View File

@ -3,22 +3,30 @@ if(WIN32)
set(SIGNALCATCHER_SOURCE signalcatcher_win32.cpp)
endif(WIN32)
target_sources(squawk PRIVATE
set(SOURCE_FILES
account.cpp
account.h
adapterfunctions.cpp
adapterfunctions.h
conference.cpp
conference.h
contact.cpp
contact.h
rosteritem.cpp
rosteritem.h
${SIGNALCATCHER_SOURCE}
signalcatcher.h
squawk.cpp
)
set(HEADER_FILES
account.h
adapterfunctions.h
conference.h
contact.h
rosteritem.h
signalcatcher.h
squawk.h
)
)
target_sources(squawk PRIVATE
${SOURCE_FILES}
${HEADER_FILES}
)
target_include_directories(squawk PRIVATE ${LMDB_INCLUDE_DIRS})

View File

@ -1,6 +1,16 @@
target_sources(squawk PRIVATE
set(SOURCE_FILES
networkaccess.cpp
networkaccess.h
clientcache.cpp
clientcache.h
delaymanager.cpp
)
set(HEADER_FILES
networkaccess.h
clientcache.h
delaymanager.h
)
target_sources(squawk PRIVATE
${SOURCE_FILES}
${HEADER_FILES}
)

View File

@ -0,0 +1,136 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "delaymanager.h"
Core::DelayManager::DelayManager(uint16_t mpj, QObject* parent) :
QObject(parent),
maxParallelJobs(mpj),
nextJobId(0),
scheduledJobs(),
runningJobs(),
pendingVCards(),
requestedVCards()
{
}
Core::DelayManager::~DelayManager() {}
void Core::DelayManager::requestInfo(const QString& jid) {
bool needToRequest = false;
std::pair<std::set<QString>::const_iterator, bool> result = pendingVCards.insert(jid);
if (result.second) { //if there is a clear evidence that we have not alredy been asked to request a VCard - just request it
needToRequest = true;
} else {
std::map<QString, uint16_t>::const_iterator itr = requestedVCards.find(jid);
if (itr != requestedVCards.end()) { //first check if the card is already requested, and if it is make sure we reply to user after we receive it
runningJobs[itr->second].first = TaskType::infoForUser;
} else {
needToRequest = true;
for (Job& job : scheduledJobs) { //looks like we need to manually check all the scheduled job and find the one with that jid there
if (job.first == TaskType::cardInternal || job.first == TaskType::infoForUser) { //to make sure we reply to user after we receive it
QString* jobJid = static_cast<QString*>(job.second);
if (*jobJid == jid) {
needToRequest = false;
job.first = TaskType::infoForUser;
break;
}
}
}
if (needToRequest) {
throw 8573; //something went terribly wrong here, the state is not correct;
}
}
}
if (needToRequest)
scheduleJob(TaskType::infoForUser, new QString(jid));
}
void Core::DelayManager::requestVCard(const QString& jid) {
std::pair<std::set<QString>::const_iterator, bool> result = pendingVCards.insert(jid);
if (result.second)
scheduleJob(TaskType::cardInternal, new QString(jid));
}
void Core::DelayManager::scheduleJob(Core::DelayManager::TaskType type, void* data) {
if (runningJobs.size() < maxParallelJobs) {
uint16_t currentJobId = nextJobId++;
runningJobs.emplace(currentJobId, std::make_pair(type, data));
switch (type) {
case TaskType::cardInternal:
case TaskType::infoForUser: {
QString* jid = static_cast<QString*>(data);
requestedVCards.emplace(*jid, currentJobId);
emit requestVCard(*jid);
}
break;
}
} else {
scheduledJobs.emplace_back(type, data);
}
}
void Core::DelayManager::executeJob(Core::DelayManager::Job job) {
uint16_t currentJobId = nextJobId++;
runningJobs.emplace(currentJobId, job);
switch (job.first) {
case TaskType::cardInternal:
case TaskType::infoForUser: {
QString* jid = static_cast<QString*>(job.second);
requestedVCards.emplace(*jid, currentJobId);
emit requestVCard(*jid);
}
break;
}
}
void Core::DelayManager::jobIsDone(uint16_t jobId) {
std::map<uint16_t, Job>::const_iterator itr = runningJobs.find(jobId);
if (itr == runningJobs.end()) {
throw 8574; //not supposed to happen, never
}
runningJobs.erase(itr);
if (scheduledJobs.size() > 0) {
Job job = scheduledJobs.front();
scheduledJobs.pop_front();
executeJob(job);
}
}
void Core::DelayManager::receivedVCard(const QString& jid, const Shared::VCard& card) {
std::map<QString, uint16_t>::const_iterator cardItr = requestedVCards.find(jid);
if (cardItr == requestedVCards.end()) {
throw 8575; //never supposed to happen, the state is not correct;
} else {
uint16_t jobId = cardItr->second;
requestedVCards.erase(cardItr);
pendingVCards.erase(jid);
const Job& job = runningJobs[jobId];
switch (job.first) {
case TaskType::cardInternal:
jobIsDone(jobId);
break;
case TaskType::infoForUser:
//all that stuff with further requesting
break;
default:
throw 8576;
}
}
}

View File

@ -0,0 +1,71 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CORE_DELAYMANAGER_H
#define CORE_DELAYMANAGER_H
#include <list>
#include <set>
#include <QObject>
#include <QString>
#include <shared/vcard.h>
#include <shared/info.h>
namespace Core {
class DelayManager : public QObject
{
Q_OBJECT
enum class TaskType {
cardInternal,
infoForUser
}
public:
DelayManager(uint16_t maxParallelJobs = 5, QObject* parent = nullptr);
~DelayManager();
void requestVCard(const QString& jid);
void requestInfo(const QString& jid);
signals:
void requestVCard(const QString& jid);
public slots:
void receivedVCard(const QString& jid, const Shared::VCard& card);
private:
typedef std::pair<TaskType, void*> Job;
void scheduleJob(TaskType type, void* data);
void executeJob(Job job);
void jobIsDone(uint16_t jobId);
private:
uint16_t maxParallelJobs;
uint16_t nextJobId;
std::list<Job> scheduledJobs;
std::map<uint16_t, Job> runningJobs;
std::set<QString> pendingVCards;
std::map<QString, uint16_t> requestedVCards;
};
}
#endif // CORE_DELAYMANAGER_H

View File

@ -1,14 +1,22 @@
target_sources(squawk PRIVATE
set(SOURCE_FILES
messagehandler.cpp
messagehandler.h
rosterhandler.cpp
rosterhandler.h
vcardhandler.cpp
vcardhandler.h
discoveryhandler.cpp
discoveryhandler.h
omemohandler.cpp
omemohandler.h
trusthandler.cpp
)
set(HEADER_FILES
messagehandler.h
rosterhandler.h
vcardhandler.h
discoveryhandler.h
omemohandler.h
trusthandler.h
)
)
target_sources(squawk PRIVATE
${SOURCE_FILES}
${HEADER_FILES}
)

View File

@ -1,7 +1,19 @@
target_sources(squawk PRIVATE
set(SOURCE_FILES
omemo.cpp
omemo.h
omemo.ui
keydelegate.cpp
)
set(UI_FILES
omemo.ui
)
set(HEADER_FILES
omemo.h
keydelegate.h
)
target_sources(squawk PRIVATE
${SOURCE_FILES}
${UI_FILES}
${HEADER_FILES}
)