From fd9d14d00c1ca053d16c44a434f2cb447891bfd2 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 13 Nov 2023 16:23:49 +0100 Subject: [PATCH] tox private impl + dht caps histo --- src/CMakeLists.txt | 3 ++ src/main_screen.cpp | 5 ++- src/main_screen.hpp | 2 ++ src/tox_dht_cap_histo.cpp | 69 +++++++++++++++++++++++++++++++++++++++ src/tox_dht_cap_histo.hpp | 22 +++++++++++++ src/tox_private_impl.hpp | 2 ++ src/tox_ui_utils.cpp | 2 ++ 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/tox_dht_cap_histo.cpp create mode 100644 src/tox_dht_cap_histo.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e23b3dfd..8c2af2c3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,6 +58,9 @@ add_executable(tomato ./tox_ui_utils.hpp ./tox_ui_utils.cpp + ./tox_dht_cap_histo.hpp + ./tox_dht_cap_histo.cpp + ./chat_gui4.hpp ./chat_gui4.cpp ) diff --git a/src/main_screen.cpp b/src/main_screen.cpp index 0e9f185d..e57231df 100644 --- a/src/main_screen.cpp +++ b/src/main_screen.cpp @@ -21,7 +21,8 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::stri sdlrtu(renderer_), cg(conf, rmm, cr, sdlrtu), sw(conf), - tuiu(tc, conf) + tuiu(tc, conf), + tdch(tpi) { tel.subscribeAll(tc); @@ -109,12 +110,14 @@ Screen* MainScreen::poll(bool& quit) { } pm.tick(time_delta); + tdch.tick(time_delta); mts.iterate(); cg.render(); sw.render(); tuiu.render(); + tdch.render(); if constexpr (false) { ImGui::ShowDemoWindow(); diff --git a/src/main_screen.hpp b/src/main_screen.hpp index e4e8ab2e..83276974 100644 --- a/src/main_screen.hpp +++ b/src/main_screen.hpp @@ -24,6 +24,7 @@ #include "./chat_gui4.hpp" #include "./settings_window.hpp" #include "./tox_ui_utils.hpp" +#include "./tox_dht_cap_histo.hpp" #include #include @@ -63,6 +64,7 @@ struct MainScreen final : public Screen { ChatGui4 cg; SettingsWindow sw; ToxUIUtils tuiu; + ToxDHTCapHisto tdch; MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector plugins); ~MainScreen(void); diff --git a/src/tox_dht_cap_histo.cpp b/src/tox_dht_cap_histo.cpp new file mode 100644 index 00000000..e9cfc25f --- /dev/null +++ b/src/tox_dht_cap_histo.cpp @@ -0,0 +1,69 @@ +#include "./tox_dht_cap_histo.hpp" + +#include + +void ToxDHTCapHisto::tick(float time_delta) { + if (!_enabled) { + return; + } + + _time_since_last_add += time_delta; + if (_time_since_last_add >= _value_add_interval) { + _time_since_last_add = 0.f; // very loose + + const auto total = _tpi.toxDHTGetNumCloselist(); + const auto with_cap = _tpi.toxDHTGetNumCloselistAnnounceCapable(); + + if (total == 0 || with_cap == 0) { + _ratios.push_back(0.f); + } else { + _ratios.push_back(float(with_cap) / float(total)); + } + + // TODO: limit + while (_ratios.size() > 5*60) { + _ratios.erase(_ratios.begin()); + } + } +} + +void ToxDHTCapHisto::render(void) { + { // main window menubar injection + // assumes the window "tomato" was rendered already by cg + if (ImGui::Begin("tomato")) { + if (ImGui::BeginMenuBar()) { + ImGui::Separator(); + if (ImGui::BeginMenu("Tox")) { + ImGui::SeparatorText("DHT diagnostics"); + + ImGui::Checkbox("enabled", &_enabled); + + if (ImGui::MenuItem("show DHT announce capability histogram")) { + _show_window = true; + } + + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + + } + ImGui::End(); + } + + + if (_show_window) { + if (ImGui::Begin("Tox DHT announce capability histogram", &_show_window)) { + if (_enabled) { + ImGui::PlotHistogram("##histogram", _ratios.data(), _ratios.size(), 0, nullptr, 0.f, 1.f, {-1, -1}); + } else { + ImGui::TextUnformatted("logging disabled!"); + if (ImGui::Button("enable")) { + _enabled = true; + } + } + } + ImGui::End(); + } +} + diff --git a/src/tox_dht_cap_histo.hpp b/src/tox_dht_cap_histo.hpp new file mode 100644 index 00000000..f025cd41 --- /dev/null +++ b/src/tox_dht_cap_histo.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include + +#include + +class ToxDHTCapHisto { + ToxPrivateI& _tpi; + + bool _enabled {true}; + bool _show_window {false}; + + std::vector _ratios; + const float _value_add_interval {1.f}; // every second + float _time_since_last_add {0.f}; + + public: + ToxDHTCapHisto(ToxPrivateI& tpi) : _tpi(tpi) {} + + void tick(float time_delta); + void render(void); +}; diff --git a/src/tox_private_impl.hpp b/src/tox_private_impl.hpp index 85f83a41..fe2605d9 100644 --- a/src/tox_private_impl.hpp +++ b/src/tox_private_impl.hpp @@ -1,3 +1,5 @@ +#pragma once + #include #include diff --git a/src/tox_ui_utils.cpp b/src/tox_ui_utils.cpp index ca3e5bc0..7773ac54 100644 --- a/src/tox_ui_utils.cpp +++ b/src/tox_ui_utils.cpp @@ -23,6 +23,8 @@ void ToxUIUtils::render(void) { if (ImGui::BeginMenuBar()) { ImGui::Separator(); if (ImGui::BeginMenu("Tox")) { + ImGui::SeparatorText("Friends/Groups"); + if (ImGui::MenuItem("add Friend by ID")) { _show_add_friend_window = true; }