lokinet/llarp/dht/tx.hpp

80 lines
1.8 KiB
C++
Raw Normal View History

2019-01-22 01:14:02 +00:00
#ifndef LLARP_DHT_TX
#define LLARP_DHT_TX
#include "key.hpp"
#include "txowner.hpp"
#include <llarp/util/logging.hpp>
#include <llarp/util/status.hpp>
2019-01-22 01:14:02 +00:00
#include <set>
#include <vector>
namespace llarp
2019-01-22 01:14:02 +00:00
{
struct Router;
2019-01-22 01:14:02 +00:00
namespace dht
{
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
TX(const TXOwner& asker, const K& k, Router* r) : target(k), router{r}, whoasked(asker)
{}
2019-01-22 01:14:02 +00:00
virtual ~TX() = default;
2019-01-22 23:50:26 +00:00
void
OnFound(const Key_t& askedPeer, const V& value);
2019-02-11 17:14:43 +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
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
virtual bool
Validate(const V& value) const = 0;
2019-01-22 01:14:02 +00:00
virtual void
Start(const TXOwner& peer) = 0;
};
2019-01-22 01:14:02 +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
{
peersAsked.insert(askedPeer);
if (Validate(value))
{
valuesFound.push_back(value);
}
2019-01-22 01:14:02 +00:00
}
} // namespace dht
} // namespace llarp
2019-01-22 01:14:02 +00:00
#endif