some hopefully final preparations for delay manager

This commit is contained in:
Blue 2023-03-08 23:28:48 +03:00
parent 9fff409630
commit 5ba97ecc25
Signed by: blue
GPG Key ID: 9B203B252A63EE38
8 changed files with 148 additions and 56 deletions

View File

@ -107,7 +107,6 @@ Account::Account(const QString& p_login, const QString& p_server, const QString&
client.addExtension(tm); client.addExtension(tm);
client.addExtension(om); client.addExtension(om);
om->setSecurityPolicy(QXmpp::Toakafa); om->setSecurityPolicy(QXmpp::Toakafa);
om->setNewDeviceAutoSessionBuildingEnabled(true);
if (oh->hasOwnDevice()) { if (oh->hasOwnDevice()) {
QXmppTask<bool> future = om->load(); QXmppTask<bool> future = om->load();

View File

@ -21,7 +21,7 @@
#include "owncardinternal.h" #include "owncardinternal.h"
#include "owninfoforuser.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), QObject(parent),
maxParallelJobs(mpj), maxParallelJobs(mpj),
nextJobId(1), nextJobId(1),
@ -34,8 +34,9 @@ Core::DelayManager::Manager::Manager(Job::Id mpj, QObject* parent) :
scheduledVCards(), scheduledVCards(),
requestedVCards(), requestedVCards(),
#ifdef WITH_OMEMO #ifdef WITH_OMEMO
requestedBundles() requestedBundles(),
#endif #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++; Job::Id id = nextJobId++;
if (id == 0) if (id == 0)
id = nextJobId++; id = nextJobId++;
@ -94,7 +95,7 @@ void Core::DelayManager::Manager::getOwnVCard() {
scheduleJob(new OwnCardInternal(getNextJobId())); 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; Job* job = nullptr;
std::map<QString, Job::Id>::const_iterator sitr = scheduledVCards.find(jid); std::map<QString, Job::Id>::const_iterator sitr = scheduledVCards.find(jid);
if (sitr == scheduledVCards.end()) { if (sitr == scheduledVCards.end()) {
@ -107,7 +108,7 @@ Core::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) {
return job; return job;
} }
void Core::DelayManager::Manager::preScheduleJob(Core::Job* job) { void Core::DelayManager::Manager::preScheduleJob(Job* job) {
switch (job->type) { switch (job->type) {
case Job::Type::cardInternal: case Job::Type::cardInternal:
scheduledVCards.emplace(dynamic_cast<CardInternal*>(job)->jid, job->id); scheduledVCards.emplace(dynamic_cast<CardInternal*>(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); preScheduleJob(job);
if (runningJobs.size() < maxParallelJobs) { if (runningJobs.size() < maxParallelJobs) {
executeJob(job); 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) { switch (job->type) {
case Job::Type::cardInternal: case Job::Type::cardInternal:
case Job::Type::infoForUser: { 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); preExecuteJob(job);
runningJobs.emplace(job->id, job); runningJobs.emplace(job->id, job);
switch (job->type) { 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); preScheduleJob(job);
std::map<Job::Id, Job*>::iterator itr = runningJobs.find(job->id); std::map<Job::Id, Job*>::iterator itr = runningJobs.find(job->id);
if (itr != runningJobs.end()) { 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) { switch (job->type) {
case Job::Type::cardInternal: { case Job::Type::cardInternal: {
CardInternal* jb = dynamic_cast<CardInternal*>(job); CardInternal* jb = dynamic_cast<CardInternal*>(job);
@ -211,13 +212,20 @@ void Core::DelayManager::Manager::jobIsCanceled(Core::Job* job, bool wasRunning)
break; break;
case Job::Type::infoForUser: { case Job::Type::infoForUser: {
InfoForUser* jb = dynamic_cast<InfoForUser*>(job); InfoForUser* jb = dynamic_cast<InfoForUser*>(job);
if (jb->getStage() == InfoForUser::Stage::waitingForVCard) { switch (jb->getStage()) {
if (wasRunning) case InfoForUser::Stage::waitingForVCard:
requestedVCards.erase(jb->jid); if (wasRunning)
else requestedVCards.erase(jb->jid);
scheduledVCards.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)); emit receivedInfo(Shared::Info(jb->jid));
} }
@ -229,7 +237,7 @@ void Core::DelayManager::Manager::jobIsCanceled(Core::Job* job, bool wasRunning)
emit receivedOwnCard(Shared::VCard()); emit receivedOwnCard(Shared::VCard());
} }
ownInfoJobId = 0; ownInfoJobId = 0;
emit receivedOwnInfo(Shared::Info ("")); emit receivedOwnInfo(Shared::Info (ownJid));
} }
break; break;
@ -257,33 +265,32 @@ void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared
std::map<QString, Job::Id>::const_iterator cardItr = requestedVCards.find(jid); std::map<QString, Job::Id>::const_iterator cardItr = requestedVCards.find(jid);
if (cardItr == requestedVCards.end()) { if (cardItr == requestedVCards.end()) {
throw 8575; //never supposed to happen, the state is not correct; throw 8575; //never supposed to happen, the state is not correct;
} else { }
Job::Id jobId = cardItr->second; Job::Id jobId = cardItr->second;
requestedVCards.erase(cardItr); requestedVCards.erase(cardItr);
Job* job = runningJobs.at(jobId); Job* job = runningJobs.at(jobId);
switch (job->type) { switch (job->type) {
case Job::Type::cardInternal: case Job::Type::cardInternal:
jobIsDone(jobId); jobIsDone(jobId);
emit receivedCard(jid, card); emit receivedCard(jid, card);
break; break;
case Job::Type::infoForUser: case Job::Type::infoForUser: {
#ifdef WITH_OMEMO #ifdef WITH_OMEMO
requestedBundles.emplace(jid, jobId); requestedBundles.emplace(jid, jobId);
//TODO save card! InfoForUser* jb = dynamic_cast<InfoForUser*>(job);
emit requestBundles(jid); jb->receivedVCard(card);
emit requestBundles(jid);
#else #else
{ Shared::Info info(jid);
Shared::Info info(jid); info.turnIntoContact(card);
info.turnIntoContact(card); emit receivedInfo(info);
emit receivedInfo(info); jobIsDone(jobId);
}
jobIsDone(jobId);
#endif #endif
emit receivedCard(jid, card); emit receivedCard(jid, card);
break;
default:
throw 8576;
} }
break;
default:
throw 8576;
} }
} }
@ -296,26 +303,54 @@ void Core::DelayManager::Manager::ownVCardReceived(const Shared::VCard& card) {
jobIsDone(jobId); jobIsDone(jobId);
emit receivedOwnCard(card); emit receivedOwnCard(card);
break; break;
case Job::Type::ownInfoForUser: case Job::Type::ownInfoForUser: {
#ifdef WITH_OMEMO #ifdef WITH_OMEMO
//requestedBundles.emplace(jid, jobId); OwnInfoForUser* jb = dynamic_cast<OwnInfoForUser*>(job);
//TODO save card! jb->receivedVCard(card);
emit requestOwnBundle(); emit requestOwnBundle();
#else #else
{ Shared::Info info(ownJid);
Shared::Info info(""); info.turnIntoOwnAccount(card);
info.turnIntoOwnAccount(card); emit receivedOwnInfo(info);
emit receivedOwnInfo(info);
}
jobIsDone(jobId); jobIsDone(jobId);
#endif #endif
emit receivedOwnCard(card); emit receivedOwnCard(card);
}
break; break;
default: default:
throw 8576; throw 8576;
} }
} }
void Core::DelayManager::Manager::receivedBundles(const QString& jid) { void Core::DelayManager::Manager::receivedBundles(const QString& jid, const std::list<Shared::KeyInfo>& keys) {
std::map<QString, Job::Id>::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<InfoForUser*>(jb);
Shared::Info info(jid);
info.turnIntoContact(job->claim(), new std::list<Shared::KeyInfo>(keys));
emit receivedInfo(info);
jobIsDone(jobId);
}
void Core::DelayManager::Manager::receivedOwnBundles(const std::list<Shared::KeyInfo>& keys) {
Job::Id jobId = ownInfoJobId;
ownInfoJobId = 0;
Job* jb = runningJobs.at(jobId);
OwnInfoForUser* job = dynamic_cast<OwnInfoForUser*>(jb);
Shared::Info info(ownJid);
info.turnIntoOwnAccount(job->claim(), new std::list<Shared::KeyInfo>(keys));
emit receivedOwnInfo(info);
jobIsDone(jobId);
}
void Core::DelayManager::Manager::setOwnJid(const QString& jid) {
ownJid = jid;
} }

View File

@ -40,19 +40,21 @@ class Manager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
Manager(Job::Id maxParallelJobs = 5, QObject* parent = nullptr); Manager(const QString& ownJid, Job::Id maxParallelJobs = 5, QObject* parent = nullptr);
~Manager(); ~Manager();
void getOwnVCard(); void getOwnVCard();
void getOwnInfo(); void getOwnInfo();
void getVCard(const QString& jid); void getVCard(const QString& jid);
void getInfo(const QString& jid); void getInfo(const QString& jid);
void setOwnJid(const QString& jid);
signals: signals:
void requestVCard(const QString& jid); void requestVCard(const QString& jid);
void requestOwnVCard(); void requestOwnVCard();
void requestBundles(const QString& jid); void requestBundles(const QString& jid);
void requestOwnBundle(); void requestOwnBundle();
void receivedCard(const QString& jid, const Shared::VCard& info); void receivedCard(const QString& jid, const Shared::VCard& info);
void receivedOwnCard(const Shared::VCard& info); void receivedOwnCard(const Shared::VCard& info);
void receivedInfo(const Shared::Info& info); void receivedInfo(const Shared::Info& info);
@ -62,7 +64,8 @@ public slots:
void disconnected(); void disconnected();
void ownVCardReceived(const Shared::VCard& card); void ownVCardReceived(const Shared::VCard& card);
void receivedVCard(const QString& jid, 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<Shared::KeyInfo>& keys);
void receivedOwnBundles(const std::list<Shared::KeyInfo>& keys);
private: private:
void preScheduleJob(Job* job); void preScheduleJob(Job* job);
@ -114,6 +117,7 @@ private:
#ifdef WITH_OMEMO #ifdef WITH_OMEMO
std::map<QString, Job::Id> requestedBundles; std::map<QString, Job::Id> requestedBundles;
#endif #endif
QString ownJid;
}; };
} }

View File

@ -18,17 +18,39 @@
Core::DelayManager::Info::Info(Id p_id, Type p_type) : Core::DelayManager::Info::Info(Id p_id, Type p_type) :
Job(p_id, p_type), Job(p_id, p_type),
stage(Stage::waitingForVCard) stage(Stage::waitingForVCard),
info(nullptr)
{} {}
Core::DelayManager::Info::Info(const Info& other) : Core::DelayManager::Info::Info(const Info& other) :
Job(other), Job(other),
stage(other.stage) stage(other.stage),
info(nullptr)
{} {}
Core::DelayManager::Info::~Info() { Core::DelayManager::Info::~Info() {
if (stage == Stage::waitingForBundles) {
delete info;
}
} }
Core::DelayManager::Info::Stage Core::DelayManager::Info::getStage() const { Core::DelayManager::Info::Stage Core::DelayManager::Info::getStage() const {
return stage; 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;
}

View File

@ -19,6 +19,9 @@
#include "job.h" #include "job.h"
#include <shared/vcard.h>
#include <shared/info.h>
namespace Core { namespace Core {
namespace DelayManager { namespace DelayManager {
@ -26,7 +29,8 @@ class Info : public virtual Job {
public: public:
enum class Stage { enum class Stage {
waitingForVCard, waitingForVCard,
waitingForBundles waitingForBundles,
finished
}; };
protected: protected:
@ -36,10 +40,14 @@ protected:
public: public:
~Info(); ~Info();
void receivedVCard(const Shared::VCard& card);
Shared::VCard* claim();
Stage getStage() const; Stage getStage() const;
private: private:
Stage stage; Stage stage;
Shared::VCard* info;
}; };
} }

View File

@ -27,4 +27,3 @@ Core::DelayManager::InfoForUser::InfoForUser(const InfoForUser& other) :
Contact(other), Contact(other),
Info(other) Info(other)
{} {}

View File

@ -328,3 +328,26 @@ Shared::VCard * Shared::Info::getVCard() {
throw 365; throw 365;
} }
} }
void Shared::Info::setActiveKeys(std::list<KeyInfo>* 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;
}
}

View File

@ -69,11 +69,13 @@ public:
VCard& getVCardRef(); VCard& getVCardRef();
const VCard* getVCard() const; const VCard* getVCard() const;
VCard* getVCard(); VCard* getVCard();
void setVCard(Shared::VCard* card);
const std::list<KeyInfo>& getActiveKeysRef() const; const std::list<KeyInfo>& getActiveKeysRef() const;
std::list<KeyInfo>& getActiveKeysRef(); std::list<KeyInfo>& getActiveKeysRef();
const std::list<KeyInfo>* getActiveKeys() const; const std::list<KeyInfo>* getActiveKeys() const;
std::list<KeyInfo>* getActiveKeys(); std::list<KeyInfo>* getActiveKeys();
void setActiveKeys(std::list<KeyInfo>* keys);
const std::list<KeyInfo>& getInactiveKeysRef() const; const std::list<KeyInfo>& getInactiveKeysRef() const;
std::list<KeyInfo>& getInactiveKeysRef(); std::list<KeyInfo>& getInactiveKeysRef();