From dcbb0430145667604aa906365978cd8f43228b3f Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 3 May 2019 10:48:20 +0300
Subject: [PATCH] Debug info and lock in net code

---
 CMakeLists.txt                       |  2 +-
 net/cpp/include/ftl/net/universe.hpp |  2 ++
 net/cpp/src/universe.cpp             | 12 ++++++++++++
 vision/src/local.cpp                 |  8 +++++++-
 vision/src/main.cpp                  | 16 +++++++++++++++-
 5 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d5a06e10..8a86df76f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -103,7 +103,7 @@ include(ftl_paths)
 if (WIN32) # TODO(nick) Should do based upon compiler (VS)
 	add_definitions(-DWIN32)
 	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
-	set(CMAKE_CXX_FLAGS_DEBUG "-D_DEBUG -Wall")
+	set(CMAKE_CXX_FLAGS_DEBUG "-DFTL_DEBUG -Wall")
 	set(CMAKE_CXX_FLAGS_RELEASE "/O2")
 else()
 	add_definitions(-DUNIX)
diff --git a/net/cpp/include/ftl/net/universe.hpp b/net/cpp/include/ftl/net/universe.hpp
index 81f2a6f26..59eb49a27 100644
--- a/net/cpp/include/ftl/net/universe.hpp
+++ b/net/cpp/include/ftl/net/universe.hpp
@@ -13,6 +13,7 @@
 #include <vector>
 #include <string>
 #include <thread>
+#include <mutex>
 #include <map>
 
 namespace ftl {
@@ -148,6 +149,7 @@ class Universe {
 	std::map<ftl::UUID, ftl::net::Peer*> peer_ids_;
 	ftl::UUID id_;
 	ftl::net::Dispatcher disp_;
+	std::mutex net_mutex_;
 	
 	// std::map<std::string, std::vector<ftl::net::Peer*>> subscriptions_;
 };
diff --git a/net/cpp/src/universe.cpp b/net/cpp/src/universe.cpp
index 066e7514d..f6d7e2acb 100644
--- a/net/cpp/src/universe.cpp
+++ b/net/cpp/src/universe.cpp
@@ -1,4 +1,5 @@
 #include <ftl/net/universe.hpp>
+#include <chrono>
 
 #ifdef WIN32
 #include <Ws2tcpip.h>
@@ -14,6 +15,8 @@ using ftl::net::Universe;
 using nlohmann::json;
 using ftl::UUID;
 using std::optional;
+using std::unique_lock;
+using std::mutex;
 
 Universe::Universe() : active_(true), thread_(Universe::__start, this) {
 	_installBindings();
@@ -58,6 +61,7 @@ Universe::~Universe() {
 bool Universe::listen(const string &addr) {
 	auto l = new Listener(addr.c_str());
 	if (!l) return false;
+	unique_lock<mutex> lk(net_mutex_);
 	listeners_.push_back(l);
 	return l->isListening();
 }
@@ -67,6 +71,7 @@ bool Universe::connect(const string &addr) {
 	if (!p) return false;
 	
 	if (p->status() != Peer::kInvalid) {
+		unique_lock<mutex> lk(net_mutex_);
 		peers_.push_back(p);
 	}
 	
@@ -86,6 +91,8 @@ int Universe::_setDescriptors() {
 
 	int n = 0;
 
+	unique_lock<mutex> lk(net_mutex_);
+
 	//Set file descriptor for the listening sockets.
 	for (auto l : listeners_) {
 		if (l != nullptr && l->isListening()) {
@@ -200,6 +207,11 @@ void Universe::_run() {
 		int n = _setDescriptors();
 		int selres = 1;
 
+		if (n == 0) {
+			std::this_thread::sleep_for(std::chrono::milliseconds(300));
+			continue;
+		}
+
 		//Wait for a network event or timeout in 3 seconds
 		block.tv_sec = 0;
 		block.tv_usec = 10000;
diff --git a/vision/src/local.cpp b/vision/src/local.cpp
index d0e542669..d8d5229e8 100644
--- a/vision/src/local.cpp
+++ b/vision/src/local.cpp
@@ -29,8 +29,14 @@ LocalSource::LocalSource(nlohmann::json &config)
 		flip_v_(config["flip_vert"]),
 		nostereo_(config["nostereo"]),
 		downsize_(config.value("scale", 1.0f)) {
+
 	// Use cameras
-	camera_a_ = new VideoCapture((flip_) ? 1 : 0);
+	camera_a_ = new VideoCapture;
+	LOG(INFO) << "Cameras check... ";
+	camera_a_->open((flip_) ? 1 : 0);
+
+	LOG(INFO) << "Cameras open? " << flip_;
+
 	if (!nostereo_) {
 		camera_b_ = new VideoCapture((flip_) ? 0 : 1);
 	} else {
diff --git a/vision/src/main.cpp b/vision/src/main.cpp
index fbfcae649..0665b797c 100644
--- a/vision/src/main.cpp
+++ b/vision/src/main.cpp
@@ -61,32 +61,40 @@ static void run(const string &file) {
 	ctpl::thread_pool pool(2);
 	Universe net(config["net"]);
 
+	LOG(INFO) << "Net started.";
+
 	LocalSource *lsrc;
 	if (ftl::is_video(file)) {
 		// Load video file
+		LOG(INFO) << "Using video file...";
 		lsrc = new LocalSource(file, config["source"]);
 	} else if (file != "") {
 		auto vid = ftl::locateFile("video.mp4");
 		if (!vid) {
 			LOG(FATAL) << "No video.mp4 file found in provided paths";
 		} else {
+			LOG(INFO) << "Using test directory...";
 			lsrc = new LocalSource(*vid, config["source"]);
 		}
 	} else {
 		// Use cameras
+		LOG(INFO) << "Using cameras...";
 		lsrc = new LocalSource(config["source"]);
 	}
 
-	auto sync = new SyncSource();  // TODO(nick) Pass protocol object
+	//auto sync = new SyncSource();  // TODO(nick) Pass protocol object
 	// Add any remote channels
 	/* for (auto c : OPTION_channels) {
 		sync->addChannel(c);
 	} */
 
+	LOG(INFO) << "Locating calibration...";
+
 	// Perform or load calibration intrinsics + extrinsics
 	Calibrate calibrate(lsrc, config["calibration"]);
 	if (config["calibrate"]) calibrate.recalibrate();
 	if (!calibrate.isCalibrated()) LOG(WARNING) << "Cameras are not calibrated!";
+	else LOG(INFO) << "Calibration initiated.";
 	
 	cv::Mat Q_32F;
 	calibrate.getQ().convertTo(Q_32F, CV_32F);
@@ -115,6 +123,8 @@ static void run(const string &file) {
 	
 	Streamer stream(net, config["stream"]);
 
+	LOG(INFO) << "Beginning capture...";
+
 	while (display.active()) {
 		mutex m;
 		condition_variable cv;
@@ -134,7 +144,9 @@ static void run(const string &file) {
 			//sync->get(ftl::LEFT, l);
 			//sync->get(ftl::RIGHT, r);
 
+			LOG(INFO) << "PRE DISPARITY";
 		    disparity->compute(l, r, disp);
+			LOG(INFO) << "POST DISPARITY";
 
 			unique_lock<mutex> lk(m);
 			jobs++;
@@ -218,6 +230,8 @@ static void run(const string &file) {
 
 		//net.publish(uri, rgb_buf, d_buf);
 	}
+
+	LOG(INFO) << "Finished.";
 }
 
 int main(int argc, char **argv) {
-- 
GitLab