From 5ba97ecc2538b4f823ec68d75dc751e3f8038933 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 8 Mar 2023 23:28:48 +0300 Subject: [PATCH] some hopefully final preparations for delay manager --- core/account.cpp | 1 - core/delayManager/delaymanager.cpp | 133 ++++++++++++++++++----------- core/delayManager/delaymanager.h | 8 +- core/delayManager/info.cpp | 26 +++++- core/delayManager/info.h | 10 ++- core/delayManager/infoforuser.cpp | 1 - shared/info.cpp | 23 +++++ shared/info.h | 2 + 8 files changed, 148 insertions(+), 56 deletions(-) diff --git a/core/account.cpp b/core/account.cpp index 0a6c59c..d158970 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -107,7 +107,6 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& client.addExtension(tm); client.addExtension(om); om->setSecurityPolicy(QXmpp::Toakafa); - om->setNewDeviceAutoSessionBuildingEnabled(true); if (oh->hasOwnDevice()) { QXmppTask future = om->load(); diff --git a/core/delayManager/delaymanager.cpp b/core/delayManager/delaymanager.cpp index 906724f..0ecc9a4 100644 --- a/core/delayManager/delaymanager.cpp +++ b/core/delayManager/delaymanager.cpp @@ -21,7 +21,7 @@ #include "owncardinternal.h" #include "owninfoforuser.h" -Core::DelayManager::Manager::Manager(Job::Id mpj, QObject* parent) : +Core::DelayManager::Manager::Manager(const QString& poj, Job::Id mpj, QObject* parent) : QObject(parent), maxParallelJobs(mpj), nextJobId(1), @@ -34,8 +34,9 @@ Core::DelayManager::Manager::Manager(Job::Id mpj, QObject* parent) : scheduledVCards(), requestedVCards(), #ifdef WITH_OMEMO - requestedBundles() + requestedBundles(), #endif + ownJid(poj) { } @@ -49,7 +50,7 @@ Core::DelayManager::Manager::~Manager() { } } -Core::Job::Id Core::DelayManager::Manager::getNextJobId() { +Core::DelayManager::Job::Id Core::DelayManager::Manager::getNextJobId() { Job::Id id = nextJobId++; if (id == 0) id = nextJobId++; @@ -94,7 +95,7 @@ void Core::DelayManager::Manager::getOwnVCard() { scheduleJob(new OwnCardInternal(getNextJobId())); } -Core::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) { +Core::DelayManager::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) { Job* job = nullptr; std::map::const_iterator sitr = scheduledVCards.find(jid); if (sitr == scheduledVCards.end()) { @@ -107,7 +108,7 @@ Core::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) { return job; } -void Core::DelayManager::Manager::preScheduleJob(Core::Job* job) { +void Core::DelayManager::Manager::preScheduleJob(Job* job) { switch (job->type) { case Job::Type::cardInternal: scheduledVCards.emplace(dynamic_cast(job)->jid, job->id); @@ -125,7 +126,7 @@ void Core::DelayManager::Manager::preScheduleJob(Core::Job* job) { } } -void Core::DelayManager::Manager::scheduleJob(Core::Job* job) { +void Core::DelayManager::Manager::scheduleJob(Job* job) { preScheduleJob(job); if (runningJobs.size() < maxParallelJobs) { executeJob(job); @@ -134,7 +135,7 @@ void Core::DelayManager::Manager::scheduleJob(Core::Job* job) { } } -void Core::DelayManager::Manager::preExecuteJob(Core::Job* job) { +void Core::DelayManager::Manager::preExecuteJob(Job* job) { switch (job->type) { case Job::Type::cardInternal: case Job::Type::infoForUser: { @@ -149,7 +150,7 @@ void Core::DelayManager::Manager::preExecuteJob(Core::Job* job) { } } -void Core::DelayManager::Manager::executeJob(Core::Job* job) { +void Core::DelayManager::Manager::executeJob(Job* job) { preExecuteJob(job); runningJobs.emplace(job->id, job); switch (job->type) { @@ -179,7 +180,7 @@ void Core::DelayManager::Manager::jobIsDone(Job::Id jobId) { } } -void Core::DelayManager::Manager::replaceJob(Core::Job* job) { +void Core::DelayManager::Manager::replaceJob(Job* job) { preScheduleJob(job); std::map::iterator itr = runningJobs.find(job->id); if (itr != runningJobs.end()) { @@ -197,7 +198,7 @@ void Core::DelayManager::Manager::replaceJob(Core::Job* job) { } } -void Core::DelayManager::Manager::jobIsCanceled(Core::Job* job, bool wasRunning) { +void Core::DelayManager::Manager::jobIsCanceled(Job* job, bool wasRunning) { switch (job->type) { case Job::Type::cardInternal: { CardInternal* jb = dynamic_cast(job); @@ -211,13 +212,20 @@ void Core::DelayManager::Manager::jobIsCanceled(Core::Job* job, bool wasRunning) break; case Job::Type::infoForUser: { InfoForUser* jb = dynamic_cast(job); - if (jb->getStage() == InfoForUser::Stage::waitingForVCard) { - if (wasRunning) - requestedVCards.erase(jb->jid); - else - scheduledVCards.erase(jb->jid); + switch (jb->getStage()) { + case InfoForUser::Stage::waitingForVCard: + if (wasRunning) + requestedVCards.erase(jb->jid); + else + scheduledVCards.erase(jb->jid); - emit receivedVCard(jb->jid, Shared::VCard()); + emit receivedVCard(jb->jid, Shared::VCard()); + break; + case InfoForUser::Stage::waitingForBundles: + requestedBundles.erase(jb->jid); + break; + default: + break; } emit receivedInfo(Shared::Info(jb->jid)); } @@ -229,7 +237,7 @@ void Core::DelayManager::Manager::jobIsCanceled(Core::Job* job, bool wasRunning) emit receivedOwnCard(Shared::VCard()); } ownInfoJobId = 0; - emit receivedOwnInfo(Shared::Info ("")); + emit receivedOwnInfo(Shared::Info (ownJid)); } break; @@ -257,33 +265,32 @@ void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared std::map::const_iterator cardItr = requestedVCards.find(jid); if (cardItr == requestedVCards.end()) { throw 8575; //never supposed to happen, the state is not correct; - } else { - Job::Id jobId = cardItr->second; - requestedVCards.erase(cardItr); - Job* job = runningJobs.at(jobId); - switch (job->type) { - case Job::Type::cardInternal: - jobIsDone(jobId); - emit receivedCard(jid, card); - break; - case Job::Type::infoForUser: + } + Job::Id jobId = cardItr->second; + requestedVCards.erase(cardItr); + Job* job = runningJobs.at(jobId); + switch (job->type) { + case Job::Type::cardInternal: + jobIsDone(jobId); + emit receivedCard(jid, card); + break; + case Job::Type::infoForUser: { #ifdef WITH_OMEMO - requestedBundles.emplace(jid, jobId); - //TODO save card! - emit requestBundles(jid); + requestedBundles.emplace(jid, jobId); + InfoForUser* jb = dynamic_cast(job); + jb->receivedVCard(card); + emit requestBundles(jid); #else - { - Shared::Info info(jid); - info.turnIntoContact(card); - emit receivedInfo(info); - } - jobIsDone(jobId); + Shared::Info info(jid); + info.turnIntoContact(card); + emit receivedInfo(info); + jobIsDone(jobId); #endif - emit receivedCard(jid, card); - break; - default: - throw 8576; + emit receivedCard(jid, card); } + break; + default: + throw 8576; } } @@ -296,26 +303,54 @@ void Core::DelayManager::Manager::ownVCardReceived(const Shared::VCard& card) { jobIsDone(jobId); emit receivedOwnCard(card); break; - case Job::Type::ownInfoForUser: + case Job::Type::ownInfoForUser: { #ifdef WITH_OMEMO - //requestedBundles.emplace(jid, jobId); - //TODO save card! + OwnInfoForUser* jb = dynamic_cast(job); + jb->receivedVCard(card); emit requestOwnBundle(); #else - { - Shared::Info info(""); - info.turnIntoOwnAccount(card); - emit receivedOwnInfo(info); - } + Shared::Info info(ownJid); + info.turnIntoOwnAccount(card); + emit receivedOwnInfo(info); jobIsDone(jobId); #endif emit receivedOwnCard(card); + } break; default: throw 8576; } } -void Core::DelayManager::Manager::receivedBundles(const QString& jid) { +void Core::DelayManager::Manager::receivedBundles(const QString& jid, const std::list& keys) { + std::map::const_iterator itr = requestedBundles.find(jid); + if (itr == requestedBundles.end()) { + throw 8577; //never supposed to happen, the state is not correct; + } + Job::Id jobId = itr->second; + requestedBundles.erase(itr); + Job* jb = runningJobs.at(jobId); + InfoForUser* job = dynamic_cast(jb); + + Shared::Info info(jid); + info.turnIntoContact(job->claim(), new std::list(keys)); + emit receivedInfo(info); + jobIsDone(jobId); +} + +void Core::DelayManager::Manager::receivedOwnBundles(const std::list& keys) { + Job::Id jobId = ownInfoJobId; + ownInfoJobId = 0; + Job* jb = runningJobs.at(jobId); + OwnInfoForUser* job = dynamic_cast(jb); + + Shared::Info info(ownJid); + info.turnIntoOwnAccount(job->claim(), new std::list(keys)); + emit receivedOwnInfo(info); + jobIsDone(jobId); +} + +void Core::DelayManager::Manager::setOwnJid(const QString& jid) { + ownJid = jid; } diff --git a/core/delayManager/delaymanager.h b/core/delayManager/delaymanager.h index c3d1da8..d859a18 100644 --- a/core/delayManager/delaymanager.h +++ b/core/delayManager/delaymanager.h @@ -40,19 +40,21 @@ class Manager : public QObject { Q_OBJECT public: - Manager(Job::Id maxParallelJobs = 5, QObject* parent = nullptr); + Manager(const QString& ownJid, Job::Id maxParallelJobs = 5, QObject* parent = nullptr); ~Manager(); void getOwnVCard(); void getOwnInfo(); void getVCard(const QString& jid); void getInfo(const QString& jid); + void setOwnJid(const QString& jid); signals: void requestVCard(const QString& jid); void requestOwnVCard(); void requestBundles(const QString& jid); void requestOwnBundle(); + void receivedCard(const QString& jid, const Shared::VCard& info); void receivedOwnCard(const Shared::VCard& info); void receivedInfo(const Shared::Info& info); @@ -62,7 +64,8 @@ public slots: void disconnected(); void ownVCardReceived(const Shared::VCard& card); void receivedVCard(const QString& jid, const Shared::VCard& card); - void receivedBundles(const QString& jid); + void receivedBundles(const QString& jid, const std::list& keys); + void receivedOwnBundles(const std::list& keys); private: void preScheduleJob(Job* job); @@ -114,6 +117,7 @@ private: #ifdef WITH_OMEMO std::map requestedBundles; #endif + QString ownJid; }; } diff --git a/core/delayManager/info.cpp b/core/delayManager/info.cpp index 3b61f63..b5b619d 100644 --- a/core/delayManager/info.cpp +++ b/core/delayManager/info.cpp @@ -18,17 +18,39 @@ Core::DelayManager::Info::Info(Id p_id, Type p_type) : Job(p_id, p_type), - stage(Stage::waitingForVCard) + stage(Stage::waitingForVCard), + info(nullptr) {} Core::DelayManager::Info::Info(const Info& other) : Job(other), - stage(other.stage) + stage(other.stage), + info(nullptr) {} Core::DelayManager::Info::~Info() { + if (stage == Stage::waitingForBundles) { + delete info; + } } Core::DelayManager::Info::Stage Core::DelayManager::Info::getStage() const { return stage; } + +void Core::DelayManager::Info::receivedVCard(const Shared::VCard& card) { + if (stage != Stage::waitingForVCard) + throw 245; + + info = new Shared::VCard(card); + stage = Stage::waitingForBundles; +} + +Shared::VCard * Core::DelayManager::Info::claim() { + if (stage != Stage::waitingForBundles) + throw 246; + + Shared::VCard* res = info; + info = nullptr; + return res; +} diff --git a/core/delayManager/info.h b/core/delayManager/info.h index 0c4249a..a6601a1 100644 --- a/core/delayManager/info.h +++ b/core/delayManager/info.h @@ -19,6 +19,9 @@ #include "job.h" +#include +#include + namespace Core { namespace DelayManager { @@ -26,7 +29,8 @@ class Info : public virtual Job { public: enum class Stage { waitingForVCard, - waitingForBundles + waitingForBundles, + finished }; protected: @@ -36,10 +40,14 @@ protected: public: ~Info(); + void receivedVCard(const Shared::VCard& card); + Shared::VCard* claim(); Stage getStage() const; + private: Stage stage; + Shared::VCard* info; }; } diff --git a/core/delayManager/infoforuser.cpp b/core/delayManager/infoforuser.cpp index fc494fb..067be80 100644 --- a/core/delayManager/infoforuser.cpp +++ b/core/delayManager/infoforuser.cpp @@ -27,4 +27,3 @@ Core::DelayManager::InfoForUser::InfoForUser(const InfoForUser& other) : Contact(other), Info(other) {} - diff --git a/shared/info.cpp b/shared/info.cpp index c0d6f26..36cd4b5 100644 --- a/shared/info.cpp +++ b/shared/info.cpp @@ -328,3 +328,26 @@ Shared::VCard * Shared::Info::getVCard() { throw 365; } } + +void Shared::Info::setActiveKeys(std::list* keys) { + switch (type) { + case EntryType::contact: + case EntryType::ownAccount: + activeKeys = keys; + break; + default: + throw 366; + } +} + +void Shared::Info::setVCard(Shared::VCard* card) { + switch (type) { + case EntryType::contact: + case EntryType::ownAccount: + vcard = card; + break; + default: + throw 367; + } +} + diff --git a/shared/info.h b/shared/info.h index b0f495e..e42eddb 100644 --- a/shared/info.h +++ b/shared/info.h @@ -69,11 +69,13 @@ public: VCard& getVCardRef(); const VCard* getVCard() const; VCard* getVCard(); + void setVCard(Shared::VCard* card); const std::list& getActiveKeysRef() const; std::list& getActiveKeysRef(); const std::list* getActiveKeys() const; std::list* getActiveKeys(); + void setActiveKeys(std::list* keys); const std::list& getInactiveKeysRef() const; std::list& getInactiveKeysRef();