#include "./message_cleanser.hpp" #include #include #include #include MessageCleanser::MessageCleanser(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) { // config // has MessageCleanser::old_age } 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::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()) { if (auto* reg = _rmm.get(c); reg != nullptr) { std::vector to_remove; // assuming all messages have Timestamp comp reg->view().each([this, now_ts, &to_remove](const Message3 ent, const Message::Components::Timestamp& ts) { if (now_ts >= ts.ts + static_cast(_old_age*1000.f)) { to_remove.push_back(ent); } }); //reg->destroy(to_remove.cbegin(), to_remove.cend()); // we need to notify for every destruction, and give every listener a last chance for (const auto c : to_remove) { _rmm.throwEventDestroy(*reg, c); reg->destroy(c); } deleted_count += to_remove.size(); } } if (deleted_count > 0) { std::cout << "MC: cleaned up " << deleted_count << "\n"; } } }