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
<< ", 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<int>() == 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<int>& 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<int>() == 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<int> gainIndices;
std::vector<int> 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<int>() == 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<int>();
std::vector<int> 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<int>() == FAIL) {
char mess[MAX_STR_LENGTH]{};
client.Receive(mess, MAX_STR_LENGTH);
throw RuntimeError("Detector " + std::to_string(moduleId) +

View File

@ -1,10 +1,12 @@
#pragma once
#include "TypeTraits.h"
#include <cstddef>
#include <cstdint>
#include <netdb.h>
#include <numeric>
#include <string>
#include <vector>
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 <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));
}
template <typename T> int Send(const std::vector<T> &vec) {
return Send(vec.data(), sizeof(T) * vec.size());
}
// Variadic template to send all arguments
template <class... Args> int SendAll(Args &&... args) {
auto l = std::initializer_list<int>{Send(args)...};

View File

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