From dad6a64ea86d162ed44741cc8abe879e095d97fa Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 24 May 2019 21:32:12 +0300
Subject: [PATCH] Fixes #28 and perhaps also #34

---
 applications/vision/src/streamer.cpp            | 8 ++++++--
 components/net/cpp/include/ftl/net/universe.hpp | 1 +
 components/net/cpp/src/universe.cpp             | 9 ++++++++-
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/applications/vision/src/streamer.cpp b/applications/vision/src/streamer.cpp
index b9a3f78bb..0a4d08535 100644
--- a/applications/vision/src/streamer.cpp
+++ b/applications/vision/src/streamer.cpp
@@ -54,7 +54,11 @@ void Streamer::send(const Mat &rgb, const Mat &depth) {
 
     cv::imencode(".png", d2, d_buf);
     LOG(INFO) << "Depth Size = " << ((float)d_buf.size() / (1024.0f*1024.0f));
-    
-    net_.publish(uri_, rgb_buf, d_buf);
+
+	try {
+    	net_.publish(uri_, rgb_buf, d_buf);
+	} catch (...) {
+		LOG(ERROR) << "Exception on net publish to " << uri_;
+	}
 }
 
diff --git a/components/net/cpp/include/ftl/net/universe.hpp b/components/net/cpp/include/ftl/net/universe.hpp
index ccb6e1215..873728671 100644
--- a/components/net/cpp/include/ftl/net/universe.hpp
+++ b/components/net/cpp/include/ftl/net/universe.hpp
@@ -138,6 +138,7 @@ class Universe : public ftl::Configurable {
 	void _installBindings();
 	void _installBindings(Peer *);
 	bool _subscribe(const std::string &res);
+	void _remove(Peer *);
 	
 	static void __start(Universe *u);
 	
diff --git a/components/net/cpp/src/universe.cpp b/components/net/cpp/src/universe.cpp
index 130d7ec41..721f72a1f 100644
--- a/components/net/cpp/src/universe.cpp
+++ b/components/net/cpp/src/universe.cpp
@@ -150,6 +150,13 @@ void Universe::_installBindings() {
 	});
 }
 
+// Note: should be called inside a net lock
+void Universe::_remove(Peer *p) {
+	auto ix = peer_ids_.find(p->id());
+	if (ix != peer_ids_.end()) peer_ids_.erase(ix);
+	delete p;
+}
+
 Peer *Universe::getPeer(const UUID &id) const {
 	auto ix = peer_ids_.find(id);
 	if (ix == peer_ids_.end()) return nullptr;
@@ -295,8 +302,8 @@ void Universe::_run() {
 				for (auto i=peers_.begin(); i!=peers_.end(); i++) {
 					if ((*i) == s) {
 						LOG(INFO) << "REMOVING SOCKET";
+						_remove(*i);
 						peers_.erase(i); break;
-						// TODO Remove subscribers
 					}
 				}
 			}
-- 
GitLab