manual rerun bootstrap option

This commit is contained in:
Green Sky 2024-06-07 10:58:42 +02:00
parent 85b5c9200a
commit a2001b34ea
No known key found for this signature in database
4 changed files with 58 additions and 45 deletions

View File

@ -80,8 +80,39 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password)
// no callbacks, use events
tox_events_init(_tox);
// dht bootstrap
{
runBootstrap();
}
ToxClient::~ToxClient(void) {
if (_tox_profile_dirty) {
saveToxProfile();
}
tox_kill(_tox);
}
bool ToxClient::iterate(float time_delta) {
Tox_Err_Events_Iterate err_e_it = TOX_ERR_EVENTS_ITERATE_OK;
auto* events = tox_events_iterate(_tox, false, &err_e_it);
if (err_e_it == TOX_ERR_EVENTS_ITERATE_OK && events != nullptr) {
_subscriber_raw(events);
// forward events to event handlers
dispatchEvents(events);
}
tox_events_free(events);
_save_heat -= time_delta;
if (_tox_profile_dirty && _save_heat <= 0.f) {
saveToxProfile();
}
return true;
}
void ToxClient::runBootstrap(void) {
// TODO: extend and read from json?
// TODO: seperate out relays
struct DHT_node {
const char *ip;
uint16_t port;
@ -117,34 +148,6 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password)
tox_add_tcp_relay(_tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL);
}
}
}
ToxClient::~ToxClient(void) {
if (_tox_profile_dirty) {
saveToxProfile();
}
tox_kill(_tox);
}
bool ToxClient::iterate(float time_delta) {
Tox_Err_Events_Iterate err_e_it = TOX_ERR_EVENTS_ITERATE_OK;
auto* events = tox_events_iterate(_tox, false, &err_e_it);
if (err_e_it == TOX_ERR_EVENTS_ITERATE_OK && events != nullptr) {
_subscriber_raw(events);
// forward events to event handlers
dispatchEvents(events);
}
tox_events_free(events);
_save_heat -= time_delta;
if (_tox_profile_dirty && _save_heat <= 0.f) {
saveToxProfile();
}
return true;
}
void ToxClient::subscribeRaw(std::function<void(const Tox_Events*)> fn) {
_subscriber_raw = fn;

View File

@ -41,6 +41,8 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase {
void setToxProfilePath(const std::string& new_path) { _tox_profile_path = new_path; }
void setSelfName(std::string_view new_name) { _self_name = new_name; toxSelfSetName(new_name); }
void runBootstrap(void);
public: // raw events
void subscribeRaw(std::function<void(const Tox_Events*)> fn);

View File

@ -1,8 +1,9 @@
#include "./tox_ui_utils.hpp"
#include "./tox_client.hpp"
#include <tox/tox.h>
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/util/utils.hpp>
#include <solanaceae/util/config_model.hpp>
@ -11,9 +12,9 @@
#include <imgui/misc/cpp/imgui_stdlib.h>
ToxUIUtils::ToxUIUtils(
ToxI& t,
ToxClient& tc,
ConfigModelI& conf
) : _t(t), _conf(conf) {
) : _tc(tc), _conf(conf) {
}
void ToxUIUtils::render(void) {
@ -31,6 +32,13 @@ void ToxUIUtils::render(void) {
if (ImGui::MenuItem("join Group by ID (ngc)")) {
_show_add_group_window = true;
}
ImGui::SeparatorText("DHT");
if (ImGui::MenuItem("rerun bootstrap")) {
_tc.runBootstrap();
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
@ -51,7 +59,7 @@ void ToxUIUtils::render(void) {
static Tox_Err_Friend_Add err = Tox_Err_Friend_Add::TOX_ERR_FRIEND_ADD_OK;
if (ImGui::Button("add")) {
// TODO: add string_view variant to utils
auto [_, err_r] = _t.toxFriendAdd(hex2bin(std::string{tox_id}), message);
auto [_, err_r] = _tc.toxFriendAdd(hex2bin(std::string{tox_id}), message);
err = err_r;
}
if (err != Tox_Err_Friend_Add::TOX_ERR_FRIEND_ADD_OK) {
@ -78,7 +86,7 @@ void ToxUIUtils::render(void) {
static Tox_Err_Group_Join err = Tox_Err_Group_Join::TOX_ERR_GROUP_JOIN_OK;
if (ImGui::Button("join")) {
auto [_, err_r] = _t.toxGroupJoin(
auto [_, err_r] = _tc.toxGroupJoin(
hex2bin(std::string{chat_id}), // TODO: add string_view variant to utils
self_name,
password

View File

@ -1,18 +1,18 @@
#pragma once
struct ToxI;
class ToxClient;
struct ConfigModelI;
class ToxUIUtils {
bool _show_add_friend_window {false};
bool _show_add_group_window {false};
ToxI& _t;
ToxClient& _tc;
ConfigModelI& _conf;
public:
ToxUIUtils(
ToxI& t,
ToxClient& tc,
ConfigModelI& conf
);