oeAware-manager/0005-modify-C-interface-name-add-enable-count-and-fix-bug.patch
fly_1997 8c2082720c fix data free, plugin display, command plugin bugs
(cherry picked from commit 2ca264904491e249e48381aa6f19969427484d0a)
2024-12-06 15:53:22 +08:00

234 lines
8.2 KiB
Diff

From 1a84134fbe63c1a7bea679b5696bafd99a1b6666 Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Sat, 30 Nov 2024 15:25:03 +0800
Subject: [PATCH 5/5] modify C interface name, add enable count and fix bugs
---
src/client/cmd_handler.cpp | 7 ++++---
src/client/main.cpp | 4 ++--
src/plugin_mgr/config.cpp | 5 +++++
src/plugin_mgr/config.h | 1 +
src/plugin_mgr/event/enable_handler.cpp | 3 +++
src/plugin_mgr/instance_run_handler.cpp | 3 ++-
src/plugin_mgr/plugin.cpp | 2 +-
src/plugin_mgr/plugin.h | 1 +
src/sdk/oe_client.cpp | 14 +++++++++-----
src/sdk/oe_client.h | 10 +++++-----
10 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/src/client/cmd_handler.cpp b/src/client/cmd_handler.cpp
index b1e2ba0..289461b 100644
--- a/src/client/cmd_handler.cpp
+++ b/src/client/cmd_handler.cpp
@@ -33,7 +33,7 @@ void LoadHandler::Handler(Message &msg)
void LoadHandler::ResHandler(Message &msg)
{
if (msg.opt == Opt::RESPONSE_OK) {
- std::cout << "Plugin loaded successfully.";
+ std::cout << "Plugin loaded successfully.\n";
} else {
std::cout << "Plugin loaded failed, because "<< msg.payload[0] << ".\n";
}
@@ -54,9 +54,10 @@ void QueryHandler::PrintFormat()
{
std::cout << "format:\n"
"[plugin]\n"
- "\t[instance]([dependency status], [running status])\n"
+ "\t[instance]([dependency status], [running status], [enable cnt])\n"
"dependency status: available means satisfying dependency, otherwise unavailable.\n"
- "running status: running means that instance is running, otherwise close.\n";
+ "running status: running means that instance is running, otherwise close.\n"
+ "enable cnt: number of instances enabled.\n";
}
void QueryHandler::ResHandler(Message &msg)
diff --git a/src/client/main.cpp b/src/client/main.cpp
index 188d5e2..1c50f1a 100644
--- a/src/client/main.cpp
+++ b/src/client/main.cpp
@@ -41,8 +41,8 @@ int main(int argc, char *argv[])
std::string analysis = argv[1];
if (analysis == "analysis") {
CTopic topic = {"analysis_aware", "analysis_aware", ""};
- Init();
- Subscribe(&topic, AnalysisCallback);
+ OeInit();
+ OeSubscribe(&topic, AnalysisCallback);
std::unique_lock<std::mutex> lock(g_mutex);
g_cv.wait(lock, []{ return g_finish; });
return 0;
diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp
index 8cd1432..aee3243 100644
--- a/src/plugin_mgr/config.cpp
+++ b/src/plugin_mgr/config.cpp
@@ -61,6 +61,10 @@ void Config::SetEnableList(const YAML::Node &node)
std::string pluginName = enableList[i]["name"].as<std::string>();
YAML::Node instances = enableList[i]["instances"];
EnableItem enableItem(pluginName);
+ if (!instances.IsSequence()) {
+ WARN(logger, "the format of the enable list is incorrect.");
+ continue;
+ }
if (!instances.IsDefined() || instances.IsNull()) {
enableItem.SetEnabled(true);
} else {
@@ -75,6 +79,7 @@ void Config::SetEnableList(const YAML::Node &node)
bool Config::Load(const std::string &path)
{
+ logger = Logger::GetInstance().Get("Main");
YAML::Node node;
struct stat buffer;
if (stat(path.c_str(), &buffer) != 0) {
diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h
index 19e6d33..dc02b37 100644
--- a/src/plugin_mgr/config.h
+++ b/src/plugin_mgr/config.h
@@ -127,6 +127,7 @@ private:
std::string logType;
std::unordered_map<std::string, PluginInfo> pluginList;
std::vector<EnableItem> enableList;
+ log4cplus::Logger logger;
};
std::string GetPath();
diff --git a/src/plugin_mgr/event/enable_handler.cpp b/src/plugin_mgr/event/enable_handler.cpp
index 66eba6c..a5d8e1a 100644
--- a/src/plugin_mgr/event/enable_handler.cpp
+++ b/src/plugin_mgr/event/enable_handler.cpp
@@ -29,6 +29,9 @@ ErrorCode EnableHandler::InstanceEnabled(const std::string &name)
instanceRunHandler->RecvQueuePush(msg);
/* Wait for InstanceRunHandler to finsh this task. */
msg->Wait();
+ if (msg->result.code < 0) {
+ return ErrorCode::ENABLE_INSTANCE_ENV;
+ }
return ErrorCode::OK;
}
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
index abee581..aa428ff 100644
--- a/src/plugin_mgr/instance_run_handler.cpp
+++ b/src/plugin_mgr/instance_run_handler.cpp
@@ -26,6 +26,7 @@ Result InstanceRunHandler::EnableInstance(const std::string &name)
return result;
}
instance->enabled = true;
+ instance->enableCnt++;
if (instance->interface->GetType() & SCENARIO) {
scheduleQueue.push(ScheduleInstance{instance, time + instance->interface->GetPeriod()});
} else if (instance->interface->GetType() & TUNE) {
@@ -196,7 +197,7 @@ bool InstanceRunHandler::HandleMessage()
DEBUG(logger, "handle message " << (int)msg->GetType());
switch (msg->GetType()) {
case RunType::ENABLED: {
- EnableInstance(msg->payload[0]);
+ msg->result = EnableInstance(msg->payload[0]);
break;
}
case RunType::DISABLED: {
diff --git a/src/plugin_mgr/plugin.cpp b/src/plugin_mgr/plugin.cpp
index 5789737..8b894b6 100644
--- a/src/plugin_mgr/plugin.cpp
+++ b/src/plugin_mgr/plugin.cpp
@@ -72,6 +72,6 @@ std::string Instance::GetInfo() const
{
std::string stateText = this->state ? pluginStateOn : pluginStateOff;
std::string runText = this->enabled ? pluginEnabled : pluginDisabled;
- return name + "(" + stateText + ", " + runText + ")";
+ return name + "(" + stateText + ", " + runText + ", count: " + std::to_string(enableCnt) + ")";
}
}
diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h
index ef5fc1d..bf63852 100644
--- a/src/plugin_mgr/plugin.h
+++ b/src/plugin_mgr/plugin.h
@@ -23,6 +23,7 @@ struct Instance {
std::string pluginName;
bool state = true;
bool enabled;
+ uint64_t enableCnt = 0;
std::shared_ptr<Interface> interface;
std::unordered_map<std::string, Topic> supportTopics;
const static std::string pluginEnabled;
diff --git a/src/sdk/oe_client.cpp b/src/sdk/oe_client.cpp
index 040433a..a9f82ef 100644
--- a/src/sdk/oe_client.cpp
+++ b/src/sdk/oe_client.cpp
@@ -22,6 +22,7 @@
namespace oeaware {
class Impl {
public:
+ Impl() noexcept : domainSocket(nullptr), socketStream(nullptr) { }
int Init();
int Subscribe(const CTopic &topic, Callback callback);
int Unsubscribe(const CTopic &topic);
@@ -114,6 +115,9 @@ int Impl::Init()
int Impl::HandleRequest(const Opt &opt, const std::vector<std::string> &payload)
{
MessageProtocol protocol(MessageHeader(MessageType::REQUEST), Message(opt, payload));
+ if (socketStream == nullptr) {
+ return -1;
+ }
SendMessage(*socketStream, protocol);
Result result;
if (!resultQueue->WaitTimeAndPop(result)) {
@@ -166,28 +170,28 @@ void Impl::Close()
static oeaware::Impl impl;
-int Init()
+int OeInit()
{
oeaware::Register::GetInstance().InitRegisterData();
return impl.Init();
}
-int Subscribe(const CTopic *topic, Callback callback)
+int OeSubscribe(const CTopic *topic, Callback callback)
{
return impl.Subscribe(*topic, callback);
}
-int Unsubscribe(const CTopic *topic)
+int OeUnsubscribe(const CTopic *topic)
{
return impl.Unsubscribe(*topic);
}
-int Publish(const DataList *dataList)
+int OePublish(const DataList *dataList)
{
return impl.Publish(*dataList);
}
-void Close()
+void OeClose()
{
impl.Close();
}
diff --git a/src/sdk/oe_client.h b/src/sdk/oe_client.h
index 17d8e68..267462c 100644
--- a/src/sdk/oe_client.h
+++ b/src/sdk/oe_client.h
@@ -16,11 +16,11 @@
extern "C" {
#endif
typedef int(*Callback)(const DataList *);
-int Init();
-int Subscribe(const CTopic *topic, Callback callback);
-int Unsubscribe(const CTopic *topic);
-int Publish(const DataList *dataList);
-void Close();
+int OeInit();
+int OeSubscribe(const CTopic *topic, Callback callback);
+int OeUnsubscribe(const CTopic *topic);
+int OePublish(const DataList *dataList);
+void OeClose();
#ifdef __cplusplus
}
#endif
--
2.33.0