2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "path/path.hpp"
|
|
|
|
#include "router_id.hpp"
|
|
|
|
#include "util/bencode.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "util/thread/threading.hpp"
|
|
|
|
|
|
|
|
#include <map>
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2022-10-05 19:35:16 +00:00
|
|
|
namespace oxenc
|
|
|
|
{
|
|
|
|
class bt_dict_consumer;
|
|
|
|
class bt_dict_producer;
|
|
|
|
} // namespace oxenc
|
|
|
|
|
2018-09-13 16:41:53 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-05-24 02:01:36 +00:00
|
|
|
struct RouterProfile
|
2018-09-13 16:41:53 +00:00
|
|
|
{
|
2018-09-14 14:50:37 +00:00
|
|
|
static constexpr size_t MaxSize = 256;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint64_t connectTimeoutCount = 0;
|
|
|
|
uint64_t connectGoodCount = 0;
|
|
|
|
uint64_t pathSuccessCount = 0;
|
|
|
|
uint64_t pathFailCount = 0;
|
2020-10-27 21:34:09 +00:00
|
|
|
uint64_t pathTimeoutCount = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_time_t lastUpdated = 0s;
|
|
|
|
llarp_time_t lastDecay = 0s;
|
2022-05-26 15:59:44 +00:00
|
|
|
uint64_t version = llarp::constants::proto_version;
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2022-10-05 19:35:16 +00:00
|
|
|
RouterProfile() = default;
|
|
|
|
RouterProfile(oxenc::bt_dict_consumer dict);
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2022-10-05 19:35:16 +00:00
|
|
|
void
|
|
|
|
BEncode(oxenc::bt_dict_producer& dict) const;
|
|
|
|
void
|
|
|
|
BEncode(oxenc::bt_dict_producer&& dict) const
|
|
|
|
{
|
|
|
|
BEncode(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BDecode(oxenc::bt_dict_consumer dict);
|
2018-09-13 16:41:53 +00:00
|
|
|
|
|
|
|
bool
|
2018-09-14 14:50:37 +00:00
|
|
|
IsGood(uint64_t chances) const;
|
2019-03-04 17:03:18 +00:00
|
|
|
|
2019-04-16 11:44:55 +00:00
|
|
|
bool
|
|
|
|
IsGoodForConnect(uint64_t chances) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsGoodForPath(uint64_t chances) const;
|
|
|
|
|
2019-03-05 13:38:50 +00:00
|
|
|
/// decay stats
|
2019-03-04 17:03:18 +00:00
|
|
|
void
|
2019-03-05 13:38:50 +00:00
|
|
|
Decay();
|
2019-03-04 17:03:18 +00:00
|
|
|
|
|
|
|
// rotate stats if timeout reached
|
|
|
|
void
|
|
|
|
Tick();
|
2018-09-13 16:41:53 +00:00
|
|
|
};
|
|
|
|
|
2019-05-24 02:01:36 +00:00
|
|
|
struct Profiling
|
2018-09-13 16:41:53 +00:00
|
|
|
{
|
2019-04-25 11:00:18 +00:00
|
|
|
Profiling();
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2021-05-04 21:01:29 +00:00
|
|
|
inline static const int profiling_chances = 4;
|
|
|
|
|
2019-04-16 11:44:55 +00:00
|
|
|
/// generic variant
|
2018-09-13 16:41:53 +00:00
|
|
|
bool
|
2023-10-25 19:43:32 +00:00
|
|
|
IsBad(const RouterID& r, uint64_t chances = profiling_chances);
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2019-05-24 02:01:36 +00:00
|
|
|
/// check if this router should have paths built over it
|
2019-04-16 13:20:48 +00:00
|
|
|
bool
|
2023-10-25 19:43:32 +00:00
|
|
|
IsBadForPath(const RouterID& r, uint64_t chances = profiling_chances);
|
2019-04-16 11:44:55 +00:00
|
|
|
|
|
|
|
/// check if this router should be connected directly to
|
2019-04-16 13:20:48 +00:00
|
|
|
bool
|
2023-10-25 19:43:32 +00:00
|
|
|
IsBadForConnect(const RouterID& r, uint64_t chances = profiling_chances);
|
2019-04-16 11:44:55 +00:00
|
|
|
|
2018-09-13 16:41:53 +00:00
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
MarkConnectTimeout(const RouterID& r);
|
2018-09-13 16:41:53 +00:00
|
|
|
|
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
MarkConnectSuccess(const RouterID& r);
|
2019-03-03 20:51:47 +00:00
|
|
|
|
2020-10-27 21:34:09 +00:00
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
MarkPathTimeout(path::Path* p);
|
2020-10-27 21:34:09 +00:00
|
|
|
|
2019-03-03 20:51:47 +00:00
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
MarkPathFail(path::Path* p);
|
2019-03-03 20:51:47 +00:00
|
|
|
|
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
MarkPathSuccess(path::Path* p);
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2020-01-03 12:52:19 +00:00
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
MarkHopFail(const RouterID& r);
|
2020-01-03 12:52:19 +00:00
|
|
|
|
2019-03-31 15:25:13 +00:00
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
ClearProfile(const RouterID& r);
|
2019-03-31 15:25:13 +00:00
|
|
|
|
2019-03-04 17:03:18 +00:00
|
|
|
void
|
2023-10-25 19:43:32 +00:00
|
|
|
Tick();
|
2019-03-04 17:03:18 +00:00
|
|
|
|
2018-09-13 16:41:53 +00:00
|
|
|
bool
|
2023-10-25 19:43:32 +00:00
|
|
|
Load(const fs::path fname);
|
2018-09-13 16:41:53 +00:00
|
|
|
|
|
|
|
bool
|
2023-10-25 19:43:32 +00:00
|
|
|
Save(const fs::path fname);
|
2018-09-14 14:50:37 +00:00
|
|
|
|
2019-03-25 15:41:37 +00:00
|
|
|
bool
|
|
|
|
ShouldSave(llarp_time_t now) const;
|
|
|
|
|
2019-04-25 11:00:18 +00:00
|
|
|
void
|
|
|
|
Disable();
|
|
|
|
|
|
|
|
void
|
|
|
|
Enable();
|
|
|
|
|
2018-09-13 16:41:53 +00:00
|
|
|
private:
|
2022-10-05 19:35:16 +00:00
|
|
|
void
|
|
|
|
BEncode(oxenc::bt_dict_producer& dict) const;
|
|
|
|
|
|
|
|
void
|
|
|
|
BDecode(oxenc::bt_dict_consumer dict);
|
|
|
|
|
2019-03-03 20:51:47 +00:00
|
|
|
mutable util::Mutex m_ProfilesMutex; // protects m_Profiles
|
2023-10-25 19:43:32 +00:00
|
|
|
std::map<RouterID, RouterProfile> m_Profiles;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t m_LastSave = 0s;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::atomic<bool> m_DisableProfiling;
|
2018-09-13 16:41:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|