some more thinking about delay manager

This commit is contained in:
Blue 2023-03-07 21:45:01 +03:00
parent 99fd001292
commit 9fff409630
Signed by: blue
GPG Key ID: 9B203B252A63EE38
17 changed files with 320 additions and 96 deletions

View File

@ -5,6 +5,8 @@ set(SOURCE_FILES
infoforuser.cpp
owncardinternal.cpp
owninfoforuser.cpp
contact.cpp
info.cpp
)
set(HEADER_FILES
@ -14,6 +16,8 @@ set(HEADER_FILES
infoforuser.h
owncardinternal.h
owninfoforuser.h
contact.h
info.h
)
target_sources(squawk PRIVATE

View File

@ -16,17 +16,13 @@
#include "cardinternal.h"
Core::CardInternal::CardInternal(Job::Id p_id, const QString& p_jid) :
Job(p_id, Type::cardInternal),
jid(p_id)
Core::DelayManager::CardInternal::CardInternal(Id p_id, const QString& p_jid) :
Job(p_id, Type::cardInternal),
Contact(p_id, p_jid, Type::cardInternal)
{}
Core::CardInternal::CardInternal(Job::Id p_id, const QString& p_jid, Job::Type p_type) :
Job(p_id, p_type),
jid(p_id)
{}
Core::CardInternal::CardInternal(const Core::CardInternal& other) :
Core::DelayManager::CardInternal::CardInternal(const CardInternal& other) :
Job(other),
jid(other.jid)
Contact(other)
{}

View File

@ -14,28 +14,24 @@
// 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_CARDINTERNAL_H
#define CORE_CARDINTERNAL_H
#ifndef CORE_DELAYMANAGER_CARDINTERNAL_H
#define CORE_DELAYMANAGER_CARDINTERNAL_H
#include <QString>
#include "job.h"
#include "contact.h"
namespace Core {
namespace DelayManager {
class CardInternal : public Job {
protected:
CardInternal(Job::Id id, const QString& jid, Job::Type type);
class CardInternal : public Contact {
public:
CardInternal(Job::Id id, const QString& jid);
CardInternal(Id id, const QString& jid);
CardInternal(const CardInternal& other);
const QString jid;
};
}
}
#endif // CORE_CARDINTERNAL_H
#endif // CORE_DELAYMANAGER_CARDINTERNAL_H

View File

@ -0,0 +1,26 @@
// 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 "contact.h"
Core::DelayManager::Contact::Contact(const Contact& other):
Job(other),
jid(other.jid) {}
Core::DelayManager::Contact::Contact(Id p_id, const QString& p_jid, Type p_type):
Job(p_id, p_type),
jid(p_jid) {}

View File

@ -0,0 +1,39 @@
// 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_CONTACT_H
#define CORE_DELAYMANAGER_CONTACT_H
#include <QString>
#include "job.h"
namespace Core {
namespace DelayManager {
class Contact : public virtual Job {
protected:
Contact(Id id, const QString& jid, Type type);
Contact(const Contact& other);
public:
const QString jid;
};
}
}
#endif // CORE_DELAYMANAGER_CONTACT_H

View File

@ -21,7 +21,7 @@
#include "owncardinternal.h"
#include "owninfoforuser.h"
Core::DelayManager::DelayManager(Job::Id mpj, QObject* parent) :
Core::DelayManager::Manager::Manager(Job::Id mpj, QObject* parent) :
QObject(parent),
maxParallelJobs(mpj),
nextJobId(1),
@ -33,13 +33,23 @@ Core::DelayManager::DelayManager(Job::Id mpj, QObject* parent) :
ownInfoJobId(0),
scheduledVCards(),
requestedVCards(),
#ifdef WITH_OMEMO
requestedBundles()
#endif
{
}
Core::DelayManager::~DelayManager() {}
Core::DelayManager::Manager::~Manager() {
for (const std::pair<const Job::Id, Job*>& pair : runningJobs) {
delete pair.second;
}
Core::Job::Id Core::DelayManager::getNextJobId() {
for (Job* job : jobSequence) {
delete job;
}
}
Core::Job::Id Core::DelayManager::Manager::getNextJobId() {
Job::Id id = nextJobId++;
if (id == 0)
id = nextJobId++;
@ -47,7 +57,7 @@ Core::Job::Id Core::DelayManager::getNextJobId() {
return id;
}
void Core::DelayManager::getInfo(const QString& jid) {
void Core::DelayManager::Manager::getInfo(const QString& jid) {
Job* job = nullptr;
#ifdef WITH_OMEMO
std::map<QString, Job::Id>::const_iterator bitr = requestedBundles.find(jid);
@ -58,13 +68,13 @@ void Core::DelayManager::getInfo(const QString& jid) {
job = getVCardJob(jid);
if (job != nullptr) {
if (job->getType() == Job::Type::cardInternal)
if (job->type == Job::Type::cardInternal)
replaceJob(new InfoForUser(job->id, jid));
} else
scheduleJob(new InfoForUser(getNextJobId(), jid));
}
void Core::DelayManager::getOwnInfo() {
void Core::DelayManager::Manager::getOwnInfo() {
if (ownInfoJobId == 0) {
if (ownVCardJobId != 0)
replaceJob(new OwnInfoForUser(ownVCardJobId));
@ -73,18 +83,18 @@ void Core::DelayManager::getOwnInfo() {
}
}
void Core::DelayManager::getVCard(const QString& jid) {
void Core::DelayManager::Manager::getVCard(const QString& jid) {
Job* job = getVCardJob(jid);
if (job == nullptr)
scheduleJob(new CardInternal(getNextJobId(), jid));
}
void Core::DelayManager::getOwnVCard() {
void Core::DelayManager::Manager::getOwnVCard() {
if (ownInfoJobId == 0)
scheduleJob(new OwnCardInternal(getNextJobId()));
}
Core::Job* Core::DelayManager::getVCardJob(const QString& jid) {
Core::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) {
Job* job = nullptr;
std::map<QString, Job::Id>::const_iterator sitr = scheduledVCards.find(jid);
if (sitr == scheduledVCards.end()) {
@ -97,16 +107,16 @@ Core::Job* Core::DelayManager::getVCardJob(const QString& jid) {
return job;
}
void Core::DelayManager::preScheduleJob(Core::Job* job) {
switch (job->getType()) {
void Core::DelayManager::Manager::preScheduleJob(Core::Job* job) {
switch (job->type) {
case Job::Type::cardInternal:
scheduledVCards.emplace(static_cast<CardInternal*>(job)->jid, job->id);
scheduledVCards.emplace(dynamic_cast<CardInternal*>(job)->jid, job->id);
break;
case Job::Type::ownCardInternal:
ownVCardJobId = job->id;
break;
case Job::Type::infoForUser:
scheduledVCards.emplace(static_cast<InfoForUser*>(job)->jid, job->id);
scheduledVCards.emplace(dynamic_cast<InfoForUser*>(job)->jid, job->id);
break;
case Job::Type::ownInfoForUser:
ownVCardJobId = job->id;
@ -115,7 +125,7 @@ void Core::DelayManager::preScheduleJob(Core::Job* job) {
}
}
void Core::DelayManager::scheduleJob(Core::Job* job) {
void Core::DelayManager::Manager::scheduleJob(Core::Job* job) {
preScheduleJob(job);
if (runningJobs.size() < maxParallelJobs) {
executeJob(job);
@ -124,11 +134,11 @@ void Core::DelayManager::scheduleJob(Core::Job* job) {
}
}
void Core::DelayManager::preExecuteJob(Core::Job* job) {
switch (job->getType()) {
void Core::DelayManager::Manager::preExecuteJob(Core::Job* job) {
switch (job->type) {
case Job::Type::cardInternal:
case Job::Type::infoForUser: {
CardInternal* cij = static_cast<CardInternal*>(job);
Contact* cij = dynamic_cast<Contact*>(job);
requestedVCards.emplace(cij->jid, job->id);
scheduledVCards.erase(cij->jid);
}
@ -139,13 +149,13 @@ void Core::DelayManager::preExecuteJob(Core::Job* job) {
}
}
void Core::DelayManager::executeJob(Core::Job* job) {
void Core::DelayManager::Manager::executeJob(Core::Job* job) {
preExecuteJob(job);
runningJobs.emplace(job->id, job);
switch (job->getType()) {
switch (job->type) {
case Job::Type::cardInternal:
case Job::Type::infoForUser:
emit requestVCard(static_cast<CardInternal*>(job)->jid);
emit requestVCard(dynamic_cast<Contact*>(job)->jid);
break;
case Job::Type::ownInfoForUser:
case Job::Type::ownCardInternal:
@ -154,7 +164,7 @@ void Core::DelayManager::executeJob(Core::Job* job) {
}
}
void Core::DelayManager::jobIsDone(Job::Id jobId) {
void Core::DelayManager::Manager::jobIsDone(Job::Id jobId) {
std::map<Job::Id, Job*>::const_iterator itr = runningJobs.find(jobId);
if (itr == runningJobs.end()) {
throw 8573; //not supposed to happen, ever
@ -169,7 +179,7 @@ void Core::DelayManager::jobIsDone(Job::Id jobId) {
}
}
void Core::DelayManager::replaceJob(Core::Job* job) {
void Core::DelayManager::Manager::replaceJob(Core::Job* job) {
preScheduleJob(job);
std::map<Job::Id, Job*>::iterator itr = runningJobs.find(job->id);
if (itr != runningJobs.end()) {
@ -187,7 +197,63 @@ void Core::DelayManager::replaceJob(Core::Job* job) {
}
}
void Core::DelayManager::receivedVCard(const QString& jid, const Shared::VCard& card) {
void Core::DelayManager::Manager::jobIsCanceled(Core::Job* job, bool wasRunning) {
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);
if (jb->getStage() == InfoForUser::Stage::waitingForVCard) {
if (wasRunning)
requestedVCards.erase(jb->jid);
else
scheduledVCards.erase(jb->jid);
emit receivedVCard(jb->jid, Shared::VCard());
}
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 (""));
}
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) {
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;
@ -195,7 +261,7 @@ void Core::DelayManager::receivedVCard(const QString& jid, const Shared::VCard&
Job::Id jobId = cardItr->second;
requestedVCards.erase(cardItr);
Job* job = runningJobs.at(jobId);
switch (job->getType()) {
switch (job->type) {
case Job::Type::cardInternal:
jobIsDone(jobId);
emit receivedCard(jid, card);
@ -221,11 +287,11 @@ void Core::DelayManager::receivedVCard(const QString& jid, const Shared::VCard&
}
}
void Core::DelayManager::ownVCardReceived(const Shared::VCard& card) {
void Core::DelayManager::Manager::ownVCardReceived(const Shared::VCard& card) {
Job::Id jobId = ownVCardJobId;
ownVCardJobId = 0;
Job* job = runningJobs.at(jobId);
switch (job->getType()) {
switch (job->type) {
case Job::Type::ownCardInternal:
jobIsDone(jobId);
emit receivedOwnCard(card);
@ -250,6 +316,6 @@ void Core::DelayManager::ownVCardReceived(const Shared::VCard& card) {
}
}
void Core::DelayManager::receivedBundles(const QString& jid) {
void Core::DelayManager::Manager::receivedBundles(const QString& jid) {
}

View File

@ -14,8 +14,8 @@
// 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
#ifndef CORE_DELAYMANAGER_MANAGER_H
#define CORE_DELAYMANAGER_MANAGER_H
#include <list>
#include <set>
@ -34,13 +34,14 @@
#include "job.h"
namespace Core {
namespace DelayManager {
class DelayManager : public QObject
class Manager : public QObject
{
Q_OBJECT
public:
DelayManager(Job::Id maxParallelJobs = 5, QObject* parent = nullptr);
~DelayManager();
Manager(Job::Id maxParallelJobs = 5, QObject* parent = nullptr);
~Manager();
void getOwnVCard();
void getOwnInfo();
@ -58,6 +59,7 @@ signals:
void receivedOwnInfo(const Shared::Info& info);
public slots:
void disconnected();
void ownVCardReceived(const Shared::VCard& card);
void receivedVCard(const QString& jid, const Shared::VCard& card);
void receivedBundles(const QString& jid);
@ -67,6 +69,7 @@ private:
void scheduleJob(Job* job);
void preExecuteJob(Job* job);
void executeJob(Job* job);
void jobIsCanceled(Job* job, bool wasRunning);
void jobIsDone(Job::Id jobId);
Job::Id getNextJobId();
void replaceJob(Job* job);
@ -108,9 +111,12 @@ private:
Job::Id ownInfoJobId;
std::map<QString, Job::Id> scheduledVCards;
std::map<QString, Job::Id> requestedVCards;
#ifdef WITH_OMEMO
std::map<QString, Job::Id> requestedBundles;
#endif
};
}
}
#endif // CORE_DELAYMANAGER_H
#endif // CORE_DELAYMANAGER_MANAGER_H

View File

@ -0,0 +1,34 @@
// 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 "info.h"
Core::DelayManager::Info::Info(Id p_id, Type p_type) :
Job(p_id, p_type),
stage(Stage::waitingForVCard)
{}
Core::DelayManager::Info::Info(const Info& other) :
Job(other),
stage(other.stage)
{}
Core::DelayManager::Info::~Info() {
}
Core::DelayManager::Info::Stage Core::DelayManager::Info::getStage() const {
return stage;
}

48
core/delayManager/info.h Normal file
View File

@ -0,0 +1,48 @@
// 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_INFO_H
#define CORE_DELAYMANAGER_INFO_H
#include "job.h"
namespace Core {
namespace DelayManager {
class Info : public virtual Job {
public:
enum class Stage {
waitingForVCard,
waitingForBundles
};
protected:
Info(Id id, Type type);
Info(const Info& other);
public:
~Info();
Stage getStage() const;
private:
Stage stage;
};
}
}
#endif // CORE_DELAYMANAGER_INFO_H

View File

@ -16,11 +16,15 @@
#include "infoforuser.h"
Core::InfoForUser::InfoForUser(Job::Id p_id, const QString& p_jid) :
CardInternal(p_id, p_jid, Type::infoForUser)
Core::DelayManager::InfoForUser::InfoForUser(Id p_id, const QString& p_jid) :
Job(p_id, Type::infoForUser),
Contact(p_id, p_jid, Type::infoForUser),
Info(p_id, Type::infoForUser)
{}
Core::InfoForUser::InfoForUser(const Core::InfoForUser& other) :
CardInternal(other)
Core::DelayManager::InfoForUser::InfoForUser(const InfoForUser& other) :
Job(other),
Contact(other),
Info(other)
{}

View File

@ -14,19 +14,22 @@
// 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_INFOFORUSER_H
#define CORE_INFOFORUSER_H
#ifndef CORE_DELAYMANAGER_INFOFORUSER_H
#define CORE_DELAYMANAGER_INFOFORUSER_H
#include "cardinternal.h"
#include "contact.h"
#include "info.h"
namespace Core {
namespace DelayManager {
class InfoForUser : public CardInternal {
class InfoForUser : public Contact, public Info {
public:
InfoForUser(Job::Id id, const QString& jid);
InfoForUser(Id id, const QString& jid);
InfoForUser(const InfoForUser& other);
};
}
}
#endif // CORE_INFOFORUSER_H
#endif // CORE_DELAYMANAGER_INFOFORUSER_H

View File

@ -16,17 +16,13 @@
#include "job.h"
Core::Job::Job(Core::Job::Id p_id, Core::Job::Type p_type) :
Core::DelayManager::Job::Job(Id p_id, Type p_type) :
id (p_id),
type (p_type) {}
Core::Job::Job(const Core::Job& other) :
Core::DelayManager::Job::Job(const Job& other) :
id(other.id),
type(other.type) {}
Core::Job::~Job() {}
Core::Job::Type Core::Job::getType() const {
return type;
}
Core::DelayManager::Job::~Job() {}

View File

@ -20,6 +20,7 @@
#include <stdint.h>
namespace Core {
namespace DelayManager {
class Job {
public:
@ -31,20 +32,18 @@ public:
infoForUser,
ownInfoForUser
};
protected:
Job(Id id, Type type);
Job(const Job& other);
public:
virtual ~Job();
const Id id;
public:
Type getType() const;
protected:
Type type;
const Type type;
};
}
}
#endif // CORE_DELAYMANAGER_JOB_H

View File

@ -16,15 +16,15 @@
#include "owncardinternal.h"
Core::OwnCardInternal::OwnCardInternal(Job::Id p_id) :
Core::DelayManager::OwnCardInternal::OwnCardInternal(Id p_id) :
Job(p_id, Type::ownCardInternal)
{}
Core::OwnCardInternal::OwnCardInternal(Job::Id p_id, Job::Type p_type) :
Core::DelayManager::OwnCardInternal::OwnCardInternal(Id p_id, Type p_type) :
Job(p_id, p_type)
{}
Core::OwnCardInternal::OwnCardInternal(const Core::OwnCardInternal& other) :
Core::DelayManager::OwnCardInternal::OwnCardInternal(const OwnCardInternal& other) :
Job(other)
{}

View File

@ -14,22 +14,24 @@
// 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_OWNCARDINTERNAL_H
#define CORE_OWNCARDINTERNAL_H
#ifndef CORE_DELAYMANAGER_OWNCARDINTERNAL_H
#define CORE_DELAYMANAGER_OWNCARDINTERNAL_H
#include "job.h"
namespace Core {
namespace DelayManager {
class OwnCardInternal : public Job {
protected:
OwnCardInternal(Job::Id id, Job::Type type);
OwnCardInternal(Id id, Type type);
public:
OwnCardInternal(Job::Id id);
OwnCardInternal(Id id);
OwnCardInternal(const OwnCardInternal& other);
};
}
}
#endif // CORE_OWNCARDINTERNAL_H
#endif // CORE_DELAYMANAGER_OWNCARDINTERNAL_H

View File

@ -16,10 +16,12 @@
#include "owninfoforuser.h"
Core::OwnInfoForUser::OwnInfoForUser(Job::Id p_id) :
OwnCardInternal(p_id, Type::ownInfoForUser)
Core::DelayManager::OwnInfoForUser::OwnInfoForUser(Id p_id) :
Job(p_id, Type::ownInfoForUser),
Info(p_id, Type::ownInfoForUser)
{}
Core::OwnInfoForUser::OwnInfoForUser(const Core::OwnInfoForUser& other) :
OwnCardInternal(other)
Core::DelayManager::OwnInfoForUser::OwnInfoForUser(const OwnInfoForUser& other) :
Job(other),
Info(other)
{}

View File

@ -14,19 +14,22 @@
// 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_OWNINFOFORUSER_H
#define CORE_OWNINFOFORUSER_H
#ifndef CORE_DELAYMANAGER_OWNINFOFORUSER_H
#define CORE_DELAYMANAGER_OWNINFOFORUSER_H
#include "owncardinternal.h"
#include "contact.h"
#include "info.h"
namespace Core {
namespace DelayManager {
class OwnInfoForUser : public OwnCardInternal {
class OwnInfoForUser : public Info {
public:
OwnInfoForUser(Job::Id id);
OwnInfoForUser(Id id);
OwnInfoForUser(const OwnInfoForUser& other);
};
}
}
#endif // CORE_OWNINFOFORUSER_H
#endif // CORE_DELAYMANAGER_OWNINFOFORUSER_H