diff options
Diffstat (limited to 'src/DetectorModule')
-rw-r--r-- | src/DetectorModule/DetectorModule.cpp | 62 | ||||
-rw-r--r-- | src/DetectorModule/DetectorModule.h | 14 |
2 files changed, 55 insertions, 21 deletions
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 <boost/log/trivial.hpp> + #include <exception> #include <fstream> +void timer_start(std::function<void(void)> 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<int*>(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<void(void)> 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 <thread> #include <functional> -//void timer_start(std::function<void(void)> 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<unsigned short> buffer_; + std::vector<char> 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; }; |