/* * Copyright 2016 Tobias Frust * * DetectorModule.cpp * * Created on: 29.06.2016 * Author: Tobias Frust */ #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_{1}, 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; unsigned int bufferSizeIndex = index_ % 1000; unsigned int sinoSize = numberOfDetectorsPerModule_*numberOfProjections_; *reinterpret_cast(sendBuffer_.data()) = index_; std::copy(((char*)buffer_.data())+sinoSize*bufferSizeIndex*sizeof(unsigned short), ((char*)buffer_.data())+(sinoSize*(1+bufferSizeIndex))*sizeof(unsigned short), 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 { std::function f = [=]() { this->send(); }; timer_start(f, timeIntervall); } auto DetectorModule::readInput() -> void { if(path_.back() != '/') path_.append("/"); //open file const std::string filePath = path_ + fileName_ + std::to_string(detectorID_+1) + 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 { 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; }