summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Frust <tobiasfrust@gmail.com>2016-10-13 10:27:01 +0200
committerGitHub <noreply@github.com>2016-10-13 10:27:01 +0200
commita38a5f1b646cdc992c3b602330a41036bc9bf7b1 (patch)
tree7fc93e5f5c3729d0fa013946fe60fa881a53579c
parent8af3d595e2856f81a46a91d67e96f53cb3b25d0f (diff)
parentcdab7a0b05f655a2f98f00f3fb8928e54b8c28d2 (diff)
downloadods-a38a5f1b646cdc992c3b602330a41036bc9bf7b1.tar.gz
ods-a38a5f1b646cdc992c3b602330a41036bc9bf7b1.tar.bz2
ods-a38a5f1b646cdc992c3b602330a41036bc9bf7b1.tar.xz
ods-a38a5f1b646cdc992c3b602330a41036bc9bf7b1.zip
Merge pull request #2 from tobiasfrust/master
Merge of the DetectorSimulator
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/CMakeLists.txt~14
-rw-r--r--src/Detector/Detector.cpp4
-rw-r--r--src/Detector/Detector.h2
-rw-r--r--src/DetectorModule/DetectorModule.cpp30
-rw-r--r--src/DetectorModule/DetectorModule.h6
-rw-r--r--src/ReceiverThreads/ReceiverThreads.cpp49
-rw-r--r--src/ReceiverThreads/ReceiverThreads.h35
-rw-r--r--src/UDPServer/UDPServer.cpp26
-rw-r--r--src/main_client.cpp10
-rw-r--r--src/main_server.cpp33
11 files changed, 156 insertions, 54 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d77a039..d4ee49d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -45,6 +45,7 @@ set(SOURCES_CLIENT
)
set(SOURCES_SERVER
+ "${CMAKE_SOURCE_DIR}/ReceiverThreads/ReceiverThreads.cpp"
"${CMAKE_SOURCE_DIR}/UDPServer/UDPServer.cpp"
"${CMAKE_SOURCE_DIR}/main_server.cpp"
)
diff --git a/src/CMakeLists.txt~ b/src/CMakeLists.txt~
index 80196d9..ebe2f50 100644
--- a/src/CMakeLists.txt~
+++ b/src/CMakeLists.txt~
@@ -31,9 +31,18 @@ include_directories(
${BOOST_INCLUDE_DIRS}
)
+set(LINK_LIBRARIES ${LINK_LIBRARIES}
+ ${LIBCONFIGPP_LIBRARY}
+ ${Boost_LIBRARIES}
+)
+
set(SOURCES_CLIENT
+ "${CMAKE_SOURCE_DIR}/ConfigReader/ConfigReader.cpp"
"${CMAKE_SOURCE_DIR}/UDPClient/UDPClient.cpp"
- "${CMAKE_SOURCE_DIR}/main.cpp"
+ "${CMAKE_SOURCE_DIR}/DetectorModule/DetectorModule.cpp"
+ "${CMAKE_SOURCE_DIR}/Detector/Detector.cpp"
+ "${CMAKE_SOURCE_DIR}/main_client.cpp"
+ "${CMAKE_SOURCE_DIR}/ReceiverThreads/ReceiverThreads.cpp"
)
set(SOURCES_SERVER
@@ -43,5 +52,6 @@ set(SOURCES_SERVER
add_executable(onlineDetectorSimulatorServer ${SOURCES_SERVER})
add_executable(onlineDetectorSimulatorClient ${SOURCES_CLIENT})
-
+target_link_libraries(onlineDetectorSimulatorClient ${LINK_LIBRARIES})
+target_link_libraries(onlineDetectorSimulatorServer ${LINK_LIBRARIES})
diff --git a/src/Detector/Detector.cpp b/src/Detector/Detector.cpp
index 5dde6d1..03abffe 100644
--- a/src/Detector/Detector.cpp
+++ b/src/Detector/Detector.cpp
@@ -10,12 +10,12 @@
#include "Detector.h"
-Detector::Detector(const std::string& address, const std::string& configPath, const unsigned int timeIntervall) :
+Detector::Detector(const std::string& address, const std::string& configPath, const unsigned int timeIntervall, const int packetSize) :
timeIntervall_{timeIntervall},
numberOfDetectorModules_{27} {
modules_.reserve(numberOfDetectorModules_);
for(auto i = 0; i < numberOfDetectorModules_; i++){
- modules_.emplace_back(i, address, configPath);
+ modules_.emplace_back(i, address, configPath, packetSize);
}
}
diff --git a/src/Detector/Detector.h b/src/Detector/Detector.h
index 1969dbd..16f946a 100644
--- a/src/Detector/Detector.h
+++ b/src/Detector/Detector.h
@@ -17,7 +17,7 @@
class Detector {
public:
- Detector(const std::string& address, const std::string& configPath, const unsigned int timeIntervall);
+ Detector(const std::string& address, const std::string& configPath, const unsigned int timeIntervall, const int packetSize);
auto run() -> void;
private:
diff --git a/src/DetectorModule/DetectorModule.cpp b/src/DetectorModule/DetectorModule.cpp
index bee50e9..8193a03 100644
--- a/src/DetectorModule/DetectorModule.cpp
+++ b/src/DetectorModule/DetectorModule.cpp
@@ -25,11 +25,12 @@ void timer_start(std::function<void(void)> func, unsigned int interval){
}).detach();
}
-DetectorModule::DetectorModule(const int detectorID, const std::string& address, const std::string& configPath) :
+DetectorModule::DetectorModule(const int detectorID, const std::string& address, const std::string& configPath, const int packetSize) :
detectorID_{detectorID},
numberOfDetectorsPerModule_{16},
- index_{0},
- client_{address, detectorID+4000} {
+ index_{0u},
+ client_{address, detectorID+4000},
+ packetSize_{packetSize} {
printf("Creating %d\n", detectorID);
@@ -37,15 +38,18 @@ DetectorModule::DetectorModule(const int detectorID, const std::string& address,
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));
+ sendBuffer_.resize(numberOfProjectionsPerPacket_*numberOfDetectorsPerModule_+sizeof(std::size_t)+sizeof(unsigned short));
//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_ << ".";
+ 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;
@@ -55,11 +59,15 @@ auto DetectorModule::send() -> void{
// 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(), sizeof(unsigned short)*numberOfDetectorsPerModule_*numberOfProjections_+sizeof(std::size_t));
- ++index_;
+ unsigned int sinoSize = numberOfDetectorsPerModule_*numberOfProjectionsPerPacket_;
+ *reinterpret_cast<int*>(sendBuffer_.data()) = index_;
+ *reinterpret_cast<unsigned short*>(sendBuffer_.data()+sizeof(std::size_t)) = partID_;
+ std::copy(((char*)buffer_.data())+sinoSize*(bufferSizeIndex*numberOfParts+partID_)*sizeof(unsigned short), ((char*)buffer_.data())+(sinoSize*(1+bufferSizeIndex*numberOfParts+partID_))*sizeof(unsigned short), sendBuffer_.begin()+sizeof(std::size_t)+sizeof(unsigned short));
+ BOOST_LOG_TRIVIAL(debug) << "INDEX: " << (bufferSizeIndex*numberOfParts+partID_);
+ client_.send(sendBuffer_.data(), sendBuffer_.size());
+ partID_ = (partID_+1) % numberOfParts;
+ if(partID_ == 0)
+ ++index_;
}
auto DetectorModule::sendPeriodically(unsigned int timeIntervall) -> void {
@@ -99,7 +107,9 @@ auto DetectorModule::readConfig(const std::string& configFile) -> bool {
&& configReader.lookupValue("numberOfPlanes", numberOfPlanes_)
&& configReader.lookupValue("samplingRate", samplingRate)
&& configReader.lookupValue("scanRate", scanRate)
- && configReader.lookupValue("numberOfDataFrames", numberOfFrames_)) {
+ && configReader.lookupValue("numberOfDataFrames", numberOfFrames_)
+ && configReader.lookupValue("numberOfProjectionsPerPacket", numberOfProjectionsPerPacket_)
+ && configReader.lookupValue("numberOfDetectorsPerModule", numberOfProjectionsPerPacket_)) {
numberOfProjections_ = samplingRate * 1000000 / scanRate;
return EXIT_SUCCESS;
}
diff --git a/src/DetectorModule/DetectorModule.h b/src/DetectorModule/DetectorModule.h
index 1bc36bb..3ca5f23 100644
--- a/src/DetectorModule/DetectorModule.h
+++ b/src/DetectorModule/DetectorModule.h
@@ -21,7 +21,7 @@
class DetectorModule {
public:
- DetectorModule(const int detectorID, const std::string& address, const std::string& configPath);
+ DetectorModule(const int detectorID, const std::string& address, const std::string& configPath, const int packetSize);
auto sendPeriodically(unsigned int timeIntervall) -> void;
@@ -32,14 +32,18 @@ private:
int detectorID_;
UDPClient client_;
+ int packetSize_;
+
int numberOfDetectors_;
int numberOfPlanes_;
int numberOfProjections_;
int numberOfDetectorsPerModule_;
+ int numberOfProjectionsPerPacket_;
unsigned int numberOfFrames_;
std::string path_, fileName_, fileEnding_;
std::size_t index_;
+ unsigned short partID_{0};
auto readConfig(const std::string& configFile) -> bool;
auto readInput() -> void;
diff --git a/src/ReceiverThreads/ReceiverThreads.cpp b/src/ReceiverThreads/ReceiverThreads.cpp
new file mode 100644
index 0000000..3d22c66
--- /dev/null
+++ b/src/ReceiverThreads/ReceiverThreads.cpp
@@ -0,0 +1,49 @@
+/*
+ * 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(33000);
+ 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(), 65536, timeIntervall_);
+ if(bytes < 0){
+ break;
+ }
+ BOOST_LOG_TRIVIAL(debug) << "Received " << bytes << " Bytes.";
+ std::size_t index = *((std::size_t *)buf.data());
+ int diff = index - lastIndex - 1;
+ if(diff > 0){
+ loss_ += diff;
+ BOOST_LOG_TRIVIAL(debug) << "Packet loss or wrong order! new: " << index << " old: " << lastIndex;
+ }
+ lastIndex = index;
+ }
+ BOOST_LOG_TRIVIAL(info) << "Lost " << loss_ << " from " << lastIndex << " packets; (" << loss_/(double)lastIndex*100.0 << "%)";
+}
+
diff --git a/src/ReceiverThreads/ReceiverThreads.h b/src/ReceiverThreads/ReceiverThreads.h
new file mode 100644
index 0000000..7cb04c0
--- /dev/null
+++ b/src/ReceiverThreads/ReceiverThreads.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2016
+ *
+ * ReceiverThreads.h
+ *
+ * Created on: 21.07.2016
+ * Author: Tobias Frust
+ */
+
+#ifndef RECEIVERTHREADS_H_
+#define RECEIVERTHREADS_H_
+
+#include <vector>
+#include <thread>
+
+class ReceiverThreads {
+public:
+ ReceiverThreads(const std::string& address, const int timeIntervall, const int numberOfDetectorModules);
+
+ auto run() -> void;
+private:
+ auto receiverThread(const int port) -> void;
+
+ std::vector<std::thread> receiverModules_;
+
+ std::size_t loss_;
+
+ int timeIntervall_;
+ int numberOfDetectorModules_;
+
+ std::string address_;
+
+};
+
+#endif /* RECEIVERTHREADS_H_ */
diff --git a/src/UDPServer/UDPServer.cpp b/src/UDPServer/UDPServer.cpp
index 3a50d0c..8c9decf 100644
--- a/src/UDPServer/UDPServer.cpp
+++ b/src/UDPServer/UDPServer.cpp
@@ -168,31 +168,19 @@ int UDPServer::recv(char *msg, size_t max_size)
*
* \param[in] msg The buffer where the message will be saved.
* \param[in] max_size The size of the \p msg buffer in bytes.
- * \param[in] max_wait_ms The maximum number of milliseconds to wait for a message.
+ * \param[in] max_wait_s The maximum number of seconds to wait for a message.
*
* \return -1 if an error occurs or the function timed out, the number of bytes received otherwise.
*/
-int UDPServer::timed_recv(char *msg, size_t max_size, int max_wait_ms)
+int UDPServer::timed_recv(char *msg, size_t max_size, int max_wait_s)
{
fd_set s;
FD_ZERO(&s);
FD_SET(f_socket, &s);
struct timeval timeout;
- timeout.tv_sec = max_wait_ms / 1000;
- timeout.tv_usec = (max_wait_ms % 1000) * 1000;
- int retval = select(f_socket + 1, &s, &s, &s, &timeout);
- if(retval == -1)
- {
- // select() set errno accordingly
- return -1;
- }
- if(retval > 0)
- {
- // our socket has data
- return ::recv(f_socket, msg, max_size, 0);
- }
-
- // our socket has no data
- errno = EAGAIN;
- return -1;
+ timeout.tv_sec = max_wait_s;
+ timeout.tv_usec = 0;
+ setsockopt(f_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(struct timeval));
+ // our socket has data
+ return ::recv(f_socket, msg, max_size, 0);
}
diff --git a/src/main_client.cpp b/src/main_client.cpp
index 2b9927d..e97e911 100644
--- a/src/main_client.cpp
+++ b/src/main_client.cpp
@@ -19,13 +19,15 @@ void initLog() {
int main (int argc, char *argv[]){
- if(argc < 2){
- BOOST_LOG_TRIVIAL(error) << "Program usage: ./onlineDetectorSimulatorClient <images_per_second>!";
+ if(argc < 3){
+ BOOST_LOG_TRIVIAL(error) << "Program usage: ./onlineDetectorSimulatorClient <images_per_second> <packet_size_in_byte>!";
return 0;
}
int imagesPerSec = std::stoi(argv[1]);
+ int packetSize = std::stoi(argv[2]);
+
double timegap = 1./(double)imagesPerSec;
unsigned int intervall = timegap*1000*1000;
@@ -34,9 +36,9 @@ int main (int argc, char *argv[]){
std::cout << "Sending UDP packages: " << std::endl;
auto configPath = std::string { "config.cfg" };
- std::string address = "127.0.0.1";
+ std::string address = "10.0.0.10";
- Detector detector{address, configPath, intervall};
+ Detector detector{address, configPath, intervall, packetSize};
//DetectorModule detModule0 = DetectorModule(1, address, configPath);
diff --git a/src/main_server.cpp b/src/main_server.cpp
index 6e936e4..90b3835 100644
--- a/src/main_server.cpp
+++ b/src/main_server.cpp
@@ -1,4 +1,5 @@
#include "UDPServer/UDPServer.h"
+#include "ReceiverThreads/ReceiverThreads.h"
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
@@ -30,9 +31,9 @@ int main (int argc, char *argv[]){
initLog();
std::string address = "localhost";
- int port = 4002;
-
- UDPServer server = UDPServer(address, port);
+// int port = 4002;
+//
+// UDPServer server = UDPServer(address, port);
std::size_t length{32768};
std::size_t lastIndex{0};
@@ -41,6 +42,8 @@ int main (int argc, char *argv[]){
std::cout << "Receiving UDP packages: " << std::endl;
+ ReceiverThreads(address, 10, 27);
+
// for(auto i = 0; i < 27; i++){
// std::function<void(void)> f = [=]() {
// server.recv();
@@ -48,18 +51,18 @@ int main (int argc, char *argv[]){
// start();
// }
- while(true){
- int bytes = server.recv((char*)buf.data(), length);
- std::size_t index = *((std::size_t *)buf.data());
- if(index%1000 == 99) printf("%lu\n", index);
-
- if(lastIndex != (index-1))
- BOOST_LOG_TRIVIAL(warning) << "Packet loss or wrong order!";
-
- lastIndex = index;
-
- BOOST_LOG_TRIVIAL(debug) << "Server: Received " << bytes << " Bytes with Index " << index;
- }
+// while(true){
+// int bytes = server.recv((char*)buf.data(), length);
+// std::size_t index = *((std::size_t *)buf.data());
+// if(index%1000 == 99) printf("%lu\n", index);
+//
+// if(lastIndex != (index-1))
+// BOOST_LOG_TRIVIAL(warning) << "Packet loss or wrong order!";
+//
+// lastIndex = index;
+//
+// BOOST_LOG_TRIVIAL(debug) << "Server: Received " << bytes << " Bytes with Index " << index;
+// }
return 0;