lokinet/llarp/rpc/lokid_rpc_client.hpp

80 lines
2.0 KiB
C++
Raw Normal View History

2020-05-18 16:06:52 +00:00
#pragma once
#include <llarp/router_id.hpp>
2020-05-18 16:06:52 +00:00
#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <llarp/crypto/types.hpp>
#include <llarp/dht/key.hpp>
#include <llarp/service/name.hpp>
2020-05-18 16:06:52 +00:00
namespace llarp
{
2020-05-19 18:53:03 +00:00
struct AbstractRouter;
2020-05-18 16:06:52 +00:00
namespace rpc
{
2021-02-03 18:12:21 +00:00
using LMQ_ptr = std::shared_ptr<oxenmq::OxenMQ>;
2020-05-19 18:53:03 +00:00
2020-05-18 16:06:52 +00:00
/// The LokidRpcClient uses loki-mq to talk to make API requests to lokid.
2020-05-19 18:53:03 +00:00
struct LokidRpcClient : public std::enable_shared_from_this<LokidRpcClient>
2020-05-21 14:09:45 +00:00
{
2020-05-18 16:06:52 +00:00
explicit LokidRpcClient(LMQ_ptr lmq, AbstractRouter* r);
2020-05-18 16:06:52 +00:00
2020-05-19 18:53:03 +00:00
/// Connect to lokid async
2020-05-18 16:06:52 +00:00
void
2021-02-03 18:12:21 +00:00
ConnectAsync(oxenmq::address url);
2020-05-18 16:06:52 +00:00
2020-05-20 11:41:42 +00:00
/// blocking request identity key from lokid
/// throws on failure
SecretKey
2020-05-20 11:41:42 +00:00
ObtainIdentityKey();
void
LookupLNSNameHash(
dht::Key_t namehash,
std::function<void(std::optional<service::EncryptedName>)> resultHandler);
2020-05-18 16:06:52 +00:00
private:
2020-05-19 18:53:03 +00:00
/// called when we have connected to lokid via lokimq
void
Connected();
/// do a lmq command on the current connection
void
Command(std::string_view cmd);
2020-05-20 11:41:42 +00:00
void
UpdateServiceNodeList();
2020-05-19 18:53:03 +00:00
template <typename HandlerFunc_t, typename Args_t>
void
Request(std::string_view cmd, HandlerFunc_t func, const Args_t& args)
{
m_lokiMQ->request(*m_Connection, std::move(cmd), std::move(func), args);
}
2020-05-20 11:41:42 +00:00
template <typename HandlerFunc_t>
void
Request(std::string_view cmd, HandlerFunc_t func)
{
m_lokiMQ->request(*m_Connection, std::move(cmd), std::move(func));
}
2020-05-19 18:53:03 +00:00
void
HandleGotServiceNodeList(std::string json);
// Handles request from lokid for peer stats on a specific peer
void
2021-02-03 18:12:21 +00:00
HandleGetPeerStats(oxenmq::Message& msg);
2021-02-03 18:12:21 +00:00
std::optional<oxenmq::ConnectionID> m_Connection;
2020-05-19 18:53:03 +00:00
LMQ_ptr m_lokiMQ;
std::string m_CurrentBlockHash;
AbstractRouter* const m_Router;
2020-05-18 16:06:52 +00:00
};
} // namespace rpc
} // namespace llarp