From e209a295592517584e7dfbe1054bd9d7d7f6ca1e Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 17 Jun 2019 10:39:31 +0300
Subject: [PATCH] Add pause button

---
 applications/gui/src/ctrl_window.cpp          | 8 ++++----
 applications/reconstruct/CMakeLists.txt       | 2 +-
 applications/reconstruct/src/main.cpp         | 4 +++-
 components/control/cpp/include/ftl/master.hpp | 4 ++++
 components/control/cpp/include/ftl/slave.hpp  | 7 +++++++
 components/control/cpp/src/master.cpp         | 8 ++++++++
 components/control/cpp/src/slave.cpp          | 8 ++++++++
 7 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/applications/gui/src/ctrl_window.cpp b/applications/gui/src/ctrl_window.cpp
index 38a496376..ef2854640 100644
--- a/applications/gui/src/ctrl_window.cpp
+++ b/applications/gui/src/ctrl_window.cpp
@@ -30,9 +30,9 @@ ControlWindow::ControlWindow(nanogui::Widget *parent, ftl::ctrl::Master *ctrl)
 	button->setCallback([this] {
 		ctrl_->restart();
 	});
-	button = new Button(this, "Shutdown All", ENTYPO_ICON_POWER_PLUG);
+	button = new Button(this, "Pause All", ENTYPO_ICON_POWER_PLUG);
 	button->setCallback([this] {
-		ctrl_->shutdown();
+		ctrl_->pause();
 	});
 
 	new Label(this, "Select Node","sans-bold");
@@ -47,9 +47,9 @@ ControlWindow::ControlWindow(nanogui::Widget *parent, ftl::ctrl::Master *ctrl)
 		ctrl_->restart(_getActiveID());
 	});
 
-	button = new Button(this, "Shutdown Node", ENTYPO_ICON_POWER_PLUG);
+	button = new Button(this, "Pause Node", ENTYPO_ICON_POWER_PLUG);
 	button->setCallback([this] {
-		ctrl_->shutdown(_getActiveID());
+		ctrl_->pause(_getActiveID());
 	});
 
 	_changeActive(0);
diff --git a/applications/reconstruct/CMakeLists.txt b/applications/reconstruct/CMakeLists.txt
index 82e0182d3..409a425e4 100644
--- a/applications/reconstruct/CMakeLists.txt
+++ b/applications/reconstruct/CMakeLists.txt
@@ -26,6 +26,6 @@ set_property(TARGET ftl-reconstruct PROPERTY CUDA_SEPARABLE_COMPILATION ON)
 endif()
 
 #target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(ftl-reconstruct ftlcommon ftlrgbd Threads::Threads ${OpenCV_LIBS} glog::glog ftlnet ftlrender)
+target_link_libraries(ftl-reconstruct ftlcommon ftlrgbd Threads::Threads ${OpenCV_LIBS} ftlctrl ftlnet ftlrender)
 
 
diff --git a/applications/reconstruct/src/main.cpp b/applications/reconstruct/src/main.cpp
index a041142b6..d1c2c095a 100644
--- a/applications/reconstruct/src/main.cpp
+++ b/applications/reconstruct/src/main.cpp
@@ -13,6 +13,7 @@
 #include <ftl/rgbd.hpp>
 #include <ftl/virtual_source.hpp>
 #include <ftl/rgbd/streamer.hpp>
+#include <ftl/slave.hpp>
 
 // #include <zlib.h>
 // #include <lz4.h>
@@ -74,6 +75,7 @@ struct Cameras {
 
 static void run(ftl::Configurable *root) {
 	Universe *net = ftl::create<Universe>(root, "net");
+	ftl::ctrl::Slave slave(net, root);
 	
 	net->start();
 	net->waitConnections();
@@ -190,7 +192,7 @@ static void run(ftl::Configurable *root) {
 
 		active = 0;
 
-		if (!paused) {
+		if (!slave.isPaused()) {
 			//net.broadcast("grab");  // To sync cameras
 			scene->nextFrame();
 		
diff --git a/components/control/cpp/include/ftl/master.hpp b/components/control/cpp/include/ftl/master.hpp
index e7c8e209e..91a913ed9 100644
--- a/components/control/cpp/include/ftl/master.hpp
+++ b/components/control/cpp/include/ftl/master.hpp
@@ -30,6 +30,10 @@ class Master {
 
 	void shutdown(const ftl::UUID &peer);
 
+	void pause();
+
+	void pause(const ftl::UUID &peer);
+
 	void set(const std::string &uri, ftl::config::json_t &value);
 
 	void set(const ftl::UUID &peer, const std::string &uri, ftl::config::json_t &value);
diff --git a/components/control/cpp/include/ftl/slave.hpp b/components/control/cpp/include/ftl/slave.hpp
index d62261df1..a7ae0075a 100644
--- a/components/control/cpp/include/ftl/slave.hpp
+++ b/components/control/cpp/include/ftl/slave.hpp
@@ -9,6 +9,10 @@
 namespace ftl {
 namespace ctrl {
 
+struct SystemState {
+	bool paused;
+};
+
 /**
  * Allows a node to be remote controlled and observed over the network. All
  * such nodes should create a single instance of this class, but must call
@@ -29,12 +33,15 @@ class Slave {
 	 */
 	void sendLog(const loguru::Message& message);
 
+	bool isPaused() const { return state_.paused; }
+
 	private:
 	std::vector<ftl::UUID> log_peers_;
 	ftl::net::Universe *net_;
 	std::recursive_mutex mutex_;
 	bool in_log_;
 	bool active_;
+	SystemState state_;
 };
 
 }
diff --git a/components/control/cpp/src/master.cpp b/components/control/cpp/src/master.cpp
index bd4cadfe6..823939d64 100644
--- a/components/control/cpp/src/master.cpp
+++ b/components/control/cpp/src/master.cpp
@@ -53,6 +53,14 @@ void Master::shutdown(const ftl::UUID &peer) {
 	net_->send(peer, "shutdown");
 }
 
+void Master::pause() {
+	net_->broadcast("pause");
+}
+
+void Master::pause(const ftl::UUID &peer) {
+	net_->send(peer, "pause");
+}
+
 void Master::set(const string &uri, json_t &value) {
 	net_->broadcast("update_cfg", uri, (string)value);
 }
diff --git a/components/control/cpp/src/slave.cpp b/components/control/cpp/src/slave.cpp
index e0740b3c2..b4823e1d1 100644
--- a/components/control/cpp/src/slave.cpp
+++ b/components/control/cpp/src/slave.cpp
@@ -13,6 +13,10 @@ static void netLog(void* user_data, const loguru::Message& message) {
 }
 
 Slave::Slave(Universe *net, ftl::Configurable *root) : net_(net), in_log_(false), active_(true) {
+
+	// Init system state
+	state_.paused = false;
+
 	net->bind("restart", []() {
 		LOG(WARNING) << "Remote restart...";
 		//exit(1);
@@ -26,6 +30,10 @@ Slave::Slave(Universe *net, ftl::Configurable *root) : net_(net), in_log_(false)
 		ftl::running = false;
 	});
 
+	net->bind("pause", [this]() {
+		state_.paused = !state_.paused;
+	});
+
 	net->bind("update_cfg", [](const std::string &uri, const std::string &value) {
 		ftl::config::update(uri, nlohmann::json::parse(value));
 	});
-- 
GitLab