From 0c33319451deec9b5461b57856423bc619817245 Mon Sep 17 00:00:00 2001 From: Tobias Frust Date: Thu, 30 Jun 2016 15:22:07 +0200 Subject: added classes for detector; Sending out in n different streams with n different ports --- src/CMakeLists.txt | 3 +- src/Detector/Detector.cpp | 14 ++++++++ src/Detector/Detector.h | 20 +++++++++++ src/DetectorModule/DetectorModule.cpp | 62 +++++++++++++++++++++++++++++------ src/DetectorModule/DetectorModule.h | 14 ++------ src/UDPClient/UDPClient.cpp | 2 ++ src/UDPServer/UDPServer.cpp | 10 +++--- src/main_client.cpp | 25 ++++++++++++-- src/main_server.cpp | 56 ++++++++++++++++++++++++------- 9 files changed, 164 insertions(+), 42 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8e2b5f4..d77a039 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,6 +40,7 @@ set(SOURCES_CLIENT "${CMAKE_SOURCE_DIR}/ConfigReader/ConfigReader.cpp" "${CMAKE_SOURCE_DIR}/UDPClient/UDPClient.cpp" "${CMAKE_SOURCE_DIR}/DetectorModule/DetectorModule.cpp" + "${CMAKE_SOURCE_DIR}/Detector/Detector.cpp" "${CMAKE_SOURCE_DIR}/main_client.cpp" ) @@ -51,5 +52,5 @@ set(SOURCES_SERVER add_executable(onlineDetectorSimulatorServer ${SOURCES_SERVER}) add_executable(onlineDetectorSimulatorClient ${SOURCES_CLIENT}) target_link_libraries(onlineDetectorSimulatorClient ${LINK_LIBRARIES}) - +target_link_libraries(onlineDetectorSimulatorServer ${LINK_LIBRARIES}) diff --git a/src/Detector/Detector.cpp b/src/Detector/Detector.cpp index bc6c0da..658f485 100644 --- a/src/Detector/Detector.cpp +++ b/src/Detector/Detector.cpp @@ -8,5 +8,19 @@ */ +#include "Detector.h" +Detector::Detector(const std::string& address, const std::string& configPath, const unsigned int timeIntervall) : + timeIntervall_{timeIntervall}, + numberOfDetectorModules_{27} { + modules_.reserve(numberOfDetectorModules_); + for(auto i = 1; i <= numberOfDetectorModules_; i++){ + modules_.emplace_back(i, address, configPath); + } +} + +auto Detector::run() -> void { + for(auto i = 0; i < numberOfDetectorModules_; i++) + modules_[i].sendPeriodically(timeIntervall_); +} diff --git a/src/Detector/Detector.h b/src/Detector/Detector.h index 17acb8d..1969dbd 100644 --- a/src/Detector/Detector.h +++ b/src/Detector/Detector.h @@ -10,6 +10,26 @@ #ifndef DETECTOR_H_ #define DETECTOR_H_ +#include "../DetectorModule/DetectorModule.h" +#include +#include + +class Detector { +public: + Detector(const std::string& address, const std::string& configPath, const unsigned int timeIntervall); + + auto run() -> void; +private: + std::vector modules_; + + std::vector moduleThreads_; + + unsigned int timeIntervall_; + int numberOfDetectorModules_; + + auto readConfig(const std::string& configFile) -> bool; + +}; #endif /* DETECTOR_H_ */ diff --git a/src/DetectorModule/DetectorModule.cpp b/src/DetectorModule/DetectorModule.cpp index d2c8298..789ac0e 100644 --- a/src/DetectorModule/DetectorModule.cpp +++ b/src/DetectorModule/DetectorModule.cpp @@ -10,39 +10,81 @@ #include "DetectorModule.h" #include "../ConfigReader/ConfigReader.h" +#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::microseconds(interval)); + } + }).detach(); +} + DetectorModule::DetectorModule(const int detectorID, const std::string& address, const std::string& configPath) : detectorID_{detectorID}, numberOfDetectorsPerModule_{16}, index_{0}, - client_{address, detectorID + 4000} { + client_{address, detectorID+4000} { + + printf("Creating %d\n", detectorID); if (readConfig(configPath)) { throw std::runtime_error("DetectorModule: Configuration file could not be loaded successfully. Please check!"); } + sendBuffer_.resize(numberOfDetectorsPerModule_*numberOfProjections_*sizeof(unsigned short) + sizeof(std::size_t)); + //read the input data from the file corresponding to the detectorModuleID readInput(); + printf("Created %d\n", detectorID); +} + +auto DetectorModule::send() -> void{ + BOOST_LOG_TRIVIAL(debug) << "Detectormodule " << detectorID_ << " :sending udp packet with index " << index_ << "."; +// sendBuffer_[0] = (sizeof(std::size_t)) & 0xff; +// sendBuffer_[1] = (sizeof(std::size_t) >> 8) & 0xff; +// sendBuffer_[2] = (sizeof(std::size_t) >> 16) & 0xff; +// sendBuffer_[3] = (sizeof(std::size_t) >> 24) & 0xff; +// sendBuffer_[4] = (sizeof(std::size_t) >> 32) & 0xff; +// sendBuffer_[5] = (sizeof(std::size_t) >> 40) & 0xff; +// sendBuffer_[6] = (sizeof(std::size_t) >> 48) & 0xff; +// sendBuffer_[7] = (sizeof(std::size_t) >> 56) & 0xff; + *reinterpret_cast(sendBuffer_.data()) = index_; + std::copy(buffer_.cbegin(), buffer_.cbegin()+numberOfDetectorsPerModule_*numberOfProjections_, sendBuffer_.begin()+sizeof(std::size_t)); + client_.send(sendBuffer_.data(), sizeof(unsigned short)*numberOfDetectorsPerModule_*numberOfProjections_+sizeof(std::size_t)); + ++index_; } auto DetectorModule::sendPeriodically(unsigned int timeIntervall) -> void { - client_.send((char*)buffer_.data(), sizeof(unsigned short)*numberOfDetectorsPerModule_*numberOfProjections_); + std::function f = [=]() { + this->send(); + }; + timer_start(f, timeIntervall); } 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(unsigned short)); - input.read((char*) &buffer_[0], fileSize); + const std::string filePath = path_ + fileName_ + std::to_string(detectorID_) + fileEnding_; + BOOST_LOG_TRIVIAL(debug) << "DetectorModule: Path = " << filePath; + std::ifstream input(filePath, std::ios::in | std::ios::binary); + if(input){ + //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(unsigned short)); + input.read((char*) &buffer_[0], fileSize); + }else{ + throw std::runtime_error("File not found."); + } } auto DetectorModule::readConfig(const std::string& configFile) -> bool { diff --git a/src/DetectorModule/DetectorModule.h b/src/DetectorModule/DetectorModule.h index a1c2754..1bc36bb 100644 --- a/src/DetectorModule/DetectorModule.h +++ b/src/DetectorModule/DetectorModule.h @@ -19,16 +19,6 @@ #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(); -//} - class DetectorModule { public: DetectorModule(const int detectorID, const std::string& address, const std::string& configPath); @@ -37,6 +27,7 @@ public: private: std::vector buffer_; + std::vector sendBuffer_; int detectorID_; UDPClient client_; @@ -45,13 +36,14 @@ private: int numberOfPlanes_; int numberOfProjections_; int numberOfDetectorsPerModule_; - unsigned long long numberOfFrames_; + unsigned int numberOfFrames_; std::string path_, fileName_, fileEnding_; std::size_t index_; auto readConfig(const std::string& configFile) -> bool; auto readInput() -> void; + auto send() -> void; }; diff --git a/src/UDPClient/UDPClient.cpp b/src/UDPClient/UDPClient.cpp index 844b7f9..1d427ba 100644 --- a/src/UDPClient/UDPClient.cpp +++ b/src/UDPClient/UDPClient.cpp @@ -50,6 +50,7 @@ UDPClient::UDPClient(const std::string& addr, int port) : f_port(port) , f_addr(addr){ + printf("Creating client %d\n", f_port); char decimal_port[16]; snprintf(decimal_port, sizeof(decimal_port), "%d", f_port); decimal_port[sizeof(decimal_port) / sizeof(decimal_port[0]) - 1] = '\0'; @@ -69,6 +70,7 @@ freeaddrinfo(f_addrinfo); throw udp_client_server_runtime_error(("could not create socket for: \"" + addr + ":" + decimal_port + "\"").c_str()); } + printf("Created client %d\n", f_port); } /** \brief Clean up the UDP client object. diff --git a/src/UDPServer/UDPServer.cpp b/src/UDPServer/UDPServer.cpp index 854835a..3a50d0c 100644 --- a/src/UDPServer/UDPServer.cpp +++ b/src/UDPServer/UDPServer.cpp @@ -44,12 +44,12 @@ #include "UDPServer.h" - #include - #include +#include +#include - #ifndef SOCK_CLOEXEC - #define SOCK_CLOEXEC 0 - #endif +#ifndef SOCK_CLOEXEC +#define SOCK_CLOEXEC 0 +#endif UDPServer::UDPServer(const std::string& addr, int port) : f_port(port) diff --git a/src/main_client.cpp b/src/main_client.cpp index 451bd02..ed7d285 100644 --- a/src/main_client.cpp +++ b/src/main_client.cpp @@ -1,19 +1,38 @@ #include "UDPClient/UDPClient.h" #include "DetectorModule/DetectorModule.h" +#include "Detector/Detector.h" + +#include +#include +#include #include #include +void initLog() { +#ifndef NDEBUG + boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug); +#else + boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info); +#endif +} + int main (int argc, char *argv[]){ + initLog(); + std::cout << "Sending UDP packages: " << std::endl; auto configPath = std::string { "config.cfg" }; - std::string address = "10.0.0.10"; + std::string address = "localhost"; + + Detector detector{address, configPath, 1000u}; + + //DetectorModule detModule0 = DetectorModule(1, address, configPath); - DetectorModule detModule0 = DetectorModule(0, address, configPath); + detector.run(); - detModule0.sendPeriodically(5000u); + std::cin.ignore(); return 0; diff --git a/src/main_server.cpp b/src/main_server.cpp index 167dfca..a645f49 100644 --- a/src/main_server.cpp +++ b/src/main_server.cpp @@ -1,28 +1,60 @@ #include "UDPServer/UDPServer.h" +#include +#include +#include + #include #include +#include + +void initLog() { +#ifndef NDEBUG + boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug); +#else + boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info); +#endif +} + +void start(std::function func){ + std::thread([func]() { + while (true) + { + func(); + } + }).detach(); +} int main (int argc, char *argv[]){ - std::cout << "Receiving UDP packages: " << std::endl; + initLog(); + + std::string address = "localhost"; + int port = 4002; + + UDPServer server = UDPServer(address, port); - std::string address = "10.0.0.10"; - int port = 4000; + std::size_t length{32768}; - UDPServer server = UDPServer(address, port); + std::vector buf(16000); - std::size_t length{16*500}; + std::cout << "Receiving UDP packages: " << std::endl; - unsigned short buf[length]; +// for(auto i = 0; i < 27; i++){ +// std::function f = [=]() { +// server.recv(); +// }; +// start(); +// } - server.recv((char *)buf, length*sizeof(unsigned short)); + while(true){ + int bytes = server.recv((char*)buf.data(), length); + std::size_t index = *((std::size_t *)buf.data()); + if(index%1000 == 99) printf("%lu\n", index); - for(auto i = 0; i < length; i++){ - printf("%i ", buf[i]); - } - printf("\n"); + BOOST_LOG_TRIVIAL(debug) << "Server: Received " << bytes << " Bytes with Index " << index; + } - return 0; + return 0; } -- cgit v1.2.1