From be0c66652f4deebf2e31f2ca653ef309987337ab Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Thu, 5 Dec 2019 12:10:24 +0200
Subject: [PATCH] Resolves #256 Net config refresh

---
 applications/gui/src/config_window.cpp              | 1 +
 components/common/cpp/include/ftl/configurable.hpp  | 6 ++++++
 components/common/cpp/src/configurable.cpp          | 6 +++++-
 components/net/cpp/include/ftl/net_configurable.hpp | 2 ++
 components/net/cpp/src/net_configurable.cpp         | 4 ++++
 5 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/applications/gui/src/config_window.cpp b/applications/gui/src/config_window.cpp
index 4832454ad..322f817ae 100644
--- a/applications/gui/src/config_window.cpp
+++ b/applications/gui/src/config_window.cpp
@@ -55,6 +55,7 @@ void ConfigWindow::_addElements(nanogui::FormHelper *form, const std::string &su
 	Configurable *configurable = ftl::config::find(suri);
 	ftl::config::json_t data;
 	if (configurable) {
+		configurable->refresh();
 		data = configurable->getConfig();
 	}
 
diff --git a/components/common/cpp/include/ftl/configurable.hpp b/components/common/cpp/include/ftl/configurable.hpp
index 219bd171e..61dca8ea3 100644
--- a/components/common/cpp/include/ftl/configurable.hpp
+++ b/components/common/cpp/include/ftl/configurable.hpp
@@ -101,6 +101,12 @@ class Configurable {
 
 	void patchPtr(nlohmann::json &newcfg) { config_ = &newcfg; }
 
+	/**
+	 * Allow configurables to refresh their internal state, perhaps from a
+	 * remote source.
+	 */
+	virtual void refresh();
+
 	protected:
 	nlohmann::json *config_;
 
diff --git a/components/common/cpp/src/configurable.cpp b/components/common/cpp/src/configurable.cpp
index 5116292ad..8186713b1 100644
--- a/components/common/cpp/src/configurable.cpp
+++ b/components/common/cpp/src/configurable.cpp
@@ -59,4 +59,8 @@ void Configurable::on(const string &prop, function<void(const ftl::config::Event
 	} else {
 		(*ix).second.push_back(f);
 	}
-}
\ No newline at end of file
+}
+
+void Configurable::refresh() {
+	// Do nothing by default
+}
diff --git a/components/net/cpp/include/ftl/net_configurable.hpp b/components/net/cpp/include/ftl/net_configurable.hpp
index d9fd6e528..2c6495410 100644
--- a/components/net/cpp/include/ftl/net_configurable.hpp
+++ b/components/net/cpp/include/ftl/net_configurable.hpp
@@ -12,6 +12,8 @@ namespace ftl {
 	NetConfigurable(ftl::UUID peer, const std::string &suri, ftl::ctrl::Master &ctrl, ftl::config::json_t &config);
 	~NetConfigurable();
 
+	void refresh() override;
+
 	protected:
 	void inject(const std::string &name, nlohmann::json &value);
 
diff --git a/components/net/cpp/src/net_configurable.cpp b/components/net/cpp/src/net_configurable.cpp
index be98cf7ed..cf597c5c7 100644
--- a/components/net/cpp/src/net_configurable.cpp
+++ b/components/net/cpp/src/net_configurable.cpp
@@ -10,3 +10,7 @@ ftl::NetConfigurable::~NetConfigurable(){}
 void ftl::NetConfigurable::inject(const std::string &name, nlohmann::json &value) {
     ctrl.set(peer, suri + std::string("/") + name, value);
 }
+
+void ftl::NetConfigurable::refresh() {
+    (*config_) = ctrl.get(peer, suri);
+}
-- 
GitLab