From caef8c111cc6a8198c503648a8cdae634f35e213 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Fri, 31 Jul 2020 11:05:39 +0200 Subject: [PATCH] added overload to send vector --- slsDetectorSoftware/src/Module.cpp | 50 ++++++++++++------------------ slsSupportLib/include/DataSocket.h | 16 +++++++++- slsSupportLib/include/TypeTraits.h | 25 ++++++++++----- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index e56918755..1417efc7f 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -1399,18 +1399,16 @@ void Module::sendVetoPhoton(const int chipIndex, const std::vector& gainInd } LOG(logDEBUG1) << "Sending veto photon/file to detector [chip:" << chipIndex << ", nch:" << nch << "]"; - int fnum = F_SET_VETO_PHOTON; - int ret = FAIL; - int args[]{chipIndex, nch}; + + const int args[]{chipIndex, nch}; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); - client.Send(&fnum, sizeof(fnum)); - client.Send(args, sizeof(args)); + client.Send(F_SET_VETO_PHOTON); + client.Send(args); client.Send(gainIndices.data(), sizeof(gainIndices[0]) * nch); client.Send(values.data(), sizeof(values[0]) * nch); - client.Send(gainIndices.data(), sizeof(int) * nch); - client.Send(values.data(), sizeof(int) * nch); - client.Receive(&ret, sizeof(ret)); - if (ret == FAIL) { + client.Send(gainIndices.data(), sizeof(gainIndices[0]) * nch); + client.Send(values.data(), sizeof(values[0]) * nch); + if (client.Receive() == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); throw RuntimeError("Detector " + std::to_string(moduleId) + @@ -1421,13 +1419,10 @@ void Module::sendVetoPhoton(const int chipIndex, const std::vector& gainInd void Module::getVetoPhoton(const int chipIndex, const std::string &fname) const { LOG(logDEBUG1) << "Getting veto photon [" << chipIndex << "]\n"; - int fnum = F_GET_VETO_PHOTON; - int ret = FAIL; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); - client.Send(&fnum, sizeof(fnum)); - client.Send(&chipIndex, sizeof(chipIndex)); - client.Receive(&ret, sizeof(ret)); - if (ret == FAIL) { + client.Send(F_GET_VETO_PHOTON); + client.Send(chipIndex); + if (client.Receive() == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); throw RuntimeError("Detector " + std::to_string(moduleId) + @@ -1484,7 +1479,7 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons, } LOG(logDEBUG1) << "Setting veto photon. Reading Gain values from file"; - int totalEnergy = numPhotons * energy; + const int totalEnergy = numPhotons * energy; std::vector gainIndices; std::vector values; @@ -1512,7 +1507,7 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons, std::to_string(gainIndices.size())); } - // caluclate gain index from gain thresholds and threhsold energy + // caluclate gain index from gain thresholds and threshold energy int gainIndex = 2; if (totalEnergy < gainThreshold[0]) { gainIndex = 0; @@ -1665,20 +1660,16 @@ void Module::setADCConfiguration(const int chipIndex, const int adcIndex, void Module::getBadChannels(const std::string &fname) const { LOG(logDEBUG1) << "Getting bad channels to " << fname; - int fnum = F_GET_BAD_CHANNELS; - int ret = FAIL; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); - client.Send(&fnum, sizeof(fnum)); - client.Receive(&ret, sizeof(ret)); - if (ret == FAIL) { + client.Send(F_GET_BAD_CHANNELS); + if (client.Receive() == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); throw RuntimeError("Detector " + std::to_string(moduleId) + " returned error: " + std::string(mess)); } // receive badchannels - int nch = -1; - client.Receive(&nch, sizeof(nch)); + auto nch = client.Receive(); std::vector badchannels(nch); if (nch > 0) { client.Receive(badchannels.data(), @@ -1690,7 +1681,7 @@ void Module::getBadChannels(const std::string &fname) const { // save to file std::ofstream outfile(fname); - if (!outfile.is_open()) { + if (!outfile) { throw RuntimeError("Could not create file to save bad channels"); } for (auto ch : badchannels) @@ -1701,7 +1692,7 @@ void Module::getBadChannels(const std::string &fname) const { void Module::setBadChannels(const std::string &fname) { // read bad channels file std::ifstream input_file(fname); - if (!input_file.is_open()) { + if (!input_file) { throw RuntimeError("Could not open bad channels file " + fname + " for reading"); } @@ -1724,17 +1715,16 @@ void Module::setBadChannels(const std::string &fname) { // send bad channels to module int fnum = F_SET_BAD_CHANNELS; - int ret = FAIL; int nch = badchannels.size(); LOG(logDEBUG1) << "Sending bad channels to detector, nch:" << nch; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); client.Send(&fnum, sizeof(fnum)); client.Send(&nch, sizeof(nch)); if (nch > 0) { - client.Send(badchannels.data(), sizeof(int) * nch); + // client.Send(badchannels.data(), sizeof(badchannels[0]) * nch); + client.Send(badchannels); } - client.Receive(&ret, sizeof(ret)); - if (ret == FAIL) { + if (client.Receive() == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); throw RuntimeError("Detector " + std::to_string(moduleId) + diff --git a/slsSupportLib/include/DataSocket.h b/slsSupportLib/include/DataSocket.h index ffe79030d..825eb83f5 100644 --- a/slsSupportLib/include/DataSocket.h +++ b/slsSupportLib/include/DataSocket.h @@ -1,10 +1,12 @@ #pragma once +#include "TypeTraits.h" #include #include #include #include #include +#include namespace sls { /* Base class for TCP socket, this is used to send data between detector, client @@ -24,9 +26,21 @@ class DataSocket { int getSocketId() const { return sockfd_; } int Send(const void *buffer, size_t size); - template int Send(T &&data) { + + // Send everything that is not a vector by using address and sizeof + // TODO! We probably should restrict this even more to avoid bugs when + // we send object instead of data + template + typename std::enable_if< + !is_vector::type>::value, int>::type + Send(T &&data) { return Send(&data, sizeof(data)); } + + template int Send(const std::vector &vec) { + return Send(vec.data(), sizeof(T) * vec.size()); + } + // Variadic template to send all arguments template int SendAll(Args &&... args) { auto l = std::initializer_list{Send(args)...}; diff --git a/slsSupportLib/include/TypeTraits.h b/slsSupportLib/include/TypeTraits.h index 6a5ef7fb4..bd32049e1 100644 --- a/slsSupportLib/include/TypeTraits.h +++ b/slsSupportLib/include/TypeTraits.h @@ -1,5 +1,6 @@ #pragma once #include +#include namespace sls { @@ -62,14 +63,17 @@ template struct is_container< T, typename std::conditional< false, - is_container_helper().size()), - decltype(std::declval().begin()), - decltype(std::declval().end()), - decltype(std::declval().cbegin()), - decltype(std::declval().cend()), - decltype(std::declval().empty())>, + is_container_helper< + typename std::remove_reference::type::value_type, + typename std::remove_reference::type::size_type, + typename std::remove_reference::type::iterator, + typename std::remove_reference::type::const_iterator, + decltype(std::declval().size()), + decltype(std::declval().begin()), + decltype(std::declval().end()), + decltype(std::declval().cbegin()), + decltype(std::declval().cend()), + decltype(std::declval().empty())>, void>::type> : public std::true_type {}; /** @@ -92,4 +96,9 @@ struct is_light_container< decltype(std::declval().end())>, void>::type> : public std::true_type {}; +template struct is_vector : public std::false_type {}; + +template +struct is_vector> : public std::true_type {}; + } // namespace sls \ No newline at end of file