added overload to send vector

This commit is contained in:
Erik Frojdh
2020-07-31 11:05:39 +02:00
parent 02d5cf14e4
commit caef8c111c
3 changed files with 52 additions and 39 deletions

View File

@ -1399,18 +1399,16 @@ void Module::sendVetoPhoton(const int chipIndex, const std::vector<int>& gainInd
} }
LOG(logDEBUG1) << "Sending veto photon/file to detector [chip:" << chipIndex LOG(logDEBUG1) << "Sending veto photon/file to detector [chip:" << chipIndex
<< ", nch:" << nch << "]"; << ", nch:" << nch << "]";
int fnum = F_SET_VETO_PHOTON;
int ret = FAIL; const int args[]{chipIndex, nch};
int args[]{chipIndex, nch};
auto client = DetectorSocket(shm()->hostname, shm()->controlPort); auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum)); client.Send(F_SET_VETO_PHOTON);
client.Send(args, sizeof(args)); client.Send(args);
client.Send(gainIndices.data(), sizeof(gainIndices[0]) * nch); client.Send(gainIndices.data(), sizeof(gainIndices[0]) * nch);
client.Send(values.data(), sizeof(values[0]) * nch); client.Send(values.data(), sizeof(values[0]) * nch);
client.Send(gainIndices.data(), sizeof(int) * nch); client.Send(gainIndices.data(), sizeof(gainIndices[0]) * nch);
client.Send(values.data(), sizeof(int) * nch); client.Send(values.data(), sizeof(values[0]) * nch);
client.Receive(&ret, sizeof(ret)); if (client.Receive<int>() == FAIL) {
if (ret == FAIL) {
char mess[MAX_STR_LENGTH]{}; char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH); client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) + throw RuntimeError("Detector " + std::to_string(moduleId) +
@ -1421,13 +1419,10 @@ void Module::sendVetoPhoton(const int chipIndex, const std::vector<int>& gainInd
void Module::getVetoPhoton(const int chipIndex, void Module::getVetoPhoton(const int chipIndex,
const std::string &fname) const { const std::string &fname) const {
LOG(logDEBUG1) << "Getting veto photon [" << chipIndex << "]\n"; LOG(logDEBUG1) << "Getting veto photon [" << chipIndex << "]\n";
int fnum = F_GET_VETO_PHOTON;
int ret = FAIL;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort); auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum)); client.Send(F_GET_VETO_PHOTON);
client.Send(&chipIndex, sizeof(chipIndex)); client.Send(chipIndex);
client.Receive(&ret, sizeof(ret)); if (client.Receive<int>() == FAIL) {
if (ret == FAIL) {
char mess[MAX_STR_LENGTH]{}; char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH); client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) + 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"; LOG(logDEBUG1) << "Setting veto photon. Reading Gain values from file";
int totalEnergy = numPhotons * energy; const int totalEnergy = numPhotons * energy;
std::vector<int> gainIndices; std::vector<int> gainIndices;
std::vector<int> values; std::vector<int> values;
@ -1512,7 +1507,7 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons,
std::to_string(gainIndices.size())); 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; int gainIndex = 2;
if (totalEnergy < gainThreshold[0]) { if (totalEnergy < gainThreshold[0]) {
gainIndex = 0; gainIndex = 0;
@ -1665,20 +1660,16 @@ void Module::setADCConfiguration(const int chipIndex, const int adcIndex,
void Module::getBadChannels(const std::string &fname) const { void Module::getBadChannels(const std::string &fname) const {
LOG(logDEBUG1) << "Getting bad channels to " << fname; LOG(logDEBUG1) << "Getting bad channels to " << fname;
int fnum = F_GET_BAD_CHANNELS;
int ret = FAIL;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort); auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum)); client.Send(F_GET_BAD_CHANNELS);
client.Receive(&ret, sizeof(ret)); if (client.Receive<int>() == FAIL) {
if (ret == FAIL) {
char mess[MAX_STR_LENGTH]{}; char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH); client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) + throw RuntimeError("Detector " + std::to_string(moduleId) +
" returned error: " + std::string(mess)); " returned error: " + std::string(mess));
} }
// receive badchannels // receive badchannels
int nch = -1; auto nch = client.Receive<int>();
client.Receive(&nch, sizeof(nch));
std::vector<int> badchannels(nch); std::vector<int> badchannels(nch);
if (nch > 0) { if (nch > 0) {
client.Receive(badchannels.data(), client.Receive(badchannels.data(),
@ -1690,7 +1681,7 @@ void Module::getBadChannels(const std::string &fname) const {
// save to file // save to file
std::ofstream outfile(fname); std::ofstream outfile(fname);
if (!outfile.is_open()) { if (!outfile) {
throw RuntimeError("Could not create file to save bad channels"); throw RuntimeError("Could not create file to save bad channels");
} }
for (auto ch : badchannels) for (auto ch : badchannels)
@ -1701,7 +1692,7 @@ void Module::getBadChannels(const std::string &fname) const {
void Module::setBadChannels(const std::string &fname) { void Module::setBadChannels(const std::string &fname) {
// read bad channels file // read bad channels file
std::ifstream input_file(fname); std::ifstream input_file(fname);
if (!input_file.is_open()) { if (!input_file) {
throw RuntimeError("Could not open bad channels file " + fname + throw RuntimeError("Could not open bad channels file " + fname +
" for reading"); " for reading");
} }
@ -1724,17 +1715,16 @@ void Module::setBadChannels(const std::string &fname) {
// send bad channels to module // send bad channels to module
int fnum = F_SET_BAD_CHANNELS; int fnum = F_SET_BAD_CHANNELS;
int ret = FAIL;
int nch = badchannels.size(); int nch = badchannels.size();
LOG(logDEBUG1) << "Sending bad channels to detector, nch:" << nch; LOG(logDEBUG1) << "Sending bad channels to detector, nch:" << nch;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort); auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(&fnum, sizeof(fnum)); client.Send(&fnum, sizeof(fnum));
client.Send(&nch, sizeof(nch)); client.Send(&nch, sizeof(nch));
if (nch > 0) { 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 (client.Receive<int>() == FAIL) {
if (ret == FAIL) {
char mess[MAX_STR_LENGTH]{}; char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH); client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) + throw RuntimeError("Detector " + std::to_string(moduleId) +

View File

@ -1,10 +1,12 @@
#pragma once #pragma once
#include "TypeTraits.h"
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <netdb.h> #include <netdb.h>
#include <numeric> #include <numeric>
#include <string> #include <string>
#include <vector>
namespace sls { namespace sls {
/* Base class for TCP socket, this is used to send data between detector, client /* 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 getSocketId() const { return sockfd_; }
int Send(const void *buffer, size_t size); int Send(const void *buffer, size_t size);
template <typename T> 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 T>
typename std::enable_if<
!is_vector<typename std::remove_reference<T>::type>::value, int>::type
Send(T &&data) {
return Send(&data, sizeof(data)); return Send(&data, sizeof(data));
} }
template <typename T> int Send(const std::vector<T> &vec) {
return Send(vec.data(), sizeof(T) * vec.size());
}
// Variadic template to send all arguments // Variadic template to send all arguments
template <class... Args> int SendAll(Args &&... args) { template <class... Args> int SendAll(Args &&... args) {
auto l = std::initializer_list<int>{Send(args)...}; auto l = std::initializer_list<int>{Send(args)...};

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <type_traits> #include <type_traits>
#include <vector>
namespace sls { namespace sls {
@ -62,14 +63,17 @@ template <typename T>
struct is_container< struct is_container<
T, typename std::conditional< T, typename std::conditional<
false, false,
is_container_helper<typename T::value_type, typename T::size_type, is_container_helper<
typename T::iterator, typename T::const_iterator, typename std::remove_reference<T>::type::value_type,
decltype(std::declval<T>().size()), typename std::remove_reference<T>::type::size_type,
decltype(std::declval<T>().begin()), typename std::remove_reference<T>::type::iterator,
decltype(std::declval<T>().end()), typename std::remove_reference<T>::type::const_iterator,
decltype(std::declval<T>().cbegin()), decltype(std::declval<T>().size()),
decltype(std::declval<T>().cend()), decltype(std::declval<T>().begin()),
decltype(std::declval<T>().empty())>, decltype(std::declval<T>().end()),
decltype(std::declval<T>().cbegin()),
decltype(std::declval<T>().cend()),
decltype(std::declval<T>().empty())>,
void>::type> : public std::true_type {}; void>::type> : public std::true_type {};
/** /**
@ -92,4 +96,9 @@ struct is_light_container<
decltype(std::declval<T>().end())>, decltype(std::declval<T>().end())>,
void>::type> : public std::true_type {}; void>::type> : public std::true_type {};
template <typename T> struct is_vector : public std::false_type {};
template <typename T>
struct is_vector<std::vector<T>> : public std::true_type {};
} // namespace sls } // namespace sls