86 lines
2.4 KiB
C++
86 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <vector>
|
|
#include <thread>
|
|
#include <atomic>
|
|
|
|
#include "node.h"
|
|
#include "plugin_loader.h"
|
|
#include "utils/simple_json.h"
|
|
|
|
namespace rk3588 {
|
|
|
|
class Graph {
|
|
public:
|
|
explicit Graph(std::string name);
|
|
~Graph();
|
|
|
|
bool Build(const SimpleJson& graph_cfg, PluginLoader& loader, size_t default_queue_size,
|
|
QueueDropStrategy default_strategy, std::string& err);
|
|
bool Start();
|
|
void Stop();
|
|
|
|
const std::string& Name() const { return name_; }
|
|
const SimpleJson& GraphConfig() const { return graph_cfg_; }
|
|
|
|
// Attempt in-place update via INode::UpdateConfig for nodes whose config changed.
|
|
// Returns true if fully updated without rebuild.
|
|
// Returns false with empty err if rebuild is required.
|
|
// Returns false with non-empty err if update failed.
|
|
bool TryUpdateInPlace(const SimpleJson& new_graph_cfg, size_t default_queue_size,
|
|
QueueDropStrategy default_strategy, std::string& err);
|
|
|
|
private:
|
|
struct NodeEntry {
|
|
std::string id;
|
|
std::string type;
|
|
std::string role;
|
|
bool enabled = true;
|
|
SimpleJson config;
|
|
NodeContext context;
|
|
std::unique_ptr<INode> node;
|
|
std::thread worker;
|
|
};
|
|
|
|
std::string name_;
|
|
SimpleJson graph_cfg_;
|
|
size_t built_default_queue_size_ = 8;
|
|
QueueDropStrategy built_default_strategy_ = QueueDropStrategy::DropOldest;
|
|
std::vector<NodeEntry> nodes_;
|
|
std::atomic<bool> running_{false};
|
|
std::atomic<bool> stop_requested_{false};
|
|
};
|
|
|
|
class GraphManager {
|
|
public:
|
|
explicit GraphManager(std::string plugin_dir = "plugins");
|
|
~GraphManager();
|
|
|
|
static bool LoadConfigFile(const std::string& path, SimpleJson& out, std::string& err);
|
|
|
|
bool Build(const SimpleJson& root_cfg, std::string& err);
|
|
bool StartAll();
|
|
void StopAll();
|
|
void RequestStop();
|
|
void BlockUntilStop();
|
|
|
|
bool ReloadFromFile(const std::string& path, std::string& err);
|
|
|
|
private:
|
|
bool running_ = false;
|
|
PluginLoader loader_;
|
|
std::vector<std::unique_ptr<Graph>> graphs_;
|
|
SimpleJson last_good_root_;
|
|
size_t default_queue_size_ = 8;
|
|
QueueDropStrategy default_strategy_ = QueueDropStrategy::DropOldest;
|
|
std::mutex graphs_mu_;
|
|
std::mutex mu_;
|
|
std::condition_variable cv_;
|
|
};
|
|
|
|
} // namespace rk3588
|