From d5b893e452538e29cc96962cb56f25206f3fff5a Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Fri, 31 Jul 2020 13:32:53 +0200 Subject: [PATCH] readErrorMessage --- slsDetectorSoftware/src/Module.cpp | 31 ++++++++++------------------ slsSupportLib/include/ClientSocket.h | 1 + slsSupportLib/include/DataSocket.h | 1 + slsSupportLib/src/ClientSocket.cpp | 6 ++++++ 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 9eef67a74..b3eedf714 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -1380,8 +1380,9 @@ void Module::setInjectChannel(const int offsetChannel, sendToDetector(F_SET_INJECT_CHANNEL, args, nullptr); } -void Module::sendVetoPhoton(const int chipIndex, const std::vector& gainIndices, - const std::vector& values) { +void Module::sendVetoPhoton(const int chipIndex, + const std::vector &gainIndices, + const std::vector &values) { const int nch = gainIndices.size(); if (gainIndices.size() != values.size()) { throw RuntimeError("Number of Gain Indices and values do not match! " @@ -1399,10 +1400,8 @@ void Module::sendVetoPhoton(const int chipIndex, const std::vector& gainInd client.Send(gainIndices); client.Send(values); 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)); + " returned error: " + client.readErrorMessage()); } } @@ -1413,10 +1412,8 @@ void Module::getVetoPhoton(const int chipIndex, 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) + - " returned error: " + std::string(mess)); + " returned error: " + client.readErrorMessage()); } auto nch = client.Receive(); @@ -1647,10 +1644,8 @@ void Module::getBadChannels(const std::string &fname) const { auto client = DetectorSocket(shm()->hostname, shm()->controlPort); 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)); + " returned error: " + client.readErrorMessage()); } // receive badchannels auto nch = client.Receive(); @@ -1706,10 +1701,8 @@ void Module::setBadChannels(const std::string &fname) { client.Send(badchannels); } 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)); + " returned error: " + client.readErrorMessage()); } } @@ -2104,7 +2097,7 @@ void Module::startPattern() { sendToDetector(F_START_PATTERN); } // Moench std::map Module::getAdditionalJsonHeader() const { - //TODO, refactor this function with a more robust sending. + // TODO, refactor this function with a more robust sending. // Now assuming whitespace separated key value if (!shm()->useReceiverFlag) { throw RuntimeError("Set rx_hostname first to use receiver parameters " @@ -2114,10 +2107,8 @@ std::map Module::getAdditionalJsonHeader() const { client.Send(F_GET_ADDITIONAL_JSON_HEADER); auto ret = client.Receive(); if (ret == FAIL) { - char mess[MAX_STR_LENGTH]{}; - client.Receive(mess, MAX_STR_LENGTH); throw RuntimeError("Receiver " + std::to_string(moduleId) + - " returned error: " + std::string(mess)); + " returned error: " + client.readErrorMessage()); } else { auto size = client.Receive(); std::string buff(size, '\0'); @@ -2126,7 +2117,7 @@ std::map Module::getAdditionalJsonHeader() const { client.Receive(&buff[0], buff.size()); std::istringstream iss(buff); std::string key, value; - while(iss >> key){ + while (iss >> key) { iss >> value; retval[key] = value; } @@ -2152,7 +2143,7 @@ void Module::setAdditionalJsonHeader( } } std::ostringstream oss; - for (auto& it : jsonHeader) + for (auto &it : jsonHeader) oss << it.first << ' ' << it.second << ' '; auto buff = oss.str(); const auto size = static_cast(buff.size()); diff --git a/slsSupportLib/include/ClientSocket.h b/slsSupportLib/include/ClientSocket.h index eb68a8bcd..9aafb647c 100755 --- a/slsSupportLib/include/ClientSocket.h +++ b/slsSupportLib/include/ClientSocket.h @@ -15,6 +15,7 @@ class ClientSocket : public DataSocket { int sendCommandThenRead(int fnum, const void *args, size_t args_size, void *retval, size_t retval_size); + std::string readErrorMessage(); private: void readReply(int &ret, void *retval, size_t retval_size); struct sockaddr_in serverAddr {}; diff --git a/slsSupportLib/include/DataSocket.h b/slsSupportLib/include/DataSocket.h index e82efc74e..eb1726b8a 100644 --- a/slsSupportLib/include/DataSocket.h +++ b/slsSupportLib/include/DataSocket.h @@ -48,6 +48,7 @@ class DataSocket { return sum; } int Receive(void *buffer, size_t size); + template int Receive(T &arg) { return Receive(&arg, sizeof(arg)); } diff --git a/slsSupportLib/src/ClientSocket.cpp b/slsSupportLib/src/ClientSocket.cpp index b001400d8..7dda44e7d 100644 --- a/slsSupportLib/src/ClientSocket.cpp +++ b/slsSupportLib/src/ClientSocket.cpp @@ -101,4 +101,10 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { } } +std::string ClientSocket::readErrorMessage(){ + std::string error_msg(MAX_STR_LENGTH, '\0'); + Receive(&error_msg[0], error_msg.size()); + return error_msg; +} + }; // namespace sls