From 236f7bffcc975d0e4d4ff969c7ac0685936ea4b7 Mon Sep 17 00:00:00 2001
From: Philipp Spilger <philipp.spilger@kip.uni-heidelberg.de>
Date: Tue, 12 Nov 2019 13:52:21 +0100
Subject: [PATCH] Add default constructor to SimConnection with enviroment port
 extraction

* remove mandatory command-line options of simtests

Change-Id: I4de943a230e3636b1fdee7fb56c78e314e20250f
---
 include/flange/simulator_client.h       | 12 ++++++++++
 include/hxcomm/common/simconnection.h   |  9 +++++++-
 include/hxcomm/common/simconnection.tcc | 29 ++++++++++++++++++++++---
 src/flange/simulator_client.cpp         | 13 +++++++++++
 tests/hw/hxcomm/main.cpp                | 13 -----------
 5 files changed, 59 insertions(+), 17 deletions(-)

diff --git a/include/flange/simulator_client.h b/include/flange/simulator_client.h
index d3a32c0..74ad453 100644
--- a/include/flange/simulator_client.h
+++ b/include/flange/simulator_client.h
@@ -30,6 +30,18 @@ public:
 	 */
 	SimulatorClient(ip_t ip, port_t port);
 
+	/**
+	 * Create and start connection to simulation server.
+	 * Automatically extract RCF port from environment, the simulation server is expected to run on
+	 * the same host.
+	 */
+	SimulatorClient();
+
+	/**
+	 * Copy constructor.
+	 */
+	SimulatorClient(SimulatorClient const& other);
+
 	/**
 	 * Close connection to simulator.
 	 */
diff --git a/include/hxcomm/common/simconnection.h b/include/hxcomm/common/simconnection.h
index 60bb932..a981ee0 100644
--- a/include/hxcomm/common/simconnection.h
+++ b/include/hxcomm/common/simconnection.h
@@ -56,6 +56,13 @@ public:
 	 */
 	SimConnection(ip_t ip, port_t port);
 
+	/**
+	 * Create and start connection to simulation server.
+	 * The RCF port is automatically extracted from the enviroment, the simulation server is
+	 * expected to run on the same host.
+	 */
+	SimConnection();
+
 	/**
 	 * Copy constructor (deleted because no two instances with the same simulator allocation can
 	 * coexist).
@@ -177,7 +184,7 @@ private:
 	constexpr static size_t receive_buffer_size = 100000;
 	DoubleBuffer<Packet<subpacket_type, receive_buffer_size>> m_receive_buffer;
 
-	void work_fill_receive_buffer(ip_t ip, port_t port);
+	void work_fill_receive_buffer();
 	std::thread m_worker_fill_receive_buffer;
 
 	void work_decode_messages();
diff --git a/include/hxcomm/common/simconnection.tcc b/include/hxcomm/common/simconnection.tcc
index 5f49a2c..de3f578 100644
--- a/include/hxcomm/common/simconnection.tcc
+++ b/include/hxcomm/common/simconnection.tcc
@@ -14,7 +14,30 @@ SimConnection<ConnectionParameter>::SimConnection(ip_t ip, port_t port) :
     m_run_receive(true),
     m_receive_buffer(m_run_receive),
     m_worker_fill_receive_buffer(
-        &SimConnection<ConnectionParameter>::work_fill_receive_buffer, this, ip, port),
+        &SimConnection<ConnectionParameter>::work_fill_receive_buffer, this),
+    m_worker_decode_messages(&SimConnection<ConnectionParameter>::work_decode_messages, this),
+    m_runnable_mutex(),
+    m_terminate_on_destruction(false),
+    m_logger(log4cxx::Logger::getLogger("hxcomm.SimConnection"))
+{
+	HXCOMM_LOG_TRACE(m_logger, "SimConnection(): Sim connection started.");
+
+    // reset synplify wrapper to align behavior to ARQ FPGA reset of ARQConnection.
+	m_sim.issue_reset();
+}
+
+template <typename ConnectionParameter>
+SimConnection<ConnectionParameter>::SimConnection() :
+    m_sim(),
+    m_send_queue(),
+    m_encoder(m_send_queue),
+    m_receive_queue(),
+    m_listener_halt(),
+    m_decoder(m_receive_queue, m_listener_halt),
+    m_run_receive(true),
+    m_receive_buffer(m_run_receive),
+    m_worker_fill_receive_buffer(
+        &SimConnection<ConnectionParameter>::work_fill_receive_buffer, this),
     m_worker_decode_messages(&SimConnection<ConnectionParameter>::work_decode_messages, this),
     m_runnable_mutex(),
     m_terminate_on_destruction(false),
@@ -85,9 +108,9 @@ bool SimConnection<ConnectionParameter>::try_receive(receive_message_type& messa
 }
 
 template <typename ConnectionParameter>
-void SimConnection<ConnectionParameter>::work_fill_receive_buffer(ip_t ip, port_t port)
+void SimConnection<ConnectionParameter>::work_fill_receive_buffer()
 {
-	thread_local decltype(m_sim) local_sim(ip, port);
+	thread_local decltype(m_sim) local_sim(m_sim);
 
 	while (true) {
 		auto const write_pointer = m_receive_buffer.start_write();
diff --git a/src/flange/simulator_client.cpp b/src/flange/simulator_client.cpp
index 8d27616..fce0f05 100644
--- a/src/flange/simulator_client.cpp
+++ b/src/flange/simulator_client.cpp
@@ -21,6 +21,19 @@ struct SimulatorClient::Impl
 
 SimulatorClient::SimulatorClient(ip_t ip, port_t port) : m_impl(std::make_unique<Impl>(ip, port)) {}
 
+SimulatorClient::SimulatorClient() : m_impl()
+{
+	char const* env_port = std::getenv("FLANGE_SIMULATION_RCF_PORT");
+	if (env_port == nullptr) {
+		throw std::runtime_error("No port to simulator found in environment.");
+	}
+	m_impl = std::make_unique<Impl>("127.0.0.1", static_cast<port_t>(std::atoi(env_port)));
+}
+
+SimulatorClient::SimulatorClient(SimulatorClient const& other) :
+    m_impl(std::make_unique<Impl>(*(other.m_impl)))
+{}
+
 SimulatorClient::~SimulatorClient()
 {}
 
diff --git a/tests/hw/hxcomm/main.cpp b/tests/hw/hxcomm/main.cpp
index ef34e43..5b756d6 100644
--- a/tests/hw/hxcomm/main.cpp
+++ b/tests/hw/hxcomm/main.cpp
@@ -7,18 +7,9 @@
 // logger include directory structure omits prefix
 #include "logging_ctrl.h"
 
-#ifndef HXCOMM_TEST_ARQ_CONNECTION
-std::string simulation_ip;
-unsigned int simulation_port;
-#endif
-
 TestConnection generate_test_connection()
 {
-#ifdef HXCOMM_TEST_ARQ_CONNECTION
 	return TestConnection();
-#else
-	return TestConnection(simulation_ip, simulation_port);
-#endif
 }
 
 int main(int argc, char* argv[])
@@ -29,10 +20,6 @@ int main(int argc, char* argv[])
 	namespace bpo = boost::program_options;
 	bpo::options_description desc("Options");
 	// clang-format off
-#ifndef HXCOMM_TEST_ARQ_CONNECTION
-	desc.add_options()("simulation_ip", bpo::value<std::string>(&simulation_ip)->default_value("127.0.0.1"));
-	desc.add_options()("simulation_port", bpo::value<unsigned int>(&simulation_port)->required());
-#endif
 	desc.add_options()("loglevel", bpo::value<std::string>(&loglevel)->default_value("trace"));
 	// clang-format on
 
-- 
GitLab