zmqip now uint

This commit is contained in:
Erik Frojdh 2019-06-06 18:17:04 +02:00
parent c86a1e7d48
commit 170aca0e1f
6 changed files with 90 additions and 176 deletions

View File

@ -16,6 +16,7 @@
class multiSlsDetector; class multiSlsDetector;
class ServerInterface; class ServerInterface;
#define SLS_SHMVERSION 0x190515 #define SLS_SHMVERSION 0x190515
@ -176,10 +177,10 @@ struct sharedSlsDetector {
int rxReadFreq; int rxReadFreq;
/** zmq tcp src ip address in client (only data) **/ /** zmq tcp src ip address in client (only data) **/
char zmqip[MAX_STR_LENGTH]; sls::IpAddr zmqip;
/** zmq tcp src ip address in receiver (only data) **/ /** zmq tcp src ip address in receiver (only data) **/
char rxZmqip[MAX_STR_LENGTH]; sls::IpAddr rxZmqip;
/** gap pixels enable */ /** gap pixels enable */
int gappixels; int gappixels;

View File

@ -348,9 +348,9 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
(detId * ((shm()->myDetectorType == EIGER) ? 2 : 1)); (detId * ((shm()->myDetectorType == EIGER) ? 2 : 1));
shm()->rxUpstream = false; shm()->rxUpstream = false;
shm()->rxReadFreq = 0; shm()->rxReadFreq = 0;
memset(shm()->zmqip, 0, MAX_STR_LENGTH); shm()->zmqip = 0u;
memset(shm()->rxZmqip, 0, MAX_STR_LENGTH); shm()->rxZmqip = 0u;
shm()->gappixels = 0; shm()->gappixels = 0u;
memset(shm()->rxAdditionalJsonHeader, 0, MAX_STR_LENGTH); memset(shm()->rxAdditionalJsonHeader, 0, MAX_STR_LENGTH);
shm()->detectorControlAPIVersion = 0; shm()->detectorControlAPIVersion = 0;
shm()->detectorStopAPIVersion = 0; shm()->detectorStopAPIVersion = 0;
@ -413,7 +413,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
// update #nchans and databytes, as it depends on #samples, adcmask, // update #nchans and databytes, as it depends on #samples, adcmask,
// readoutflags (ctb only) // readoutflags (ctb only)
updateTotalNumberOfChannels(); updateTotalNumberOfChannels();
} }
int slsDetector::sendModule(sls_detector_module *myMod, int slsDetector::sendModule(sls_detector_module *myMod,
@ -474,8 +474,7 @@ int slsDetector::sendModule(sls_detector_module *myMod,
int slsDetector::receiveModule(sls_detector_module *myMod, int slsDetector::receiveModule(sls_detector_module *myMod,
sls::ClientSocket &client) { sls::ClientSocket &client) {
int ts = 0; int ts = 0;
ts += ts += client.Receive(&(myMod->serialnumber), sizeof(myMod->serialnumber));
client.Receive(&(myMod->serialnumber), sizeof(myMod->serialnumber));
ts += client.Receive(&(myMod->nchan), sizeof(myMod->nchan)); ts += client.Receive(&(myMod->nchan), sizeof(myMod->nchan));
ts += client.Receive(&(myMod->nchip), sizeof(myMod->nchip)); ts += client.Receive(&(myMod->nchip), sizeof(myMod->nchip));
ts += client.Receive(&(myMod->ndac), sizeof(myMod->ndac)); ts += client.Receive(&(myMod->ndac), sizeof(myMod->ndac));
@ -511,7 +510,7 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multi_id,
std::ostringstream ss; std::ostringstream ss;
ss << "Single shared memory (" << multi_id << "-" << detId ss << "Single shared memory (" << multi_id << "-" << detId
<< ":)version mismatch (expected 0x" << std::hex << SLS_SHMVERSION << ":)version mismatch (expected 0x" << std::hex << SLS_SHMVERSION
<< " but got 0x" << shm()->shmversion << ")" << std::dec << " but got 0x" << shm()->shmversion << ")" << std::dec
<< ". Clear Shared memory to continue."; << ". Clear Shared memory to continue.";
shm.UnmapSharedMemory(); shm.UnmapSharedMemory();
throw SharedMemoryError(ss.str()); throw SharedMemoryError(ss.str());
@ -591,24 +590,26 @@ void slsDetector::updateTotalNumberOfChannels() {
++nachans; ++nachans;
} }
} }
adatabytes = nachans * (shm()->dynamicRange / 8) * adatabytes = nachans * (shm()->dynamicRange / 8) *
shm()->timerValue[ANALOG_SAMPLES]; shm()->timerValue[ANALOG_SAMPLES];
FILE_LOG(logDEBUG1) FILE_LOG(logDEBUG1) << "#Analog Channels:" << nachans
<< "#Analog Channels:" << nachans << " Databytes: " << adatabytes; << " Databytes: " << adatabytes;
} }
// digital channels (ctb only, digital, analog/digital readout) // digital channels (ctb only, digital, analog/digital readout)
if (shm()->myDetectorType == CHIPTESTBOARD && if (shm()->myDetectorType == CHIPTESTBOARD &&
(((shm()->roFlags & DIGITAL_ONLY) != 0) || ((shm()->roFlags & ANALOG_AND_DIGITAL) != 0))) { (((shm()->roFlags & DIGITAL_ONLY) != 0) ||
((shm()->roFlags & ANALOG_AND_DIGITAL) != 0))) {
ndchans = 64; ndchans = 64;
ddatabytes = (sizeof(uint64_t) * shm()->timerValue[DIGITAL_SAMPLES]); ddatabytes =
(sizeof(uint64_t) * shm()->timerValue[DIGITAL_SAMPLES]);
FILE_LOG(logDEBUG1) << "#Digital Channels:" << ndchans FILE_LOG(logDEBUG1) << "#Digital Channels:" << ndchans
<< " Databytes: " << ddatabytes; << " Databytes: " << ddatabytes;
} }
shm()->nChans = nachans + ndchans; shm()->nChans = nachans + ndchans;
shm()->dataBytes = adatabytes + ddatabytes; shm()->dataBytes = adatabytes + ddatabytes;
FILE_LOG(logDEBUG1) << "# Total #Channels:" << shm()->nChans FILE_LOG(logDEBUG1) << "# Total #Channels:" << shm()->nChans
<< " Databytes: " << shm()->dataBytes; << " Databytes: " << shm()->dataBytes;
} }
} }
@ -884,7 +885,7 @@ int slsDetector::updateDetectorNoWait(sls::ClientSocket &client) {
if (i64 >= 0) { if (i64 >= 0) {
shm()->timerValue[ANALOG_SAMPLES] = i64; shm()->timerValue[ANALOG_SAMPLES] = i64;
} }
// digital samples // digital samples
n += client.Receive(&i64, sizeof(i64)); n += client.Receive(&i64, sizeof(i64));
if (i64 >= 0) { if (i64 >= 0) {
@ -897,9 +898,9 @@ int slsDetector::updateDetectorNoWait(sls::ClientSocket &client) {
shm()->adcEnableMask = u32; shm()->adcEnableMask = u32;
if (shm()->myDetectorType == MOENCH) if (shm()->myDetectorType == MOENCH)
setAdditionalJsonParameter("adcmask", std::to_string(u32)); setAdditionalJsonParameter("adcmask", std::to_string(u32));
// update #nchans and databytes, as it depends on #samples, adcmask, // update #nchans and databytes, as it depends on #samples, adcmask,
// readoutflags // readoutflags
updateTotalNumberOfChannels(); updateTotalNumberOfChannels();
} }
@ -1472,7 +1473,7 @@ int slsDetector::configureMAC() {
ret = client.sendCommandThenRead(fnum, args, sizeof(args), retvals, ret = client.sendCommandThenRead(fnum, args, sizeof(args), retvals,
sizeof(retvals)); sizeof(retvals));
//TODO!(Erik) Send as int already from detector // TODO!(Erik) Send as int already from detector
uint64_t detector_mac = 0; uint64_t detector_mac = 0;
uint32_t detector_ip = 0; uint32_t detector_ip = 0;
sscanf(retvals[0], "%lx", &detector_mac); sscanf(retvals[0], "%lx", &detector_mac);
@ -1537,7 +1538,7 @@ int64_t slsDetector::setTimer(timerIndex index, int64_t t) {
FILE_LOG(logDEBUG1) << getTimerType(index) << ": " << retval; FILE_LOG(logDEBUG1) << getTimerType(index) << ": " << retval;
shm()->timerValue[index] = retval; shm()->timerValue[index] = retval;
// update #nchans and databytes, as it depends on #samples, adcmask, // update #nchans and databytes, as it depends on #samples, adcmask,
// readoutflags // readoutflags
if (index == ANALOG_SAMPLES || index == DIGITAL_SAMPLES) { if (index == ANALOG_SAMPLES || index == DIGITAL_SAMPLES) {
updateTotalNumberOfChannels(); updateTotalNumberOfChannels();
} }
@ -1729,7 +1730,7 @@ int slsDetector::setReadOutFlags(readOutFlags flag) {
FILE_LOG(logDEBUG1) << "Readout flag: " << retval; FILE_LOG(logDEBUG1) << "Readout flag: " << retval;
shm()->roFlags = retval; shm()->roFlags = retval;
// update #nchans and databytes, as it depends on #samples, adcmask, // update #nchans and databytes, as it depends on #samples, adcmask,
// readoutflags // readoutflags
if (shm()->myDetectorType == CHIPTESTBOARD) { if (shm()->myDetectorType == CHIPTESTBOARD) {
updateTotalNumberOfChannels(); updateTotalNumberOfChannels();
} }
@ -1904,7 +1905,7 @@ std::string slsDetector::setReceiverHostname(const std::string &receiverIP) {
<< (shm()->timerValue[SUBFRAME_ACQUISITION_TIME]) << (shm()->timerValue[SUBFRAME_ACQUISITION_TIME])
<< "\nsub dead time:" << (shm()->timerValue[SUBFRAME_DEADTIME]) << "\nsub dead time:" << (shm()->timerValue[SUBFRAME_DEADTIME])
<< "\nasamples:" << (shm()->timerValue[ANALOG_SAMPLES]) << "\nasamples:" << (shm()->timerValue[ANALOG_SAMPLES])
<< "\ndsamples:" << (shm()->timerValue[DIGITAL_SAMPLES]) << "\ndsamples:" << (shm()->timerValue[DIGITAL_SAMPLES])
<< "\ndynamic range:" << shm()->dynamicRange << "\ndynamic range:" << shm()->dynamicRange
<< "\nflippeddatax:" << (shm()->flippedData[X]) << "\nflippeddatax:" << (shm()->flippedData[X])
<< "\nactivated: " << shm()->activated << "\nactivated: " << shm()->activated
@ -1919,8 +1920,7 @@ std::string slsDetector::setReceiverHostname(const std::string &receiverIP) {
<< "\nrx additional json header:" << shm()->rxAdditionalJsonHeader << "\nrx additional json header:" << shm()->rxAdditionalJsonHeader
<< "\nrx_datastream:" << enableDataStreamingFromReceiver(-1) << "\nrx_datastream:" << enableDataStreamingFromReceiver(-1)
<< "\nrx_dbitlistsize:" << shm()->rxDbitList.size() << "\nrx_dbitlistsize:" << shm()->rxDbitList.size()
<< "\nrx_DbitOffset:" << shm()->rxDbitOffset << "\nrx_DbitOffset:" << shm()->rxDbitOffset << std::endl;
<< std::endl;
if (setDetectorType(shm()->myDetectorType) != GENERIC) { if (setDetectorType(shm()->myDetectorType) != GENERIC) {
sendMultiDetectorSize(); sendMultiDetectorSize();
@ -1959,7 +1959,7 @@ std::string slsDetector::setReceiverHostname(const std::string &receiverIP) {
enableTenGigabitEthernet(shm()->tenGigaEnable); enableTenGigabitEthernet(shm()->tenGigaEnable);
setReadOutFlags(GET_READOUT_FLAGS); setReadOutFlags(GET_READOUT_FLAGS);
break; break;
case CHIPTESTBOARD: case CHIPTESTBOARD:
setTimer(ANALOG_SAMPLES, shm()->timerValue[ANALOG_SAMPLES]); setTimer(ANALOG_SAMPLES, shm()->timerValue[ANALOG_SAMPLES]);
setTimer(DIGITAL_SAMPLES, shm()->timerValue[DIGITAL_SAMPLES]); setTimer(DIGITAL_SAMPLES, shm()->timerValue[DIGITAL_SAMPLES]);
@ -1977,8 +1977,8 @@ std::string slsDetector::setReceiverHostname(const std::string &receiverIP) {
break; break;
case GOTTHARD: case GOTTHARD:
sendROI(-1, nullptr); sendROI(-1, nullptr);
break; break;
default: default:
break; break;
@ -2141,25 +2141,17 @@ void slsDetector::setReceiverStreamingPort(int port) {
int slsDetector::getReceiverStreamingPort() { return shm()->rxZmqport; } int slsDetector::getReceiverStreamingPort() { return shm()->rxZmqport; }
void slsDetector::setClientStreamingIP(const std::string &sourceIP) { void slsDetector::setClientStreamingIP(const std::string &sourceIP) {
struct addrinfo *result; auto ip = HostnameToIp(sourceIP.c_str());
// on failure to convert to a valid ip if (ip != 0) {
if (sls::ConvertHostnameToInternetAddress(sourceIP.c_str(), &result) != 0) { shm()->zmqip = ip;
throw RuntimeError("Could not convert zmqip into a valid IP" + } else {
sourceIP); throw sls::RuntimeError("Could not set zmqip");
} }
// on success put IP as std::string into arg
memset(shm()->zmqip, 0, MAX_STR_LENGTH);
sls::ConvertInternetAddresstoIpString(result, shm()->zmqip, MAX_STR_LENGTH);
} }
std::string slsDetector::getClientStreamingIP() { std::string slsDetector::getClientStreamingIP() { return shm()->zmqip.str(); }
return std::string(shm()->zmqip);
}
void slsDetector::setReceiverStreamingIP(std::string sourceIP) { void slsDetector::setReceiverStreamingIP(std::string sourceIP) {
char args[MAX_STR_LENGTH]{};
char retvals[MAX_STR_LENGTH]{};
// if empty, give rx_hostname // if empty, give rx_hostname
if (sourceIP.empty()) { if (sourceIP.empty()) {
if (strcmp(shm()->rxHostname, "none") == 0) { if (strcmp(shm()->rxHostname, "none") == 0) {
@ -2171,40 +2163,28 @@ void slsDetector::setReceiverStreamingIP(std::string sourceIP) {
FILE_LOG(logDEBUG1) << "Sending receiver streaming IP to receiver: " FILE_LOG(logDEBUG1) << "Sending receiver streaming IP to receiver: "
<< sourceIP; << sourceIP;
// verify the ip shm()->rxZmqip = HostnameToIp(sourceIP.c_str());
{
struct addrinfo *result;
// on failure to convert to a valid ip
if (sls::ConvertHostnameToInternetAddress(sourceIP.c_str(), &result) !=
0) {
throw RuntimeError("Could not convert rx_zmqip into a valid IP" +
sourceIP);
}
// on success put IP as std::string into arg
sls::ConvertInternetAddresstoIpString(result, args, sizeof(args));
}
// set it anyway, else it is lost if rx_hostname is not set yet
memset(shm()->rxZmqip, 0, MAX_STR_LENGTH);
sls::strcpy_safe(shm()->rxZmqip, args);
// if zmqip is empty, update it // if zmqip is empty, update it
if (shm()->zmqip != 0u) { if (shm()->zmqip == 0) {
sls::strcpy_safe(shm()->zmqip, args); shm()->zmqip = shm()->rxZmqip;
} }
FILE_LOG(logDEBUG1) << "Sending receiver streaming IP to receiver: "
<< args;
// send to receiver // send to receiver
if (shm()->rxOnlineFlag == ONLINE_FLAG) { if (shm()->rxOnlineFlag == ONLINE_FLAG) {
char retvals[MAX_STR_LENGTH]{};
char args[MAX_STR_LENGTH]{};
sls::strcpy_safe(args, shm()->rxZmqip.str()); //TODO send int
FILE_LOG(logDEBUG1)
<< "Sending receiver streaming IP to receiver: " << args;
sendToReceiver(F_RECEIVER_STREAMING_SRC_IP, args, retvals); sendToReceiver(F_RECEIVER_STREAMING_SRC_IP, args, retvals);
FILE_LOG(logDEBUG1) << "Receiver streaming port: " << retvals; FILE_LOG(logDEBUG1) << "Receiver streaming ip: " << retvals;
memset(shm()->rxZmqip, 0, MAX_STR_LENGTH); shm()->rxZmqip = retvals;
sls::strcpy_safe(shm()->rxZmqip, retvals);
} }
} }
std::string slsDetector::getReceiverStreamingIP() { std::string slsDetector::getReceiverStreamingIP() {
return std::string(shm()->rxZmqip); return shm()->rxZmqip.str();
} }
int slsDetector::setDetectorNetworkParameter(networkParameter index, int slsDetector::setDetectorNetworkParameter(networkParameter index,
@ -2645,25 +2625,28 @@ int slsDetector::sendROI(int n, ROI roiLimits[]) {
void slsDetector::setADCEnableMask(uint32_t mask) { void slsDetector::setADCEnableMask(uint32_t mask) {
uint32_t arg = mask; uint32_t arg = mask;
FILE_LOG(logDEBUG1) << "Setting ADC Enable mask to 0x" << std::hex << arg << std::dec; FILE_LOG(logDEBUG1) << "Setting ADC Enable mask to 0x" << std::hex << arg
<< std::dec;
if (shm()->onlineFlag == ONLINE_FLAG) { if (shm()->onlineFlag == ONLINE_FLAG) {
sendToDetector(F_SET_ADC_ENABLE_MASK, &arg, sizeof(arg), nullptr, 0); sendToDetector(F_SET_ADC_ENABLE_MASK, &arg, sizeof(arg), nullptr, 0);
shm()->adcEnableMask = mask; shm()->adcEnableMask = mask;
} }
// update #nchans and databytes, as it depends on #samples, adcmask, readoutflags // update #nchans and databytes, as it depends on #samples, adcmask,
// readoutflags
updateTotalNumberOfChannels(); updateTotalNumberOfChannels();
// send to processor // send to processor
if (shm()->myDetectorType == MOENCH) if (shm()->myDetectorType == MOENCH)
setAdditionalJsonParameter("adcmask", std::to_string(shm()->adcEnableMask)); setAdditionalJsonParameter("adcmask",
std::to_string(shm()->adcEnableMask));
if (shm()->rxOnlineFlag == ONLINE_FLAG) { if (shm()->rxOnlineFlag == ONLINE_FLAG) {
int fnum = F_RECEIVER_SET_ADC_MASK; int fnum = F_RECEIVER_SET_ADC_MASK;
int retval = -1; int retval = -1;
mask = shm()->adcEnableMask; mask = shm()->adcEnableMask;
FILE_LOG(logDEBUG1) FILE_LOG(logDEBUG1) << "Setting ADC Enable mask to 0x" << std::hex
<< "Setting ADC Enable mask to 0x" << std:: hex << mask << std::dec << " in receiver"; << mask << std::dec << " in receiver";
sendToReceiver(fnum, &mask, sizeof(mask), &retval, sizeof(retval)); sendToReceiver(fnum, &mask, sizeof(mask), &retval, sizeof(retval));
} }
} }
@ -2672,15 +2655,18 @@ uint32_t slsDetector::getADCEnableMask() {
uint32_t retval = -1; uint32_t retval = -1;
FILE_LOG(logDEBUG1) << "Getting ADC Enable mask"; FILE_LOG(logDEBUG1) << "Getting ADC Enable mask";
if (shm()->onlineFlag == ONLINE_FLAG) { if (shm()->onlineFlag == ONLINE_FLAG) {
sendToDetector(F_GET_ADC_ENABLE_MASK, nullptr, 0, &retval, sizeof(retval)); sendToDetector(F_GET_ADC_ENABLE_MASK, nullptr, 0, &retval,
sizeof(retval));
shm()->adcEnableMask = retval; shm()->adcEnableMask = retval;
FILE_LOG(logDEBUG1) << "ADC Enable Mask: 0x" << std::hex << retval << std::dec; FILE_LOG(logDEBUG1)
<< "ADC Enable Mask: 0x" << std::hex << retval << std::dec;
} }
return shm()->adcEnableMask; return shm()->adcEnableMask;
} }
void slsDetector::setADCInvert(uint32_t value) { void slsDetector::setADCInvert(uint32_t value) {
FILE_LOG(logDEBUG1) << "Setting ADC Invert to 0x" << std::hex << value << std::dec; FILE_LOG(logDEBUG1) << "Setting ADC Invert to 0x" << std::hex << value
<< std::dec;
if (shm()->onlineFlag == ONLINE_FLAG) { if (shm()->onlineFlag == ONLINE_FLAG) {
sendToDetector(F_SET_ADC_INVERT, value, nullptr); sendToDetector(F_SET_ADC_INVERT, value, nullptr);
} }
@ -2691,7 +2677,8 @@ uint32_t slsDetector::getADCInvert() {
FILE_LOG(logDEBUG1) << "Getting ADC Invert"; FILE_LOG(logDEBUG1) << "Getting ADC Invert";
if (shm()->onlineFlag == ONLINE_FLAG) { if (shm()->onlineFlag == ONLINE_FLAG) {
sendToDetector(F_GET_ADC_INVERT, nullptr, retval); sendToDetector(F_GET_ADC_INVERT, nullptr, retval);
FILE_LOG(logDEBUG1) << "ADC Invert: 0x" << std::hex << retval << std::dec; FILE_LOG(logDEBUG1)
<< "ADC Invert: 0x" << std::hex << retval << std::dec;
} }
return retval; return retval;
} }
@ -2722,9 +2709,7 @@ int slsDetector::setExternalSampling(int value) {
return retval; return retval;
} }
int slsDetector::getExternalSampling() { int slsDetector::getExternalSampling() { return setExternalSampling(-1); }
return setExternalSampling(-1);
}
void slsDetector::setReceiverDbitList(std::vector<int> list) { void slsDetector::setReceiverDbitList(std::vector<int> list) {
FILE_LOG(logDEBUG1) << "Setting Receiver Dbit List"; FILE_LOG(logDEBUG1) << "Setting Receiver Dbit List";
@ -2734,7 +2719,8 @@ void slsDetector::setReceiverDbitList(std::vector<int> list) {
} }
for (auto &it : list) { for (auto &it : list) {
if (it < 0 || it > 63) { if (it < 0 || it > 63) {
throw sls::RuntimeError("Dbit list value must be between 0 and 63\n"); throw sls::RuntimeError(
"Dbit list value must be between 0 and 63\n");
} }
} }
shm()->rxDbitList = list; shm()->rxDbitList = list;
@ -2749,7 +2735,7 @@ std::vector<int> slsDetector::getReceiverDbitList() {
if (shm()->rxOnlineFlag == ONLINE_FLAG) { if (shm()->rxOnlineFlag == ONLINE_FLAG) {
sendToReceiver(F_GET_RECEIVER_DBIT_LIST, nullptr, retval); sendToReceiver(F_GET_RECEIVER_DBIT_LIST, nullptr, retval);
shm()->rxDbitList = retval; shm()->rxDbitList = retval;
} }
return shm()->rxDbitList; return shm()->rxDbitList;
} }
@ -2757,7 +2743,8 @@ int slsDetector::setReceiverDbitOffset(int value) {
int retval = -1; int retval = -1;
if (value >= 0) if (value >= 0)
shm()->rxDbitOffset = value; shm()->rxDbitOffset = value;
FILE_LOG(logDEBUG1) << "Setting digital bit offset in receiver to " << value; FILE_LOG(logDEBUG1) << "Setting digital bit offset in receiver to "
<< value;
if (shm()->rxOnlineFlag == ONLINE_FLAG) { if (shm()->rxOnlineFlag == ONLINE_FLAG) {
sendToReceiver(F_RECEIVER_DBIT_OFFSET, value, retval); sendToReceiver(F_RECEIVER_DBIT_OFFSET, value, retval);
FILE_LOG(logDEBUG1) << "Receiver digital bit offset: " << retval; FILE_LOG(logDEBUG1) << "Receiver digital bit offset: " << retval;
@ -2765,9 +2752,7 @@ int slsDetector::setReceiverDbitOffset(int value) {
return shm()->rxDbitOffset; return shm()->rxDbitOffset;
} }
int slsDetector::getReceiverDbitOffset() { int slsDetector::getReceiverDbitOffset() { return shm()->rxDbitOffset; }
return shm()->rxDbitOffset;
}
int slsDetector::writeAdcRegister(uint32_t addr, uint32_t val) { int slsDetector::writeAdcRegister(uint32_t addr, uint32_t val) {
uint32_t args[]{addr, val}; uint32_t args[]{addr, val};
@ -3375,7 +3360,7 @@ int slsDetector::updateCachedReceiverVariables() const {
// streaming source ip // streaming source ip
n += receiver.Receive(cstring, sizeof(cstring)); n += receiver.Receive(cstring, sizeof(cstring));
sls::strcpy_safe(shm()->rxZmqip, cstring); shm()->rxZmqip = cstring;
// additional json header // additional json header
n += receiver.Receive(cstring, sizeof(cstring)); n += receiver.Receive(cstring, sizeof(cstring));
@ -3643,7 +3628,8 @@ bool slsDetector::getFileWrite() const { return shm()->rxFileWrite; }
bool slsDetector::setMasterFileWrite(bool value) { bool slsDetector::setMasterFileWrite(bool value) {
int arg = static_cast<int>(value); int arg = static_cast<int>(value);
int retval = -1; int retval = -1;
FILE_LOG(logDEBUG1) << "Sending enable master file write to receiver: " << arg; FILE_LOG(logDEBUG1) << "Sending enable master file write to receiver: "
<< arg;
if (shm()->rxOnlineFlag == ONLINE_FLAG) { if (shm()->rxOnlineFlag == ONLINE_FLAG) {
sendToReceiver(F_ENABLE_RECEIVER_MASTER_FILE_WRITE, arg, retval); sendToReceiver(F_ENABLE_RECEIVER_MASTER_FILE_WRITE, arg, retval);
FILE_LOG(logDEBUG1) << "Receiver master file write enable: " << retval; FILE_LOG(logDEBUG1) << "Receiver master file write enable: " << retval;
@ -3652,7 +3638,9 @@ bool slsDetector::setMasterFileWrite(bool value) {
return getMasterFileWrite(); return getMasterFileWrite();
} }
bool slsDetector::getMasterFileWrite() const { return shm()->rxMasterFileWrite; } bool slsDetector::getMasterFileWrite() const {
return shm()->rxMasterFileWrite;
}
bool slsDetector::setFileOverWrite(bool value) { bool slsDetector::setFileOverWrite(bool value) {
int arg = static_cast<int>(value); int arg = static_cast<int>(value);

View File

@ -3,10 +3,13 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <netdb.h> #include <netdb.h>
#include <string>
#include <numeric> #include <numeric>
#include <string>
namespace sls { namespace sls {
/* Base class for TCP socket, this is used to send data between detector, client
and receiver. Specific protocols inherit from this class.*/
class DataSocket { class DataSocket {
public: public:
DataSocket(int socketId); DataSocket(int socketId);
@ -14,27 +17,28 @@ class DataSocket {
virtual ~DataSocket(); virtual ~DataSocket();
DataSocket &operator=(DataSocket &&move) noexcept; DataSocket &operator=(DataSocket &&move) noexcept;
void swap(DataSocket &other) noexcept; void swap(DataSocket &other) noexcept;
//No copy since the class manage the underlying socket
DataSocket(const DataSocket &) = delete; DataSocket(const DataSocket &) = delete;
DataSocket &operator=(DataSocket const &) = delete; DataSocket &operator=(DataSocket const &) = delete;
int getSocketId() const { return socketId_; } int getSocketId() const { return socketId_; }
int Send(const void *buffer, size_t size); int Send(const void *buffer, size_t size);
template <typename T> int Send(T &&data) { template <typename T> int Send(T &&data) {
return Send(&data, sizeof(data)); return Send(&data, sizeof(data));
} }
// Variadic template to send all arguments
// Trick to send all
template <class... Args> int SendAll(Args &&... args) { template <class... Args> int SendAll(Args &&... args) {
auto l = std::initializer_list<int>{Send(args)...}; auto l = std::initializer_list<int>{Send(args)...};
auto sum = std::accumulate(begin(l), end(l), 0); auto sum = std::accumulate(begin(l), end(l), 0);
return sum; return sum;
} }
int Receive(void *buffer, size_t size); int Receive(void *buffer, size_t size);
template <typename T> int Receive(T &arg) { template <typename T> int Receive(T &arg) {
return Receive(&arg, sizeof(arg)); return Receive(&arg, sizeof(arg));
} }
template <typename T> T Receive() { template <typename T> T Receive() {
T arg; T arg;
Receive(&arg, sizeof(arg)); Receive(&arg, sizeof(arg));
@ -52,12 +56,4 @@ class DataSocket {
int socketId_ = -1; int socketId_ = -1;
}; };
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);
}; // namespace sls }; // namespace sls

View File

@ -56,7 +56,7 @@ class MacAddr {
constexpr uint64_t uint64() const noexcept { return addr_; } constexpr uint64_t uint64() const noexcept { return addr_; }
}; };
uint32_t HostnameToIp(const char *hostname); IpAddr HostnameToIp(const char *hostname);
std::string IpToInterfaceName(const std::string& ip); std::string IpToInterfaceName(const std::string& ip);
MacAddr InterfaceNameToMac(std::string inf); MacAddr InterfaceNameToMac(std::string inf);

View File

@ -27,7 +27,6 @@ DataSocket::~DataSocket() {
try { try {
close(); close();
} catch (...) { } catch (...) {
// pass
} }
} }
} }
@ -73,7 +72,7 @@ int DataSocket::Send(const void *buffer, size_t size) {
break; break;
bytes_sent += this_send; bytes_sent += this_send;
} }
if (bytes_sent != data_size){ if (bytes_sent != data_size) {
std::ostringstream ss; std::ostringstream ss;
ss << "TCP socket sent " << bytes_sent << " bytes instead of " ss << "TCP socket sent " << bytes_sent << " bytes instead of "
<< data_size << " bytes"; << data_size << " bytes";
@ -127,7 +126,6 @@ void DataSocket::close() {
throw SocketError("could not close socket"); throw SocketError("could not close socket");
} }
socketId_ = -1; socketId_ = -1;
} else { } else {
throw std::runtime_error("Socket ERROR: close called on bad socket\n"); throw std::runtime_error("Socket ERROR: close called on bad socket\n");
} }
@ -138,73 +136,4 @@ void DataSocket::shutDownSocket() {
close(); close();
} }
struct sockaddr_in
ConvertHostnameToInternetAddress(const std::string &hostname) {
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
struct sockaddr_in serverAddr {};
if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result) != 0) {
freeaddrinfo(result);
std::string msg = "ClientSocket cannot decode host:" + hostname + "\n";
throw SocketError(msg);
}
serverAddr.sin_family = AF_INET;
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) {
// criteria in selecting socket address structures returned by res
struct ::addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// get host info into res
int errcode = getaddrinfo(hostname, nullptr, &hints, res);
if (errcode != 0) {
FILE_LOG(logERROR) << "Could not convert hostname (" << hostname
<< ") to internet address (zmq):"
<< gai_strerror(errcode);
} else {
if (*res == nullptr) {
FILE_LOG(logERROR) << "Could not converthostname (" << hostname
<< ") to internet address (zmq):"
"gettaddrinfo returned null";
} else {
return 0;
}
}
FILE_LOG(logERROR) << "Could not convert hostname to internet address";
return 1;
};
/**
* Convert Internet Address structure pointer to ip string (char*)
* Clears the internet address structure as well
* @param res pointer to internet address structure
* @param ip pointer to char array to store result in
* @param ipsize size available in ip buffer
* @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) != nullptr) {
::freeaddrinfo(res);
return 0;
}
FILE_LOG(logERROR) << "Could not convert internet address to ip string";
return 1;
}
} // namespace sls } // namespace sls

View File

@ -75,7 +75,7 @@ std::ostream &operator<<(std::ostream &out, const MacAddr &addr) {
return out << addr.str(); return out << addr.str();
} }
uint32_t HostnameToIp(const char *hostname) { IpAddr HostnameToIp(const char *hostname) {
addrinfo hints; addrinfo hints;
addrinfo *result = nullptr; addrinfo *result = nullptr;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
@ -87,7 +87,7 @@ uint32_t HostnameToIp(const char *hostname) {
} }
uint32_t ip = ((sockaddr_in *)result->ai_addr)->sin_addr.s_addr; uint32_t ip = ((sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result); freeaddrinfo(result);
return ip; return IpAddr(ip);
} }
std::string IpToInterfaceName(const std::string &ip) { std::string IpToInterfaceName(const std::string &ip) {