Skip to content

Commit

Permalink
tox private impl + dht caps histo
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Nov 13, 2023
1 parent 4e4f62d commit fd9d14d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
5 changes: 4 additions & 1 deletion src/main_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/main_screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "./chat_gui4.hpp"
#include "./settings_window.hpp"
#include "./tox_ui_utils.hpp"
#include "./tox_dht_cap_histo.hpp"

#include <string>
#include <iostream>
Expand Down Expand Up @@ -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<std::string> plugins);
~MainScreen(void);
Expand Down
69 changes: 69 additions & 0 deletions src/tox_dht_cap_histo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "./tox_dht_cap_histo.hpp"

#include <imgui/imgui.h>

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();
}
}

22 changes: 22 additions & 0 deletions src/tox_dht_cap_histo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <solanaceae/toxcore/tox_private_interface.hpp>

#include <vector>

class ToxDHTCapHisto {
ToxPrivateI& _tpi;

bool _enabled {true};
bool _show_window {false};

std::vector<float> _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);
};
2 changes: 2 additions & 0 deletions src/tox_private_impl.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <tox/tox_private.h>
#include <solanaceae/toxcore/tox_private_interface.hpp>

Expand Down
2 changes: 2 additions & 0 deletions src/tox_ui_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit fd9d14d

Please sign in to comment.