summaryrefslogtreecommitdiffstats
path: root/src/ReceiverThreads/ReceiverThreads.cpp
blob: 6f389f2325d35f4b086bfdc6150a564af68119d8 (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
/*
 * Copyright 2016
 * 
 * ReceiverThreads.cpp
 *
 *  Created on: 21.07.2016
 *      Author: Tobias Frust
 */

#include "ReceiverThreads.h"
#include "../UDPServer/UDPServer.h"

#include <boost/log/trivial.hpp>

ReceiverThreads::ReceiverThreads(const std::string& address, const int timeIntervall, const int numberOfDetectorModules)
   : timeIntervall_{timeIntervall}, numberOfDetectorModules_{numberOfDetectorModules}, address_{address}, loss_{0} {

   for(auto i = 0; i < numberOfDetectorModules; i++){
      receiverModules_.emplace_back(&ReceiverThreads::receiverThread, this, 4000+i);
   }

   for(auto i = 0; i < numberOfDetectorModules; i++){
      receiverModules_[i].join();
   }

}

auto ReceiverThreads::receiverThread(const int port) -> void {
   UDPServer server = UDPServer(address_, port);
   std::vector<unsigned short> buf(16000);
   std::size_t lastIndex{0};
   BOOST_LOG_TRIVIAL(info) << "Address: " << address_ << " port: " << port << " timeout: " << timeIntervall_;
   while(true){
      int bytes = server.timed_recv((char*)buf.data(), buf.size()*sizeof(unsigned short), timeIntervall_);
      if(bytes < 0){
         break;
      }
      std::size_t index = *((std::size_t *)buf.data());
      int diff = index - lastIndex - 1;
      if(diff > 0){
         BOOST_LOG_TRIVIAL(warning) << "Packet loss or wrong order! new: " << index << " old: " << lastIndex;
      }
      loss_ += diff;
      lastIndex = index;
   }
   BOOST_LOG_TRIVIAL(info) << "Lost " << loss_ << " from " << lastIndex << " packets; (" << loss_/(double)lastIndex << "%)";
}