2019-01-22 01:14:02 +00:00
|
|
|
#ifndef LLARP_DHT_TX
|
|
|
|
#define LLARP_DHT_TX
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "key.hpp"
|
|
|
|
#include "txowner.hpp"
|
2023-10-19 11:49:46 +00:00
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/status.hpp>
|
2019-01-22 01:14:02 +00:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
namespace llarp
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
2023-10-19 11:49:46 +00:00
|
|
|
struct Router;
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
namespace dht
|
2023-10-12 13:43:07 +00:00
|
|
|
{
|
2023-10-19 11:49:46 +00:00
|
|
|
template <typename K, typename V>
|
|
|
|
struct TX
|
|
|
|
{
|
|
|
|
K target;
|
|
|
|
Router* router;
|
|
|
|
std::set<Key_t> peersAsked;
|
|
|
|
std::vector<V> valuesFound;
|
|
|
|
TXOwner whoasked;
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
TX(const TXOwner& asker, const K& k, Router* r) : target(k), router{r}, whoasked(asker)
|
|
|
|
{}
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
virtual ~TX() = default;
|
2019-01-22 23:50:26 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
void
|
|
|
|
OnFound(const Key_t& askedPeer, const V& value);
|
2019-02-11 17:14:43 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const
|
|
|
|
{
|
|
|
|
util::StatusObject obj{
|
|
|
|
{"whoasked", whoasked.ExtractStatus()}, {"target", target.ExtractStatus()}};
|
|
|
|
std::vector<util::StatusObject> foundObjs;
|
|
|
|
std::transform(
|
|
|
|
valuesFound.begin(),
|
|
|
|
valuesFound.end(),
|
|
|
|
std::back_inserter(foundObjs),
|
|
|
|
[](const auto& item) -> util::StatusObject { return item.ExtractStatus(); });
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
obj["found"] = foundObjs;
|
|
|
|
std::vector<std::string> asked;
|
|
|
|
std::transform(
|
|
|
|
peersAsked.begin(),
|
|
|
|
peersAsked.end(),
|
|
|
|
std::back_inserter(asked),
|
|
|
|
[](const auto& item) -> std::string { return item.ToString(); });
|
|
|
|
obj["asked"] = asked;
|
|
|
|
return obj;
|
|
|
|
}
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
virtual bool
|
|
|
|
Validate(const V& value) const = 0;
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
virtual void
|
|
|
|
Start(const TXOwner& peer) = 0;
|
|
|
|
};
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
template <typename K, typename V>
|
|
|
|
inline void
|
|
|
|
TX<K, V>::OnFound(const Key_t& askedPeer, const V& value)
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
2023-10-19 11:49:46 +00:00
|
|
|
peersAsked.insert(askedPeer);
|
|
|
|
if (Validate(value))
|
|
|
|
{
|
|
|
|
valuesFound.push_back(value);
|
|
|
|
}
|
2019-01-22 01:14:02 +00:00
|
|
|
}
|
2023-10-19 11:49:46 +00:00
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|
2019-01-22 01:14:02 +00:00
|
|
|
|
|
|
|
#endif
|