summaryrefslogtreecommitdiffstats
path: root/src/DetectorModule/DetectorModule.cpp
blob: 6767eb98a42bfdcd5d0efdab9b5ac4b63dce9150 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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(int)> func, unsigned int interval, unsigned int max_packets){
    std::thread([func, interval, max_packets]() {
	int packets = 1;
	auto next = std::chrono::high_resolution_clock::now();
        while (true)
        {
            func(packets);

            next += std::chrono::microseconds(packets * interval);
            auto now = std::chrono::high_resolution_clock::now();
            if (now > next) {
        	std::chrono::nanoseconds late = now - next;
        	packets = 1 + (late.count() / interval / 1000);
        	if (packets > max_packets)
        	    packets = max_packets;
            } else {
        	packets = 1;
            }

	    std::this_thread::sleep_until(next);
//            std::this_thread::sleep_for(std::chrono::microseconds(interval));
        }
    }).detach();
}

DetectorModule::DetectorModule(const int detectorID, const std::string& address, const int port, const std::string& configPath) :
   detectorID_{detectorID},
   numberOfDetectorsPerModule_{16},
   index_{0u},
   client_{address, port},
   max_packets_{1000u}{

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

   sendBuffer_.resize(max_packets_);
   for(auto &it: sendBuffer_) {
      it.resize(numberOfProjectionsPerPacket_ * numberOfDetectorsPerModule_ * sizeof(unsigned short) + sizeof(size_t) + sizeof(short int));
   }

   //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(int packets = 1) -> void{
   BOOST_LOG_TRIVIAL(debug) << "Detectormodule " << detectorID_ << " :sending udp packet with index " << index_ << ".";
   int numberOfParts = numberOfProjections_/numberOfProjectionsPerPacket_;
//   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;


    struct mmsghdr msg[packets];
    struct iovec msgvec[packets];

    unsigned int hdrSize = sizeof(size_t) + sizeof(short int);
    unsigned int sinoSize = numberOfDetectorsPerModule_ * numberOfProjectionsPerPacket_;

    memset(msg, 0, sizeof(msg));
    memset(msgvec, 0, sizeof(msgvec));
    for (int i = 0; i < packets; i++) {
	unsigned int bufferSizeIndex = index_ % 1000;
	
	char *ptr = sendBuffer_[i].data();

	msgvec[i].iov_base = sendBuffer_[i].data();
	msgvec[i].iov_len = sendBuffer_[i].size();
	msg[i].msg_hdr.msg_iov = &msgvec[i];
	msg[i].msg_hdr.msg_iovlen = 1;
	

	*reinterpret_cast<size_t*>(ptr) = index_ * numberOfParts + partID_;
	*reinterpret_cast<unsigned short*>(ptr + sizeof(size_t)) = partID_;
	memcpy(ptr + hdrSize, buffer_.data() + sinoSize * (bufferSizeIndex * numberOfParts + partID_), sinoSize * sizeof(unsigned short));

	partID_ = (partID_ + 1) % numberOfParts;
	if (partID_ == 0) ++index_;
    }

    client_.msend(packets, msg);
    
   auto ts = std::chrono::high_resolution_clock::now();
   std::chrono::nanoseconds d = ts - ts_;
    counter_ += packets;
   if (d.count() >= 1000000000) {
	printf("Packets %i (%zu bytes, %.3lf GBit/s) in %.3lf ms\n", counter_, sendBuffer_[0].size(), 8. * counter_ * sendBuffer_[0].size() / 1024 / 1024 / 1024, 1. * d.count() / 1000000);
	counter_ = 0;
	ts_ = ts;
   }
}

auto DetectorModule::sendPeriodically(unsigned int timeIntervall) -> void {
   counter_ = 0;
   ips_ = 1000000. / ((double)timeIntervall);
   ts_ = std::chrono::high_resolution_clock::now();

   std::function<void(int)> f = [=](int packets = 1) {
      this->send(packets);
   };
   timer_start(f, timeIntervall, max_packets_);
}

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_)
        && configReader.lookupValue("numberOfProjectionsPerPacket", numberOfProjectionsPerPacket_)
        && configReader.lookupValue("numberOfDetectorsPerModule", numberOfDetectorsPerModule_)) {
     numberOfProjections_ = samplingRate * 1000000 / scanRate;
     return EXIT_SUCCESS;
  }

  return EXIT_FAILURE;
}