summaryrefslogtreecommitdiffstats
path: root/src/DetectorModule/DetectorModule.cpp
blob: b222341b190f3474963313ed3a9e1c0c7a5479d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright 2016 Tobias Frust
 *
 * DetectorModule.cpp
 *
 *  Created on: 29.06.2016
 *      Author: Tobias Frust
 */

#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, const int packetSize) :
   detectorID_{detectorID},
   numberOfDetectorsPerModule_{16},
   index_{1},
   client_{address, detectorID+4000},
   packetSize_{packetSize} {

      printf("Creating %d\n", detectorID);

   if (readConfig(configPath)) {
      throw std::runtime_error("DetectorModule: Configuration file could not be loaded successfully. Please check!");
   }

   sendBuffer_.resize(packetSize);

   //read the input data from the file corresponding to the detectorModuleID
   readInput();
   //unsigned int sinoSize = numberOfDetectorsPerModule_*numberOfProjections_;
   //std::copy(((char*)buffer_.data()), ((char*)buffer_.data())+sinoSize*sizeof(unsigned short), sendBuffer_.begin()+sizeof(std::size_t));
   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<int*>(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(), sendBuffer_.size());
   ++index_;
}

auto DetectorModule::sendPeriodically(unsigned int timeIntervall) -> void {
   std::function<void(void)> 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;
}