From 6676778d3f53db86782a4624c09bdc21a6859cda Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 3 Jun 2019 09:25:38 +0300
Subject: [PATCH] Catch net exceptions and log output

---
 components/rgbd-sources/src/net_source.cpp    | 47 ++++++++++++-------
 components/rgbd-sources/src/rgbd_streamer.cpp |  8 +++-
 2 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/components/rgbd-sources/src/net_source.cpp b/components/rgbd-sources/src/net_source.cpp
index 5084063f9..9777306a1 100644
--- a/components/rgbd-sources/src/net_source.cpp
+++ b/components/rgbd-sources/src/net_source.cpp
@@ -15,23 +15,28 @@ using std::this_thread::sleep_for;
 using std::chrono::milliseconds;
 
 bool NetSource::_getCalibration(Universe &net, const UUID &peer, const string &src, ftl::rgbd::CameraParameters &p) {
-	while(true) {
-		auto buf = net.call<vector<unsigned char>>(peer_, "source_calibration", src);
-		if (buf.size() > 0) {
-			memcpy((char*)&p, buf.data(), buf.size());
-			
-			if (sizeof(p) != buf.size()) {
-				LOG(ERROR) << "Corrupted calibration";
-				return false;
+	try {
+		while(true) {
+			auto buf = net.call<vector<unsigned char>>(peer_, "source_calibration", src);
+
+			if (buf.size() > 0) {
+				memcpy((char*)&p, buf.data(), buf.size());
+				
+				if (sizeof(p) != buf.size()) {
+					LOG(ERROR) << "Corrupted calibration";
+					return false;
+				}
+
+				LOG(INFO) << "Calibration received: " << p.cx << ", " << p.cy << ", " << p.fx << ", " << p.fy;
+				
+				return true;
+			} else {
+				LOG(INFO) << "Could not get calibration, retrying";
+				sleep_for(milliseconds(500));
 			}
-
-			LOG(INFO) << "Calibration received: " << p.cx << ", " << p.cy << ", " << p.fx << ", " << p.fy;
-			
-			return true;
-		} else {
-			LOG(INFO) << "Could not get calibration, retrying";
-			sleep_for(milliseconds(500));
 		}
+	} catch (...) {
+		return false;
 	}
 }
 
@@ -59,7 +64,11 @@ NetSource::NetSource(nlohmann::json &config, ftl::net::Universe *net)
 	N_ = 10;
 
 	// Initiate stream with request for first 10 frames
-	net->send(peer_, "get_stream", getURI(), 10, 0, net->id(), getURI());
+	try {
+		net->send(peer_, "get_stream", getURI(), 10, 0, net->id(), getURI());
+	} catch(...) {
+		LOG(ERROR) << "Could not connect to stream " << getURI();
+	}
 }
 
 NetSource::~NetSource() {
@@ -81,7 +90,11 @@ void NetSource::_recv(const vector<unsigned char> &jpg, const vector<unsigned ch
 
 void NetSource::setPose(const Eigen::Matrix4f &pose) {
 	vector<unsigned char> vec((unsigned char*)pose.data(), (unsigned char*)(pose.data()+(pose.size())));
-	net_->send(peer_, "set_pose", getURI(), vec);
+	try {
+		net_->send(peer_, "set_pose", getURI(), vec);
+	} catch (...) {
+
+	}
 	RGBDSource::setPose(pose);
 }
 
diff --git a/components/rgbd-sources/src/rgbd_streamer.cpp b/components/rgbd-sources/src/rgbd_streamer.cpp
index ad4e700c0..583c6f199 100644
--- a/components/rgbd-sources/src/rgbd_streamer.cpp
+++ b/components/rgbd-sources/src/rgbd_streamer.cpp
@@ -30,8 +30,10 @@ Streamer::Streamer(nlohmann::json &config, Universe *net)
 	net_ = net;
 	
 	net->bind("find_stream", [this](const std::string &uri) -> optional<UUID> {
-		if (sources_.find(uri) != sources_.end()) return net_->id();
-		else return {};
+		if (sources_.find(uri) != sources_.end()) {
+			LOG(INFO) << "Valid source request received: " << uri;
+			return net_->id();
+		} else return {};
 	});
 
 	net->bind("list_streams", [this]() -> vector<string> {
@@ -91,6 +93,8 @@ void Streamer::add(RGBDSource *src) {
 	s->src = src;
 	s->state = 0;
 	sources_[src->getURI()] = s;
+
+	LOG(INFO) << "Added source: " << src->getURI();
 }
 
 void Streamer::_addClient(const string &source, int N, int rate, const ftl::UUID &peer, const string &dest) {
-- 
GitLab