Compare commits

...

3 Commits

4 changed files with 58 additions and 37 deletions

View File

@ -2,16 +2,6 @@ cmake_minimum_required(VERSION 3.14...3.24 FATAL_ERROR)
include(FetchContent)
# TODO: move entt dep into solanaceae_contact
if (NOT TARGET EnTT::EnTT)
FetchContent_Declare(EnTT
GIT_REPOSITORY https://github.com/skypjack/entt.git
GIT_TAG v3.12.2
EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(EnTT)
endif()
if (NOT TARGET solanaceae_util)
FetchContent_Declare(solanaceae_util
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_util.git

View File

@ -44,6 +44,16 @@
#include <signal.h>
#endif
// why is min not variadic?
template<typename... T>
float min_var(float v0, T... args) {
if constexpr (sizeof...(args) == 0) {
return v0;
} else {
return std::min(v0, min_var(args...));
}
}
std::atomic_bool quit = false;
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
@ -172,7 +182,7 @@ int main(int argc, char** argv) {
Contact3Registry cr;
RegistryMessageModel rmm{cr};
MessageTimeSort mts{rmm};
MessageCleanser mc{cr, rmm};
MessageCleanser mc{cr, rmm, conf};
MessageCommandDispatcher mcd{cr, rmm, conf};
{ // setup basic commands for bot
@ -310,19 +320,15 @@ int main(int argc, char** argv) {
mts.iterate();
const float pm_interval = pm.tick(time_delta_tick);
mc.iterate(time_delta_tick);
const float mc_interval = mc.iterate(time_delta_tick);
const float mcd_interval = mcd.iterate(time_delta_tick);
const float tox_interval = std::pow(tc.toxIterationInterval(), 1.6f) / 1000.f;
last_min_interval = std::min<float>(
tox_interval,
pm_interval
);
last_min_interval = std::min<float>(
last_min_interval,
mcd_interval
last_min_interval = min_var(
pm_interval,
mc_interval,
mcd_interval,
tox_interval
);
// dont sleep and do an extra check

View File

@ -2,19 +2,26 @@
#include <solanaceae/contact/components.hpp>
#include <solanaceae/message3/components.hpp>
#include <solanaceae/util/utils.hpp>
#include <iostream>
#include <chrono>
#include <cstdint>
MessageCleanser::MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) {
// config
// has MessageCleanser::old_age
MessageCleanser::MessageCleanser(
Contact3Registry& cr,
RegistryMessageModel& rmm,
ConfigModelI& conf
) : _cr(cr), _rmm(rmm), _conf(conf) {
if (!_conf.has_int("MessageCleanser", "old_age_minutes")) {
_conf.set("MessageCleanser", "old_age_minutes", int64_t(_old_age_default));
}
}
MessageCleanser::~MessageCleanser(void) {
}
void MessageCleanser::iterate(float time_delta) {
float MessageCleanser::iterate(float time_delta) {
_timer += time_delta;
if (_timer >= _interval) {
_timer = 0.f;
@ -23,17 +30,27 @@ void MessageCleanser::iterate(float time_delta) {
uint64_t now_ts = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
uint64_t deleted_count{0};
// TODO: iterate rmm directly
//_rmm.get();
// workaround by iterating contacts
for (const auto& c : _cr.view<Contact::Components::TagBig>()) {
if (auto* reg = _rmm.get(c); reg != nullptr) {
float old_age {0.f};
{ // old age from conf
// TODO: find some way to extract this (maybe map into contact reg?)
if (const auto* id_comp = _cr.try_get<Contact::Components::ID>(c); id_comp != nullptr) {
const auto id_hex = bin2hex(id_comp->data);
old_age = _conf.get_int("MessageCleanser", "old_age_minutes", id_hex).value_or(_old_age_default);
} else {
old_age = _conf.get_int("MessageCleanser", "old_age_minutes").value_or(_old_age_default);
}
old_age *= 60.f; // to sec
}
std::vector<Message3> to_remove;
// assuming all messages have Timestamp comp
reg->view<Message::Components::Timestamp>().each([this, now_ts, &to_remove](const Message3 ent, const Message::Components::Timestamp& ts) {
if (now_ts >= ts.ts + static_cast<uint64_t>(_old_age*1000.f)) {
reg->view<Message::Components::Timestamp>().each([this, now_ts, old_age, &to_remove](const Message3 ent, const Message::Components::Timestamp& ts) {
if (now_ts >= ts.ts + static_cast<uint64_t>(old_age*1000.f)) {
to_remove.push_back(ent);
}
});
@ -44,12 +61,13 @@ void MessageCleanser::iterate(float time_delta) {
_rmm.throwEventDestroy(*reg, c);
reg->destroy(c);
}
deleted_count += to_remove.size();
if (!to_remove.empty()) {
std::cout << "MC: cleaned up " << to_remove.size() << ", age >= " << old_age << "sec\n";
}
}
}
if (deleted_count > 0) {
std::cout << "MC: cleaned up " << deleted_count << "\n";
}
}
return _interval - _timer;
}

View File

@ -1,18 +1,25 @@
#pragma once
#include <solanaceae/message3/registry_message_model.hpp>
#include <solanaceae/util/config_model.hpp>
class MessageCleanser {
Contact3Registry& _cr;
RegistryMessageModel& _rmm;
ConfigModelI& _conf;
static constexpr int64_t _old_age_default{150}; // minutes
float _old_age{150.f*60.f}; // max 150min
float _interval{3.f*60.f}; // every 3min
float _timer{0.f};
public:
MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm);
MessageCleanser(
Contact3Registry& cr,
RegistryMessageModel& rmm,
ConfigModelI& conf
);
~MessageCleanser(void);
void iterate(float time_delta);
float iterate(float time_delta);
};