mirror of
https://github.com/oxen-io/lokinet-control-panel.git
synced 2023-12-14 05:53:00 +01:00
move rpc to use lokimq
This commit is contained in:
parent
4471698463
commit
a09c1bcccf
|
@ -8,8 +8,11 @@ project(lokinet-gui
|
|||
HOMEPAGE_URL https://github.com/loki-project/loki-network-control-panel
|
||||
LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
|
||||
|
||||
find_package(Qt5 COMPONENTS Qml Quick Widgets Charts REQUIRED)
|
||||
find_package(Threads)
|
||||
find_package(LokiMQ 1.2 REQUIRED)
|
||||
|
||||
add_executable(lokinet-gui
|
||||
src/main.cpp
|
||||
|
@ -27,11 +30,12 @@ add_executable(lokinet-gui
|
|||
target_link_libraries(lokinet-gui PRIVATE
|
||||
Qt5::Core Qt5::Qml Qt5::Quick Qt5::Widgets Qt5::Charts
|
||||
Threads::Threads
|
||||
lokimq
|
||||
)
|
||||
|
||||
set_target_properties(lokinet-gui
|
||||
PROPERTIES
|
||||
CXX_STANDARD 14
|
||||
CXX_STANDARD 17
|
||||
CXX_EXTENSIONS OFF
|
||||
CXX_STANDARD_REQUIRED ON
|
||||
|
||||
|
|
21
cmake/FindLokiMQ.cmake
Normal file
21
cmake/FindLokiMQ.cmake
Normal file
|
@ -0,0 +1,21 @@
|
|||
find_package(PkgConfig)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(PC_LOKIMQ QUIET liblokimq>=${LokiMQ_VERSION})
|
||||
endif()
|
||||
|
||||
find_path(LOKIMQ_INCLUDE_DIR lokimq/lokimq.h
|
||||
HINTS ${PC_LOKIMQ_INCLUDEDIR} ${PC_LOKIMQ_INCLUDE_DIRS})
|
||||
|
||||
find_library(LOKIMQ_LIBRARY NAMES lokimq
|
||||
HINTS ${PC_LOKIMQ_LIBDIR} ${PC_LOKIMQ_LIBRARY_DIRS})
|
||||
|
||||
mark_as_advanced(LOKIMQ_INCLUDE_DIR LOKIMQ_LIBRARY)
|
||||
|
||||
set(LOKIMQ_LIBRARIES ${LOKIMQ_LIBRARY} ${PC_LOKIMQ_LIBRARIES})
|
||||
set(LOKIMQ_INCLUDE_DIRS ${LOKIMQ_INCLUDE_DIR})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(LokiMQ DEFAULT_MSG
|
||||
LOKIMQ_LIBRARY LOKIMQ_INCLUDE_DIR)
|
||||
|
||||
mark_as_advanced(LOKIMQ_INCLUDE_DIR LOKIMQ_LIBRARY)
|
|
@ -3,42 +3,55 @@
|
|||
#include <stdexcept>
|
||||
#include <cstdio>
|
||||
|
||||
bool LokinetApiClient::invoke(const std::string& endpoint, HttpClient::ReplyCallback callback) {
|
||||
|
||||
char buffer[1024];
|
||||
int result = snprintf(
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
R"JSON({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "%s",
|
||||
"params": {},
|
||||
"id": "empty"
|
||||
})JSON",
|
||||
endpoint.c_str());
|
||||
|
||||
if (result < 0) {
|
||||
qDebug() << "snprintf failed: " << result;
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "invoking json rpc payload: " << buffer;
|
||||
|
||||
m_httpClient.postJson("http://localhost:1190", buffer, std::move(callback));
|
||||
bool LokinetApiClient::invoke(const std::string& endpoint, ReplyCallback callback) {
|
||||
if(not m_lmqConnection.has_value())
|
||||
{
|
||||
m_lmqClient.start();
|
||||
m_lmqConnection =
|
||||
m_lmqClient.connect_remote(
|
||||
"tcp://127.0.0.1:1190",
|
||||
[](auto){},
|
||||
[&](auto, std::string_view reason) {
|
||||
// qDebug() << "failed to connect to lokinet: "<< reason;
|
||||
m_lmqConnection = std::nullopt;
|
||||
});
|
||||
}
|
||||
m_lmqClient.request(
|
||||
*m_lmqConnection,
|
||||
std::string_view{endpoint},
|
||||
[cb = std::move(callback)](bool success, std::vector<std::string> data)
|
||||
{
|
||||
if(success and not data.empty())
|
||||
{
|
||||
cb(std::move(data[0]));
|
||||
}
|
||||
else
|
||||
{
|
||||
cb(std::nullopt);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
Q_INVOKABLE bool LokinetApiClient::invoke(const std::string& endpoint, QJSValue callback) {
|
||||
|
||||
if (! callback.isUndefined() && ! callback.isCallable()) {
|
||||
qDebug() << "callback should be a function (if present)";
|
||||
// qDebug() << "callback should be a function (if present)";
|
||||
return false;
|
||||
}
|
||||
|
||||
return invoke(std::move(endpoint), [=](QNetworkReply* reply) mutable {
|
||||
return invoke(endpoint, [=](std::optional<std::string> reply) mutable {
|
||||
QJSValueList args;
|
||||
args << QJSValue(reply->readAll().data());
|
||||
args << QJSValue(reply->error());
|
||||
if(reply.has_value())
|
||||
{
|
||||
args << QJSValue(reply->c_str());
|
||||
args << QJSValue(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
args << QJSValue(false);
|
||||
args << QJSValue("no response given from lokinet");
|
||||
}
|
||||
callback.call(args);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
#include <QObject>
|
||||
#include <QJSValue>
|
||||
|
||||
#include "HttpClient.hpp"
|
||||
#include <lokimq/lokimq.h>
|
||||
#include <optional>
|
||||
|
||||
/**
|
||||
* A client that implements convenience wrappers around making specific
|
||||
|
@ -31,6 +32,8 @@ class LokinetApiClient : public QObject
|
|||
|
||||
public:
|
||||
|
||||
using ReplyCallback = std::function<void(std::optional<std::string>)>;
|
||||
|
||||
/**
|
||||
* Invoke an endpoint.
|
||||
*
|
||||
|
@ -41,7 +44,7 @@ public:
|
|||
* @param callback is a callback that will receive the reply or error
|
||||
* @return true on success, false otherwise
|
||||
*/
|
||||
bool invoke(const std::string& endpoint, HttpClient::ReplyCallback callback);
|
||||
bool invoke(const std::string& endpoint, ReplyCallback callback);
|
||||
Q_INVOKABLE bool invoke(const std::string& endpoint, QJSValue callback);
|
||||
|
||||
/**
|
||||
|
@ -64,13 +67,14 @@ public:
|
|||
return invoke("llarp.admin.status", callback);
|
||||
}
|
||||
|
||||
bool llarpAdminDie(HttpClient::ReplyCallback callback) {
|
||||
bool llarpAdminDie(ReplyCallback callback) {
|
||||
return invoke("llarp.admin.die", callback);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
HttpClient m_httpClient;
|
||||
lokimq::LokiMQ m_lmqClient;
|
||||
std::optional<lokimq::ConnectionID> m_lmqConnection;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -81,8 +81,7 @@ bool LokinetProcessManager::stopLokinetProcess()
|
|||
|
||||
bool LokinetProcessManager::doStopLokinetProcess()
|
||||
{
|
||||
bool success = m_apiClient.llarpAdminDie([](QNetworkReply* reply) {
|
||||
qDebug() << "llarp.admin.die response: " << reply->readAll();
|
||||
bool success = m_apiClient.llarpAdminDie([](auto) {
|
||||
});
|
||||
if (!success)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue