diff --git a/slsDetectorSoftware/include/slsDetector.h b/slsDetectorSoftware/include/slsDetector.h index 6ee11c826..741ceb66b 100755 --- a/slsDetectorSoftware/include/slsDetector.h +++ b/slsDetectorSoftware/include/slsDetector.h @@ -291,10 +291,20 @@ class slsDetector : public virtual slsDetectorDefs{ */ int64_t getId(idMode mode); - - int sendToDetector(int fnum, void* args, size_t args_size, void* retval, size_t retval_size); + int sendToDetector(int fnum, const void *args, size_t args_size, + void *retval, size_t retval_size); - int64_t getReceiverSoftwareVersion() const; + template + typename std::enable_if< + !(std::is_pointer::value & std::is_pointer::value), int>::type + sendToDetector(int fnum, const Arg &args, Ret &retval); + int sendToDetectorStop(int fnum, const void *args, size_t args_size, + void *retval, size_t retval_size); + int sendToDetector(int fnum); + int sendToReceiver(int fnum, const void *args, size_t args_size, + void *retval, size_t retval_size); + + int64_t getReceiverSoftwareVersion() const; /** * Free shared memory without creating objects diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index 9255a53c5..312d5a987 100755 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -96,13 +96,8 @@ int slsDetector::checkDetectorVersionCompatibility() { detector_shm()->detectorStopAPIVersion = 0; detector_shm()->onlineFlag = OFFLINE_FLAG; - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - client.sendCommandThenRead(fnum, &arg, sizeof(arg), nullptr, 0); - - auto stop = - DetectorSocket(detector_shm()->hostname, detector_shm()->stopPort); - ret = stop.sendCommandThenRead(fnum, &arg, sizeof(arg), nullptr, 0); + sendToDetector(fnum, &arg, sizeof(arg), nullptr, 0); + ret = sendToDetectorStop(fnum, &arg, sizeof(arg), nullptr, 0); // success detector_shm()->detectorControlAPIVersion = arg; @@ -663,27 +658,19 @@ int slsDetector::setControlPort(int port_number) { int slsDetector::setStopPort(int port_number) { int fnum = F_SET_PORT; - int ret = FAIL; int retval = -1; FILE_LOG(logDEBUG1) << "Setting stop port " << " to " << port_number; - if (port_number >= 0 && port_number != detector_shm()->stopPort) { if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto stop = DetectorSocket(detector_shm()->hostname, - detector_shm()->stopPort); - ret = stop.sendCommandThenRead(fnum, &port_number, - sizeof(port_number), &retval, - sizeof(retval)); + sendToDetectorStop(fnum, &port_number, sizeof(port_number), &retval, + sizeof(retval)); detector_shm()->stopPort = retval; FILE_LOG(logDEBUG1) << "Stop port: " << retval; } else { detector_shm()->stopPort = port_number; } } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return detector_shm()->stopPort; } @@ -696,9 +683,9 @@ int slsDetector::setReceiverPort(int port_number) { if (port_number >= 0 && port_number != detector_shm()->receiverTCPPort) { if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG) { - auto stop = ReceiverSocket(detector_shm()->receiver_hostname, + auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, detector_shm()->receiverTCPPort); - ret = stop.sendCommandThenRead(fnum, &port_number, + ret = receiver.sendCommandThenRead(fnum, &port_number, sizeof(port_number), &retval, sizeof(retval)); detector_shm()->receiverTCPPort = retval; @@ -746,14 +733,10 @@ std::string slsDetector::getLastClientIP() { } int slsDetector::exitServer() { - int fnum = F_EXIT_SERVER; int ret = FAIL; FILE_LOG(logDEBUG1) << "Sending exit command to detector server"; - if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + ret = sendToDetector(F_EXIT_SERVER); FILE_LOG(logINFO) << "Shutting down the Detector server"; } return ret; @@ -1301,37 +1284,23 @@ slsDetectorDefs::runStatus slsDetector::getRunStatus() { } int slsDetector::prepareAcquisition() { - int fnum = F_PREPARE_ACQUISITION; int ret = FAIL; FILE_LOG(logDEBUG1) << "Preparing Detector for Acquisition"; - if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + ret = sendToDetector(F_PREPARE_ACQUISITION); FILE_LOG(logDEBUG1) << "Prepare Acquisition successful"; } - if (ret == FORCE_UPDATE) { - ret = updateDetector(); - } return ret; } int slsDetector::startAcquisition() { - int fnum = F_START_ACQUISITION; int ret = FAIL; FILE_LOG(logDEBUG1) << "Starting Acquisition"; - detector_shm()->stoppedFlag = 0; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + ret = sendToDetector(F_START_ACQUISITION); FILE_LOG(logDEBUG1) << "Starting Acquisition successful"; } - if (ret == FORCE_UPDATE) { - ret = updateDetector(); - } return ret; } @@ -1364,76 +1333,49 @@ int slsDetector::stopAcquisition() { } int slsDetector::sendSoftwareTrigger() { - int fnum = F_SOFTWARE_TRIGGER; int ret = FAIL; FILE_LOG(logDEBUG1) << "Sending software trigger"; - detector_shm()->stoppedFlag = 0; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + ret = sendToDetector(F_SOFTWARE_TRIGGER); FILE_LOG(logDEBUG1) << "Sending software trigger successful"; } - if (ret == FORCE_UPDATE) { - ret = updateDetector(); - } return ret; } int slsDetector::startAndReadAll() { - int fnum = F_START_AND_READ_ALL; int ret = FAIL; FILE_LOG(logDEBUG1) << "Starting and reading all frames"; - detector_shm()->stoppedFlag = 0; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + ret = sendToDetector(F_START_AND_READ_ALL); // TODO! how to we hande this? ret == FAIL --> // detector_shm()->stoppedFlag = 1; FILE_LOG(logDEBUG1) << "Detector successfully finished acquisition"; } - if (ret == FORCE_UPDATE) { - ret = updateDetector(); - } return ret; } int slsDetector::startReadOut() { - int fnum = F_START_READOUT; int ret = FAIL; FILE_LOG(logDEBUG1) << "Starting readout"; - if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + ret = sendToDetector(F_START_READOUT); FILE_LOG(logDEBUG1) << "Starting detector readout successful"; } - if (ret == FORCE_UPDATE) { - ret = updateDetector(); - } return ret; } int slsDetector::readAll() { - int fnum = F_READ_ALL; int ret = FAIL; FILE_LOG(logDEBUG1) << "Reading all frames"; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + ret = sendToDetector(F_READ_ALL); // TODO! how to we hande this? ret == FAIL --> // detector_shm()->stoppedFlag = 1; FILE_LOG(logDEBUG1) << "Detector successfully finished reading all frames"; } - if (ret == FORCE_UPDATE) { - ret = updateDetector(); - } return ret; } @@ -1753,14 +1695,14 @@ int slsDetector::setDAC(int val, dacIndex index, int mV) { << (mV != 0 ? "mV" : "dac units"); if (detector_shm()->onlineFlag == ONLINE_FLAG) { - sendToDetector(fnum, args, sizeof(args), &retval, sizeof(retval)); + sendToDetector(fnum, args, retval); FILE_LOG(logDEBUG1) << "Dac index " << index << ": " << retval << (mV != 0 ? "mV" : "dac units"); } return retval; } -int slsDetector::sendToDetector(int fnum, void *args, size_t args_size, +int slsDetector::sendToDetector(int fnum, const void *args, size_t args_size, void *retval, size_t retval_size) { auto client = DetectorSocket(detector_shm()->hostname, detector_shm()->controlPort); @@ -1773,6 +1715,58 @@ int slsDetector::sendToDetector(int fnum, void *args, size_t args_size, return ret; } +template +typename std::enable_if< + !(std::is_pointer::value & std::is_pointer::value), int>::type +slsDetector::sendToDetector(int fnum, const Arg &args, Ret &retval) { + auto client = + DetectorSocket(detector_shm()->hostname, detector_shm()->controlPort); + auto ret = client.sendCommandThenRead(fnum, &args, sizeof(args), &retval, + sizeof(retval)); + client.close(); + if (ret == FORCE_UPDATE) { + ret = updateDetector(); + } + return ret; +} + +int slsDetector::sendToDetector(int fnum) { + auto client = + DetectorSocket(detector_shm()->hostname, detector_shm()->controlPort); + auto ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); + client.close(); + if (ret == FORCE_UPDATE) { + ret = updateDetector(); + } + return ret; +} + +int slsDetector::sendToDetectorStop(int fnum, const void *args, size_t args_size, + void *retval, size_t retval_size) { + auto client = + DetectorSocket(detector_shm()->hostname, detector_shm()->stopPort); + auto ret = + client.sendCommandThenRead(fnum, args, args_size, retval, retval_size); + client.close(); + //no update on stop port + return ret; +} + +int slsDetector::sendToReceiver(int fnum, const void *args, size_t args_size, + void *retval, size_t retval_size) { + + auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, + detector_shm()->receiverTCPPort); + + auto ret = receiver.sendCommandThenRead(fnum, args, args_size, retval, + args_size); + receiver.close(); + if (ret == FORCE_UPDATE) { + ret = updateCachedReceiverVariables(); + } + return ret; +} + int slsDetector::getADC(dacIndex index) { int fnum = F_GET_ADC; int arg = static_cast(index); @@ -1807,22 +1801,14 @@ slsDetector::setExternalCommunicationMode(externalCommunicationMode pol) { slsDetectorDefs::externalSignalFlag slsDetector::setExternalSignalFlags(externalSignalFlag pol, int signalindex) { int fnum = F_SET_EXTERNAL_SIGNAL_FLAG; - int ret = FAIL; - int args[2]{signalindex, pol}; - externalSignalFlag retval = GET_EXTERNAL_SIGNAL_FLAG; + int args[]{signalindex, pol}; + auto retval = GET_EXTERNAL_SIGNAL_FLAG; FILE_LOG(logDEBUG1) << "Setting signal " << signalindex << " to flag " << pol; - if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, args, sizeof(args), &retval, - sizeof(retval)); + sendToDetector(fnum, args, sizeof(args), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Ext Signal (" << signalindex << "): " << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } @@ -1843,25 +1829,16 @@ int slsDetector::setReadOutFlags(readOutFlags flag) { updateTotalNumberOfChannels(); } } - - // sending to receiver if (ret != FAIL) { - fnum = F_RECEIVER_SET_READOUT_FLAGS; - ret = FAIL; - arg = detector_shm()->roFlags; - retval = static_cast(-1); FILE_LOG(logDEBUG1) << "Setting receiver readout flags to " << arg; - if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG) { - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + fnum = F_RECEIVER_SET_READOUT_FLAGS; + ret = FAIL; + arg = detector_shm()->roFlags; + retval = static_cast(-1); + sendToReceiver(fnum, &arg, sizeof(arg), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Receiver readout flag: " << retval; } - if (ret == FORCE_UPDATE) { - updateCachedReceiverVariables(); - } } return detector_shm()->roFlags; } @@ -1874,7 +1851,7 @@ uint32_t slsDetector::writeRegister(uint32_t addr, uint32_t val) { << "data: 0x" << std::hex << val << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - sendToDetector(fnum, args, sizeof(args), &retval, sizeof(retval)); + sendToDetector(fnum, args, retval); FILE_LOG(logDEBUG1) << "Register 0x" << std::hex << addr << ": 0x" << std::hex << retval << std::dec; } @@ -1882,14 +1859,11 @@ uint32_t slsDetector::writeRegister(uint32_t addr, uint32_t val) { } uint32_t slsDetector::readRegister(uint32_t addr) { - int fnum = F_READ_REGISTER; uint32_t retval = -1; - FILE_LOG(logDEBUG1) << "Reading register 0x" << std::hex << addr - << std::dec; - + FILE_LOG(logDEBUG1) << "Reading reg 0x" << std::hex << addr << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - sendToDetector(fnum, &addr, sizeof(addr), &retval, sizeof(retval)); - FILE_LOG(logDEBUG1) << "Register 0x" << std::hex << addr << ": 0x" + sendToDetector(F_READ_REGISTER, addr, retval); + FILE_LOG(logDEBUG1) << "Reg 0x" << std::hex << addr << ": 0x" << std::hex << retval << std::dec; } return retval; @@ -2259,25 +2233,15 @@ int slsDetector::getClientStreamingPort() { return detector_shm()->zmqport; } void slsDetector::setReceiverStreamingPort(int port) { // copy now else it is lost if rx_hostname not set yet detector_shm()->receiver_zmqport = port; - int fnum = F_SET_RECEIVER_STREAMING_PORT; - int ret = FAIL; - int arg = detector_shm()->receiver_zmqport; int retval = -1; FILE_LOG(logDEBUG1) << "Sending receiver streaming port to receiver: " - << arg; - + << port; if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG) { - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToReceiver(fnum, &port, sizeof(port), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Receiver streaming port: " << retval; detector_shm()->receiver_zmqport = retval; } - if (ret == FORCE_UPDATE) { - updateCachedReceiverVariables(); - } } int slsDetector::getReceiverStreamingPort() { @@ -2347,6 +2311,7 @@ void slsDetector::setReceiverStreamingIP(std::string sourceIP) { detector_shm()->receiverTCPPort); ret = receiver.sendCommandThenRead(fnum, args, sizeof(args), retvals, sizeof(retvals)); + FILE_LOG(logDEBUG1) << "Receiver streaming port: " << retvals; memset(detector_shm()->receiver_zmqip, 0, MAX_STR_LENGTH); sls::strcpy_safe(detector_shm()->receiver_zmqip, retvals); @@ -2380,9 +2345,8 @@ int slsDetector::setDetectorNetworkParameter(networkParameter index, std::string slsDetector::setAdditionalJsonHeader(const std::string &jsonheader) { int fnum = F_ADDITIONAL_JSON_HEADER; - int ret = FAIL; - char args[MAX_STR_LENGTH] = {0}; - char retvals[MAX_STR_LENGTH] = {0}; + char args[MAX_STR_LENGTH]{}; + char retvals[MAX_STR_LENGTH]{}; sls::strcpy_safe(args, jsonheader.c_str()); FILE_LOG(logDEBUG1) << "Sending additional json header " << args; @@ -2390,38 +2354,24 @@ slsDetector::setAdditionalJsonHeader(const std::string &jsonheader) { sls::strcpy_safe(detector_shm()->rxAdditionalJsonHeader, jsonheader.c_str()); } else { - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, args, sizeof(args), retvals, - sizeof(retvals)); + sendToReceiver(fnum, args, sizeof(args), retvals, sizeof(retvals)); FILE_LOG(logDEBUG1) << "Additional json header: " << retvals; memset(detector_shm()->rxAdditionalJsonHeader, 0, MAX_STR_LENGTH); sls::strcpy_safe(detector_shm()->rxAdditionalJsonHeader, retvals); } - if (ret == FORCE_UPDATE) { - updateCachedReceiverVariables(); - } - return std::string(detector_shm()->rxAdditionalJsonHeader); + return detector_shm()->rxAdditionalJsonHeader; } std::string slsDetector::getAdditionalJsonHeader() { int fnum = F_GET_ADDITIONAL_JSON_HEADER; - int ret = FAIL; - char retvals[MAX_STR_LENGTH] = {0}; + char retvals[MAX_STR_LENGTH]{}; FILE_LOG(logDEBUG1) << "Getting additional json header "; - if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG) { - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, nullptr, 0, retvals, - sizeof(retvals)); + sendToReceiver(fnum, nullptr, 0, retvals, sizeof(retvals)); FILE_LOG(logDEBUG1) << "Additional json header: " << retvals; memset(detector_shm()->rxAdditionalJsonHeader, 0, MAX_STR_LENGTH); sls::strcpy_safe(detector_shm()->rxAdditionalJsonHeader, retvals); } - if (ret == FORCE_UPDATE) { - updateCachedReceiverVariables(); - } return std::string(detector_shm()->rxAdditionalJsonHeader); } @@ -2504,7 +2454,6 @@ std::string slsDetector::getAdditionalJsonParameter(const std::string &key) { return pairs[1]; } } - // return empty string as no match found with key return std::string(); } @@ -2536,21 +2485,13 @@ int64_t slsDetector::getReceiverUDPSocketBufferSize() { int64_t slsDetector::getReceiverRealUDPSocketBufferSize() { int fnum = F_RECEIVER_REAL_UDP_SOCK_BUF_SIZE; - int ret = FAIL; int64_t retval = -1; FILE_LOG(logDEBUG1) << "Getting real UDP Socket Buffer size to receiver"; - if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG) { - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, nullptr, 0, &retval, - sizeof(retval)); + sendToReceiver(fnum, nullptr, 0, &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Real Receiver UDP Socket Buffer size: " << retval; } - if (ret == FORCE_UPDATE) { - updateCachedReceiverVariables(); - } return retval; } @@ -2619,10 +2560,6 @@ int slsDetector::setUDPConnection() { << "Receiver UDP MAC2 returned : " << retvals[1]; detector_shm()->receiverUDPMAC2 = retvals[1]; } - if (ret == FORCE_UPDATE) { - receiver.close(); - ret = updateCachedReceiverVariables(); - } // configure detector with udp details if (configureMAC() == FAIL) { setReceiverOnline(OFFLINE_FLAG); @@ -2630,7 +2567,6 @@ int slsDetector::setUDPConnection() { } else { throw ReceiverError("setUDPConnection: Receiver is OFFLINE"); } - printReceiverConfiguration(logDEBUG1); return ret; } @@ -2912,55 +2848,33 @@ int slsDetector::writeAdcRegister(uint32_t addr, uint32_t val) { int slsDetector::activate(int enable) { int fnum = F_ACTIVATE; int ret = FAIL; - int arg = enable; int retval = -1; - FILE_LOG(logDEBUG1) << "Setting activate flag to " << arg; - - // detector + FILE_LOG(logDEBUG1) << "Setting activate flag to " << enable; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - ret = sendToDetector(fnum, &arg, sizeof(arg), &retval, sizeof(retval)); + ret = sendToDetector(fnum, &enable, sizeof(enable), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Activate: " << retval; detector_shm()->activated = static_cast(retval); } - - // receiver if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG && ret == OK) { fnum = F_RECEIVER_ACTIVATE; - arg = static_cast(detector_shm()->activated); + enable = static_cast(detector_shm()->activated); retval = -1; FILE_LOG(logDEBUG1) - << "Setting activate flag " << arg << " to receiver"; - - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); - if (ret == FORCE_UPDATE) { - receiver.close(); - updateCachedReceiverVariables(); - } + << "Setting activate flag " << enable << " to receiver"; + sendToReceiver(fnum, &enable, sizeof(enable), &retval, sizeof(retval)); } return static_cast(detector_shm()->activated); } bool slsDetector::setDeactivatedRxrPaddingMode(int padding) { int fnum = F_RECEIVER_DEACTIVATED_PADDING_ENABLE; - int ret = OK; - int arg = padding; int retval = -1; - FILE_LOG(logDEBUG1) << "Deactivated Receiver Padding Enable: " << arg; - + FILE_LOG(logDEBUG1) << "Deactivated Receiver Padding Enable: " << padding; if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG) { - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToReceiver(fnum, &padding, sizeof(padding), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Deactivated Receiver Padding Enable:" << retval; detector_shm()->rxPadDeactivatedModules = static_cast(retval); } - if (ret == FORCE_UPDATE) { - updateCachedReceiverVariables(); - } return detector_shm()->rxPadDeactivatedModules; } @@ -3014,19 +2928,12 @@ int slsDetector::setAllTrimbits(int val) { int slsDetector::enableGapPixels(int val) { if (val >= 0) { int fnum = F_ENABLE_GAPPIXELS_IN_RECEIVER; - int ret = OK; - int arg = val; int retval = -1; - FILE_LOG(logDEBUG1) << "Sending gap pixels enable to receiver: " << arg; - + FILE_LOG(logDEBUG1) << "Sending gap pixels enable to receiver: " << val; if (detector_shm()->receiverOnlineFlag == ONLINE_FLAG) { - auto receiver = ReceiverSocket(detector_shm()->receiver_hostname, - detector_shm()->receiverTCPPort); - ret = receiver.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToReceiver(fnum, &val, sizeof(val), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Gap pixels enable to receiver:" << retval; detector_shm()->gappixels = retval; - // update databytes detector_shm()->dataBytesInclGapPixels = 0; if (detector_shm()->dynamicRange != 4) { @@ -3040,9 +2947,6 @@ int slsDetector::enableGapPixels(int val) { detector_shm()->dynamicRange / 8; } } - if (ret == FORCE_UPDATE) { - updateCachedReceiverVariables(); - } } return detector_shm()->gappixels; } @@ -3109,51 +3013,33 @@ int slsDetector::pulseChip(int n) { int slsDetector::setThresholdTemperature(int val) { int fnum = F_THRESHOLD_TEMP; - int arg = val; int retval = -1; FILE_LOG(logDEBUG1) << "Setting threshold temperature to " << val; - if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto stop = - DetectorSocket(detector_shm()->hostname, detector_shm()->stopPort); - stop.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToDetectorStop(fnum, &val, sizeof(val), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Threshold temperature: " << retval; - // no updateDetector as it is stop server } return retval; } int slsDetector::setTemperatureControl(int val) { int fnum = F_TEMP_CONTROL; - int arg = val; int retval = -1; FILE_LOG(logDEBUG1) << "Setting temperature control to " << val; - if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto stop = - DetectorSocket(detector_shm()->hostname, detector_shm()->stopPort); - stop.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToDetectorStop(fnum, &val, sizeof(val), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Temperature control: " << retval; - // no updateDetector as it is stop server } return retval; } int slsDetector::setTemperatureEvent(int val) { int fnum = F_TEMP_EVENT; - int arg = val; int retval = -1; FILE_LOG(logDEBUG1) << "Setting temperature event to " << val; - if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto stop = - DetectorSocket(detector_shm()->hostname, detector_shm()->stopPort); - stop.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToDetectorStop(fnum, &val, sizeof(val), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Temperature event: " << retval; - // no updateDetector as it is stop server } return retval; } @@ -3275,13 +3161,11 @@ int slsDetector::programFPGA(std::vector buffer) { } int slsDetector::resetFPGA() { - int fnum = F_RESET_FPGA; - int ret = FAIL; FILE_LOG(logDEBUG1) << "Sending reset FPGA"; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - ret = sendToDetector(fnum, nullptr, 0, nullptr, 0); + return sendToDetector(F_RESET_FPGA); } - return ret; + return FAIL; } int slsDetector::copyDetectorServer(const std::string &fname, @@ -4151,16 +4035,9 @@ int slsDetector::enableTenGigabitEthernet(int i) { FILE_LOG(logDEBUG1) << "Enabling / Disabling 10Gbe: " << arg; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToDetector(fnum, &arg, sizeof(arg), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "10Gbe: " << retval; detector_shm()->tenGigaEnable = retval; - client.close(); - if (ret == FORCE_UPDATE) { - updateDetector(); - } ret = configureMAC(); } @@ -4258,91 +4135,58 @@ int slsDetector::setPattern(const std::string &fname) { uint64_t slsDetector::setPatternIOControl(uint64_t word) { int fnum = F_SET_PATTERN_IO_CONTROL; - int ret = FAIL; - uint64_t arg = word; uint64_t retval = -1; FILE_LOG(logDEBUG1) << "Setting Pattern IO Control, word: 0x" << std::hex << word << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, sizeof(retval)); + sendToDetector(fnum, &word, sizeof(word), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Set Pattern IO Control: " << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } uint64_t slsDetector::setPatternClockControl(uint64_t word) { int fnum = F_SET_PATTERN_CLOCK_CONTROL; - int ret = FAIL; - uint64_t arg = word; uint64_t retval = -1; FILE_LOG(logDEBUG1) << "Setting Pattern Clock Control, word: 0x" << std::hex << word << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, sizeof(retval)); + sendToDetector(fnum, &word, sizeof(word), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Set Pattern Clock Control: " << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } uint64_t slsDetector::setPatternWord(int addr, uint64_t word) { int fnum = F_SET_PATTERN_WORD; - int ret = FAIL; uint64_t args[]{static_cast(addr), word}; uint64_t retval = -1; FILE_LOG(logDEBUG1) << "Setting Pattern word, addr: 0x" << std::hex << addr << ", word: 0x" << word << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, args, sizeof(args), &retval, - sizeof(retval)); + sendToDetector(fnum, args, sizeof(args), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Set Pattern word: " << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } std::array slsDetector::setPatternLoops(int level, int start, int stop, int n) { int fnum = F_SET_PATTERN_LOOP; - int ret = FAIL; int args[]{level, start, stop, n}; - int retvals[3]{}; - std::array r{}; + std::array retvals{}; FILE_LOG(logDEBUG1) << "Setting Pat Loops, level: " << level << ", start: " << start << ", stop: " << stop << ", nloops: " << n; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, args, sizeof(args), retvals, - sizeof(retvals)); + sendToDetector(fnum, args, sizeof(args), retvals.data(), sizeof(retvals)); FILE_LOG(logDEBUG1) << "Set Pat Loops: " << retvals[0] << ", " << retvals[1] << ", " << retvals[2]; - r[0] = retvals[0]; - r[1] = retvals[1]; - r[2] = retvals[2]; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } - return r; + return retvals; } int slsDetector::setPatternWaitAddr(int level, int addr) { int fnum = F_SET_PATTERN_WAIT_ADDR; - int ret = FAIL; int retval = -1; int args[]{level, addr}; FILE_LOG(logDEBUG1) << "Setting Pat Wait Addr, " @@ -4351,35 +4195,22 @@ int slsDetector::setPatternWaitAddr(int level, int addr) { << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, args, sizeof(args), - &retval, sizeof(retval)); + sendToDetector(fnum, args, sizeof(args), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Set Pat Wait Addr: " << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } uint64_t slsDetector::setPatternWaitTime(int level, uint64_t t) { int fnum = F_SET_PATTERN_WAIT_TIME; - int ret = FAIL; uint64_t retval = -1; uint64_t args[]{static_cast(level), t}; FILE_LOG(logDEBUG1) << "Setting Pat Wait Time, level: " << level << ", t: " << t; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, args, sizeof(args), - &retval, sizeof(retval)); + sendToDetector(fnum, args, sizeof(args), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Set Pat Wait Time: " << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } @@ -4390,32 +4221,20 @@ int slsDetector::setPatternMask(uint64_t mask) { FILE_LOG(logDEBUG1) << "Setting Pattern Mask " << std::hex << mask << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, &arg, sizeof(arg), nullptr, 0); + ret = sendToDetector(fnum, &arg, sizeof(arg), nullptr, 0); FILE_LOG(logDEBUG1) << "Pattern Mask successful"; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return ret; } uint64_t slsDetector::getPatternMask() { int fnum = F_GET_PATTERN_MASK; - int ret = FAIL; uint64_t retval = -1; FILE_LOG(logDEBUG1) << "Getting Pattern Mask "; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, &retval, - sizeof(retval)); + sendToDetector(fnum, nullptr, 0, &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Pattern Mask:" << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } @@ -4426,71 +4245,46 @@ int slsDetector::setPatternBitMask(uint64_t mask) { FILE_LOG(logDEBUG1) << "Setting Pattern Bit Mask " << std::hex << mask << std::dec; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, &arg, sizeof(arg), nullptr, 0); + ret = sendToDetector(fnum, &arg, sizeof(arg), nullptr, 0); FILE_LOG(logDEBUG1) << "Pattern Bit Mask successful"; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return ret; } uint64_t slsDetector::getPatternBitMask() { int fnum = F_GET_PATTERN_BIT_MASK; - int ret = FAIL; uint64_t retval = -1; FILE_LOG(logDEBUG1) << "Getting Pattern Bit Mask "; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, nullptr, 0, &retval, - sizeof(retval)); + sendToDetector(fnum, nullptr, 0, &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Pattern Bit Mask:" << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } int slsDetector::setLEDEnable(int enable) { int fnum = F_LED; - int ret = FAIL; - int arg = enable; int retval = -1; - FILE_LOG(logDEBUG1) << "Sending LED Enable: " << arg; + FILE_LOG(logDEBUG1) << "Sending LED Enable: " << enable; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, - sizeof(retval)); + sendToDetector(fnum, &enable, sizeof(enable), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "LED Enable: " << retval; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return retval; } int slsDetector::setDigitalIODelay(uint64_t pinMask, int delay) { int fnum = F_DIGITAL_IO_DELAY; int ret = FAIL; - uint64_t args[2] = {pinMask, static_cast(delay)}; + uint64_t args[]{pinMask, static_cast(delay)}; FILE_LOG(logDEBUG1) << "Sending Digital IO Delay, pin mask: " << std::hex << args[0] << ", delay: " << std::dec << args[1] << " ps"; if (detector_shm()->onlineFlag == ONLINE_FLAG) { - auto client = DetectorSocket(detector_shm()->hostname, - detector_shm()->controlPort); - ret = client.sendCommandThenRead(fnum, args, sizeof(args), nullptr, 0); + ret = sendToDetector(fnum, args, sizeof(args), nullptr, 0); FILE_LOG(logDEBUG1) << "Digital IO Delay successful"; } - if (ret == FORCE_UPDATE) { - updateDetector(); - } return ret; } @@ -4736,8 +4530,6 @@ int slsDetector::writeSettingsFile(const std::string &fname, throw RuntimeError( "Unknown detector type - unknown format for settings file"); } - - // open file std::ofstream outfile; if (detector_shm()->myDetectorType == EIGER) { outfile.open(fname.c_str(), std::ofstream::binary); @@ -4748,8 +4540,6 @@ int slsDetector::writeSettingsFile(const std::string &fname, throw RuntimeError("Could not open settings file for writing: " + fname); } - - // eiger if (detector_shm()->myDetectorType == EIGER) { for (int i = 0; i < mod.ndac; ++i) { FILE_LOG(logINFO) << "dac " << i << ":" << mod.dacs[i]; @@ -4765,7 +4555,6 @@ int slsDetector::writeSettingsFile(const std::string &fname, outfile.write(reinterpret_cast(mod.chanregs), sizeof(int) * (mod.nchan)); } - // gotthard, jungfrau else { for (int i = 0; i < mod.ndac; ++i) { @@ -4773,7 +4562,6 @@ int slsDetector::writeSettingsFile(const std::string &fname, outfile << names[i] << " " << mod.dacs[i] << std::endl; } } - outfile.close(); return OK; } diff --git a/slsSupportLib/include/ClientSocket.h b/slsSupportLib/include/ClientSocket.h index 14ec6e639..eb68a8bcd 100755 --- a/slsSupportLib/include/ClientSocket.h +++ b/slsSupportLib/include/ClientSocket.h @@ -9,10 +9,11 @@ namespace sls { class ClientSocket : public DataSocket { public: - ClientSocket(std::string stype, const std::string &hostname, uint16_t port_number); + ClientSocket(std::string stype, const std::string &hostname, + uint16_t port_number); ClientSocket(std::string stype, struct sockaddr_in addr); - int sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval, - size_t retval_size); + int sendCommandThenRead(int fnum, const void *args, size_t args_size, + void *retval, size_t retval_size); private: void readReply(int &ret, void *retval, size_t retval_size); diff --git a/slsSupportLib/include/DataSocket.h b/slsSupportLib/include/DataSocket.h index 5b455b6dc..a4ad27fe1 100755 --- a/slsSupportLib/include/DataSocket.h +++ b/slsSupportLib/include/DataSocket.h @@ -18,7 +18,7 @@ class DataSocket { int getSocketId() const { return socketId_; } - size_t sendData(void *buffer, size_t size); + size_t sendData(const void *buffer, size_t size); size_t receiveData(void *buffer, size_t size); int setTimeOut(int t_seconds); void close(); diff --git a/slsSupportLib/include/logger.h b/slsSupportLib/include/logger.h index 1a7dae07a..8de0c2f49 100755 --- a/slsSupportLib/include/logger.h +++ b/slsSupportLib/include/logger.h @@ -18,7 +18,7 @@ #ifndef FILELOG_MAX_LEVEL #define FILELOG_MAX_LEVEL logINFO -//#define FILELOG_MAX_LEVEL logDEBUG5 +// #define FILELOG_MAX_LEVEL logDEBUG5 #endif diff --git a/slsSupportLib/src/ClientSocket.cpp b/slsSupportLib/src/ClientSocket.cpp index 9db998dde..ab8978390 100755 --- a/slsSupportLib/src/ClientSocket.cpp +++ b/slsSupportLib/src/ClientSocket.cpp @@ -10,7 +10,8 @@ #include namespace sls { -ClientSocket::ClientSocket(std::string stype, const std::string &host, uint16_t port) +ClientSocket::ClientSocket(std::string stype, const std::string &host, + uint16_t port) : DataSocket(socket(AF_INET, SOCK_STREAM, 0)), socketType(stype) { struct addrinfo hints, *result; @@ -20,22 +21,25 @@ ClientSocket::ClientSocket(std::string stype, const std::string &host, uint16_t hints.ai_flags |= AI_CANONNAME; if (getaddrinfo(host.c_str(), NULL, &hints, &result) != 0) { - std::string msg = - "ClientSocket cannot decode host:" + host + " on port " + std::to_string(port) + "\n"; + std::string msg = "ClientSocket cannot decode host:" + host + + " on port " + std::to_string(port) + "\n"; throw SocketError(msg); } - // TODO! Erik, results could have multiple entries do we need to loop through them? - // struct sockaddr_in serverAddr {}; + // TODO! Erik, results could have multiple entries do we need to loop + // through them? struct sockaddr_in serverAddr {}; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port); - memcpy((char *)&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, + memcpy((char *)&serverAddr.sin_addr.s_addr, + &((struct sockaddr_in *)result->ai_addr)->sin_addr, sizeof(in_addr_t)); - if (::connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) { + if (::connect(getSocketId(), (struct sockaddr *)&serverAddr, + sizeof(serverAddr)) != 0) { freeaddrinfo(result); - std::string msg = "ClientSocket: Cannot connect to " + socketType + ":" + - host + " on port " + std::to_string(port) + "\n"; + std::string msg = "ClientSocket: Cannot connect to " + socketType + + ":" + host + " on port " + std::to_string(port) + + "\n"; throw SocketError(msg); } freeaddrinfo(result); @@ -47,13 +51,15 @@ ClientSocket::ClientSocket(std::string sType, struct sockaddr_in addr) if (::connect(getSocketId(), (struct sockaddr *)&addr, sizeof(addr)) != 0) { char address[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &addr.sin_addr, address, INET_ADDRSTRLEN); - std::string msg = "ClientSocket: Cannot connect to " + socketType + ":" + address + " on port " + + std::string msg = "ClientSocket: Cannot connect to " + socketType + + ":" + address + " on port " + std::to_string(addr.sin_port) + "\n"; throw SocketError(msg); } } -int ClientSocket::sendCommandThenRead(int fnum, void *args, size_t args_size, void *retval, +int ClientSocket::sendCommandThenRead(int fnum, const void *args, + size_t args_size, void *retval, size_t retval_size) { int ret = slsDetectorDefs::FAIL; sendData(&fnum, sizeof(fnum)); @@ -69,7 +75,7 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) { char mess[MAX_STR_LENGTH]{}; // get error message receiveData(mess, sizeof(mess)); - FILE_LOG(logERROR) << socketType << " returned error: " << mess; + FILE_LOG(logERROR) << socketType << " returned error: " << mess; std::cout << "\n"; // needed to reset the color. // Do we need to know hostname here? diff --git a/slsSupportLib/src/DataSocket.cpp b/slsSupportLib/src/DataSocket.cpp index b64b9fda8..83f223f82 100755 --- a/slsSupportLib/src/DataSocket.cpp +++ b/slsSupportLib/src/DataSocket.cpp @@ -10,10 +10,12 @@ #include #include - namespace sls { -DataSocket::DataSocket(int socketId) : socketId_(socketId) {} +DataSocket::DataSocket(int socketId) : socketId_(socketId) { + int value = 1; + setsockopt(socketId_, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)); +} DataSocket::~DataSocket() { if (socketId_ <= 0) { @@ -27,7 +29,9 @@ DataSocket::~DataSocket() { } } -void DataSocket::swap(DataSocket &other) noexcept { std::swap(socketId_, other.socketId_); } +void DataSocket::swap(DataSocket &other) noexcept { + std::swap(socketId_, other.socketId_); +} DataSocket::DataSocket(DataSocket &&move) noexcept { move.swap(*this); } DataSocket &DataSocket::operator=(DataSocket &&move) noexcept { @@ -36,21 +40,21 @@ DataSocket &DataSocket::operator=(DataSocket &&move) noexcept { } size_t DataSocket::receiveData(void *buffer, size_t size) { - // std::cout << "Sending\n"; size_t dataRead = 0; while (dataRead < size) { dataRead += - read(getSocketId(), reinterpret_cast(buffer) + dataRead, size - dataRead); + read(getSocketId(), reinterpret_cast(buffer) + dataRead, + size - dataRead); } return dataRead; } -size_t DataSocket::sendData(void *buffer, size_t size) { - // std::cout << "Receiving\n"; +size_t DataSocket::sendData(const void *buffer, size_t size) { size_t dataSent = 0; while (dataSent < size) { dataSent += - write(getSocketId(), reinterpret_cast(buffer) + dataSent, size - dataSent); + write(getSocketId(), reinterpret_cast(buffer) + dataSent, + size - dataSent); } return dataSent; } @@ -63,14 +67,16 @@ int DataSocket::setTimeOut(int t_seconds) { t.tv_sec = 0; t.tv_usec = 0; // Receive timeout indefinet - if (::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(struct timeval)) < 0) { + if (::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, &t, + sizeof(struct timeval)) < 0) { FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0; } t.tv_sec = t_seconds; t.tv_usec = 0; // Sending timeout in seconds - if (::setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, &t, sizeof(struct timeval)) < 0) { + if (::setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, &t, + sizeof(struct timeval)) < 0) { FILE_LOG(logERROR) << "setsockopt SO_SNDTIMEO " << t_seconds; } return 0; @@ -78,22 +84,23 @@ int DataSocket::setTimeOut(int t_seconds) { void DataSocket::close() { if (socketId_ > 0) { - if(::close(socketId_)){ + if (::close(socketId_)) { throw SocketError("could not close socket"); } socketId_ = -1; - + } else { throw std::runtime_error("Socket ERROR: close called on bad socket\n"); } } void DataSocket::shutDownSocket() { - shutdown(getSocketId(), SHUT_RDWR); - close(); + shutdown(getSocketId(), SHUT_RDWR); + close(); } -struct sockaddr_in ConvertHostnameToInternetAddress(const std::string &hostname) { +struct sockaddr_in +ConvertHostnameToInternetAddress(const std::string &hostname) { struct addrinfo hints, *result; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; @@ -107,13 +114,15 @@ struct sockaddr_in ConvertHostnameToInternetAddress(const std::string &hostname) throw SocketError(msg); } serverAddr.sin_family = AF_INET; - memcpy((char *)&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, + memcpy((char *)&serverAddr.sin_addr.s_addr, + &((struct sockaddr_in *)result->ai_addr)->sin_addr, sizeof(in_addr_t)); freeaddrinfo(result); return serverAddr; } -int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrinfo **res) { +int ConvertHostnameToInternetAddress(const char *const hostname, + struct ::addrinfo **res) { // criteria in selecting socket address structures returned by res struct ::addrinfo hints; memset(&hints, 0, sizeof(hints)); @@ -123,7 +132,8 @@ int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrin int errcode = getaddrinfo(hostname, NULL, &hints, res); if (errcode != 0) { FILE_LOG(logERROR) << "Could not convert hostname (" << hostname - << ") to internet address (zmq):" << gai_strerror(errcode); + << ") to internet address (zmq):" + << gai_strerror(errcode); } else { if (*res == NULL) { FILE_LOG(logERROR) << "Could not converthostname (" << hostname @@ -146,9 +156,11 @@ int ConvertHostnameToInternetAddress(const char *const hostname, struct ::addrin * @return 1 for fail, 0 for success */ // Do not make this static (for multi threading environment) -int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, const int ipsize) { - if (inet_ntop(res->ai_family, &((struct sockaddr_in *)res->ai_addr)->sin_addr, ip, ipsize) != - NULL) { +int ConvertInternetAddresstoIpString(struct ::addrinfo *res, char *ip, + const int ipsize) { + if (inet_ntop(res->ai_family, + &((struct sockaddr_in *)res->ai_addr)->sin_addr, ip, + ipsize) != NULL) { ::freeaddrinfo(res); return 0; } diff --git a/slsSupportLib/tests/CMakeLists.txt b/slsSupportLib/tests/CMakeLists.txt index dad82546a..be9ef61f3 100755 --- a/slsSupportLib/tests/CMakeLists.txt +++ b/slsSupportLib/tests/CMakeLists.txt @@ -6,4 +6,5 @@ target_sources(tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test-string_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test-Timer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test-sls_detector_defs.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test-Sockets.cpp ) \ No newline at end of file diff --git a/slsSupportLib/tests/test-Sockets.cpp b/slsSupportLib/tests/test-Sockets.cpp new file mode 100644 index 000000000..184cbc71e --- /dev/null +++ b/slsSupportLib/tests/test-Sockets.cpp @@ -0,0 +1,45 @@ +#include "ClientSocket.h" +#include "ServerSocket.h" +#include "catch.hpp" +#include +#include +#include + +std::vector server() { + std::cout << "starting server\n"; + auto server = sls::ServerSocket(1950); + auto s = server.accept(); + std::vector buffer(100, '\0'); + s.receiveData(buffer.data(), buffer.size()); + std::cout << "ServerReceived: " << std::string(buffer.begin(), buffer.end()) + << '\n'; + + std::vector to_send(100, '\0'); + to_send[0] = 'O'; + to_send[1] = 'K'; + s.sendData(to_send.data(), to_send.size()); + s.close(); + return buffer; +} + +TEST_CASE("The server recive the same message as we send", "[support]") { + std::vector received_message(100, '\0'); + std::vector sent_message(100, '\0'); + const char m[]{"some message"}; + std::copy(std::begin(m), std::end(m), sent_message.data()); + + auto s = std::async(std::launch::async, server); + auto client = sls::DetectorSocket("localhost", 1950); + client.sendData(sent_message.data(), sent_message.size()); + client.receiveData(received_message.data(), received_message.size()); + client.close(); + auto server_message = s.get(); + + CHECK(server_message == sent_message); + CHECK(std::string(received_message.data()) == "OK" ); + CHECK(client.getSocketId() == -1); +} + +TEST_CASE("throws on no server", "[support]"){ + CHECK_THROWS(sls::DetectorSocket("localhost", 1950)); +} \ No newline at end of file