From dbb0bfa3486fcb7c1ad4021fef52b6516e34e159 Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Wed, 11 Apr 2018 17:15:47 +0200
Subject: [PATCH] started config files for receiver

---
 .../cpp/src/http_client/curl_http_client.cpp  |  3 ++
 receiver/CMakeLists.txt                       |  3 +-
 receiver/src/main.cpp                         | 24 ++++++++-
 receiver/src/receiver_config.cpp              | 23 ++++++++
 receiver/src/receiver_config.h                | 18 +++++++
 receiver/src/receiver_config_factory.h        | 20 +++++++
 receiver/unittests/test_config.cpp            | 52 +++++++++++++++++++
 .../check_monitoring/check_linux.sh           |  2 +-
 .../transfer_single_file/check_linux.sh       |  2 +-
 .../transfer_single_file/check_windows.bat    |  2 +-
 10 files changed, 143 insertions(+), 6 deletions(-)
 create mode 100644 receiver/src/receiver_config.cpp
 create mode 100644 receiver/src/receiver_config.h
 create mode 100644 receiver/src/receiver_config_factory.h
 create mode 100644 receiver/unittests/test_config.cpp

diff --git a/common/cpp/src/http_client/curl_http_client.cpp b/common/cpp/src/http_client/curl_http_client.cpp
index c9f91a215..a6c98fa13 100644
--- a/common/cpp/src/http_client/curl_http_client.cpp
+++ b/common/cpp/src/http_client/curl_http_client.cpp
@@ -33,6 +33,9 @@ void SetCurlOptions(CURL* curl, bool post, const std::string& data, const std::s
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
     curl_easy_setopt(curl, CURLOPT_FAILONERROR, 0L);
     curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
+    //todo use a config parameter for this
+    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 5000L);
+
     if (post) {
         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
     }
diff --git a/receiver/CMakeLists.txt b/receiver/CMakeLists.txt
index 371b917b0..83f928fd4 100644
--- a/receiver/CMakeLists.txt
+++ b/receiver/CMakeLists.txt
@@ -7,7 +7,7 @@ set(SOURCE_FILES
         src/request_handler_file_write.cpp
         src/statistics.cpp
         src/statistics_sender_influx_db.cpp
-        )
+        src/receiver_config.cpp src/receiver_config.h)
 
 
 ################################
@@ -35,6 +35,7 @@ set(TEST_SOURCE_FILES
         unittests/test_receiver.cpp
         unittests/test_connection.cpp
         unittests/test_statistics.cpp
+        unittests/test_config.cpp
         unittests/test_request.cpp
         unittests/test_request_handler_file_write.cpp
         unittests/test_statistics_sender_influx_db.cpp
diff --git a/receiver/src/main.cpp b/receiver/src/main.cpp
index 3b871296c..61a1d2b71 100644
--- a/receiver/src/main.cpp
+++ b/receiver/src/main.cpp
@@ -1,13 +1,33 @@
 #include <iostream>
 #include "receiver.h"
 
+#include "receiver_config_factory.h"
+#include "receiver_config.h"
+
+
+hidra2::Error ReadConfigFile(int argc, char* argv[]) {
+    if (argc != 2) {
+        std::cerr << "Usage: " << argv[0] << " <config file>" << std::endl;
+        exit(EXIT_FAILURE);
+    }
+    hidra2::ReceiverConfigFactory factory;
+    return factory.SetConfigFromFile("fname");
+}
+
 int main (int argc, char* argv[]) {
+
+    auto err = ReadConfigFile(argc, argv);
+    if (err) {
+        std::cerr << "Cannot read config file: " << err << std::endl;
+        return 1;
+    }
+
+    auto config = hidra2::GetReceiverConfig();
+
     static const std::string address = "0.0.0.0:4200";
 
     auto* receiver = new hidra2::Receiver();
 
-    hidra2::Error err;
-
     std::cout << "Listening on " << address << std::endl;
     receiver->Listen(address, &err);
     if(err) {
diff --git a/receiver/src/receiver_config.cpp b/receiver/src/receiver_config.cpp
new file mode 100644
index 000000000..be8d221a2
--- /dev/null
+++ b/receiver/src/receiver_config.cpp
@@ -0,0 +1,23 @@
+#include "receiver_config.h"
+#include "receiver_config_factory.h"
+#include "io/io_factory.h"
+
+namespace hidra2 {
+
+ReceiverConfig config;
+
+ReceiverConfigFactory::ReceiverConfigFactory() : io__{GenerateDefaultIO()} {
+
+}
+
+Error ReceiverConfigFactory::SetConfigFromFile(std::string file_name) {
+    config.influxdb_uri = "localhost";
+    return {};
+}
+
+const ReceiverConfig*  GetReceiverConfig() {
+    return &config;
+}
+
+
+}
\ No newline at end of file
diff --git a/receiver/src/receiver_config.h b/receiver/src/receiver_config.h
new file mode 100644
index 000000000..38e5648b1
--- /dev/null
+++ b/receiver/src/receiver_config.h
@@ -0,0 +1,18 @@
+#ifndef HIDRA2_RECEIVER_CONFIG_H
+#define HIDRA2_RECEIVER_CONFIG_H
+
+#include "io/io.h"
+#include "common/error.h"
+
+namespace hidra2 {
+
+struct ReceiverConfig {
+    std::string influxdb_uri;
+};
+
+const ReceiverConfig* GetReceiverConfig();
+
+}
+
+
+#endif //HIDRA2_RECEIVER_CONFIG_H
diff --git a/receiver/src/receiver_config_factory.h b/receiver/src/receiver_config_factory.h
new file mode 100644
index 000000000..ea4087e85
--- /dev/null
+++ b/receiver/src/receiver_config_factory.h
@@ -0,0 +1,20 @@
+#ifndef HIDRA2_RECEIVER_CONFIG_FACTORY__H
+#define HIDRA2_RECEIVER_CONFIG_FACTORY__H
+
+#include "io/io.h"
+#include "common/error.h"
+
+namespace hidra2 {
+
+class ReceiverConfigFactory {
+  public:
+    ReceiverConfigFactory();
+    Error SetConfigFromFile(std::string file_name);
+  public:
+    std::unique_ptr<IO> io__;
+};
+
+}
+
+
+#endif //HIDRA2_RECEIVER_CONFIG_FACTORY__H
diff --git a/receiver/unittests/test_config.cpp b/receiver/unittests/test_config.cpp
new file mode 100644
index 000000000..fef17aa1f
--- /dev/null
+++ b/receiver/unittests/test_config.cpp
@@ -0,0 +1,52 @@
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+#include <unittests/MockIO.h>
+
+#include "../src/receiver_config.h"
+#include "../src/receiver_config_factory.h"
+
+using ::testing::Test;
+using ::testing::Return;
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::SetArgReferee;
+using ::testing::Gt;
+using ::testing::Eq;
+using ::testing::Ne;
+using ::testing::Mock;
+using ::testing::NiceMock;
+using ::testing::SaveArg;
+using ::testing::SaveArgPointee;
+using ::testing::InSequence;
+using ::testing::SetArgPointee;
+using ::hidra2::Error;
+using ::hidra2::ErrorInterface;
+using ::hidra2::FileDescriptor;
+using ::hidra2::SocketDescriptor;
+using ::hidra2::MockIO;
+
+using ::hidra2::ReceiverConfigFactory;
+
+
+namespace {
+
+
+class ConfigTests : public Test {
+  public:
+    MockIO mock_io;
+    ReceiverConfigFactory config_factory;
+    void SetUp() override {
+        config_factory.io__ = std::unique_ptr<hidra2::IO> {&mock_io};
+    }
+    void TearDown() override {
+        config_factory.io__.release();
+    }
+
+};
+
+
+TEST_F(ConfigTests, ErrorWaitForNewRequest) {
+
+}
+
+}
diff --git a/tests/automatic/producer_receiver/check_monitoring/check_linux.sh b/tests/automatic/producer_receiver/check_monitoring/check_linux.sh
index 7f7dbb65d..3b4b82ec5 100644
--- a/tests/automatic/producer_receiver/check_monitoring/check_linux.sh
+++ b/tests/automatic/producer_receiver/check_monitoring/check_linux.sh
@@ -16,7 +16,7 @@ Cleanup() {
 
 influx -execute "create database ${database_name}"
 
-nohup $2 &>/dev/null &
+nohup $2 receiver.json &>/dev/null &
 sleep 0.3
 receiverid=`echo $!`
 
diff --git a/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh b/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh
index f03eaad10..96b43f006 100644
--- a/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh
+++ b/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh
@@ -10,7 +10,7 @@ Cleanup() {
 	rm -rf files
 }
 
-nohup $2 &>/dev/null &
+nohup $2 receiver.json &>/dev/null &
 sleep 0.3
 receiverid=`echo $!`
 
diff --git a/tests/automatic/producer_receiver/transfer_single_file/check_windows.bat b/tests/automatic/producer_receiver/transfer_single_file/check_windows.bat
index c1f3a66fd..ee15242cd 100644
--- a/tests/automatic/producer_receiver/transfer_single_file/check_windows.bat
+++ b/tests/automatic/producer_receiver/transfer_single_file/check_windows.bat
@@ -1,7 +1,7 @@
 set full_recv_name="%2"
 set short_recv_name="%~nx2"
 
-start /B "" "%full_recv_name%"
+start /B "" "%full_recv_name%" receiver.json
 
 ping 1.0.0.0 -n 1 -w 100 > nul
 
-- 
GitLab