From a09c1bcccf14f77de8783370b3da7bf8b46d124d Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 13 Aug 2020 13:48:27 -0400 Subject: [PATCH] move rpc to use lokimq --- CMakeLists.txt | 6 ++- cmake/FindLokiMQ.cmake | 21 +++++++++ src/LokinetApiClient.cpp | 65 ++++++++++++++++----------- src/LokinetApiClient.hpp | 12 +++-- src/process/LokinetProcessManager.cpp | 3 +- 5 files changed, 74 insertions(+), 33 deletions(-) create mode 100644 cmake/FindLokiMQ.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c53a99c..153c606 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/cmake/FindLokiMQ.cmake b/cmake/FindLokiMQ.cmake new file mode 100644 index 0000000..a191d47 --- /dev/null +++ b/cmake/FindLokiMQ.cmake @@ -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) diff --git a/src/LokinetApiClient.cpp b/src/LokinetApiClient.cpp index e708e6c..5a60e06 100644 --- a/src/LokinetApiClient.cpp +++ b/src/LokinetApiClient.cpp @@ -3,42 +3,55 @@ #include #include -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 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 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); }); } diff --git a/src/LokinetApiClient.hpp b/src/LokinetApiClient.hpp index fffb4c0..164a296 100644 --- a/src/LokinetApiClient.hpp +++ b/src/LokinetApiClient.hpp @@ -4,7 +4,8 @@ #include #include -#include "HttpClient.hpp" +#include +#include /** * A client that implements convenience wrappers around making specific @@ -31,6 +32,8 @@ class LokinetApiClient : public QObject public: + using ReplyCallback = std::function)>; + /** * 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 m_lmqConnection; }; diff --git a/src/process/LokinetProcessManager.cpp b/src/process/LokinetProcessManager.cpp index 4590661..eb03b21 100644 --- a/src/process/LokinetProcessManager.cpp +++ b/src/process/LokinetProcessManager.cpp @@ -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) {