|
@@ -3,6 +3,8 @@
|
|
|
#include "core/sophiar_manager.h"
|
|
#include "core/sophiar_manager.h"
|
|
|
#include "core/tristate_obj.h"
|
|
#include "core/tristate_obj.h"
|
|
|
#include "core/basic_obj_types.hpp"
|
|
#include "core/basic_obj_types.hpp"
|
|
|
|
|
+#include "utility/coro_signal_group.hpp"
|
|
|
|
|
+#include "utility/coro_worker.hpp"
|
|
|
#include "utility/debug_utility.hpp"
|
|
#include "utility/debug_utility.hpp"
|
|
|
#include "utility/global_obj_helper.hpp"
|
|
#include "utility/global_obj_helper.hpp"
|
|
|
|
|
|
|
@@ -28,100 +30,135 @@ using namespace std::chrono_literals;
|
|
|
struct source_node_type : public tristate_obj {
|
|
struct source_node_type : public tristate_obj {
|
|
|
DEFAULT_NEW_INSTANCE(source_node_type)
|
|
DEFAULT_NEW_INSTANCE(source_node_type)
|
|
|
|
|
|
|
|
- global_obj_auto_sync_delegate<double_obj> out_obj;
|
|
|
|
|
|
|
+ global_obj_index_type output_obj_index;
|
|
|
|
|
+ coro_worker::pointer source_worker;
|
|
|
|
|
|
|
|
boost::asio::awaitable<bool> on_init(const nlohmann::json &config) override {
|
|
boost::asio::awaitable<bool> on_init(const nlohmann::json &config) override {
|
|
|
- init_value = config["output_obj_name"].get<int64_t>();
|
|
|
|
|
- co_return true; // TODO
|
|
|
|
|
|
|
+ output_obj_index = get_manager().
|
|
|
|
|
+ register_global_obj<u64int_obj>(
|
|
|
|
|
+ config["output_obj_name"].get<std::string>());
|
|
|
|
|
+ co_return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
boost::asio::awaitable<bool> on_start(const nlohmann::json &config) override {
|
|
boost::asio::awaitable<bool> on_start(const nlohmann::json &config) override {
|
|
|
- assert(config.contains("start_value"));
|
|
|
|
|
- assert(config["start_value"].is_number());
|
|
|
|
|
- start_value = config["start_value"].get<int64_t>();
|
|
|
|
|
- co_spawn(get_context(), [=]() -> awaitable<void> {
|
|
|
|
|
- co_await coro_sleep(50ms);
|
|
|
|
|
- this->output_signal.emit(init_value * start_value);
|
|
|
|
|
- }, detached);
|
|
|
|
|
|
|
+ source_worker = make_interval_coro_worker(
|
|
|
|
|
+ get_context(), std::chrono::seconds(1), [
|
|
|
|
|
+ output_obj = global_obj_auto_sync_delegate<u64int_obj>(
|
|
|
|
|
+ get_manager(), output_obj_index),
|
|
|
|
|
+ start_value = config["start_value"].get<std::uint64_t>()]() mutable
|
|
|
|
|
+ -> boost::asio::awaitable<bool> {
|
|
|
|
|
+ auto new_out = u64int_obj::new_instance(start_value);
|
|
|
|
|
+ output_obj.set_value(new_out);
|
|
|
|
|
+ SPDLOG_WARN("New value from source {}", new_out->value);
|
|
|
|
|
+ ++start_value;
|
|
|
|
|
+ co_return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ source_worker->run();
|
|
|
co_return true;
|
|
co_return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- using output_signal_type = tiny_signal<int64_t>;
|
|
|
|
|
- output_signal_type output_signal;
|
|
|
|
|
- int64_t init_value = -1, start_value = -1;
|
|
|
|
|
|
|
+ boost::asio::awaitable<void> on_stop() override {
|
|
|
|
|
+ source_worker->cancel();
|
|
|
|
|
+ co_await source_worker->coro_wait_stop();
|
|
|
|
|
+ source_worker.reset();
|
|
|
|
|
+ co_return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct proxy_node_type : public tristate_obj {
|
|
struct proxy_node_type : public tristate_obj {
|
|
|
DEFAULT_NEW_INSTANCE(proxy_node_type);
|
|
DEFAULT_NEW_INSTANCE(proxy_node_type);
|
|
|
|
|
|
|
|
- using signal_type = tiny_signal<int64_t>;
|
|
|
|
|
- using slot_type = signal_type::slot_type;
|
|
|
|
|
-
|
|
|
|
|
- struct slot_impl_type : public slot_type {
|
|
|
|
|
- proxy_node_type *p_this = nullptr;
|
|
|
|
|
-
|
|
|
|
|
- void on_signal_received(int64_t args) override {
|
|
|
|
|
- assert(p_this != nullptr);
|
|
|
|
|
- p_this->output_signal.emit(args * p_this->init_value);
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- void load_construct_config(const nlohmann::json &config) override {
|
|
|
|
|
- BOOST_TEST(config.empty());
|
|
|
|
|
- auto input_slot = new slot_impl_type();
|
|
|
|
|
- input_slot->p_this = this;
|
|
|
|
|
- get_manager().register_signal(this, "output", output_signal);
|
|
|
|
|
- get_manager().register_slot<int64_t>(this, "input", *input_slot);
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ global_obj_index_type input_obj_index;
|
|
|
|
|
+ global_obj_index_type output_obj_index;
|
|
|
|
|
+ coro_worker::pointer proxy_worker;
|
|
|
|
|
|
|
|
boost::asio::awaitable<bool> on_init(const nlohmann::json &config) override {
|
|
boost::asio::awaitable<bool> on_init(const nlohmann::json &config) override {
|
|
|
- assert(config.contains("init_value"));
|
|
|
|
|
- assert(config["init_value"].is_number());
|
|
|
|
|
- init_value = config["init_value"].get<int64_t>();
|
|
|
|
|
|
|
+ input_obj_index = get_manager().
|
|
|
|
|
+ register_global_obj<u64int_obj>(
|
|
|
|
|
+ config["input_obj_name"].get<std::string>());
|
|
|
|
|
+ output_obj_index = get_manager().
|
|
|
|
|
+ register_global_obj<u64int_obj>(
|
|
|
|
|
+ config["output_obj_name"].get<std::string>());
|
|
|
co_return true;
|
|
co_return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
boost::asio::awaitable<bool> on_start(const nlohmann::json &config) override {
|
|
boost::asio::awaitable<bool> on_start(const nlohmann::json &config) override {
|
|
|
- BOOST_TEST(config.empty());
|
|
|
|
|
- co_spawn(get_context(), [=]() -> awaitable<void> {
|
|
|
|
|
- co_await coro_sleep(100ms);
|
|
|
|
|
- co_await this->stop();
|
|
|
|
|
- }, detached);
|
|
|
|
|
|
|
+ proxy_worker = make_infinite_coro_worker(
|
|
|
|
|
+ get_context(), [
|
|
|
|
|
+ input_obj = global_obj_auto_sync_delegate<u64int_obj>(
|
|
|
|
|
+ get_manager(), input_obj_index),
|
|
|
|
|
+ output_obj = global_obj_auto_sync_delegate<u64int_obj>(
|
|
|
|
|
+ get_manager(), output_obj_index),
|
|
|
|
|
+ start_value = config["start_value"].get<std::uint64_t>()]() mutable
|
|
|
|
|
+ -> boost::asio::awaitable<bool> {
|
|
|
|
|
+ co_await input_obj.coro_wait_update();
|
|
|
|
|
+ auto new_out = u64int_obj::new_instance(input_obj->value + start_value);
|
|
|
|
|
+ output_obj.set_value(new_out);
|
|
|
|
|
+ SPDLOG_WARN("New value from proxy {}", new_out->value);
|
|
|
|
|
+ ++start_value;
|
|
|
|
|
+ co_return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ proxy_worker->run();
|
|
|
co_return true;
|
|
co_return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- signal_type output_signal;
|
|
|
|
|
- int64_t init_value = -1;
|
|
|
|
|
|
|
+ boost::asio::awaitable<void> on_stop() override {
|
|
|
|
|
+ proxy_worker->cancel();
|
|
|
|
|
+ co_await proxy_worker->coro_wait_stop();
|
|
|
|
|
+ proxy_worker.reset();
|
|
|
|
|
+ co_return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct target_node_type : public tristate_obj {
|
|
struct target_node_type : public tristate_obj {
|
|
|
DEFAULT_NEW_INSTANCE(target_node_type);
|
|
DEFAULT_NEW_INSTANCE(target_node_type);
|
|
|
|
|
|
|
|
- using signal_type = tiny_signal<int64_t>;
|
|
|
|
|
- using slot_type = signal_type::slot_type;
|
|
|
|
|
-
|
|
|
|
|
- struct slot_impl_type : public slot_type {
|
|
|
|
|
- void on_signal_received(int64_t args) override {
|
|
|
|
|
- std::cout << args << std::endl;
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- void load_construct_config(const nlohmann::json &config) override {
|
|
|
|
|
- BOOST_TEST(config.empty());
|
|
|
|
|
- get_manager().register_slot<int64_t>(this, "input", *(new slot_impl_type));
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ global_obj_index_type source_obj_index;
|
|
|
|
|
+ global_obj_index_type proxy_obj_index;
|
|
|
|
|
+ coro_worker::pointer target_worker;
|
|
|
|
|
+ coro_signal_any_group::pointer watch_group;
|
|
|
|
|
|
|
|
boost::asio::awaitable<bool> on_init(const nlohmann::json &config) override {
|
|
boost::asio::awaitable<bool> on_init(const nlohmann::json &config) override {
|
|
|
- BOOST_TEST(config.empty());
|
|
|
|
|
|
|
+ source_obj_index = get_manager().
|
|
|
|
|
+ register_global_obj<u64int_obj>(
|
|
|
|
|
+ config["source_obj_name"].get<std::string>());
|
|
|
|
|
+ proxy_obj_index = get_manager().
|
|
|
|
|
+ register_global_obj<u64int_obj>(
|
|
|
|
|
+ config["proxy_obj_name"].get<std::string>());
|
|
|
co_return true;
|
|
co_return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
boost::asio::awaitable<bool> on_start(const nlohmann::json &config) override {
|
|
boost::asio::awaitable<bool> on_start(const nlohmann::json &config) override {
|
|
|
- BOOST_TEST(config.empty());
|
|
|
|
|
|
|
+ watch_group = std::make_unique<coro_signal_any_group>(get_manager());
|
|
|
|
|
+ watch_group->add_watcher(get_manager().request_global_obj_update_watcher(source_obj_index));
|
|
|
|
|
+ watch_group->add_watcher(get_manager().request_global_obj_update_watcher(proxy_obj_index));
|
|
|
|
|
+ watch_group->start(get_context());
|
|
|
|
|
+ target_worker = make_infinite_coro_worker(
|
|
|
|
|
+ get_context(), [
|
|
|
|
|
+ watcher = watch_group->new_watcher(get_context()),
|
|
|
|
|
+ source_obj = global_obj_auto_sync_delegate<u64int_obj>(
|
|
|
|
|
+ get_manager(), source_obj_index),
|
|
|
|
|
+ proxy_obj = global_obj_auto_sync_delegate<u64int_obj>(
|
|
|
|
|
+ get_manager(), proxy_obj_index)]() mutable
|
|
|
|
|
+ -> boost::asio::awaitable<bool> {
|
|
|
|
|
+ co_await watcher.coro_wait();
|
|
|
|
|
+ SPDLOG_ERROR("source: {}, proxy: {}", source_obj->value, proxy_obj->value);
|
|
|
|
|
+ co_return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ target_worker->run();
|
|
|
co_return true;
|
|
co_return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ boost::asio::awaitable<void> on_stop() override {
|
|
|
|
|
+ co_await watch_group->stop();
|
|
|
|
|
+ target_worker->cancel();
|
|
|
|
|
+ co_await target_worker->coro_wait_stop();
|
|
|
|
|
+ target_worker.reset();
|
|
|
|
|
+ co_return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_sophiar_manager) {
|
|
BOOST_AUTO_TEST_CASE(test_sophiar_manager) {
|
|
@@ -137,14 +174,6 @@ BOOST_AUTO_TEST_CASE(test_sophiar_manager) {
|
|
|
|
|
|
|
|
global_sophiar_manager.load_config_and_start(nlohmann::json::parse(config_file));
|
|
global_sophiar_manager.load_config_and_start(nlohmann::json::parse(config_file));
|
|
|
|
|
|
|
|
- co_spawn(global_context, []() -> awaitable<void> {
|
|
|
|
|
- co_await global_sophiar_manager.switch_mode("mode_a");
|
|
|
|
|
- co_await coro_sleep(100ms);
|
|
|
|
|
- co_await global_sophiar_manager.switch_mode("mode_b");
|
|
|
|
|
-// co_await coro_sleep(100ms);
|
|
|
|
|
-// co_await global_sophiar_manager.switch_mode("mode_a");
|
|
|
|
|
- }, detached);
|
|
|
|
|
-
|
|
|
|
|
global_context.run();
|
|
global_context.run();
|
|
|
|
|
|
|
|
}
|
|
}
|