From 1db7521cfac9b8402d7423ba8d78ad60f8556a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Wed, 5 Jun 2019 15:25:18 +0200 Subject: [PATCH] exceptions TCP (#31) * WIP * first test * format * test frames * also 0: * WIP * WIP * first test * format * test frames * also 0: * test and less local * pass ret by value * level * WIP * WIP * cleaning up interface * removed ref * another * updated api version * cleanup * cleanup * WIP * fixes * fixed tests --- .../include/multiSlsDetector.h | 4 + .../src/sls_detector_client.cpp | 10 +- .../tests/test-multiSlsDetectorClient.cpp | 238 +- .../include/slsReceiverTCPIPInterface.h | 21 +- .../src/slsReceiverImplementation.cpp | 4 +- .../src/slsReceiverTCPIPInterface.cpp | 2378 +++++------------ slsSupportLib/include/DataSocket.h | 19 +- slsSupportLib/include/ServerInterface2.h | 26 +- slsSupportLib/include/ServerSocket.h | 10 +- slsSupportLib/include/versionAPI.h | 10 +- slsSupportLib/src/ClientSocket.cpp | 6 +- slsSupportLib/src/DataSocket.cpp | 18 +- slsSupportLib/src/ServerInterface2.cpp | 34 +- slsSupportLib/src/ServerSocket.cpp | 22 - 14 files changed, 1048 insertions(+), 1752 deletions(-) diff --git a/slsDetectorSoftware/include/multiSlsDetector.h b/slsDetectorSoftware/include/multiSlsDetector.h index a4617cd38..4d782e067 100755 --- a/slsDetectorSoftware/include/multiSlsDetector.h +++ b/slsDetectorSoftware/include/multiSlsDetector.h @@ -788,6 +788,10 @@ class multiSlsDetector : public virtual slsDetectorDefs { */ int getDataBytes(int detPos = -1); + /** + * Returns the number of detectors in the multi structure*/ + size_t size() const{ return detectors.size();} + /** * Set/get dacs value * @param val value (in V) diff --git a/slsDetectorSoftware/src/sls_detector_client.cpp b/slsDetectorSoftware/src/sls_detector_client.cpp index 12f4ba7c7..e140fa18d 100755 --- a/slsDetectorSoftware/src/sls_detector_client.cpp +++ b/slsDetectorSoftware/src/sls_detector_client.cpp @@ -1,12 +1,13 @@ -#include "versionAPI.h" #include "multiSlsDetectorClient.h" +#include "versionAPI.h" #include int main(int argc, char *argv[]) { for (int i = 1; i < argc; ++i) { if (!(strcmp(argv[i], "--version")) || !(strcmp(argv[i], "-v"))) { int64_t tempval = APILIB; - std::cout << argv[0] << " " << GITBRANCH << " (0x" << std::hex << tempval << ")" << std::endl; + std::cout << argv[0] << " " << GITBRANCH << " (0x" << std::hex + << tempval << ")" << std::endl; return 0; } } @@ -27,5 +28,8 @@ int main(int argc, char *argv[]) { int action = slsDetectorDefs::HELP_ACTION; #endif - multiSlsDetectorClient(argc, argv, action); + try { + multiSlsDetectorClient(argc, argv, action); + } catch (const sls::RuntimeError &e) { + } } diff --git a/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp b/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp index 861619256..bfbba0014 100644 --- a/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp +++ b/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp @@ -24,7 +24,7 @@ TEST_CASE("rx_fifodepth", "[.cmd]") { REQUIRE(oss.str() == "0:rx_fifodepth 100\n"); } -TEST_CASE("frames", "[.cmd]"){ +TEST_CASE("frames", "[.cmd]") { auto oss = std::ostringstream{}; multiSlsDetectorClient("frames 1000", PUT, nullptr, oss); REQUIRE(oss.str() == "frames 1000\n"); @@ -36,5 +36,239 @@ TEST_CASE("frames", "[.cmd]"){ oss = std::ostringstream{}; multiSlsDetectorClient("frames 1", PUT, nullptr, oss); REQUIRE(oss.str() == "frames 1\n"); +} -} \ No newline at end of file +TEST_CASE("receiver", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("receiver start", PUT, nullptr, oss); + REQUIRE(oss.str() == "receiver running\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("receiver", GET, nullptr, oss); + REQUIRE(oss.str() == "receiver running\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("receiver stop", PUT, nullptr, oss); + REQUIRE(oss.str() == "receiver idle\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("receiver", GET, nullptr, oss); + REQUIRE(oss.str() == "receiver idle\n"); +} + +TEST_CASE("enablefwrite", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("enablefwrite 1", PUT, nullptr, oss); + REQUIRE(oss.str() == "enablefwrite 1\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("enablefwrite", GET, nullptr, oss); + REQUIRE(oss.str() == "enablefwrite 1\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("enablefwrite 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "enablefwrite 0\n"); +} + +TEST_CASE("enableoverwrite", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("overwrite 1", PUT, nullptr, oss); + REQUIRE(oss.str() == "overwrite 1\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("overwrite", GET, nullptr, oss); + REQUIRE(oss.str() == "overwrite 1\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("overwrite 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "overwrite 0\n"); +} + +TEST_CASE("activatecmd", "[.cmd]") { + // TODO! read padding from somewhere + auto oss = std::ostringstream{}; + multiSlsDetectorClient("activate 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "activate 0 padding\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("activate", GET, nullptr, oss); + REQUIRE(oss.str() == "activate 0 padding\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("activate 1", PUT, nullptr, oss); + REQUIRE(oss.str() == "activate 1 padding\n"); +} + +TEST_CASE("masterfile", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("masterfile 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "masterfile 0\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("masterfile", GET, nullptr, oss); + REQUIRE(oss.str() == "masterfile 0\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("masterfile 1", PUT, nullptr, oss); + REQUIRE(oss.str() == "masterfile 1\n"); +} + +TEST_CASE("index", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("index 57", PUT, nullptr, oss); + REQUIRE(oss.str() == "index 57\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("index", GET, nullptr, oss); + REQUIRE(oss.str() == "index 57\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("index 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "index 0\n"); +} + +TEST_CASE("rx_tcpport", "[.cmd]") { + multiSlsDetector d; + int port = 1500; + int base = 1954; + for (size_t i = 0; i != d.size(); ++i) { + std::ostringstream oss; + std::string cmd = + std::to_string(i) + ":rx_tcpport " + std::to_string(port + i); + std::cout << cmd << "\n"; + multiSlsDetectorClient(cmd, PUT, nullptr, oss); + REQUIRE(oss.str() == cmd + "\n"); + } + { + std::ostringstream oss; + REQUIRE_THROWS( + multiSlsDetectorClient("rx_tcpport 15", PUT, nullptr, oss)); + } + + for (size_t i = 0; i != d.size(); ++i) { + std::ostringstream oss; + std::string cmd = std::to_string(i) + ":rx_tcpport"; + multiSlsDetectorClient(cmd, GET, nullptr, oss); + REQUIRE(oss.str() == cmd + " " + std::to_string(port + i) + "\n"); + } + + for (size_t i = 0; i != d.size(); ++i) { + std::ostringstream oss; + std::string cmd = + std::to_string(i) + ":rx_tcpport " + std::to_string(base + i); + multiSlsDetectorClient(cmd, PUT, nullptr, oss); + REQUIRE(oss.str() == cmd + "\n"); + } +} + +TEST_CASE("fname", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("fname somename", PUT, nullptr, oss); + REQUIRE(oss.str() == "fname somename\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("fname", GET, nullptr, oss); + REQUIRE(oss.str() == "fname somename\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("fname run", PUT, nullptr, oss); + REQUIRE(oss.str() == "fname run\n"); +} + +TEST_CASE("resetframescaught get framescaught", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("resetframescaught 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "resetframescaught successful\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("framescaught", GET, nullptr, oss); + REQUIRE(oss.str() == "framescaught 0\n"); +} + +TEST_CASE("r_silent", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("r_silent 1", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_silent 1\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_silent", GET, nullptr, oss); + REQUIRE(oss.str() == "r_silent 1\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_silent 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_silent 0\n"); +} + +// TEST_CASE("rx_jsonaddheader", "[.cmd]") { +// auto oss = std::ostringstream{}; +// multiSlsDetectorClient("rx_jsonaddheader \"hej\":\"5\"", PUT, nullptr, +// oss); REQUIRE(oss.str() == "rx_jsonaddheader \"hej\":\"5\"\n"); + +// oss = std::ostringstream{}; +// multiSlsDetectorClient("rx_jsonaddheader", GET, nullptr, oss); +// REQUIRE(oss.str() == "rx_jsonaddheader \"hej\":\"5\"\n"); + +// oss = std::ostringstream{}; +// multiSlsDetectorClient("rx_jsonaddheader \"\"", PUT, nullptr, oss); +// REQUIRE(oss.str() == "rx_jsonaddheader\n"); +// } + +// TEST_CASE("rx_udpsocksize", "[.cmd]") { +// auto oss = std::ostringstream{}; +// multiSlsDetectorClient("rx_udpsocksize 4857600", PUT, nullptr, oss); +// REQUIRE(oss.str() == "rx_udpsocksize 4857600\n"); + +// oss = std::ostringstream{}; +// multiSlsDetectorClient("rx_udpsocksize", GET, nullptr, oss); +// REQUIRE(oss.str() == "rx_udpsocksize 4857600\n"); + +// oss = std::ostringstream{}; +// multiSlsDetectorClient("rx_udpsocksize 104857600", PUT, nullptr, oss); +// REQUIRE(oss.str() == "rx_udpsocksize 104857600\n"); +// } + +TEST_CASE("r_framesperfile", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("r_framesperfile 50", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_framesperfile 50\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_framesperfile", GET, nullptr, oss); + REQUIRE(oss.str() == "r_framesperfile 50\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_framesperfile 10000", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_framesperfile 10000\n"); +} + +TEST_CASE("r_discardpolicy", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("r_discardpolicy discardempty", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_discardpolicy discardempty\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_discardpolicy", GET, nullptr, oss); + REQUIRE(oss.str() == "r_discardpolicy discardempty\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_discardpolicy discardpartial", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_discardpolicy discardpartial\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_discardpolicy nodiscard", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_discardpolicy nodiscard\n"); +} + +TEST_CASE("r_padding", "[.cmd]") { + auto oss = std::ostringstream{}; + multiSlsDetectorClient("r_padding 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_padding 0\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_padding", GET, nullptr, oss); + REQUIRE(oss.str() == "r_padding 0\n"); + + oss = std::ostringstream{}; + multiSlsDetectorClient("r_padding 1", PUT, nullptr, oss); + REQUIRE(oss.str() == "r_padding 1\n"); +} diff --git a/slsReceiverSoftware/include/slsReceiverTCPIPInterface.h b/slsReceiverSoftware/include/slsReceiverTCPIPInterface.h index 19b13b3b4..5c217c148 100755 --- a/slsReceiverSoftware/include/slsReceiverTCPIPInterface.h +++ b/slsReceiverSoftware/include/slsReceiverTCPIPInterface.h @@ -125,11 +125,6 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs { template void validate(T arg, T retval, std::string modename, numberMode hex); - /** Unrecognized Function */ - int M_nofunc(sls::ServerInterface2 & socket); - - - /** Execute command */ int exec_command(sls::ServerInterface2 &socket); @@ -298,10 +293,6 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs { /** set dbit offset */ int set_dbit_offset(sls::ServerInterface2 &socket); - - int LogSocketCrash(); - void NullObjectError(int& ret, char* mess); - /** detector type */ detectorType myDetectorType; @@ -390,6 +381,14 @@ protected: std::unique_ptr server{nullptr}; private: - int VerifyLock(int &ret, char *mess); - int VerifyLockAndIdle(int &ret, char *mess, int fnum); + void VerifyLock(); + void VerifyIdle(sls::ServerInterface2& socket); + + slsReceiverImplementation* impl(){ + if (receiver!=nullptr){ + return receiver.get(); + }else{ + throw sls::SocketError("Receiver not set up. Please use rx_hostname first.\n"); + } + } }; diff --git a/slsReceiverSoftware/src/slsReceiverImplementation.cpp b/slsReceiverSoftware/src/slsReceiverImplementation.cpp index 65c7a7106..9a9ff9707 100755 --- a/slsReceiverSoftware/src/slsReceiverImplementation.cpp +++ b/slsReceiverSoftware/src/slsReceiverImplementation.cpp @@ -571,7 +571,7 @@ void slsReceiverImplementation::setFilePath(const char c[]) { if(stat(c,&st) == 0) strcpy(filePath,c); else - FILE_LOG(logERROR) << "FilePath does not exist: " << filePath; + FILE_LOG(logERROR) << "FilePath does not exist: " << c; } FILE_LOG(logINFO) << "File path: " << filePath; } @@ -1059,7 +1059,7 @@ int slsReceiverImplementation::setFifoDepth(const uint32_t i) { if (fifoDepth != i) { fifoDepth = i; if (SetupFifoStructure() == FAIL) - return FAIL; + throw sls::RuntimeError("Failed to setup fifo structure"); } FILE_LOG(logINFO) << "Fifo Depth: " << i; return OK; diff --git a/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp b/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp index 3692720d8..4779a8431 100755 --- a/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp +++ b/slsReceiverSoftware/src/slsReceiverTCPIPInterface.cpp @@ -25,6 +25,8 @@ #include using sls::SocketError; +using sls::RuntimeError; +using Interface = sls::ServerInterface2; slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() { stop(); @@ -71,8 +73,6 @@ int64_t slsReceiverTCPIPInterface::getReceiverVersion(){ return APIRECEIVER; } - - /***callback functions***/ void slsReceiverTCPIPInterface::registerCallBackStartAcquisition(int (*func)(char*, char*, uint64_t, uint32_t, void*),void *arg){ startAcquisitionCallBack=func; @@ -105,16 +105,25 @@ void slsReceiverTCPIPInterface::startTCPServer() { FILE_LOG(logINFOBLUE) << "Created [ TCP server Tid: " << syscall(SYS_gettid) << "]"; FILE_LOG(logINFO) << "SLS Receiver starting TCP Server on port " - << portNumber << std::endl; - int ret = OK; + << portNumber << '\n'; server = sls::make_unique(portNumber); while (true) { FILE_LOG(logDEBUG1) << "Start accept loop"; try { auto socket = server->accept(); - constexpr int time_us = 5000000; - socket.setReceiveTimeout(time_us); - ret = decode_function(socket); + // constexpr int time_us = 5000000; + // socket.setReceiveTimeout(time_us); + try{ + VerifyLock(); + ret = decode_function(socket); + }catch(const RuntimeError& e){ + //We had an error needs to be sent to client + int r = FAIL; + strcpy(mess, e.what()); + socket.write(&r, sizeof(r)); + socket.write(mess, sizeof(mess)); + } + // if tcp command was to exit server if (ret == GOODBYE) { @@ -127,7 +136,7 @@ void slsReceiverTCPIPInterface::startTCPServer() { << "]"; pthread_exit(nullptr); } - }catch(const sls::SocketError& e){ + }catch(const RuntimeError& e){ std::cout << "Accept failed\n"; } @@ -209,234 +218,141 @@ int slsReceiverTCPIPInterface::function_table(){ return OK; } -int slsReceiverTCPIPInterface::decode_function(sls::ServerInterface2 &socket) { +int slsReceiverTCPIPInterface::decode_function(Interface &socket) { ret = FAIL; - int n = socket.read(&fnum, sizeof(fnum)); - if (n <= 0) { - FILE_LOG(logDEBUG3) - << "Could not read socket. Received " << n - << " bytes, fnum:" << fnum << " (" - << getFunctionNameFromEnum((enum detFuncs)fnum) << ")"; - return FAIL; - } else { - FILE_LOG(logDEBUG3) << "Received " << n << " bytes"; - } + socket.receiveArg(fnum); if (fnum <= NUM_DET_FUNCTIONS || fnum >= NUM_REC_FUNCTIONS) { - FILE_LOG(logERROR) << "Unknown function enum " << fnum; - ret = (this->M_nofunc)(socket); + throw RuntimeError("Unrecognized Function enum " + + std::to_string(fnum) + "\n"); } else { FILE_LOG(logDEBUG1) << "calling function fnum: " << fnum << " (" << getFunctionNameFromEnum((enum detFuncs)fnum) << ") located at " << flist[fnum]; ret = (this->*flist[fnum])(socket); - if (ret == FAIL) { - FILE_LOG(logDEBUG1) - << "Failed to execute function = " << fnum << " (" - << getFunctionNameFromEnum((enum detFuncs)fnum) << ")"; - } else - FILE_LOG(logDEBUG1) - << "Function " << getFunctionNameFromEnum((enum detFuncs)fnum) - << " executed OK"; + FILE_LOG(logDEBUG1) + << "Function " << getFunctionNameFromEnum((enum detFuncs)fnum) + << " finished"; } return ret; } void slsReceiverTCPIPInterface::functionNotImplemented() { - ret = FAIL; - sprintf(mess, "Function (%s) is not implemented for this detector\n", + char message[MAX_STR_LENGTH]; + sprintf(message, "Function (%s) is not implemented for this detector\n", getFunctionNameFromEnum((enum detFuncs)fnum)); - FILE_LOG(logERROR) << mess; + throw RuntimeError(mess); } void slsReceiverTCPIPInterface::modeNotImplemented(std::string modename, int mode) { - ret = FAIL; - sprintf(mess, "%s (%d) is not implemented for this detector\n", modename.c_str(), mode); - FILE_LOG(logERROR) << mess; + char message[MAX_STR_LENGTH]; + sprintf(message, "%s (%d) is not implemented for this detector\n", modename.c_str(), mode); + throw RuntimeError(message); } template void slsReceiverTCPIPInterface::validate(T arg, T retval, std::string modename, numberMode hex) { if (ret == OK && arg != -1 && retval != arg) { - ret = FAIL; if (hex) sprintf(mess, "Could not %s. Set 0x%x, but read 0x%x\n", modename.c_str(), (unsigned int) arg, (unsigned int) retval); else sprintf(mess, "Could not %s. Set %d, but read %d\n", modename.c_str(), (unsigned int) arg, (unsigned int) retval); - FILE_LOG(logERROR) << mess; + throw RuntimeError(mess); } } -int slsReceiverTCPIPInterface::M_nofunc(sls::ServerInterface2 &socket){ - ret = FAIL; - memset(mess, 0, sizeof(mess)); - - int n = 1; - while (n > 0) - n = socket.read(mess, MAX_STR_LENGTH); - - sprintf(mess,"Unrecognized Function enum %d. Please do not proceed.\n", fnum); - FILE_LOG(logERROR) << mess; - return socket.sendResult(false, ret, nullptr, 0, mess); -} - -int slsReceiverTCPIPInterface::VerifyLock(int &ret, char *mess) { - if (server->getThisClient() != server->getLockedBy() && lockStatus) { - ret = FAIL; - sprintf(mess, "Receiver locked\n"); - FILE_LOG(logERROR) << mess; +void slsReceiverTCPIPInterface::VerifyLock() { + if (lockStatus && server->getThisClient() != server->getLockedBy()) { + strcpy(mess, "Receiver locked\n"); + throw sls::SocketError(mess); } - return ret; } -int slsReceiverTCPIPInterface::VerifyLockAndIdle(int &ret, char *mess, int fnum) { - VerifyLock(ret, mess); - if (ret == FAIL) - return ret; - if (receiver->getStatus() != IDLE) { +void slsReceiverTCPIPInterface::VerifyIdle(Interface &socket) { + if (impl()->getStatus() != IDLE) { sprintf(mess, "Can not execute %s when receiver is not idle\n", getFunctionNameFromEnum((enum detFuncs)fnum)); + throw sls::SocketError(mess); } - ret = OK; - return ret; } -int slsReceiverTCPIPInterface::exec_command(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); +int slsReceiverTCPIPInterface::exec_command(Interface &socket) { char cmd[MAX_STR_LENGTH]{}; char retval[MAX_STR_LENGTH]{}; - - // get args, return if socket crashed - if (socket.receiveArg(ret, mess, cmd, MAX_STR_LENGTH) == FAIL) - return FAIL; + socket.receiveArg(cmd); FILE_LOG(logINFO) << "Executing command (" << cmd << ")"; - - // verify if receiver is unlocked - if (VerifyLock(ret, mess) == OK) { - const size_t tempsize = 256; - std::array temp; - std::string sresult; - std::shared_ptr pipe(popen(cmd, "r"), pclose); - if (!pipe) { - ret = FAIL; - strcpy(mess, "Executing Command failed\n"); - FILE_LOG(logERROR) << mess; - } else { - while (!feof(pipe.get())) { - if (fgets(temp.data(), tempsize, pipe.get()) != nullptr) - sresult += temp.data(); - } - strncpy(retval, sresult.c_str(), MAX_STR_LENGTH); - ret = OK; - FILE_LOG(logINFO) << "Result of cmd (" << cmd << "):\n" << retval; + const size_t tempsize = 256; + std::array temp; + std::string sresult; + std::shared_ptr pipe(popen(cmd, "r"), pclose); + if (!pipe) { + throw RuntimeError("Executing Command failed\n"); + } else { + while (!feof(pipe.get())) { + if (fgets(temp.data(), tempsize, pipe.get()) != nullptr) + sresult += temp.data(); } + strncpy(retval, sresult.c_str(), MAX_STR_LENGTH); + FILE_LOG(logINFO) << "Result of cmd (" << cmd << "):\n" << retval; } - return socket.sendResult(false, ret, retval, MAX_STR_LENGTH, mess); + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::exit_server(sls::ServerInterface2 &socket) { +int slsReceiverTCPIPInterface::exit_server(Interface &socket) { FILE_LOG(logINFO) << "Closing server"; - ret = OK; - memset(mess, 0, sizeof(mess)); - socket.sendResult(false, ret, nullptr, 0, nullptr); + socket.sendData(OK); return GOODBYE; } -int slsReceiverTCPIPInterface::lock_receiver(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int lock = 0; - - // get args, return if socket crashed - if (socket.receiveArg(ret, mess, &lock, sizeof(lock)) == FAIL) - return FAIL; +int slsReceiverTCPIPInterface::lock_receiver(Interface &socket) { + auto lock = socket.receive(); FILE_LOG(logDEBUG1) << "Locking Server to " << lock; - - // execute action if (lock >= 0) { if (!lockStatus || (server->getLockedBy() == server->getThisClient())) { lockStatus = lock; lock ? server->setLockedBy(server->getThisClient()) : server->setLockedBy(sls::IpAddr{}); } else{ - sprintf(mess, "Receiver locked\n"); - FILE_LOG(logERROR) << mess; + throw RuntimeError("Receiver locked\n"); } - } - return socket.sendResult(true, ret, &lockStatus, - sizeof(lockStatus), mess); + return socket.sendResult(lockStatus); } -int slsReceiverTCPIPInterface::get_last_client_ip(sls::ServerInterface2 &socket) { +int slsReceiverTCPIPInterface::get_last_client_ip(Interface &socket) { ret = OK; memset(mess, 0, sizeof(mess)); char ip[INET_ADDRSTRLEN]{}; sls::strcpy_safe(ip, server->getLastClient().str().c_str()); - return socket.sendResult(true, ret, &ip, sizeof(ip)); + return socket.sendResult(ret, &ip, sizeof(ip)); } - -int slsReceiverTCPIPInterface::set_port(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int p_number = -1; - if (socket.receiveArg(ret, mess, &p_number, sizeof(p_number)) == FAIL) - return FAIL; - - if (VerifyLock(ret, mess) == OK) { - if (p_number < 1024) { - ret = FAIL; - sprintf(mess,"Port Number (%d) too low\n", p_number); - FILE_LOG(logERROR) << mess; - } else { - FILE_LOG(logINFO) << "set port to " << p_number <(p_number); - new_server->setLockedBy(server->getLockedBy()); - new_server->setLastClient(server->getLastClient()); - server = std::move(new_server); - } catch(SocketError &e) { - ret = FAIL; - sprintf(mess, "%s", e.what()); - FILE_LOG(logERROR) << mess; - } catch (...) { - ret = FAIL; - sprintf(mess, "Could not set port %d.\n", p_number); - FILE_LOG(logERROR) << mess; - } - } - } - - socket.sendResult(true, ret, &p_number,sizeof(p_number), mess); - return ret; +int slsReceiverTCPIPInterface::set_port(Interface &socket) { + auto p_number = socket.receive(); + if (p_number < 1024) + throw RuntimeError("Port Number: " + std::to_string(p_number)+ " is too low (<1024)"); + + FILE_LOG(logINFO) << "set port to " << p_number <(p_number); + new_server->setLockedBy(server->getLockedBy()); + new_server->setLastClient(server->getThisClient()); + server = std::move(new_server); + socket.sendResult(p_number); + return OK; } - - -int slsReceiverTCPIPInterface::update_client(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - +int slsReceiverTCPIPInterface::update_client(Interface &socket) { if(receiver == nullptr) - NullObjectError(ret, mess); - socket.sendResult(false, ret, nullptr, 0, mess); - - if (ret == FAIL) - return ret; - - // update + throw sls::SocketError("Receiver not set up. Please use rx_hostname first.\n"); + socket.sendData(OK); return send_update(socket); } -int slsReceiverTCPIPInterface::send_update(sls::ServerInterface2 &socket) { +int slsReceiverTCPIPInterface::send_update(Interface &socket) { int n = 0; int i32 = -1; char cstring[MAX_STR_LENGTH]{}; @@ -538,138 +454,77 @@ int slsReceiverTCPIPInterface::send_update(sls::ServerInterface2 &socket) { return OK; } - - -int slsReceiverTCPIPInterface::get_id(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int64_t retval = getReceiverVersion(); - return socket.sendResult(true, ret, &retval, sizeof(retval)); +int slsReceiverTCPIPInterface::get_id(Interface &socket){ + return socket.sendResult(getReceiverVersion()); } - - -int slsReceiverTCPIPInterface::set_detector_type(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - detectorType arg = GENERIC; - detectorType retval = GENERIC; - - // get args, return if socket crashed - if (socket.receiveArg(ret, mess, &arg, sizeof(arg)) == FAIL) - return FAIL; - +int slsReceiverTCPIPInterface::set_detector_type(Interface &socket){ + auto arg = socket.receive(); // set if (arg >= 0) { // if object exists, verify unlocked and idle, else only verify lock (connecting first time) - if (receiver == nullptr){ - VerifyLock(ret, mess); + if (receiver != nullptr){ + VerifyIdle(socket); } - else{ - VerifyLockAndIdle(ret, mess, fnum); + switch(arg) { + case GOTTHARD: + case EIGER: + case CHIPTESTBOARD: + case MOENCH: + case JUNGFRAU: + break; + default: + throw RuntimeError("Unknown detector type: " + std::to_string(arg)); + break; } - - if (ret == OK) { - switch(arg) { - case GOTTHARD: - case EIGER: - case CHIPTESTBOARD: - case MOENCH: - case JUNGFRAU: - break; - default: - ret = FAIL; - sprintf(mess,"Unknown detector type: %d\n", arg); - FILE_LOG(logERROR) << mess; - break; - } - if(ret == OK) { - if(receiver == nullptr){ - receiver = sls::make_unique(); - } - myDetectorType = arg; - ret = receiver->setDetectorType(myDetectorType); - retval = myDetectorType; - - // callbacks after (in setdetectortype, the object is reinitialized) - if(startAcquisitionCallBack) - receiver->registerCallBackStartAcquisition(startAcquisitionCallBack,pStartAcquisition); - if(acquisitionFinishedCallBack) - receiver->registerCallBackAcquisitionFinished(acquisitionFinishedCallBack,pAcquisitionFinished); - if(rawDataReadyCallBack) - receiver->registerCallBackRawDataReady(rawDataReadyCallBack,pRawDataReady); - if(rawDataModifyReadyCallBack) - receiver->registerCallBackRawDataModifyReady(rawDataModifyReadyCallBack,pRawDataReady); - } - - } - } - //get - retval = myDetectorType; - validate((int)arg, (int)retval, std::string("set detector type"), DEC); - return socket.sendResult(false, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::set_detector_hostname(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - char hostname[MAX_STR_LENGTH] = {0}; - char retval[MAX_STR_LENGTH] = {0}; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, hostname, MAX_STR_LENGTH); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - // base object not null - if (ret == OK) { - // set - if (strlen(hostname)) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) - receiver->setDetectorHostname(hostname); + if(receiver == nullptr){ + receiver = sls::make_unique(); } - // get - std::string s = receiver->getDetectorHostname(); - strcpy(retval, s.c_str()); - if (!s.length()) { - ret = FAIL; - sprintf(mess, "hostname not set\n"); - FILE_LOG(logERROR) << mess; + myDetectorType = arg; + if (impl()->setDetectorType(myDetectorType) == FAIL){ + throw RuntimeError("Could not set detector type"); } + + // callbacks after (in setdetectortype, the object is reinitialized) + if(startAcquisitionCallBack) + impl()->registerCallBackStartAcquisition(startAcquisitionCallBack,pStartAcquisition); + if(acquisitionFinishedCallBack) + impl()->registerCallBackAcquisitionFinished(acquisitionFinishedCallBack,pAcquisitionFinished); + if(rawDataReadyCallBack) + impl()->registerCallBackRawDataReady(rawDataReadyCallBack,pRawDataReady); + if(rawDataModifyReadyCallBack) + impl()->registerCallBackRawDataModifyReady(rawDataModifyReadyCallBack,pRawDataReady); + } - - return socket.sendResult(true, ret, retval, MAX_STR_LENGTH, mess); + return socket.sendResult(myDetectorType); } -int slsReceiverTCPIPInterface::LogSocketCrash(){ - FILE_LOG(logERROR) << "Reading from socket failed. Possible socket crash"; - return FAIL; -} +int slsReceiverTCPIPInterface::set_detector_hostname(Interface &socket) { + char hostname[MAX_STR_LENGTH]{}; + char retval[MAX_STR_LENGTH]{}; + socket.receiveArg(hostname); -void slsReceiverTCPIPInterface::NullObjectError(int& ret, char* mess){ - ret=FAIL; - strcpy(mess,"Receiver not set up. Please use rx_hostname first.\n"); - FILE_LOG(logERROR) << mess; + if (strlen(hostname)) { + VerifyIdle(socket); + impl()->setDetectorHostname(hostname); + } + auto s = impl()->getDetectorHostname(); + strcpy(retval, s.c_str()); + if (s.empty()) { + throw RuntimeError("Hostname not set"); + } + return socket.sendResult(retval); } - -int slsReceiverTCPIPInterface::set_roi(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); +int slsReceiverTCPIPInterface::set_roi(Interface &socket) { static_assert(sizeof(ROI) == 4*sizeof(int), "ROI not packed"); - int narg = -1; - socket.read(&narg,sizeof(narg)); - + auto narg = socket.receive(); std::vector arg; for (int iloop = 0; iloop < narg; ++iloop) { ROI temp{}; - socket.read(&temp, sizeof(temp)); + socket.receiveArg(temp); arg.push_back(temp); } FILE_LOG(logDEBUG1) << "Set ROI narg: " << narg; @@ -682,1580 +537,777 @@ int slsReceiverTCPIPInterface::set_roi(sls::ServerInterface2 &socket) { if (myDetectorType == EIGER || myDetectorType == JUNGFRAU) functionNotImplemented(); - // base object not null - else if (receiver == nullptr) - NullObjectError(ret, mess); - else { - if (VerifyLockAndIdle(ret, mess, fnum) == OK) - ret = receiver->setROI(arg); - } - return socket.sendResult(true, ret, nullptr, 0, mess); + VerifyIdle(socket); + if (impl()->setROI(arg) == FAIL) + throw RuntimeError("Could not set ROI"); + return socket.sendData(OK); } - - -int slsReceiverTCPIPInterface::setup_udp(sls::ServerInterface2 &socket){ +int slsReceiverTCPIPInterface::setup_udp(Interface &socket){ ret = OK; - memset(mess, 0, sizeof(mess)); char args[5][MAX_STR_LENGTH]{}; char retvals[2][MAX_STR_LENGTH]{}; + socket.receiveArg(args); + VerifyIdle(socket); - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, args, sizeof(args)); - if (receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; + //setup interfaces count + int numInterfaces = atoi(args[0]) > 1 ? 2 : 1; + char* ip1 = args[1]; + char* ip2 = args[2]; + uint32_t port1 = atoi(args[3]); + uint32_t port2 = atoi(args[4]); + + // 1st interface + impl()->setUDPPortNumber(port1); + if (myDetectorType == EIGER) { + impl()->setUDPPortNumber2(port2); } - - - // base object not null - if (ret == OK) { - // only set - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - - //setup interfaces count - int numInterfaces = atoi(args[0]) > 1 ? 2 : 1; - char* ip1 = args[1]; - char* ip2 = args[2]; - uint32_t port1 = atoi(args[3]); - uint32_t port2 = atoi(args[4]); - - // 1st interface - receiver->setUDPPortNumber(port1); - if (myDetectorType == EIGER) { - receiver->setUDPPortNumber2(port2); - } - FILE_LOG(logINFO) << "Receiver UDP IP: " << ip1; - // get eth - std::string temp = sls::IpToInterfaceName(ip1); - if (temp == "none"){ - ret = FAIL; - strcpy(mess, "Failed to get ethernet interface or IP \n"); - FILE_LOG(logERROR) << mess; + FILE_LOG(logINFO) << "Receiver UDP IP: " << ip1; + // get eth + std::string temp = sls::IpToInterfaceName(ip1); + if (temp == "none"){ + throw RuntimeError("Failed to get ethernet interface or IP"); + } else { + char eth[MAX_STR_LENGTH]{}; + strcpy(eth, temp.c_str()); + // if there is a dot in eth name + if (strchr(eth, '.') != nullptr) { + strcpy(eth, ""); + sprintf(mess, "Failed to get ethernet interface from IP. Got %s\n", temp.c_str()); + FILE_LOG(logERROR) << mess; + } + impl()->setEthernetInterface(eth); + if (myDetectorType == EIGER) { + impl()->setEthernetInterface2(eth); + } + //get mac address + if (ret != FAIL) { + temp = sls::InterfaceNameToMac(eth).str(); + if (temp=="00:00:00:00:00:00") { + throw RuntimeError("failed to get mac adddress to listen to\n"); } else { - char eth[MAX_STR_LENGTH] = {""}; - memset(eth, 0, MAX_STR_LENGTH); - strcpy(eth, temp.c_str()); - // if there is a dot in eth name - if (strchr(eth, '.') != nullptr) { - strcpy(eth, ""); - ret = FAIL; - sprintf(mess, "Failed to get ethernet interface from IP. Got %s\n", temp.c_str()); - FILE_LOG(logERROR) << mess; - } - receiver->setEthernetInterface(eth); - if (myDetectorType == EIGER) { - receiver->setEthernetInterface2(eth); - } - //get mac address - if (ret != FAIL) { - temp = sls::InterfaceNameToMac(eth).str(); - if (temp=="00:00:00:00:00:00") { - ret = FAIL; - strcpy(mess,"failed to get mac adddress to listen to\n"); - FILE_LOG(logERROR) << mess; - } else { - strcpy(retvals[0],temp.c_str()); - FILE_LOG(logINFO) << "Receiver MAC Address: " << retvals[0]; - } - } - } - - // 2nd interface - if (myDetectorType == JUNGFRAU && numInterfaces == 2) { - receiver->setUDPPortNumber2(port2); - FILE_LOG(logINFO) << "Receiver UDP IP 2: " << ip2; - // get eth - temp = sls::IpToInterfaceName(ip2); - if (temp == "none"){ - ret = FAIL; - strcpy(mess, "Failed to get 2nd ethernet interface or IP \n"); - FILE_LOG(logERROR) << mess; - } else { - char eth[MAX_STR_LENGTH] = {""}; - memset(eth, 0, MAX_STR_LENGTH); - strcpy(eth, temp.c_str()); - // if there is a dot in eth name - if (strchr(eth, '.') != nullptr) { - strcpy(eth, ""); - ret = FAIL; - sprintf(mess, "Failed to get 2nd ethernet interface from IP. Got %s\n", temp.c_str()); - FILE_LOG(logERROR) << mess; - } - receiver->setEthernetInterface2(eth); - - //get mac address - if (ret != FAIL) { - temp = sls::InterfaceNameToMac(eth).str(); - if (temp=="00:00:00:00:00:00") { - ret = FAIL; - strcpy(mess,"failed to get 2nd mac adddress to listen to\n"); - FILE_LOG(logERROR) << mess; - } else { - strcpy(retvals[1],temp.c_str()); - FILE_LOG(logINFO) << "Receiver MAC Address 2: " << retvals[1]; - } - } - } - } - - // set the number of udp interfaces (changes number of threads and many others) - if (myDetectorType == JUNGFRAU && receiver->setNumberofUDPInterfaces(numInterfaces) == FAIL) { - ret = FAIL; - sprintf(mess, "Failed to set number of interfaces\n"); - FILE_LOG(logERROR) << mess; + strcpy(retvals[0],temp.c_str()); + FILE_LOG(logINFO) << "Receiver MAC Address: " << retvals[0]; } } } - return socket.sendResult(true, ret, retvals, sizeof(retvals), mess); + + // 2nd interface + if (myDetectorType == JUNGFRAU && numInterfaces == 2) { + impl()->setUDPPortNumber2(port2); + FILE_LOG(logINFO) << "Receiver UDP IP 2: " << ip2; + // get eth + temp = sls::IpToInterfaceName(ip2); + if (temp == "none"){ + throw RuntimeError("Failed to get 2nd ethernet interface or IP"); + } else { + char eth[MAX_STR_LENGTH] = {""}; + memset(eth, 0, MAX_STR_LENGTH); + strcpy(eth, temp.c_str()); + // if there is a dot in eth name + if (strchr(eth, '.') != nullptr) { + strcpy(eth, ""); + sprintf(mess, "Failed to get 2nd ethernet interface from IP. Got %s\n", temp.c_str()); + FILE_LOG(logERROR) << mess; + } + impl()->setEthernetInterface2(eth); + + //get mac address + if (ret != FAIL) { + temp = sls::InterfaceNameToMac(eth).str(); + if (temp=="00:00:00:00:00:00") { + throw RuntimeError("failed to get 2nd mac adddress to listen to"); + FILE_LOG(logERROR) << mess; + } else { + strcpy(retvals[1],temp.c_str()); + FILE_LOG(logINFO) << "Receiver MAC Address 2: " << retvals[1]; + } + } + } + } + + // set the number of udp interfaces (changes number of threads and many others) + if (myDetectorType == JUNGFRAU && impl()->setNumberofUDPInterfaces(numInterfaces) == FAIL) { + throw RuntimeError("Failed to set number of interfaces"); + } + return socket.sendResult(ret, retvals, sizeof(retvals), mess); } - - - -int slsReceiverTCPIPInterface::set_timer(sls::ServerInterface2 &socket) { - ret = OK; +int slsReceiverTCPIPInterface::set_timer(Interface &socket) { memset(mess, 0, sizeof(mess)); int64_t index[2] = {-1, -1}; int64_t retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { + socket.receiveArg(index); + if (index[1] >= 0) { FILE_LOG(logDEBUG1) << "Setting timer index " << index[0] << " to " << index[1]; - - // set - if (index[1] >= 0) { - // verify if receiver is unlocked - if (VerifyLock(ret, mess) == OK) { - switch (index[0]) { - case ACQUISITION_TIME: - ret = receiver->setAcquisitionTime(index[1]); - break; - case FRAME_PERIOD: - ret = receiver->setAcquisitionPeriod(index[1]); - break; - case FRAME_NUMBER: - case CYCLES_NUMBER: - case STORAGE_CELL_NUMBER: - receiver->setNumberOfFrames(index[1]); - break; - case SUBFRAME_ACQUISITION_TIME: - receiver->setSubExpTime(index[1]); - break; - case SUBFRAME_DEADTIME: - receiver->setSubPeriod(index[1] + receiver->getSubExpTime()); - break; - case ANALOG_SAMPLES: - if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { - modeNotImplemented("(Analog Samples) Timer index", (int)index[0]); - break; - } - receiver->setNumberofAnalogSamples(index[1]); - break; - case DIGITAL_SAMPLES: - if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { - modeNotImplemented("(Digital Samples) Timer index", (int)index[0]); - break; - } - receiver->setNumberofDigitalSamples(index[1]); - break; - default: - modeNotImplemented("Timer index", (int)index[0]); - break; - } - } - } - // get switch (index[0]) { case ACQUISITION_TIME: - retval=receiver->getAcquisitionTime(); + ret = impl()->setAcquisitionTime(index[1]); break; case FRAME_PERIOD: - retval=receiver->getAcquisitionPeriod(); + ret = impl()->setAcquisitionPeriod(index[1]); break; case FRAME_NUMBER: case CYCLES_NUMBER: case STORAGE_CELL_NUMBER: - retval=receiver->getNumberOfFrames(); + impl()->setNumberOfFrames(index[1]); break; case SUBFRAME_ACQUISITION_TIME: - retval=receiver->getSubExpTime(); + impl()->setSubExpTime(index[1]); break; case SUBFRAME_DEADTIME: - retval=(receiver->getSubPeriod() - receiver->getSubExpTime()); + impl()->setSubPeriod(index[1] + impl()->getSubExpTime()); break; case ANALOG_SAMPLES: if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { - ret = FAIL; - sprintf(mess,"This timer mode (%lld) does not exist for this receiver type\n", (long long int)index[0]); - FILE_LOG(logERROR) << "Warning: " << mess; + modeNotImplemented("(Analog Samples) Timer index", (int)index[0]); break; } - retval=receiver->getNumberofAnalogSamples(); + impl()->setNumberofAnalogSamples(index[1]); break; case DIGITAL_SAMPLES: if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { - ret = FAIL; - sprintf(mess,"This timer mode (%lld) does not exist for this receiver type\n", (long long int)index[0]); - FILE_LOG(logERROR) << "Warning: " << mess; + modeNotImplemented("(Digital Samples) Timer index", (int)index[0]); break; } - retval=receiver->getNumberofDigitalSamples(); + impl()->setNumberofDigitalSamples(index[1]); break; default: modeNotImplemented("Timer index", (int)index[0]); break; } - validate((int)index[1], (int)retval, std::string("set timer"), DEC); - FILE_LOG(logDEBUG1) << slsDetectorDefs::getTimerType((timerIndex)(index[0])) << ":" << retval; + } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + // get + switch (index[0]) { + case ACQUISITION_TIME: + retval=impl()->getAcquisitionTime(); + break; + case FRAME_PERIOD: + retval=impl()->getAcquisitionPeriod(); + break; + case FRAME_NUMBER: + case CYCLES_NUMBER: + case STORAGE_CELL_NUMBER: + retval=impl()->getNumberOfFrames(); + break; + case SUBFRAME_ACQUISITION_TIME: + retval=impl()->getSubExpTime(); + break; + case SUBFRAME_DEADTIME: + retval=(impl()->getSubPeriod() - impl()->getSubExpTime()); + break; + case ANALOG_SAMPLES: + if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { + sprintf(mess,"This timer mode (%lld) does not exist for this receiver type\n", (long long int)index[0]); + throw RuntimeError(mess); + } + retval=impl()->getNumberofAnalogSamples(); + break; + case DIGITAL_SAMPLES: + if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { + sprintf(mess,"This timer mode (%lld) does not exist for this receiver type\n", (long long int)index[0]); + throw RuntimeError(mess); + } + retval=impl()->getNumberofDigitalSamples(); + break; + default: + modeNotImplemented("Timer index", (int)index[0]); + break; + } + validate((int)index[1], (int)retval, std::string("set timer"), DEC); + FILE_LOG(logDEBUG1) << slsDetectorDefs::getTimerType((timerIndex)(index[0])) << ":" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_dynamic_range(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int dr = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &dr, sizeof(dr)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (dr >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting dynamic range: " << dr; - bool exists = false; - switch (dr) { - case 16: - exists = true; - break; - case 4: - case 8: - case 32: - if (myDetectorType == EIGER) - exists = true; - break; - default: - break; - } - // invalid dr - if (!exists) { - modeNotImplemented("Dynamic range", dr); - } - // valid dr - else { - ret = receiver->setDynamicRange(dr); - if(ret == FAIL) { - strcpy(mess, "Could not allocate memory for fifo or could not start listening/writing threads\n"); - FILE_LOG(logERROR) << mess; - } - } +int slsReceiverTCPIPInterface::set_dynamic_range(Interface &socket) { + auto dr = socket.receive(); + if (dr >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting dynamic range: " << dr; + bool exists = false; + switch (dr) { + case 16: + exists = true; + break; + case 4: + case 8: + case 32: + if (myDetectorType == EIGER) + exists = true; + break; + default: + break; + } + if (!exists) { + modeNotImplemented("Dynamic range", dr); + } + else { + ret = impl()->setDynamicRange(dr); + if(ret == FAIL) { + throw RuntimeError("Could not allocate memory for fifo or could not start listening/writing threads"); } } - // get - retval = receiver->getDynamicRange(); - validate(dr, retval, std::string("set dynamic range"), DEC); - FILE_LOG(logDEBUG1) << "dynamic range: " << retval; + } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval = impl()->getDynamicRange(); + validate(dr, retval, std::string("set dynamic range"), DEC); + FILE_LOG(logDEBUG1) << "dynamic range: " << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_streaming_frequency(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting streaming frequency: " << index; - ret = receiver->setStreamingFrequency(index); - if(ret == FAIL) { - strcpy(mess, "Could not allocate memory for listening fifo\n"); - FILE_LOG(logERROR) << mess; - } - } - } - // get - retval = receiver->getStreamingFrequency(); - validate(index, retval, std::string("set streaming frequency"), DEC); - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::get_status(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - enum runStatus retval = ERROR; - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - - if (ret == OK) { - FILE_LOG(logDEBUG1) << "Getting Status"; - retval = receiver->getStatus(); - FILE_LOG(logDEBUG1) << "Status:" << runStatusType(retval); - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::start_receiver(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - // receiver is not null - if (ret == OK) { - // only set - // verify if receiver is unlocked - if (VerifyLock(ret, mess) == OK) { - // should not be idle - enum runStatus s = receiver->getStatus(); - if (s != IDLE) { - ret=FAIL; - sprintf(mess,"Cannot start Receiver as it is in %s state\n",runStatusType(s).c_str()); - FILE_LOG(logERROR) << mess; - }else { - FILE_LOG(logDEBUG1) << "Starting Receiver"; - ret = receiver->startReceiver(mess); - if (ret == FAIL) { - FILE_LOG(logERROR) << mess; - } - } +int slsReceiverTCPIPInterface::set_streaming_frequency(Interface &socket) { + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting streaming frequency: " << index; + ret = impl()->setStreamingFrequency(index); + if(ret == FAIL) { + throw RuntimeError("Could not allocate memory for listening fifo"); } } - return socket.sendResult(true, ret, nullptr, 0, mess); + int retval = impl()->getStreamingFrequency(); + validate(index, retval, std::string("set streaming frequency"), DEC); + return socket.sendResult(retval); } +int slsReceiverTCPIPInterface::get_status(Interface &socket){ + auto retval = impl()->getStatus(); + FILE_LOG(logDEBUG1) << "Status:" << runStatusType(retval); + return socket.sendResult(retval); +} +int slsReceiverTCPIPInterface::start_receiver(Interface &socket){ + runStatus status = impl()->getStatus(); + if (status != IDLE) { + throw RuntimeError("Cannot start Receiver as it is: " +runStatusType(status)); + }else { + FILE_LOG(logDEBUG1) << "Starting Receiver"; + ret = impl()->startReceiver(mess); + if (ret == FAIL) { + throw RuntimeError(mess); + } + } + return socket.sendData(OK); +} -int slsReceiverTCPIPInterface::stop_receiver(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); +int slsReceiverTCPIPInterface::stop_receiver(Interface &socket){ + if(impl()->getStatus() != IDLE) { + FILE_LOG(logDEBUG1) << "Stopping Receiver"; + impl()->stopReceiver(); + } + auto s = impl()->getStatus(); + if (s != IDLE) + throw RuntimeError("Could not stop receiver. It as it is: " + runStatusType(s)); + return socket.sendData(OK); +} + +int slsReceiverTCPIPInterface::set_file_dir(Interface &socket) { + char fPath[MAX_STR_LENGTH]{}; + char retval[MAX_STR_LENGTH]{}; + socket.receiveArg(fPath); + + if (strlen(fPath)) { + FILE_LOG(logDEBUG1) << "Setting file path: " << fPath; + impl()->setFilePath(fPath); + } + std::string s = impl()->getFilePath(); + strcpy(retval, s.c_str()); + if ((!s.length()) || (strlen(fPath) && strcasecmp(fPath, retval))) + throw RuntimeError("Receiver file path does not exist"); + else + FILE_LOG(logDEBUG1) << "file path:" << retval; - if(receiver == nullptr){ - NullObjectError(ret, mess); + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::set_file_name(Interface &socket) { + char fName[MAX_STR_LENGTH]{}; + char retval[MAX_STR_LENGTH]{}; + socket.receiveArg(fName); + if (strlen(fName)) { + FILE_LOG(logDEBUG1) << "Setting file name: " << fName; + impl()->setFileName(fName); } - // receiver is not null - if (ret == OK) { - // only set - // verify if receiver is unlocked - if (VerifyLock(ret, mess) == OK) { - if(receiver->getStatus() != IDLE) { - FILE_LOG(logDEBUG1) << "Stopping Receiver"; - receiver->stopReceiver(); - } - enum runStatus s = receiver->getStatus(); - if (s == IDLE) - ret = OK; - else { - ret = FAIL; - sprintf(mess,"Could not stop receiver. It is in %s state\n",runStatusType(s).c_str()); - FILE_LOG(logERROR) << mess; - } - } + std::string s = impl()->getFileName(); + if (s.empty()) + throw RuntimeError("file name is empty"); + + strcpy(retval, s.c_str()); + FILE_LOG(logDEBUG1) << "file name:" << retval; + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::set_file_index(Interface &socket) { + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting file index: " << index; + impl()->setFileIndex(index); } - return socket.sendResult(true, ret, nullptr, 0, mess); + int retval = impl()->getFileIndex(); + validate(index, retval, std::string("set file index"), DEC); + FILE_LOG(logDEBUG1) << "file index:" << retval; + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::get_frame_index(Interface &socket){ + uint64_t retval = impl()->getAcquisitionIndex(); + FILE_LOG(logDEBUG1) << "frame index:" << retval; + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::get_frames_caught(Interface &socket){ + int retval = impl()->getTotalFramesCaught(); + FILE_LOG(logDEBUG1) << "frames caught:" << retval; + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::reset_frames_caught(Interface &socket){ + FILE_LOG(logDEBUG1) << "Reset frames caught"; + impl()->resetAcquisitionCount(); + return socket.sendData(OK); +} + +int slsReceiverTCPIPInterface::enable_file_write(Interface &socket){ + auto enable = socket.receive(); + if (enable >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting File write enable:" << enable; + impl()->setFileWriteEnable(enable); + } + int retval = impl()->getFileWriteEnable(); + validate(enable, retval, std::string("set file write enable"), DEC); + FILE_LOG(logDEBUG1) << "file write enable:" << retval; + return socket.sendResult(retval); } - -int slsReceiverTCPIPInterface::set_file_dir(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - char fPath[MAX_STR_LENGTH] = {0}; - char retval[MAX_STR_LENGTH] = {0}; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, fPath, sizeof(fPath)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::enable_master_file_write(Interface &socket){ + auto enable = socket.receive(); + if (enable >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting Master File write enable:" << enable; + impl()->setMasterFileWriteEnable(enable); } - - // base object not null - if (ret == OK) { - // set - if (strlen(fPath)) { - FILE_LOG(logDEBUG1) << "Setting file path: " << fPath; - receiver->setFilePath(fPath); - } - // get - std::string s = receiver->getFilePath(); - strcpy(retval, s.c_str()); - if ((!s.length()) || (strlen(fPath) && strcasecmp(fPath, retval))) { - ret = FAIL; - strcpy(mess,"receiver file path does not exist\n"); - FILE_LOG(logERROR) << mess; - } else - FILE_LOG(logDEBUG1) << "file path:" << retval; - } - return socket.sendResult(true, ret, retval, MAX_STR_LENGTH, mess); + int retval = impl()->getMasterFileWriteEnable(); + validate(enable, retval, std::string("set master file write enable"), DEC); + FILE_LOG(logDEBUG1) << "master file write enable:" << retval; + return socket.sendResult(retval); } - -int slsReceiverTCPIPInterface::set_file_name(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - char fName[MAX_STR_LENGTH] = {0}; - char retval[MAX_STR_LENGTH] = {0}; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, fName, sizeof(fName)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::enable_overwrite(Interface &socket) { + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting File overwrite enable:" << index; + impl()->setOverwriteEnable(index); } - - // base object not null - if (ret == OK) { - // set - if (strlen(fName)) { - FILE_LOG(logDEBUG1) << "Setting file name: " << fName; - receiver->setFileName(fName); - } - // get - std::string s = receiver->getFileName(); - strcpy(retval, s.c_str()); - if (!s.length()) { - ret = FAIL; - strcpy(mess, "file name is empty\n"); - FILE_LOG(logERROR) << mess; - } else - FILE_LOG(logDEBUG1) << "file name:" << retval; - } - return socket.sendResult(true, ret, retval, MAX_STR_LENGTH, mess); + int retval = impl()->getOverwriteEnable(); + validate(index, retval, std::string("set file overwrite enable"), DEC); + FILE_LOG(logDEBUG1) << "file overwrite enable:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_file_index(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting file index: " << index; - receiver->setFileIndex(index); - } - } - // get - retval=receiver->getFileIndex(); - validate(index, retval, std::string("set file index"), DEC); - FILE_LOG(logDEBUG1) << "file index:" << retval; - } - return socket.sendResult(true, ret, &retval,sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::get_frame_index(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - uint64_t retval = -1; - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - - if (ret == OK) { - FILE_LOG(logDEBUG1) << "Getting frame index"; - retval = receiver->getAcquisitionIndex(); - FILE_LOG(logDEBUG1) << "frame index:" << retval; - } - return socket.sendResult(true, ret, &retval,sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::get_frames_caught(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int retval = -1; - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - - if (ret == OK) { - FILE_LOG(logDEBUG1) << "Getting frames caught"; - retval = receiver->getTotalFramesCaught(); - FILE_LOG(logDEBUG1) << "frames caught:" << retval; - } - return socket.sendResult(true, ret, &retval,sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::reset_frames_caught(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - - // receiver is not null - if (ret == OK) { - // only set - // verify if receiver is unlocked - if (VerifyLock(ret, mess) == OK) { - FILE_LOG(logDEBUG1) << "Reset frames caught"; - receiver->resetAcquisitionCount(); - } - } - return socket.sendResult(true, ret, nullptr, 0, mess); -} - - - -int slsReceiverTCPIPInterface::enable_file_write(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int enable = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &enable, sizeof(enable)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (enable >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting File write enable:" << enable; - receiver->setFileWriteEnable(enable); - } - } - // get - retval = receiver->getFileWriteEnable(); - validate(enable, retval, std::string("set file write enable"), DEC); - FILE_LOG(logDEBUG1) << "file write enable:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::enable_master_file_write(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int enable = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &enable, sizeof(enable)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (enable >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting Master File write enable:" << enable; - receiver->setMasterFileWriteEnable(enable); - } - } - // get - retval = receiver->getMasterFileWriteEnable(); - validate(enable, retval, std::string("set master file write enable"), DEC); - FILE_LOG(logDEBUG1) << "master file write enable:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - -int slsReceiverTCPIPInterface::enable_overwrite(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting File overwrite enable:" << index; - receiver->setOverwriteEnable(index); - } - } - // get - retval = receiver->getOverwriteEnable(); - validate(index, retval, std::string("set file overwrite enable"), DEC); - FILE_LOG(logDEBUG1) << "file overwrite enable:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::enable_tengiga(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int val = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &val, sizeof(val)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - +int slsReceiverTCPIPInterface::enable_tengiga(Interface &socket) { + auto val = socket.receive(); if (myDetectorType != EIGER && myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) functionNotImplemented(); - // base object not null - else if (ret == OK) { - // set - if (val >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting 10GbE:" << val; - ret = receiver->setTenGigaEnable(val); - } - } - // get - retval = receiver->getTenGigaEnable(); - validate(val, retval, std::string("set 10GbE"), DEC); - FILE_LOG(logDEBUG1) << "10Gbe:" << retval; + if (val >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting 10GbE:" << val; + ret = impl()->setTenGigaEnable(val); + } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval = impl()->getTenGigaEnable(); + validate(val, retval, std::string("set 10GbE"), DEC); + FILE_LOG(logDEBUG1) << "10Gbe:" << retval; + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::set_fifo_depth(Interface &socket) { + auto value = socket.receive(); + if (value >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting fifo depth:" << value; + impl()->setFifoDepth(value); + } + int retval = impl()->getFifoDepth(); + validate(value, retval, std::string("set fifo depth"), DEC); + FILE_LOG(logDEBUG1) << "fifo depth:" << retval; + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::set_activate(Interface &socket) { + auto enable = socket.receive(); + if (myDetectorType != EIGER) + functionNotImplemented(); + + if (enable >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting activate:" << enable; + impl()->setActivate(enable > 0 ? true : false); + } + auto retval = static_cast(impl()->getActivate()); + validate(enable, retval, std::string("set activate"), DEC); + FILE_LOG(logDEBUG1) << "Activate: " << retval; + return socket.sendResult(retval); +} + +int slsReceiverTCPIPInterface::set_data_stream_enable(Interface &socket){ + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting data stream enable:" << index; + ret = impl()->setDataStreamEnable(index); + } + int retval = impl()->getDataStreamEnable(); + validate(index, retval, std::string("set data stream enable"), DEC); + FILE_LOG(logDEBUG1) << "data streaming enable:" << retval; + return socket.sendResult(retval); } -int slsReceiverTCPIPInterface::set_fifo_depth(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int value = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &value, sizeof(value)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_streaming_timer(Interface &socket){ + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting streaming timer:" << index; + impl()->setStreamingTimer(index); } - - // base object not null - if (ret == OK) { - // set - if (value >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting fifo depth:" << value; - ret = receiver->setFifoDepth(value); - if (ret == FAIL) { - strcpy(mess,"Could not set fifo depth"); - FILE_LOG(logERROR) << mess; - } - } - } - // get - retval = receiver->getFifoDepth(); - validate(value, retval, std::string("set fifo depth"), DEC); - FILE_LOG(logDEBUG1) << "fifo depth:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval=impl()->getStreamingTimer(); + validate(index, retval, std::string("set data stream timer"), DEC); + FILE_LOG(logDEBUG1) << "Streaming timer:" << retval; + return socket.sendResult(retval); } -int slsReceiverTCPIPInterface::set_activate(sls::ServerInterface2 &socket) { - ret = OK; +int slsReceiverTCPIPInterface::set_flipped_data(Interface &socket){ + //TODO! Why 2 args? memset(mess, 0, sizeof(mess)); - int enable = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &enable, sizeof(enable)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } + int args[2]{0,-1}; + socket.receiveArg(args); if (myDetectorType != EIGER) functionNotImplemented(); - // base object not null - else if (ret == OK) { - // set - if (enable >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting activate:" << enable; - receiver->setActivate(enable > 0 ? true : false); - } - } - // get - retval = (int)receiver->getActivate(); - validate(enable, retval, std::string("set activate"), DEC); - FILE_LOG(logDEBUG1) << "Activate: " << retval; + if (args[1] >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting flipped data:" << args[1]; + impl()->setFlippedData(args[0],args[1]); } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval = impl()->getFlippedData(args[0]); + validate(args[1], retval, std::string("set flipped data"), DEC); + FILE_LOG(logDEBUG1) << "Flipped Data:" << retval; + return socket.sendResult(retval); } -int slsReceiverTCPIPInterface::set_data_stream_enable(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting data stream enable:" << index; - ret = receiver->setDataStreamEnable(index); - } - } - // get - retval = receiver->getDataStreamEnable(); - validate(index, retval, std::string("set data stream enable"), DEC); - FILE_LOG(logDEBUG1) << "data streaming enable:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::set_streaming_timer(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting streaming timer:" << index; - receiver->setStreamingTimer(index); - } - } - // get - retval=receiver->getStreamingTimer(); - validate(index, retval, std::string("set data stream timer"), DEC); - FILE_LOG(logDEBUG1) << "Streaming timer:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::set_flipped_data(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int args[2] = {0,-1}; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, args, sizeof(args)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - if (myDetectorType != EIGER) - functionNotImplemented(); - - // base object not null - else if (ret == OK) { - // set - if (args[1] >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting flipped data:" << args[1]; - receiver->setFlippedData(args[0],args[1]); - } - } - // get - retval=receiver->getFlippedData(args[0]); - validate(args[1], retval, std::string("set flipped data"), DEC); - FILE_LOG(logDEBUG1) << "Flipped Data:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::set_file_format(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - fileFormat retval = GET_FILE_FORMAT; +int slsReceiverTCPIPInterface::set_file_format(Interface &socket) { fileFormat f = GET_FILE_FORMAT; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &f, sizeof(f)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; + socket.receiveArg(f); + if (f >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting file format:" << f; + impl()->setFileFormat(f); } - - // base object not null - if (ret == OK) { - // set - if (f >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting file format:" << f; - receiver->setFileFormat(f); - } - } - // get - retval = receiver->getFileFormat(); - validate(f, retval, std::string("set file format"), DEC); - FILE_LOG(logDEBUG1) << "File Format: " << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + auto retval = impl()->getFileFormat(); + validate(f, retval, std::string("set file format"), DEC); + FILE_LOG(logDEBUG1) << "File Format: " << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_detector_posid(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int arg = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &arg, sizeof(arg)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_detector_posid(Interface &socket) { + auto arg = socket.receive(); + if (arg >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting detector position id:" << arg; + impl()->setDetectorPositionId(arg); } - - // base object not null - if (ret == OK) { - // set - if (arg >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting detector position id:" << arg; - receiver->setDetectorPositionId(arg); - } - } - // get - retval = receiver->getDetectorPositionId(); - validate(arg, retval, std::string("set detector position id"), DEC); - FILE_LOG(logDEBUG1) << "Position Id:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + auto retval = impl()->getDetectorPositionId(); + validate(arg, retval, std::string("set detector position id"), DEC); + FILE_LOG(logDEBUG1) << "Position Id:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_multi_detector_size(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); +int slsReceiverTCPIPInterface::set_multi_detector_size(Interface &socket) { int arg[]{-1, -1}; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, arg, sizeof(arg)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; + socket.receiveArg(arg); + if((arg[0] > 0) && (arg[1] > 0)) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting multi detector size:" << arg[0] << "," << arg[1]; + impl()->setMultiDetectorSize(arg); + } - - // base object not null - if (ret == OK) { - // set - if((arg[0] > 0) && (arg[1] > 0)) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting multi detector size:" << arg[0] << "," << arg[1]; - receiver->setMultiDetectorSize(arg); - } - } - // get - int* temp = receiver->getMultiDetectorSize(); - for (int i = 0; i < MAX_DIMENSIONS; ++i) { - if (!i) - retval = temp[i]; - else - retval *= temp[i]; - } - FILE_LOG(logDEBUG1) << "Multi Detector Size:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int* temp = impl()->getMultiDetectorSize(); //TODO! return by value! + int retval = temp[0]*temp[1]; + FILE_LOG(logDEBUG1) << "Multi Detector Size:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_streaming_port(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int port = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &port, sizeof(port)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_streaming_port(Interface &socket) { + auto port = socket.receive(); + if (port >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting streaming port:" << port; + impl()->setStreamingPort(port); } - - // base object not null - if (ret == OK) { - // set - if (port >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting streaming port:" << port; - receiver->setStreamingPort(port); - } - } - // get - retval = receiver->getStreamingPort(); - validate(port, retval, std::string("set streaming port"), DEC); - FILE_LOG(logDEBUG1) << "streaming port:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval = impl()->getStreamingPort(); + validate(port, retval, std::string("set streaming port"), DEC); + FILE_LOG(logDEBUG1) << "streaming port:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_streaming_source_ip(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - char arg[MAX_STR_LENGTH] = {0}; - char retval[MAX_STR_LENGTH] = {0}; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, arg, MAX_STR_LENGTH); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // only set - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting streaming source ip:" << arg; - receiver->setStreamingSourceIP(arg); - } - // get - strcpy(retval, receiver->getStreamingSourceIP().c_str()); - FILE_LOG(logDEBUG1) << "streaming source ip:" << retval; - } - return socket.sendResult(true, ret, retval, MAX_STR_LENGTH, mess); +int slsReceiverTCPIPInterface::set_streaming_source_ip(Interface &socket) { + char arg[MAX_STR_LENGTH]{}; + char retval[MAX_STR_LENGTH]{}; + socket.receiveArg(arg); + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting streaming source ip:" << arg; + impl()->setStreamingSourceIP(arg); + strcpy(retval, impl()->getStreamingSourceIP().c_str()); + FILE_LOG(logDEBUG1) << "streaming source ip:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_silent_mode(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int value = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &value, sizeof(value)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_silent_mode(Interface &socket) { + auto value = socket.receive(); + if (value >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting silent mode:" << value; + impl()->setSilentMode(value); } - - // base object not null - if (ret == OK) { - // set - if (value >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting silent mode:" << value; - receiver->setSilentMode(value); - } - } - // get - retval = (int)receiver->getSilentMode(); - validate(value, retval, std::string("set silent mode"), DEC); - FILE_LOG(logDEBUG1) << "silent mode:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + auto retval = static_cast(impl()->getSilentMode()); + validate(value, retval, std::string("set silent mode"), DEC); + FILE_LOG(logDEBUG1) << "silent mode:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::enable_gap_pixels(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int enable = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &enable, sizeof(enable)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - +int slsReceiverTCPIPInterface::enable_gap_pixels(Interface &socket) { + auto enable = socket.receive(); if (myDetectorType != EIGER) functionNotImplemented(); - // base object not null - else if (ret == OK) { - // set - if (enable >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting gap pixels enable:" << enable; - receiver->setGapPixelsEnable(enable); - } - } - // get - retval = receiver->getGapPixelsEnable(); - validate(enable, retval, std::string("set gap pixels enable"), DEC); - FILE_LOG(logDEBUG1) << "Gap Pixels Enable: " << retval; + if (enable >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting gap pixels enable:" << enable; + impl()->setGapPixelsEnable(enable); } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval = impl()->getGapPixelsEnable(); + validate(enable, retval, std::string("set gap pixels enable"), DEC); + FILE_LOG(logDEBUG1) << "Gap Pixels Enable: " << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::restream_stop(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - - if(receiver == nullptr){ - NullObjectError(ret, mess); +int slsReceiverTCPIPInterface::restream_stop(Interface &socket){ + VerifyIdle(socket); + if (impl()->getDataStreamEnable() == false) { + throw RuntimeError("Could not restream stop packet as data Streaming is disabled"); + } else { + FILE_LOG(logDEBUG1) << "Restreaming stop"; + ret = impl()->restreamStop(); + if (ret == FAIL) + throw RuntimeError("Could not restream stop packet"); } - - // receiver is not null - if (ret == OK) { - // only set - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - if (receiver->getDataStreamEnable() == false) { - ret = FAIL; - sprintf(mess,"Could not restream stop packet as data Streaming is disabled.\n"); - FILE_LOG(logERROR) << mess; - } else { - FILE_LOG(logDEBUG1) << "Restreaming stop"; - ret = receiver->restreamStop(); - if (ret == FAIL) { - sprintf(mess,"Could not restream stop packet.\n"); - FILE_LOG(logERROR) << mess; - } - } - } - } - return socket.sendResult(true, ret, nullptr, 0, mess); + return socket.sendData(OK); } - - -int slsReceiverTCPIPInterface::set_additional_json_header(sls::ServerInterface2 &socket) { - ret = OK; +int slsReceiverTCPIPInterface::set_additional_json_header(Interface &socket) { memset(mess, 0, sizeof(mess)); - char arg[MAX_STR_LENGTH] = {0}; - char retval[MAX_STR_LENGTH] = {0}; + char arg[MAX_STR_LENGTH]{}; + char retval[MAX_STR_LENGTH]{}; + socket.receiveArg(arg); + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting additional json header: " << arg; + impl()->setAdditionalJsonHeader(arg); + strcpy(retval, impl()->getAdditionalJsonHeader().c_str()); + FILE_LOG(logDEBUG1) << "additional json header:" << retval; + return socket.sendResult(retval); +} - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, arg, sizeof(arg)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } +int slsReceiverTCPIPInterface::get_additional_json_header(Interface &socket) { + char retval[MAX_STR_LENGTH]{}; + strcpy(retval, impl()->getAdditionalJsonHeader().c_str()); + FILE_LOG(logDEBUG1) << "additional json header:" << retval; + return socket.sendResult(retval); +} - // base object not null - if (ret == OK) { - // only set - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting additional json header: " << arg; - receiver->setAdditionalJsonHeader(arg); +int slsReceiverTCPIPInterface::set_udp_socket_buffer_size(Interface &socket) { + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting UDP Socket Buffer size: " << index; + if (impl()->setUDPSocketBufferSize(index) == FAIL) { + throw RuntimeError("Could not create dummy UDP Socket to test buffer size"); } - // get - strcpy(retval, receiver->getAdditionalJsonHeader().c_str()); - FILE_LOG(logDEBUG1) << "additional json header:" << retval; } - return socket.sendResult(true, ret, retval, MAX_STR_LENGTH, mess); + int64_t retval = impl()->getUDPSocketBufferSize(); + if (index != 0) + validate(index, retval, std::string("set udp socket buffer size (No CAP_NET_ADMIN privileges?)"), DEC); + FILE_LOG(logDEBUG1) << "UDP Socket Buffer Size:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::get_additional_json_header(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - char retval[MAX_STR_LENGTH] = {0}; - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - - // base object not null - if (ret == OK) { - // get - strcpy(retval, receiver->getAdditionalJsonHeader().c_str()); - FILE_LOG(logDEBUG1) << "additional json header:" << retval; - } - return socket.sendResult(true, ret, retval, MAX_STR_LENGTH, mess); +int slsReceiverTCPIPInterface::get_real_udp_socket_buffer_size(Interface &socket){ + auto size = impl()->getActualUDPSocketBufferSize(); + FILE_LOG(logDEBUG1) << "Actual UDP socket size :" << size; + return socket.sendResult(size); } - - -int slsReceiverTCPIPInterface::set_udp_socket_buffer_size(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int64_t index = -1; - int64_t retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_frames_per_file(Interface &socket) { + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting frames per file: " << index; + impl()->setFramesPerFile(index); } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting UDP Socket Buffer size: " << index; - if (receiver->setUDPSocketBufferSize(index) == FAIL) { - ret = FAIL; - strcpy(mess, "Could not create dummy UDP Socket to test buffer size\n"); - FILE_LOG(logERROR) << mess; - } - } - } - // get - retval = receiver->getUDPSocketBufferSize(); - if (index != 0) - validate(index, retval, std::string("set udp socket buffer size (No CAP_NET_ADMIN privileges?)"), DEC); - FILE_LOG(logDEBUG1) << "UDP Socket Buffer Size:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + auto retval = static_cast(impl()->getFramesPerFile()); + validate(index, retval, std::string("set frames per file"), DEC); + FILE_LOG(logDEBUG1) << "frames per file:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::get_real_udp_socket_buffer_size(sls::ServerInterface2 &socket){ - ret = OK; - memset(mess, 0, sizeof(mess)); - int64_t retval = -1; - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - - if (ret == OK) { - FILE_LOG(logDEBUG1) << "Getting actual UDP buffer size"; - retval = receiver->getActualUDPSocketBufferSize(); - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::set_frames_per_file(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting frames per file: " << index; - receiver->setFramesPerFile(index); - } - } - // get - retval = receiver->getFramesPerFile(); - validate(index, retval, std::string("set frames per file"), DEC); - FILE_LOG(logDEBUG1) << "frames per file:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); -} - - - -int slsReceiverTCPIPInterface::check_version_compatibility(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int64_t arg = -1; - // get args, return if socket crashed - socket.receiveArg(ret, mess, &arg, sizeof(arg)); - if (ret == FAIL) - return FAIL; - +int slsReceiverTCPIPInterface::check_version_compatibility(Interface &socket) { + auto arg = socket.receive(); FILE_LOG(logDEBUG1) << "Checking versioning compatibility with value " << arg; int64_t client_requiredVersion = arg; int64_t rx_apiVersion = APIRECEIVER; int64_t rx_version = getReceiverVersion(); - // old client if (rx_apiVersion > client_requiredVersion) { + // old client ret = FAIL; sprintf(mess,"This client is incompatible.\n" "Client's receiver API Version: (0x%llx). Receiver API Version: (0x%llx).\n" "Incompatible, update client!\n", (long long unsigned int)client_requiredVersion, (long long unsigned int)rx_apiVersion); - FILE_LOG(logERROR) << mess; - } - - // old software - else if (client_requiredVersion > rx_version) { + throw RuntimeError(mess); + }else if (client_requiredVersion > rx_version) { + // old software ret = FAIL; sprintf(mess,"This receiver is incompatible.\n" "Receiver Version: (0x%llx). Client's receiver API Version: (0x%llx).\n" "Incompatible, update receiver!\n", (long long unsigned int)rx_version, (long long unsigned int)client_requiredVersion); - FILE_LOG(logERROR) << mess; + throw RuntimeError(mess); } - else FILE_LOG(logINFO) << "Compatibility with Client: Successful"; - return socket.sendResult(true, ret, nullptr, 0, mess); + else{ + FILE_LOG(logINFO) << "Compatibility with Client: Successful"; + } + return socket.sendData(OK); } - - -int slsReceiverTCPIPInterface::set_discard_policy(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_discard_policy(Interface &socket) { + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting frames discard policy: " << index; + impl()->setFrameDiscardPolicy((frameDiscardPolicy)index); } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting frames discard policy: " << index; - receiver->setFrameDiscardPolicy((frameDiscardPolicy)index); - } - } - // get - retval = receiver->getFrameDiscardPolicy(); - validate(index, retval, std::string("set discard policy"), DEC); - FILE_LOG(logDEBUG1) << "frame discard policy:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval = impl()->getFrameDiscardPolicy(); + validate(index, retval, std::string("set discard policy"), DEC); + FILE_LOG(logDEBUG1) << "frame discard policy:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_padding_enable(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int index = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &index, sizeof(index)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_padding_enable(Interface &socket) { + auto index = socket.receive(); + if (index >= 0) { + VerifyIdle(socket); + index = (index == 0) ? 0 : 1; + FILE_LOG(logDEBUG1) << "Setting frames padding enable: " << index; + impl()->setFramePaddingEnable(index); } - - // base object not null - if (ret == OK) { - // set - if (index >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - index = (index == 0) ? 0 : 1; - FILE_LOG(logDEBUG1) << "Setting frames padding enable: " << index; - receiver->setFramePaddingEnable(index); - } - } - // get - retval = (int)receiver->getFramePaddingEnable(); - validate(index, retval, std::string("set frame padding enable"), DEC); - FILE_LOG(logDEBUG1) << "Frame Padding Enable:" << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + auto retval = static_cast(impl()->getFramePaddingEnable()); + validate(index, retval, std::string("set frame padding enable"), DEC); + FILE_LOG(logDEBUG1) << "Frame Padding Enable:" << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_deactivated_padding_enable(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int enable = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &enable, sizeof(enable)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } - +int slsReceiverTCPIPInterface::set_deactivated_padding_enable(Interface &socket) { + auto enable = socket.receive(); if (myDetectorType != EIGER) functionNotImplemented(); - // base object not null - else if (ret == OK) { - // set - if (enable >= 0) { - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting deactivated padding enable: " << enable; - receiver->setDeactivatedPadding(enable > 0 ? true : false); - } - } - // get - retval = (int)receiver->getDeactivatedPadding(); - validate(enable, retval, std::string("set deactivated padding enable"), DEC); - FILE_LOG(logDEBUG1) << "Deactivated Padding Enable: " << retval; + if (enable >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting deactivated padding enable: " << enable; + impl()->setDeactivatedPadding(enable > 0 ? true : false); } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + auto retval = static_cast(impl()->getDeactivatedPadding()); + validate(enable, retval, std::string("set deactivated padding enable"), DEC); + FILE_LOG(logDEBUG1) << "Deactivated Padding Enable: " << retval; + return socket.sendResult(retval); } - -int slsReceiverTCPIPInterface::set_readout_flags(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - readOutFlags arg = GET_READOUT_FLAGS; - readOutFlags retval = GET_READOUT_FLAGS; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &arg, sizeof(arg)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; - } +int slsReceiverTCPIPInterface::set_readout_flags(Interface &socket) { + auto arg = socket.receive(); if (myDetectorType == JUNGFRAU || myDetectorType == GOTTHARD || myDetectorType == MOENCH) functionNotImplemented(); - // base object not null - else if (ret == OK) { - // set - if (arg >= 0) { - // verify if receiver is unlocked and idle - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting readout flag: " << arg; - ret = receiver->setReadOutFlags(arg); - } - } - // get - retval = receiver->getReadOutFlags(); - validate((int)arg, (int)(retval & arg), std::string("set readout flags"), HEX); - FILE_LOG(logDEBUG1) << "Readout flags: " << retval; + if (arg >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting readout flag: " << arg; + ret = impl()->setReadOutFlags(arg); } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + auto retval = impl()->getReadOutFlags(); + validate((int)arg, (int)(retval & arg), std::string("set readout flags"), HEX); + FILE_LOG(logDEBUG1) << "Readout flags: " << retval; + return socket.sendResult(retval); } - - -int slsReceiverTCPIPInterface::set_adc_mask(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - uint32_t arg = -1; - uint32_t retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - socket.receiveArg(ret, mess, &arg, sizeof(arg)); - if(receiver == nullptr){ - NullObjectError(ret, mess); - return FAIL; +int slsReceiverTCPIPInterface::set_adc_mask(Interface &socket) { + auto arg = socket.receive(); + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting ADC enable mask: " << arg; + impl()->setADCEnableMask(arg); + auto retval = impl()->getADCEnableMask(); + if (retval != arg) { + sprintf(mess, "Could not ADC enable mask. Set 0x%x, but read 0x%x\n", arg, retval); + throw RuntimeError(mess); } - - // base object not null - if (ret == OK) { - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting ADC enable mask: " << arg; - receiver->setADCEnableMask(arg); - } - retval = receiver->getADCEnableMask(); - if (ret == OK && retval != arg) { - ret = FAIL; - sprintf(mess, "Could not ADC enable mask. Set 0x%x, but read 0x%x\n", arg, retval); - FILE_LOG(logERROR) << mess; - } - FILE_LOG(logDEBUG1) << "ADC enable mask retval: " << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + FILE_LOG(logDEBUG1) << "ADC enable mask retval: " << retval; + return socket.sendResult(retval); } -int slsReceiverTCPIPInterface::set_dbit_list(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); +int slsReceiverTCPIPInterface::set_dbit_list(Interface &socket) { sls::FixedCapacityContainer args; - if (socket.receiveArg(ret, mess, &args, sizeof(args)) == FAIL) { - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - return FAIL; - - } else if (ret == OK) { - FILE_LOG(logDEBUG1) << "Setting DBIT list"; - for (auto &it : args) { - FILE_LOG(logDEBUG1) << it << " "; - } - FILE_LOG(logDEBUG1) << "\n"; - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - if (args.size() > 64) { - ret = FAIL; - sprintf(mess, "Could not set dbit list as size is > 64\n"); - FILE_LOG(logERROR) << mess; - } else - receiver->setDbitList(args); - } - } - return socket.sendResult(true, ret, nullptr, 0, mess); + socket.receiveArg(args); + FILE_LOG(logDEBUG1) << "Setting DBIT list"; + for (auto &it : args) { + FILE_LOG(logDEBUG1) << it << " "; + } + FILE_LOG(logDEBUG1) << "\n"; + VerifyIdle(socket); + impl()->setDbitList(args); + return socket.sendData(OK); } -int slsReceiverTCPIPInterface::get_dbit_list(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); +int slsReceiverTCPIPInterface::get_dbit_list(Interface &socket) { sls::FixedCapacityContainer retval; - - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - // base object not null - if (ret == OK) { - retval = receiver->getDbitList(); - FILE_LOG(logDEBUG1) << "Dbit list size retval:" << retval.size(); - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + retval = impl()->getDbitList(); + FILE_LOG(logDEBUG1) << "Dbit list size retval:" << retval.size(); + return socket.sendResult(retval); } - -int slsReceiverTCPIPInterface::set_dbit_offset(sls::ServerInterface2 &socket) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int arg = -1; - int retval = -1; - - // get args, return if socket crashed, ret is fail if receiver is not null - if (socket.receiveArg(ret, mess, &arg, sizeof(arg)) == FAIL){ - if(receiver == nullptr){ - NullObjectError(ret, mess); - } - return FAIL; +int slsReceiverTCPIPInterface::set_dbit_offset(Interface &socket) { + auto arg = socket.receive(); + if (arg >= 0) { + VerifyIdle(socket); + FILE_LOG(logDEBUG1) << "Setting Dbit offset: " << arg; + impl()->setDbitOffset(arg); } - - - // base object not null - else if (ret == OK) { - if (arg >= 0) { - if (VerifyLockAndIdle(ret, mess, fnum) == OK) { - FILE_LOG(logDEBUG1) << "Setting Dbit offset: " << arg; - receiver->setDbitOffset(arg); - } - } - retval = receiver->getDbitOffset(); - validate(arg, retval, std::string("set dbit offset"), DEC); - FILE_LOG(logDEBUG1) << "Dbit offset retval: " << retval; - } - return socket.sendResult(true, ret, &retval, sizeof(retval), mess); + int retval = impl()->getDbitOffset(); + validate(arg, retval, std::string("set dbit offset"), DEC); + FILE_LOG(logDEBUG1) << "Dbit offset retval: " << retval; + return socket.sendResult(retval); } diff --git a/slsSupportLib/include/DataSocket.h b/slsSupportLib/include/DataSocket.h index 05f008bba..caf2c7fa0 100755 --- a/slsSupportLib/include/DataSocket.h +++ b/slsSupportLib/include/DataSocket.h @@ -15,12 +15,16 @@ class DataSocket { void swap(DataSocket &other) noexcept; DataSocket(const DataSocket &) = delete; DataSocket &operator=(DataSocket const &) = delete; - int getSocketId() const { - return socketId_; - } + int getSocketId() const { return socketId_; } int sendData(const void *buffer, size_t size); + + template int sendData(T &&data) { + return sendData(&data, sizeof(data)); + } + int receiveData(void *buffer, size_t size); int read(void *buffer, size_t size); + int write(void *buffer, size_t size); int setTimeOut(int t_seconds); int setReceiveTimeout(int us); void close(); @@ -30,9 +34,12 @@ class DataSocket { int socketId_ = -1; }; -int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res); -int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize); +int ConvertHostnameToInternetAddress(const char *const hostname, + struct ::addrinfo **res); +int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, + const int ipsize); -struct ::sockaddr_in ConvertHostnameToInternetAddress(const std::string &hostname); +struct ::sockaddr_in +ConvertHostnameToInternetAddress(const std::string &hostname); }; // namespace sls diff --git a/slsSupportLib/include/ServerInterface2.h b/slsSupportLib/include/ServerInterface2.h index 997ee3e13..a25d8b9a9 100644 --- a/slsSupportLib/include/ServerInterface2.h +++ b/slsSupportLib/include/ServerInterface2.h @@ -13,16 +13,32 @@ class ServerInterface2 : public DataSocket { using defs = slsDetectorDefs; public: - ServerInterface2(int socketId) : DataSocket(socketId){} + ServerInterface2(int socketId) : DataSocket(socketId) {} - int sendResult(bool update, int ret, void *retval, int retvalSize, - char *mess = nullptr); - int receiveArg(int &ret, char *mess, void *arg, int sizeofArg); + int sendResult(int ret, void *retval, int retvalSize, char *mess = nullptr); + template int sendResult(int ret, T &retval) { + return sendResult(ret, &retval, sizeof(retval, nullptr)); + } + template int sendResult(T &&retval) { + sendData(defs::OK); + sendData(retval); + return defs::OK; + } -private: + int receiveArg(void *arg, int sizeofArg); + template int receiveArg(T &arg) { + return receiveArg(&arg, sizeof(arg)); + } + template T receive() { + T arg; + receiveArg(&arg, sizeof(arg)); + return arg; + } + + private: }; } // namespace sls \ No newline at end of file diff --git a/slsSupportLib/include/ServerSocket.h b/slsSupportLib/include/ServerSocket.h index 74831a46d..6b344cf43 100755 --- a/slsSupportLib/include/ServerSocket.h +++ b/slsSupportLib/include/ServerSocket.h @@ -15,13 +15,13 @@ class ServerSocket : public DataSocket { public: ServerSocket(int port); ServerInterface2 accept(); - IpAddr getLastClient() noexcept { return lastClient; } - IpAddr getThisClient() noexcept { return thisClient; } - IpAddr getLockedBy() noexcept { return lockedBy; } + IpAddr getLastClient() const noexcept { return lastClient; } + IpAddr getThisClient() const noexcept { return thisClient; } + IpAddr getLockedBy() const noexcept { return lockedBy; } + bool differentClients() const noexcept {return lastClient != thisClient;} void setLockedBy(IpAddr addr) { lockedBy = addr; } void setLastClient(IpAddr addr) { lastClient = addr; } - int getPort() const; - void SendResult(int &ret, void *retval, int retvalSize, char *mess); + int getPort() const noexcept { return serverPort; } private: IpAddr thisClient; diff --git a/slsSupportLib/include/versionAPI.h b/slsSupportLib/include/versionAPI.h index 51a9c4e37..74a548755 100644 --- a/slsSupportLib/include/versionAPI.h +++ b/slsSupportLib/include/versionAPI.h @@ -1,10 +1,10 @@ /** API versions */ #define GITBRANCH "refgui" #define APIMOENCH 0x181108 -#define APILIB 0x190405 -#define APIRECEIVER 0x190405 +#define APILIB 0x190604 +#define APIRECEIVER 0x190604 #define APIGUI 0x190405 -#define APIGOTTHARD 0x190604 -#define APIJUNGFRAU 0x190604 -#define APIEIGER 0x190604 #define APICTB 0x190604 +#define APIEIGER 0x190604 +#define APIJUNGFRAU 0x190604 +#define APIGOTTHARD 0x190604 \ No newline at end of file diff --git a/slsSupportLib/src/ClientSocket.cpp b/slsSupportLib/src/ClientSocket.cpp index 793ca1019..7e531c93c 100755 --- a/slsSupportLib/src/ClientSocket.cpp +++ b/slsSupportLib/src/ClientSocket.cpp @@ -70,11 +70,11 @@ int ClientSocket::sendCommandThenRead(int fnum, const void *args, void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { - receiveData(&ret, sizeof(ret)); + read(&ret, sizeof(ret)); if (ret == slsDetectorDefs::FAIL) { char mess[MAX_STR_LENGTH]{}; // get error message - receiveData(mess, sizeof(mess)); + read(mess, sizeof(mess)); FILE_LOG(logERROR) << socketType << " returned error: " << mess; std::cout << "\n"; // needed to reset the color. @@ -89,7 +89,7 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { } } // get retval - receiveData(retval, retval_size); + read(retval, retval_size); } }; // namespace sls diff --git a/slsSupportLib/src/DataSocket.cpp b/slsSupportLib/src/DataSocket.cpp index 4a4dc0c11..ce28d8b2c 100755 --- a/slsSupportLib/src/DataSocket.cpp +++ b/slsSupportLib/src/DataSocket.cpp @@ -51,7 +51,11 @@ int DataSocket::receiveData(void *buffer, size_t size) { } int DataSocket::read(void *buffer, size_t size){ - return ::read(getSocketId(), reinterpret_cast(buffer), size); + return ::read(getSocketId(), buffer, size); +} + +int DataSocket::write(void *buffer, size_t size){ + return ::write(getSocketId(), buffer, size); } int DataSocket::setReceiveTimeout(int us) { @@ -64,13 +68,11 @@ int DataSocket::setReceiveTimeout(int us) { int DataSocket::sendData(const void *buffer, size_t size) { - int dataSent = 0; - while (dataSent < (int)size) { - dataSent += - write(getSocketId(), reinterpret_cast(buffer) + dataSent, - size - dataSent); - } - return dataSent; + // int dataSent = 0; + // while (dataSent < (int)size) { + return ::write(getSocketId(), buffer, size); + // } + // return dataSent; } int DataSocket::setTimeOut(int t_seconds) { diff --git a/slsSupportLib/src/ServerInterface2.cpp b/slsSupportLib/src/ServerInterface2.cpp index 44c53782b..1d970ecf8 100644 --- a/slsSupportLib/src/ServerInterface2.cpp +++ b/slsSupportLib/src/ServerInterface2.cpp @@ -1,37 +1,37 @@ #include "ServerInterface2.h" #include +#include +#include namespace sls { -int ServerInterface2::sendResult(bool update, int ret, void *retval, - int retvalSize, char *mess) { +int ServerInterface2::sendResult(int ret, void *retval, int retvalSize, + char *mess) { - // if (update && ret == defs::OK && server_->DifferentClients()) { - // ret = defs::FORCE_UPDATE; - // } - sendData(&ret, sizeof(ret)); + write(&ret, sizeof(ret)); if (ret == defs::FAIL) { - // send error message - if (mess) - sendData(mess, MAX_STR_LENGTH); - // debugging feature. should not happen. - else + if (mess != nullptr) { + write(mess, MAX_STR_LENGTH); + } else { FILE_LOG(logERROR) << "No error message provided for this " "failure. Will mess up TCP\n"; + } + } else { + write(retval, retvalSize); } - sendData(retval, retvalSize); return ret; } -int ServerInterface2::receiveArg(int &ret, char *mess, void *arg, - int sizeofArg) { +int ServerInterface2::receiveArg(void *arg, int sizeofArg) { assert(sizeofArg > 0); int bytes_read = read(arg, sizeofArg); if (bytes_read == sizeofArg) { return defs::OK; } else { - FILE_LOG(logERROR) << "Read: " << bytes_read << " instead of " - << sizeofArg; - return defs::FAIL; + std::ostringstream ss; + ss << "TCP socket error read " << bytes_read << " bytes instead of " + << sizeofArg << " bytes"; + + throw sls::SocketError(ss.str()); } } diff --git a/slsSupportLib/src/ServerSocket.cpp b/slsSupportLib/src/ServerSocket.cpp index 35048268b..8e8bcca8d 100755 --- a/slsSupportLib/src/ServerSocket.cpp +++ b/slsSupportLib/src/ServerSocket.cpp @@ -52,26 +52,4 @@ ServerInterface2 ServerSocket::accept() { return ServerInterface2(newSocket); } - -int ServerSocket::getPort() const { return serverPort; } - -void ServerSocket::SendResult(int &ret, void* retval, int retvalSize, char* mess) { - - // send success of operation - sendData(&ret, sizeof(ret)); - - if (ret == slsDetectorDefs::FAIL) { - // create error message if empty - if (!strlen(mess)) { - strcpy(mess, "No error message provided for this failure in server. Will mess up TCP."); - } - - sendData(mess, MAX_STR_LENGTH); - throw sls::RuntimeError(mess); - } - // send return value - sendData(retval, retvalSize); -} - - }; // namespace sls