WIP: LokidRpcClient

pull/1306/head
Stephen Shelton 4 years ago committed by Jeff Becker
parent 11951510bf
commit 91725a8530
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -276,7 +276,9 @@ if(SUBMODULE_CHECK)
endif()
endif()
add_subdirectory(external/loki-mq EXCLUDE_FROM_ALL)
add_subdirectory(external/loki-mq)
include_directories(external/loki-mq)
include_directories(external/loki-mq/mapbox-variant/include)
# We only actually need pybind11 with WITH_HIVE, but if we don't load it here then something further
# down loads a broken PythonInterp that loads Python2, but Python2 headers are not C++17 compatible.

@ -174,6 +174,7 @@ add_library(liblokinet
routing/path_latency_message.cpp
routing/path_transfer_message.cpp
routing/transfer_traffic_message.cpp
rpc/lokid_rpc_client.cpp
service/address.cpp
service/async_key_exchange.cpp
service/context.cpp

@ -24,6 +24,7 @@
#include <router/rc_lookup_handler.hpp>
#include <routing/handler.hpp>
#include <routing/message_parser.hpp>
#include <rpc/lokid_rpc_client.hpp>
#include <service/context.hpp>
#include <stdexcept>
#include <util/buffer.hpp>
@ -246,7 +247,8 @@ namespace llarp
IpAddress rpcBindAddr = DefaultRPCBindAddr;
const llarp_time_t _randomStartDelay;
/// lokid caller
rpc::LokidRpcClient m_lokidRpcClient;
IpAddress lokidRPCAddr = IpAddress("127.0.0.1:22023");
std::string lokidRPCUser;
std::string lokidRPCPassword;

@ -0,0 +1,113 @@
#include <rpc/lokid_rpc_client.hpp>
#include <util/logging/logger.h>
#include <util/logging/logger.hpp>
#include <future>
namespace llarp
{
namespace rpc
{
static ::LogLevel
fromLokiMQLogLevel(lokimq::LogLevel level)
{
switch (level)
{
case lokimq::LogLevel::fatal:
case lokimq::LogLevel::error:
return eLogError;
case lokimq::LogLevel::warn:
return eLogWarn;
case lokimq::LogLevel::info:
return eLogInfo;
case lokimq::LogLevel::debug:
case lokimq::LogLevel::trace:
return eLogDebug;
default:
return eLogNone;
}
}
static lokimq::LogLevel
toLokiMQLogLevel(::LogLevel level)
{
switch (level)
{
case eLogError:
return lokimq::LogLevel::error;
case eLogWarn:
return lokimq::LogLevel::warn;
case eLogInfo:
return lokimq::LogLevel::info;
case eLogDebug:
return lokimq::LogLevel::debug;
case eLogNone:
default:
return lokimq::LogLevel::trace;
}
}
static void
lokimqLogger(lokimq::LogLevel level, const char* file, std::string msg)
{
switch (level)
{
case lokimq::LogLevel::fatal:
case lokimq::LogLevel::error:
LogError(msg);
break;
case lokimq::LogLevel::warn:
LogWarn(msg);
break;
case lokimq::LogLevel::info:
LogInfo(msg);
break;
case lokimq::LogLevel::debug:
case lokimq::LogLevel::trace:
LogDebug(msg);
break;
}
}
LokidRpcClient::LokidRpcClient(std::string lokidPubkey)
: m_lokiMQ(lokimqLogger, lokimq::LogLevel::debug), m_lokidPubkey(std::move(lokidPubkey))
{
m_lokiMQ.log_level(toLokiMQLogLevel(LogLevel::Instance().curLevel));
}
void
LokidRpcClient::connect()
{
m_lokidConnectionId = m_lokiMQ.connect_sn(m_lokidPubkey); // not a blocking call
}
std::future<void>
LokidRpcClient::ping()
{
throw std::runtime_error("TODO: LokidRpcClient::ping()");
}
std::future<std::string>
LokidRpcClient::requestNextBlockHash()
{
throw std::runtime_error("TODO: LokidRpcClient::requestNextBlockHash()");
}
std::future<std::vector<RouterID>>
LokidRpcClient::requestServiceNodeList()
{
throw std::runtime_error("TODO: LokidRpcClient::requestServiceNodeList()");
}
void
LokidRpcClient::request()
{
// TODO: ensure we are connected
// m_lokiMQ.request(m_lokidConnectionId, ...);
throw std::runtime_error("TODO: LokidRpcClient::request()");
}
} // namespace rpc
} // namespace llarp

@ -0,0 +1,62 @@
#pragma once
#include <router_id.hpp>
#include <lokimq/lokimq.h>
#include <future>
namespace llarp
{
namespace rpc
{
/// The LokidRpcClient uses loki-mq to talk to make API requests to lokid.
struct LokidRpcClient
{
/// Not copyable or movable (because lokimq::LokiMQ is not copyable or movable).
/// Consider wrapping in a std::unique_ptr or std::shared_ptr if you need to pass this around.
LokidRpcClient(const LokidRpcClient&) = delete;
LokidRpcClient&
operator=(const LokidRpcClient&) = delete;
LokidRpcClient(LokidRpcClient&&) = delete;
LokidRpcClient&
operator=(LokidRpcClient&&) = delete;
/// Constructor
/// TODO: take lokid pubkey and other auth parameters
LokidRpcClient(std::string lokidPubkey);
/// Connect to lokid
void
connect();
/// Initiates a ping request to lokid, currently used to let lokid know that lokinet is still
/// running (required to prevent a Service Node from being deregistered).
///
/// This uses the "lokinet_ping" API endpoint.
std::future<void>
ping();
/// Requests the most recent known block hash from lokid
///
/// This uses the "poll_block_hash" API endpoint.
std::future<std::string>
requestNextBlockHash();
/// Requests a full list of known service nodes from lokid
///
/// This uses the "get_n_service_nodes" API endpoint.
std::future<std::vector<RouterID>>
requestServiceNodeList();
private:
std::string m_lokidPubkey;
lokimq::ConnectionID m_lokidConnectionId;
lokimq::LokiMQ m_lokiMQ;
void
request();
};
} // namespace rpc
} // namespace llarp
Loading…
Cancel
Save