context bind
This commit is contained in:
@@ -1,8 +1,17 @@
|
||||
#include "api/ipc_dbus/CommandDispatcher.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QSaveFile>
|
||||
|
||||
#include "device_bus/DeviceBus.h"
|
||||
#include "message_bus/pipeline/rules/DynamicRuleRegistry.h"
|
||||
#include "message_bus/pipeline/discovery/DiscoveryPool.h"
|
||||
#include "message_bus/pipeline/mapping/MappingRegistry.h"
|
||||
#include "utils/logs/logging.h"
|
||||
|
||||
namespace softbus::api::ipc
|
||||
{
|
||||
@@ -10,6 +19,16 @@ namespace softbus::api::ipc
|
||||
namespace
|
||||
{
|
||||
|
||||
QString rulesConfigPath()
|
||||
{
|
||||
const QString daemonConfigPath = softbus::device_bus::DeviceBus::instance().configPath();
|
||||
if (!daemonConfigPath.isEmpty()) {
|
||||
const QFileInfo fi(daemonConfigPath);
|
||||
return fi.dir().filePath(QStringLiteral("runtime_rules.json"));
|
||||
}
|
||||
return QDir::homePath() + QStringLiteral("/softbus/config/runtime_rules.json");
|
||||
}
|
||||
|
||||
softbus::message_bus::pipeline::RuleScope parseScope(const QJsonObject& params)
|
||||
{
|
||||
softbus::message_bus::pipeline::RuleScope scope;
|
||||
@@ -19,6 +38,89 @@ softbus::message_bus::pipeline::RuleScope parseScope(const QJsonObject& params)
|
||||
return scope;
|
||||
}
|
||||
|
||||
QJsonObject scopeToJson(const softbus::message_bus::pipeline::RuleScope& scope)
|
||||
{
|
||||
QJsonObject out;
|
||||
if (scope.endpointHash != 0) {
|
||||
out.insert(QStringLiteral("endpointHash"), static_cast<qint64>(scope.endpointHash));
|
||||
}
|
||||
if (scope.deviceId >= 0) {
|
||||
out.insert(QStringLiteral("deviceId"), scope.deviceId);
|
||||
}
|
||||
if (!scope.stableKey.isEmpty()) {
|
||||
out.insert(QStringLiteral("stableKey"), scope.stableKey);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
QJsonObject filterRuleToJson(const softbus::message_bus::pipeline::FilterRule& rule)
|
||||
{
|
||||
QJsonObject out;
|
||||
out.insert(QStringLiteral("id"), rule.id);
|
||||
out.insert(QStringLiteral("scope"), scopeToJson(rule.scope));
|
||||
out.insert(QStringLiteral("condition"), rule.condition);
|
||||
return out;
|
||||
}
|
||||
|
||||
QJsonObject transformRuleToJson(const softbus::message_bus::pipeline::TransformRule& rule)
|
||||
{
|
||||
QJsonObject out;
|
||||
out.insert(QStringLiteral("id"), rule.id);
|
||||
out.insert(QStringLiteral("scope"), scopeToJson(rule.scope));
|
||||
out.insert(QStringLiteral("scale"), rule.scale);
|
||||
out.insert(QStringLiteral("offset"), rule.offset);
|
||||
if (!rule.unit.isEmpty()) {
|
||||
out.insert(QStringLiteral("unit"), rule.unit);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool persistRulesToDedicatedConfig(const softbus::message_bus::pipeline::DynamicRuleRegistry& reg)
|
||||
{
|
||||
const QString configPath = rulesConfigPath();
|
||||
if (configPath.isEmpty()) {
|
||||
LOG_ERROR() << "CommandDispatcher: rules config path is empty, cannot persist rules";
|
||||
return false;
|
||||
}
|
||||
|
||||
const QFileInfo fi(configPath);
|
||||
QJsonArray filtersJson;
|
||||
for (const auto& rule : reg.snapshotAllFilterRules()) {
|
||||
filtersJson.push_back(filterRuleToJson(rule));
|
||||
}
|
||||
QJsonArray transformsJson;
|
||||
for (const auto& rule : reg.snapshotAllTransformRules()) {
|
||||
transformsJson.push_back(transformRuleToJson(rule));
|
||||
}
|
||||
|
||||
QJsonObject root;
|
||||
root.insert(QStringLiteral("filter"), filtersJson);
|
||||
root.insert(QStringLiteral("transform"), transformsJson);
|
||||
|
||||
if (!QDir().mkpath(fi.dir().absolutePath())) {
|
||||
LOG_ERROR() << "CommandDispatcher: failed to create rules config directory:" << fi.dir().absolutePath();
|
||||
return false;
|
||||
}
|
||||
|
||||
const QByteArray bytes = QJsonDocument(root).toJson(QJsonDocument::Indented);
|
||||
QSaveFile out(configPath);
|
||||
if (!out.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
LOG_ERROR() << "CommandDispatcher: failed to open rules config for write:" << configPath;
|
||||
return false;
|
||||
}
|
||||
if (out.write(bytes) != bytes.size()) {
|
||||
LOG_ERROR() << "CommandDispatcher: failed to write rules config:" << configPath;
|
||||
out.cancelWriting();
|
||||
return false;
|
||||
}
|
||||
if (!out.commit()) {
|
||||
LOG_ERROR() << "CommandDispatcher: failed to commit rules config:" << configPath;
|
||||
return false;
|
||||
}
|
||||
LOG_INFO() << "CommandDispatcher: rules persisted to" << configPath;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DispatchResult CommandDispatcher::dispatch(const QString& action, const QJsonObject& params) const
|
||||
@@ -34,11 +136,17 @@ DispatchResult CommandDispatcher::dispatch(const QString& action, const QJsonObj
|
||||
if (!reg.addFilterRule(rule)) {
|
||||
return {4001, QStringLiteral("invalid_filter_rule"), {}};
|
||||
}
|
||||
if (!persistRulesToDedicatedConfig(reg)) {
|
||||
return {5001, QStringLiteral("persist_rules_failed"), {}};
|
||||
}
|
||||
return {0, QStringLiteral("ok"), QJsonObject{{QStringLiteral("id"), rule.id}}};
|
||||
}
|
||||
if (action == QStringLiteral("remove_filter")) {
|
||||
const QString id = params.value(QStringLiteral("id")).toString();
|
||||
const bool ok = reg.removeFilterRule(id);
|
||||
if (ok && !persistRulesToDedicatedConfig(reg)) {
|
||||
return {5001, QStringLiteral("persist_rules_failed"), {}};
|
||||
}
|
||||
return {ok ? 0 : 4041, ok ? QStringLiteral("ok") : QStringLiteral("filter_not_found"),
|
||||
QJsonObject{{QStringLiteral("id"), id}}};
|
||||
}
|
||||
@@ -52,11 +160,17 @@ DispatchResult CommandDispatcher::dispatch(const QString& action, const QJsonObj
|
||||
if (!reg.addTransformRule(rule)) {
|
||||
return {4002, QStringLiteral("invalid_transform_rule"), {}};
|
||||
}
|
||||
if (!persistRulesToDedicatedConfig(reg)) {
|
||||
return {5001, QStringLiteral("persist_rules_failed"), {}};
|
||||
}
|
||||
return {0, QStringLiteral("ok"), QJsonObject{{QStringLiteral("id"), rule.id}}};
|
||||
}
|
||||
if (action == QStringLiteral("remove_transform_rule")) {
|
||||
const QString id = params.value(QStringLiteral("id")).toString();
|
||||
const bool ok = reg.removeTransformRule(id);
|
||||
if (ok && !persistRulesToDedicatedConfig(reg)) {
|
||||
return {5001, QStringLiteral("persist_rules_failed"), {}};
|
||||
}
|
||||
return {ok ? 0 : 4042, ok ? QStringLiteral("ok") : QStringLiteral("transform_rule_not_found"),
|
||||
QJsonObject{{QStringLiteral("id"), id}}};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user