squawk/core/delayManager/delaymanager.cpp

357 lines
11 KiB
C++
Raw Normal View History

2023-03-04 22:36:53 +00:00
// 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"
#include "cardinternal.h"
#include "infoforuser.h"
#include "owncardinternal.h"
#include "owninfoforuser.h"
Core::DelayManager::Manager::Manager(const QString& poj, Job::Id mpj, QObject* parent) :
2023-03-04 22:36:53 +00:00
QObject(parent),
maxParallelJobs(mpj),
nextJobId(1),
scheduledJobs(),
scheduledJobsById(scheduledJobs.get<id>()),
jobSequence(scheduledJobs.get<sequence>()),
runningJobs(),
ownVCardJobId(0),
ownInfoJobId(0),
scheduledVCards(),
requestedVCards(),
2023-03-07 18:45:01 +00:00
#ifdef WITH_OMEMO
requestedBundles(),
2023-03-07 18:45:01 +00:00
#endif
ownJid(poj)
2023-03-04 22:36:53 +00:00
{
}
2023-03-07 18:45:01 +00:00
Core::DelayManager::Manager::~Manager() {
for (const std::pair<const Job::Id, Job*>& pair : runningJobs) {
delete pair.second;
}
for (Job* job : jobSequence) {
delete job;
}
}
2023-03-04 22:36:53 +00:00
Core::DelayManager::Job::Id Core::DelayManager::Manager::getNextJobId() {
2023-03-04 22:36:53 +00:00
Job::Id id = nextJobId++;
if (id == 0)
id = nextJobId++;
return id;
}
2023-03-07 18:45:01 +00:00
void Core::DelayManager::Manager::getInfo(const QString& jid) {
2023-03-04 22:36:53 +00:00
Job* job = nullptr;
#ifdef WITH_OMEMO
std::map<QString, Job::Id>::const_iterator bitr = requestedBundles.find(jid);
if (bitr != requestedBundles.end())
job = runningJobs.at(bitr->second);
else
#endif
job = getVCardJob(jid);
if (job != nullptr) {
2023-03-07 18:45:01 +00:00
if (job->type == Job::Type::cardInternal)
2023-03-04 22:36:53 +00:00
replaceJob(new InfoForUser(job->id, jid));
} else
scheduleJob(new InfoForUser(getNextJobId(), jid));
}
2023-03-07 18:45:01 +00:00
void Core::DelayManager::Manager::getOwnInfo() {
2023-03-04 22:36:53 +00:00
if (ownInfoJobId == 0) {
if (ownVCardJobId != 0)
replaceJob(new OwnInfoForUser(ownVCardJobId));
else
scheduleJob(new OwnInfoForUser(getNextJobId()));
}
}
2023-03-07 18:45:01 +00:00
void Core::DelayManager::Manager::getVCard(const QString& jid) {
2023-03-04 22:36:53 +00:00
Job* job = getVCardJob(jid);
if (job == nullptr)
scheduleJob(new CardInternal(getNextJobId(), jid));
}
2023-03-07 18:45:01 +00:00
void Core::DelayManager::Manager::getOwnVCard() {
2023-03-04 22:36:53 +00:00
if (ownInfoJobId == 0)
scheduleJob(new OwnCardInternal(getNextJobId()));
}
Core::DelayManager::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) {
2023-03-04 22:36:53 +00:00
Job* job = nullptr;
std::map<QString, Job::Id>::const_iterator sitr = scheduledVCards.find(jid);
if (sitr == scheduledVCards.end()) {
std::map<QString, Job::Id>::const_iterator ritr = requestedVCards.find(jid);
if (ritr != requestedVCards.end())
job = runningJobs.at(ritr->second);
} else {
job = *(scheduledJobsById.find(sitr->second));
}
return job;
}
void Core::DelayManager::Manager::preScheduleJob(Job* job) {
2023-03-07 18:45:01 +00:00
switch (job->type) {
2023-03-04 22:36:53 +00:00
case Job::Type::cardInternal:
2023-03-07 18:45:01 +00:00
scheduledVCards.emplace(dynamic_cast<CardInternal*>(job)->jid, job->id);
2023-03-04 22:36:53 +00:00
break;
case Job::Type::ownCardInternal:
ownVCardJobId = job->id;
break;
case Job::Type::infoForUser:
2023-03-07 18:45:01 +00:00
scheduledVCards.emplace(dynamic_cast<InfoForUser*>(job)->jid, job->id);
2023-03-04 22:36:53 +00:00
break;
case Job::Type::ownInfoForUser:
ownVCardJobId = job->id;
ownInfoJobId = job->id;
break;
}
}
void Core::DelayManager::Manager::scheduleJob(Job* job) {
2023-03-04 22:36:53 +00:00
preScheduleJob(job);
if (runningJobs.size() < maxParallelJobs) {
executeJob(job);
} else {
scheduledJobs.push_back(job);
}
}
void Core::DelayManager::Manager::preExecuteJob(Job* job) {
2023-03-07 18:45:01 +00:00
switch (job->type) {
2023-03-04 22:36:53 +00:00
case Job::Type::cardInternal:
case Job::Type::infoForUser: {
2023-03-07 18:45:01 +00:00
Contact* cij = dynamic_cast<Contact*>(job);
2023-03-04 22:36:53 +00:00
requestedVCards.emplace(cij->jid, job->id);
scheduledVCards.erase(cij->jid);
}
break;
case Job::Type::ownInfoForUser:
case Job::Type::ownCardInternal:
break;
}
}
void Core::DelayManager::Manager::executeJob(Job* job) {
2023-03-04 22:36:53 +00:00
preExecuteJob(job);
runningJobs.emplace(job->id, job);
2023-03-07 18:45:01 +00:00
switch (job->type) {
2023-03-04 22:36:53 +00:00
case Job::Type::cardInternal:
case Job::Type::infoForUser:
2023-03-07 18:45:01 +00:00
emit requestVCard(dynamic_cast<Contact*>(job)->jid);
2023-03-04 22:36:53 +00:00
break;
case Job::Type::ownInfoForUser:
case Job::Type::ownCardInternal:
emit requestOwnVCard();
break;
}
}
2023-03-07 18:45:01 +00:00
void Core::DelayManager::Manager::jobIsDone(Job::Id jobId) {
2023-03-04 22:36:53 +00:00
std::map<Job::Id, Job*>::const_iterator itr = runningJobs.find(jobId);
if (itr == runningJobs.end()) {
throw 8573; //not supposed to happen, ever
}
Job* job = itr->second;
delete job;
runningJobs.erase(itr);
if (scheduledJobs.size() > 0) {
Job* job = scheduledJobs.front();
scheduledJobs.pop_front();
executeJob(job);
}
}
void Core::DelayManager::Manager::replaceJob(Job* job) {
2023-03-04 22:36:53 +00:00
preScheduleJob(job);
std::map<Job::Id, Job*>::iterator itr = runningJobs.find(job->id);
if (itr != runningJobs.end()) {
preExecuteJob(job);
delete itr->second;
itr->second = job;
} else {
StorageById::iterator sitr = scheduledJobsById.find(job->id);
if (sitr != scheduledJobsById.end()) {
delete *(sitr);
scheduledJobsById.replace(sitr, job);
} else {
throw 8574; //not supposed to happen, ever
}
}
}
void Core::DelayManager::Manager::jobIsCanceled(Job* job, bool wasRunning) {
2023-03-07 18:45:01 +00:00
switch (job->type) {
case Job::Type::cardInternal: {
CardInternal* jb = dynamic_cast<CardInternal*>(job);
if (wasRunning)
requestedVCards.erase(jb->jid);
else
scheduledVCards.erase(jb->jid);
emit receivedVCard(jb->jid, Shared::VCard());
}
break;
case Job::Type::infoForUser: {
InfoForUser* jb = dynamic_cast<InfoForUser*>(job);
switch (jb->getStage()) {
case InfoForUser::Stage::waitingForVCard:
if (wasRunning)
requestedVCards.erase(jb->jid);
else
scheduledVCards.erase(jb->jid);
2023-03-07 18:45:01 +00:00
emit receivedVCard(jb->jid, Shared::VCard());
break;
case InfoForUser::Stage::waitingForBundles:
requestedBundles.erase(jb->jid);
break;
default:
break;
2023-03-07 18:45:01 +00:00
}
emit receivedInfo(Shared::Info(jb->jid));
}
break;
case Job::Type::ownInfoForUser: {
OwnInfoForUser* jb = dynamic_cast<OwnInfoForUser*>(job);
if (jb->getStage() == OwnInfoForUser::Stage::waitingForVCard) {
ownVCardJobId = 0;
emit receivedOwnCard(Shared::VCard());
}
ownInfoJobId = 0;
emit receivedOwnInfo(Shared::Info (ownJid));
2023-03-07 18:45:01 +00:00
}
break;
case Job::Type::ownCardInternal:
ownVCardJobId = 0;
emit receivedOwnCard(Shared::VCard());
break;
}
delete job;
}
void Core::DelayManager::Manager::disconnected() {
for (const std::pair<const Job::Id, Job*> pair : runningJobs)
jobIsCanceled(pair.second, true);
for (Job* job : scheduledJobs)
jobIsCanceled(job, false);
runningJobs.clear();
scheduledJobs.clear();
}
void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared::VCard& card) {
2023-03-04 22:36:53 +00:00
std::map<QString, Job::Id>::const_iterator cardItr = requestedVCards.find(jid);
if (cardItr == requestedVCards.end()) {
throw 8575; //never supposed to happen, the state is not correct;
}
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: {
2023-03-04 22:36:53 +00:00
#ifdef WITH_OMEMO
requestedBundles.emplace(jid, jobId);
InfoForUser* jb = dynamic_cast<InfoForUser*>(job);
jb->receivedVCard(card);
emit requestBundles(jid);
2023-03-04 22:36:53 +00:00
#else
Shared::Info info(jid);
info.turnIntoContact(card);
emit receivedInfo(info);
jobIsDone(jobId);
2023-03-04 22:36:53 +00:00
#endif
emit receivedCard(jid, card);
2023-03-04 22:36:53 +00:00
}
break;
default:
throw 8576;
2023-03-04 22:36:53 +00:00
}
}
2023-03-07 18:45:01 +00:00
void Core::DelayManager::Manager::ownVCardReceived(const Shared::VCard& card) {
2023-03-04 22:36:53 +00:00
Job::Id jobId = ownVCardJobId;
ownVCardJobId = 0;
Job* job = runningJobs.at(jobId);
2023-03-07 18:45:01 +00:00
switch (job->type) {
2023-03-04 22:36:53 +00:00
case Job::Type::ownCardInternal:
jobIsDone(jobId);
emit receivedOwnCard(card);
break;
case Job::Type::ownInfoForUser: {
2023-03-04 22:36:53 +00:00
#ifdef WITH_OMEMO
OwnInfoForUser* jb = dynamic_cast<OwnInfoForUser*>(job);
jb->receivedVCard(card);
2023-03-04 22:36:53 +00:00
emit requestOwnBundle();
#else
Shared::Info info(ownJid);
info.turnIntoOwnAccount(card);
emit receivedOwnInfo(info);
2023-03-04 22:36:53 +00:00
jobIsDone(jobId);
#endif
emit receivedOwnCard(card);
}
2023-03-04 22:36:53 +00:00
break;
default:
throw 8576;
}
}
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);
}
2023-03-04 22:36:53 +00:00
void Core::DelayManager::Manager::setOwnJid(const QString& jid) {
ownJid = jid;
2023-03-04 22:36:53 +00:00
}