From 5680aa99001cb50c707c4187cd8ada0c41a573dd Mon Sep 17 00:00:00 2001 From: Tobias Frust Date: Thu, 30 Jun 2016 10:13:01 +0200 Subject: implemented virtual DetectorModule with simple send via udp --- src/CMakeLists.txt | 10 +++++- src/CMakeLists.txt~ | 13 ++++++-- src/ConfigReader/ConfigReader.cpp | 25 +++++++++++++++ src/ConfigReader/ConfigReader.h | 48 ++++++++++++++++++++++++++++ src/Detector/Detector.cpp | 12 +++++++ src/Detector/Detector.h | 15 +++++++++ src/DetectorModule/DetectorModule.cpp | 60 +++++++++++++++++++++++++++++++++++ src/DetectorModule/DetectorModule.h | 39 +++++++++++++++++++++-- src/main.cpp | 27 ---------------- src/main_client.cpp | 20 ++++++++++++ src/main_server.cpp | 12 +++---- 11 files changed, 243 insertions(+), 38 deletions(-) create mode 100644 src/ConfigReader/ConfigReader.cpp create mode 100644 src/ConfigReader/ConfigReader.h create mode 100644 src/Detector/Detector.cpp create mode 100644 src/Detector/Detector.h delete mode 100644 src/main.cpp create mode 100644 src/main_client.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 80196d9..8e2b5f4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,9 +31,16 @@ include_directories( ${BOOST_INCLUDE_DIRS} ) +set(LINK_LIBRARIES ${LINK_LIBRARIES} + ${LIBCONFIGPP_LIBRARY} + ${Boost_LIBRARIES} +) + set(SOURCES_CLIENT + "${CMAKE_SOURCE_DIR}/ConfigReader/ConfigReader.cpp" "${CMAKE_SOURCE_DIR}/UDPClient/UDPClient.cpp" - "${CMAKE_SOURCE_DIR}/main.cpp" + "${CMAKE_SOURCE_DIR}/DetectorModule/DetectorModule.cpp" + "${CMAKE_SOURCE_DIR}/main_client.cpp" ) set(SOURCES_SERVER @@ -43,5 +50,6 @@ set(SOURCES_SERVER add_executable(onlineDetectorSimulatorServer ${SOURCES_SERVER}) add_executable(onlineDetectorSimulatorClient ${SOURCES_CLIENT}) +target_link_libraries(onlineDetectorSimulatorClient ${LINK_LIBRARIES}) diff --git a/src/CMakeLists.txt~ b/src/CMakeLists.txt~ index 36ea910..80196d9 100644 --- a/src/CMakeLists.txt~ +++ b/src/CMakeLists.txt~ @@ -31,8 +31,17 @@ include_directories( ${BOOST_INCLUDE_DIRS} ) -file(GLOB SOURCES - "*.cpp" +set(SOURCES_CLIENT + "${CMAKE_SOURCE_DIR}/UDPClient/UDPClient.cpp" + "${CMAKE_SOURCE_DIR}/main.cpp" ) +set(SOURCES_SERVER + "${CMAKE_SOURCE_DIR}/UDPServer/UDPServer.cpp" + "${CMAKE_SOURCE_DIR}/main_server.cpp" +) + +add_executable(onlineDetectorSimulatorServer ${SOURCES_SERVER}) +add_executable(onlineDetectorSimulatorClient ${SOURCES_CLIENT}) + diff --git a/src/ConfigReader/ConfigReader.cpp b/src/ConfigReader/ConfigReader.cpp new file mode 100644 index 0000000..7589d6a --- /dev/null +++ b/src/ConfigReader/ConfigReader.cpp @@ -0,0 +1,25 @@ +/* + * ConfigReader.cpp + * + * Created on: 18.04.2016 + * Author: Tobias Frust (t.frust@hzdr.de) + */ + +#include +#include +#include + +#include "ConfigReader.h" + +ConfigReader::ConfigReader(const char* configFile) { + try { + cfg.readFile(configFile); + } catch (const libconfig::FileIOException &fioex) { + std::cerr << "I/O error while reading file." << std::endl; + exit(1); + } catch (const libconfig::ParseException &pex) { + std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() + << " - " << pex.getError() << std::endl; + exit(1); + } +} diff --git a/src/ConfigReader/ConfigReader.h b/src/ConfigReader/ConfigReader.h new file mode 100644 index 0000000..2819f41 --- /dev/null +++ b/src/ConfigReader/ConfigReader.h @@ -0,0 +1,48 @@ +/* + * Copyright 2016 Tobias Frust + * + * ConfigReader.h + * + * Created on: 18.04.2016 + * Author: Tobias Frust (t.frust@hzdr.de) + */ + +#ifndef CONFIGREADER_H +#define CONFIGREADER_H +#pragma once + +#include + +#include + +#include + +class ConfigReader { +public: + ConfigReader(const char* configFile); + ConfigReader(const ConfigReader& configReader) { + } + + template + bool lookupValue(const std::string& identifier, T& value) { + bool ret = cfg.lookupValue(identifier.c_str(), value); + BOOST_LOG_TRIVIAL(debug) << "Configuration value " << identifier << ": " << value; + return ret; + } + + template + bool lookupValue(const std::string& identifier, int index, T& value) { + libconfig::Setting& s = cfg.lookup(identifier.c_str()); + if(s.getLength() > index){ + value = s[index]; + BOOST_LOG_TRIVIAL(debug) << "Configuration value " << identifier << "[" << index << "]: " << value; + return true; + } + return false; + } + +private: + libconfig::Config cfg; +}; + +#endif diff --git a/src/Detector/Detector.cpp b/src/Detector/Detector.cpp new file mode 100644 index 0000000..bc6c0da --- /dev/null +++ b/src/Detector/Detector.cpp @@ -0,0 +1,12 @@ +/* + * Copyright 2016 Tobias Frust + * + * Detector.cpp + * + * Created on: 30.06.2016 + * Author: Tobias Frust + */ + + + + diff --git a/src/Detector/Detector.h b/src/Detector/Detector.h new file mode 100644 index 0000000..17acb8d --- /dev/null +++ b/src/Detector/Detector.h @@ -0,0 +1,15 @@ +/* + * Copyright 2016 Tobias Frust + * + * Detector.h + * + * Created on: 30.06.2016 + * Author: Tobias Frust + */ + +#ifndef DETECTOR_H_ +#define DETECTOR_H_ + + + +#endif /* DETECTOR_H_ */ diff --git a/src/DetectorModule/DetectorModule.cpp b/src/DetectorModule/DetectorModule.cpp index ac9ee27..f0c1a06 100644 --- a/src/DetectorModule/DetectorModule.cpp +++ b/src/DetectorModule/DetectorModule.cpp @@ -7,3 +7,63 @@ * Author: Tobias Frust */ +#include "DetectorModule.h" +#include "../ConfigReader/ConfigReader.h" + +#include +#include + +template +DetectorModule::DetectorModule(const int detectorID, const std::string& address, const std::string& configPath) : + detectorID_{detectorID}, + numberOfDetectorsPerModule_{16}, + index_{0}, + client_{address, detectorID + 4000} { + + if (readConfig(configPath)) { + throw std::runtime_error("DetectorModule: Configuration file could not be loaded successfully. Please check!"); + } + + //read the input data from the file corresponding to the detectorModuleID + readInput(); +} + +template +auto DetectorModule::sendPeriodically(unsigned int timeIntervall) -> void { + client_.send(buffer_.data(), sizeof(T)*numberOfDetectorsPerModule_*numberOfProjections_); +} + +template +auto DetectorModule::readInput() -> void { + if(path_.back() != '/') + path_.append("/"); + //open file + std::ifstream input(path_ + fileName_ + std::to_string(detectorID_) + fileEnding_, std::ios::in | std::ios::binary); + //allocate memory in vector + std::streampos fileSize; + input.seekg(0, std::ios::end); + fileSize = input.tellg(); + input.seekg(0, std::ios::beg); + buffer_.resize(fileSize / sizeof(T)); + input.read((char*) &buffer_[0], fileSize); +} + +template +auto DetectorModule::readConfig(const std::string& configFile) -> bool { + ConfigReader configReader = ConfigReader(configFile.data()); + int samplingRate, scanRate; + if (configReader.lookupValue("numberOfFanDetectors", numberOfDetectors_) + && configReader.lookupValue("dataInputPath", path_) + && configReader.lookupValue("dataFileName", fileName_) + && configReader.lookupValue("dataFileEnding", fileEnding_) + && configReader.lookupValue("numberOfPlanes", numberOfPlanes_) + && configReader.lookupValue("samplingRate", samplingRate) + && configReader.lookupValue("scanRate", scanRate) + && configReader.lookupValue("numberOfDataFrames", numberOfFrames_)) { + numberOfProjections_ = samplingRate * 1000000 / scanRate; + return EXIT_SUCCESS; + } + + return EXIT_FAILURE; + } + diff --git a/src/DetectorModule/DetectorModule.h b/src/DetectorModule/DetectorModule.h index aa63d82..de259fa 100644 --- a/src/DetectorModule/DetectorModule.h +++ b/src/DetectorModule/DetectorModule.h @@ -10,15 +10,50 @@ #ifndef DETECTORMODULE_H_ #define DETECTORMODULE_H_ +#include "../UDPClient/UDPClient.h" + #include +#include +#include +#include +#include + +void timer_start(std::function func, unsigned int interval){ + std::thread([func, interval]() { + while (true) + { + func(); + std::this_thread::sleep_for(std::chrono::milliseconds(interval)); + } + }).detach(); +} + template class DetectorModule { public: - DetectorModule(); + DetectorModule(const int detectorID, const std::string& address, const std::string& configPath); + + auto sendPeriodically(unsigned int timeIntervall) -> void; private: - std::vector> buffer_; + std::vector buffer_; + + int detectorID_; + UDPClient client_; + + int numberOfDetectors_; + int numberOfPlanes_; + int numberOfProjections_; + int numberOfDetectorsPerModule_; + std::size_t numberOfFrames_; + std::string path_, fileName_, fileEnding_; + + std::size_t index_; + + auto readConfig(const std::string& configFile) -> bool; + auto readInput() -> void; + }; #endif diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 6898a09..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "UDPClient/UDPClient.h" - -#include -#include - -int main (int argc, char *argv[]){ - - std::cout << "Sending UDP packages: " << std::endl; - - std::string address = "192.168.178.33"; - int port = 1234; - - UDPClient client = UDPClient(address, port); - - std::size_t length{8008}; - - char buf[length]; - for(auto i = 0; i < length; i++){ - *(buf+i) = i%128; - } - - - client.send(buf, length); - - return 0; - -} diff --git a/src/main_client.cpp b/src/main_client.cpp new file mode 100644 index 0000000..363f610 --- /dev/null +++ b/src/main_client.cpp @@ -0,0 +1,20 @@ +#include "UDPClient/UDPClient.h" +#include "DetectorModule/DetectorModule.h" + +#include +#include + +int main (int argc, char *argv[]){ + + std::cout << "Sending UDP packages: " << std::endl; + + auto configPath = std::string { "config.cfg" }; + std::string address = "10.0.0.10"; + + DetectorModule detModule0 = DetectorModule(0, address, configPath); + + detModule0.sendPeriodically(5000); + + return 0; + +} diff --git a/src/main_server.cpp b/src/main_server.cpp index b90de52..267023f 100644 --- a/src/main_server.cpp +++ b/src/main_server.cpp @@ -7,19 +7,19 @@ int main (int argc, char *argv[]){ std::cout << "Receiving UDP packages: " << std::endl; - std::string address = "localhost"; - int port = 1234; + std::string address = "10.0.0.10"; + int port = 4000; UDPServer server = UDPServer(address, port); - std::size_t length{8000}; + std::size_t length{16*500}; - char buf[length]; + unsigned short buf[length]; - server.recv(buf, length); + server.recv(buf, length*sizeof(unsigned short)); for(auto i = 0; i < length; i++){ - printf("%c", buf[i]); + printf("%i ", buf[i]); } printf("\n"); -- cgit v1.2.1