From da647931ebba61d7117fe2cc370f88e3c51262e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Kubern=C3=A1t?= Date: Wed, 30 Oct 2024 13:33:22 +0100 Subject: [PATCH] libshvbroker: Implement .app:ping I'm getting sick of having SHV3 clients ping all the time, and it working even though all the pings result in an error. --- libshvbroker/CMakeLists.txt | 1 + libshvbroker/src/appnode.cpp | 25 +++++++++++++++++++++++++ libshvbroker/src/appnode.h | 20 ++++++++++++++++++++ libshvbroker/src/brokerapp.cpp | 2 ++ 4 files changed, 48 insertions(+) create mode 100644 libshvbroker/src/appnode.cpp create mode 100644 libshvbroker/src/appnode.h diff --git a/libshvbroker/CMakeLists.txt b/libshvbroker/CMakeLists.txt index eb730b04a..555c484d8 100644 --- a/libshvbroker/CMakeLists.txt +++ b/libshvbroker/CMakeLists.txt @@ -2,6 +2,7 @@ qt_add_library(libshvbroker src/aclmanager.cpp src/aclmanagersqlite.cpp src/appclioptions.cpp + src/appnode.cpp src/brokeraclnode.cpp src/brokerapp.cpp src/brokerappnode.cpp diff --git a/libshvbroker/src/appnode.cpp b/libshvbroker/src/appnode.cpp new file mode 100644 index 000000000..52b657819 --- /dev/null +++ b/libshvbroker/src/appnode.cpp @@ -0,0 +1,25 @@ +#include "appnode.h" + +#include + +namespace shv::broker { +AppNode::AppNode(shv::iotqt::node::ShvNode *parent) + : Super("", &m_metaMethods, parent) + , m_metaMethods { + shv::chainpack::methods::DIR, + shv::chainpack::methods::LS, + {shv::chainpack::Rpc::METH_PING, shv::chainpack::MetaMethod::Flag::None, "Null", "Null", shv::chainpack::AccessLevel::Browse, {}, "https://silicon-heaven.github.io/shv-doc/rpcmethods/app.html#appping"}, + } +{ +} + +shv::chainpack::RpcValue AppNode::callMethod(const StringViewList& shv_path, const std::string& method, const shv::chainpack::RpcValue& params, const chainpack::RpcValue& user_id) +{ + if (shv_path.empty()) { + if (method == shv::chainpack::Rpc::METH_PING) { + return chainpack::RpcValue(nullptr); + } + } + return Super::callMethod(shv_path, method, params, user_id); +} +} diff --git a/libshvbroker/src/appnode.h b/libshvbroker/src/appnode.h new file mode 100644 index 000000000..39720f72a --- /dev/null +++ b/libshvbroker/src/appnode.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace shv::broker { + +class AppNode : public shv::iotqt::node::MethodsTableNode +{ + Q_OBJECT + + using Super = shv::iotqt::node::MethodsTableNode; +public: + AppNode(shv::iotqt::node::ShvNode *parent = nullptr); + + shv::chainpack::RpcValue callMethod(const StringViewList &shv_path, const std::string &method, const shv::chainpack::RpcValue ¶ms, const shv::chainpack::RpcValue &user_id) override; +private: + std::vector m_metaMethods; +}; +} diff --git a/libshvbroker/src/brokerapp.cpp b/libshvbroker/src/brokerapp.cpp index b449daef8..2fb15f26e 100644 --- a/libshvbroker/src/brokerapp.cpp +++ b/libshvbroker/src/brokerapp.cpp @@ -1,4 +1,5 @@ #include "aclmanagersqlite.h" +#include "appnode.h" #include "brokeraclnode.h" #include "brokerappnode.h" #include "brokerrootnode.h" @@ -295,6 +296,7 @@ BrokerApp::BrokerApp(int &argc, char **argv, AppCliOptions *cli_opts) connect(m_nodesTree->root(), &shv::iotqt::node::ShvRootNode::sendRpcMessage, this, &BrokerApp::onRootNodeSendRpcMesage); auto *bn = new BrokerAppNode(); m_nodesTree->mount(cp::Rpc::DIR_BROKER_APP, bn); + m_nodesTree->mount(cp::Rpc::DIR_APP, new AppNode()); m_nodesTree->mount(BROKER_CURRENT_CLIENT_SHV_PATH, new CurrentClientShvNode()); m_nodesTree->mount(std::string(cp::Rpc::DIR_BROKER) + "/clients", new ClientsNode()); m_nodesTree->mount(std::string(cp::Rpc::DIR_BROKER) + "/masters", new MasterBrokersNode());