From 8e54c6bb9d8e15f68ca6f0d04245a867fcb92e58 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 27 Jul 2020 20:01:46 +0300
Subject: [PATCH] Prioritise non proxy connections

---
 components/streams/src/netstream.cpp | 7 +++++--
 web-service/server/src/index.js      | 4 +++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/components/streams/src/netstream.cpp b/components/streams/src/netstream.cpp
index b497ac3df..dfd3cfb4b 100644
--- a/components/streams/src/netstream.cpp
+++ b/components/streams/src/netstream.cpp
@@ -32,7 +32,7 @@ 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")) {
-		net_->bind("find_stream", [net = net_](const std::string &uri) -> optional<ftl::UUID> {
+		net_->bind("find_stream", [net = net_](const std::string &uri, bool proxy) -> optional<ftl::UUID> {
 			LOG(INFO) << "REQUEST FIND STREAM: " << uri;
 
 			ftl::URI u1(uri);
@@ -242,7 +242,10 @@ bool Net::begin() {
 		//}
 	});
 
-	auto p = net_->findOne<ftl::UUID>("find_stream", uri_);
+	// First find non-proxy version, then check for proxy version if no match
+	auto p = net_->findOne<ftl::UUID>("find_stream", uri_, false);
+	if (!p) p = net_->findOne<ftl::UUID>("find_stream", uri_, true);
+
 	if (!p) {
 		LOG(INFO) << "Hosting stream: " << uri_;
 		// TODO: Register URI as available.
diff --git a/web-service/server/src/index.js b/web-service/server/src/index.js
index 2510db14a..22eb16270 100644
--- a/web-service/server/src/index.js
+++ b/web-service/server/src/index.js
@@ -380,7 +380,9 @@ app.ws('/', (ws, req) => {
 		}
 	});
 
-	p.bind("find_stream", (uri) => {
+	p.bind("find_stream", (uri, proxy) => {
+		if (!proxy) return null;
+		
 		const parsedURI = stringSplitter(uri)
 		if (uri_to_peer.hasOwnProperty(parsedURI)) {
 			console.log("Stream found: ", uri, parsedURI);
-- 
GitLab