From 1759fcdd03588f23ee65cb736789098a2a2f817e Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 28 Jul 2020 09:04:50 +0300
Subject: [PATCH] Install default rpc handlers

---
 components/common/cpp/src/configuration.cpp |  2 +-
 components/control/cpp/src/master.cpp       | 12 ++++++++++++
 components/streams/src/feed.cpp             |  1 +
 components/streams/src/netstream.cpp        |  7 ++++---
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/components/common/cpp/src/configuration.cpp b/components/common/cpp/src/configuration.cpp
index f6300e67c..f756d1d57 100644
--- a/components/common/cpp/src/configuration.cpp
+++ b/components/common/cpp/src/configuration.cpp
@@ -341,7 +341,7 @@ void ftl::config::registerConfigurable(ftl::Configurable *cfg) {
 		LOG(ERROR) << "Attempting to create a duplicate object: " << *uri;
 	} else {
 		config_instance[*uri] = cfg;
-		LOG(INFO) << "Registering instance: " << *uri;
+		//LOG(INFO) << "Registering instance: " << *uri;
 
 		lk.unlock();
 		auto tags = cfg->get<vector<string>>("tags");
diff --git a/components/control/cpp/src/master.cpp b/components/control/cpp/src/master.cpp
index 159fc403b..d6a2f1cac 100644
--- a/components/control/cpp/src/master.cpp
+++ b/components/control/cpp/src/master.cpp
@@ -36,6 +36,18 @@ Master::Master(Configurable *root, Universe *net)
 		state_.paused = !state_.paused;
 	});
 
+	net->bind("list_streams", []() {
+		return std::list<std::string>();
+	});
+
+	net->bind("find_stream", [](const std::string &uri, bool proxy) {
+		return std::optional<ftl::UUID>{};
+	});
+
+	net->bind("add_stream", [](const std::string &uri) {
+
+	});
+
 	net->bind("update_cfg", [](const std::string &uri, const std::string &value) {
 		ftl::config::update(uri, nlohmann::json::parse(value));
 	});
diff --git a/components/streams/src/feed.cpp b/components/streams/src/feed.cpp
index 76cfc60e5..defe19720 100644
--- a/components/streams/src/feed.cpp
+++ b/components/streams/src/feed.cpp
@@ -154,6 +154,7 @@ Feed::Feed(nlohmann::json &config, ftl::net::Universe*net) :
 		});
 	});
 
+	if (net_->isBound("add_stream")) net_->unbind("add_stream");
 	net_->bind("add_stream", [this](ftl::net::Peer &p, std::string uri){
 		//UNIQUE_LOCK(mtx_, lk);
 		_updateNetSources(&p, uri);
diff --git a/components/streams/src/netstream.cpp b/components/streams/src/netstream.cpp
index dfd3cfb4b..00910ec93 100644
--- a/components/streams/src/netstream.cpp
+++ b/components/streams/src/netstream.cpp
@@ -28,10 +28,12 @@ int64_t Net::last_msg__ = 0;
 MUTEX Net::msg_mtx__;
 
 static std::list<std::string> net_streams;
+static std::atomic_flag has_bindings = ATOMIC_FLAG_INIT;
 static SHARED_MUTEX stream_mutex;
 
 Net::Net(nlohmann::json &config, ftl::net::Universe *net) : Stream(config), active_(false), net_(net), clock_adjust_(0), last_ping_(0) {
-	if (!net_->isBound("find_stream")) {
+	if (!has_bindings.test_and_set()) {
+		if (net_->isBound("find_stream")) net_->unbind("find_stream");
 		net_->bind("find_stream", [net = net_](const std::string &uri, bool proxy) -> optional<ftl::UUID> {
 			LOG(INFO) << "REQUEST FIND STREAM: " << uri;
 
@@ -48,9 +50,8 @@ Net::Net(nlohmann::json &config, ftl::net::Universe *net) : Stream(config), acti
 			}
 			return {};
 		});
-	}
 
-	if (!net_->isBound("list_streams")) {
+		if (net_->isBound("list_streams")) net_->unbind("list_streams");
 		net_->bind("list_streams", [this]() {
 			LOG(INFO) << "REQUEST LIST STREAMS";
 			SHARED_LOCK(stream_mutex, lk);
-- 
GitLab