basic message cleaner, not configurable yet

This commit is contained in:
Green Sky 2023-12-02 02:55:26 +01:00
parent e632676b33
commit 6ca3474c3e
No known key found for this signature in database
4 changed files with 76 additions and 0 deletions

View File

@ -2,11 +2,15 @@ cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
add_executable(totato
./main.cpp
./tox_client.hpp
./tox_client.cpp
./tox_private_impl.hpp
./auto_dirty.hpp
./auto_dirty.cpp
./message_cleanser.hpp
./message_cleanser.cpp
)
target_compile_features(totato PUBLIC cxx_std_17)

View File

@ -12,6 +12,7 @@
#include "./tox_client.hpp"
#include "./auto_dirty.hpp"
#include "./message_cleanser.hpp"
#include <nlohmann/json.hpp>
@ -156,6 +157,7 @@ int main(int argc, char** argv) {
Contact3Registry cr;
RegistryMessageModel rmm{cr};
MessageTimeSort mts{rmm};
MessageCleanser mc{cr, rmm};
PluginManager pm;
@ -173,6 +175,8 @@ int main(int argc, char** argv) {
tc.setSelfName(name); // TODO: this is ugly
}
std::cout << "TOTATO: own address: " << tc.toxSelfGetAddressStr() << "\n";
ToxPrivateImpl tpi{tc.getTox()};
AutoDirty ad{tc};
ToxContactModel2 tcm{cr, tc, tc};
@ -229,6 +233,8 @@ int main(int argc, char** argv) {
pm.tick(/*time_delta*/0.02f);
mc.iterate(0.02f);
//std::this_thread::sleep_for( // time left to get to 60fps
//std::chrono::duration<float, std::chrono::seconds::period>(0.0166f) // 60fps frame duration
//- std::chrono::duration<float, std::chrono::seconds::period>(std::chrono::steady_clock::now() - new_time) // time used for rendering

48
src/message_cleanser.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "./message_cleanser.hpp"
#include <solanaceae/contact/components.hpp>
#include <solanaceae/message3/components.hpp>
#include <iostream>
#include <chrono>
MessageCleanser::MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) {
}
MessageCleanser::~MessageCleanser(void) {
}
void MessageCleanser::iterate(float time_delta) {
_timer += time_delta;
if (_timer >= _interval) {
_timer = 0.f;
//std::cout << "MC: cleaning up old messages...\n";
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) {
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)) {
to_remove.push_back(ent);
}
});
reg->destroy(to_remove.cbegin(), to_remove.cend());
deleted_count += to_remove.size();
}
}
if (deleted_count > 0) {
std::cout << "MC: cleaned up " << deleted_count << "\n";
}
}
}

18
src/message_cleanser.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <solanaceae/message3/registry_message_model.hpp>
class MessageCleanser {
Contact3Registry& _cr;
RegistryMessageModel& _rmm;
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(void);
void iterate(float time_delta);
};