diff --git a/slsDetectorSoftware/CMakeLists.txt b/slsDetectorSoftware/CMakeLists.txt index 003365f82..93c5bfcab 100755 --- a/slsDetectorSoftware/CMakeLists.txt +++ b/slsDetectorSoftware/CMakeLists.txt @@ -3,6 +3,7 @@ set(SOURCES src/slsDetectorUsers.cpp src/Module.cpp src/Detector.cpp + src/Receiver.cpp src/CmdProxy.cpp src/CmdParser.cpp ) diff --git a/slsDetectorSoftware/include/Detector.h b/slsDetectorSoftware/include/Detector.h index 692711507..6f3f4e658 100644 --- a/slsDetectorSoftware/include/Detector.h +++ b/slsDetectorSoftware/include/Detector.h @@ -19,7 +19,7 @@ class IpAddr; //Free function to avoid dependence on class //and avoid the option to free another objects //shm by mistake -void freeSharedMemory(int multiId, int detPos = -1); +void freeSharedMemory(int detectorId, int detPos = -1); /** diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index f5bc47b37..a31064f6f 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -13,29 +13,29 @@ namespace sls { -void freeSharedMemory(int multiId, int detPos) { +void freeSharedMemory(int detectorId, int detPos) { // single if (detPos >= 0) { - SharedMemory temp_shm(multiId, detPos); - if (temp_shm.IsExisting()) { - temp_shm.RemoveSharedMemory(); + SharedMemory moduleShm(detectorId, detPos); + if (moduleShm.IsExisting()) { + moduleShm.RemoveSharedMemory(); } return; } // multi - get number of detectors from shm - SharedMemory multiShm(multiId, -1); + SharedMemory detectorShm(detectorId, -1); int numDetectors = 0; - if (multiShm.IsExisting()) { - multiShm.OpenSharedMemory(); - numDetectors = multiShm()->numberOfDetectors; - multiShm.RemoveSharedMemory(); + if (detectorShm.IsExisting()) { + detectorShm.OpenSharedMemory(); + numDetectors = detectorShm()->numberOfDetectors; + detectorShm.RemoveSharedMemory(); } for (int i = 0; i < numDetectors; ++i) { - SharedMemory shm(multiId, i); - shm.RemoveSharedMemory(); + SharedMemory moduleShm(detectorId, i); + moduleShm.RemoveSharedMemory(); } } @@ -95,7 +95,7 @@ void Detector::setVirtualDetectorServers(int numServers, int startingPort) { pimpl->setVirtualDetectorServers(numServers, startingPort); } -int Detector::getShmId() const { return pimpl->getMultiId(); } +int Detector::getShmId() const { return pimpl->getDetectorId(); } std::string Detector::getPackageVersion() const { return GITBRANCH; diff --git a/slsDetectorSoftware/src/DetectorImpl.cpp b/slsDetectorSoftware/src/DetectorImpl.cpp index 5867b4f4c..c06b46174 100755 --- a/slsDetectorSoftware/src/DetectorImpl.cpp +++ b/slsDetectorSoftware/src/DetectorImpl.cpp @@ -28,14 +28,14 @@ namespace sls{ -DetectorImpl::DetectorImpl(int multi_id, bool verify, bool update) - : multiId(multi_id), multi_shm(multi_id, -1) { - setupMultiDetector(verify, update); +DetectorImpl::DetectorImpl(int detector_id, bool verify, bool update) + : detectorId(detector_id), detector_shm(detector_id, -1) { + setupDetector(verify, update); } DetectorImpl::~DetectorImpl() = default; -void DetectorImpl::setupMultiDetector(bool verify, bool update) { +void DetectorImpl::setupDetector(bool verify, bool update) { initSharedMemory(verify); initializeMembers(verify); if (update) { @@ -44,34 +44,34 @@ void DetectorImpl::setupMultiDetector(bool verify, bool update) { } void DetectorImpl::setAcquiringFlag(bool flag) { - multi_shm()->acquiringFlag = flag; + detector_shm()->acquiringFlag = flag; } -int DetectorImpl::getMultiId() const { return multiId; } +int DetectorImpl::getDetectorId() const { return detectorId; } -void DetectorImpl::freeSharedMemory(int multiId, int detPos) { +void DetectorImpl::freeSharedMemory(int detectorId, int detPos) { // single if (detPos >= 0) { - SharedMemory temp_shm(multiId, detPos); - if (temp_shm.IsExisting()) { - temp_shm.RemoveSharedMemory(); + SharedMemory module_shm(detectorId, detPos); + if (module_shm.IsExisting()) { + module_shm.RemoveSharedMemory(); } return; } // multi - get number of detectors from shm - SharedMemory multiShm(multiId, -1); + SharedMemory detectorShm(detectorId, -1); int numDetectors = 0; - if (multiShm.IsExisting()) { - multiShm.OpenSharedMemory(); - numDetectors = multiShm()->numberOfDetectors; - multiShm.RemoveSharedMemory(); + if (detectorShm.IsExisting()) { + detectorShm.OpenSharedMemory(); + numDetectors = detectorShm()->numberOfDetectors; + detectorShm.RemoveSharedMemory(); } for (int i = 0; i < numDetectors; ++i) { - SharedMemory shm(multiId, i); - shm.RemoveSharedMemory(); + SharedMemory module_shm(detectorId, i); + module_shm.RemoveSharedMemory(); } } @@ -83,7 +83,7 @@ void DetectorImpl::freeSharedMemory() { detectors.clear(); // clear multi detector shm - multi_shm.RemoveSharedMemory(); + detector_shm.RemoveSharedMemory(); client_downstream = false; } @@ -101,8 +101,8 @@ std::string DetectorImpl::getUserDetails() { } sstream << "\nType: "; // get type from multi shm - if (multi_shm()->shmversion >= MULTI_SHMAPIVERSION) { - sstream << ToString(multi_shm()->multiDetectorType); + if (detector_shm()->shmversion >= DETECTOR_SHMAPIVERSION) { + sstream << ToString(detector_shm()->multiDetectorType); } // get type from slsdet shm else { @@ -114,33 +114,33 @@ std::string DetectorImpl::getUserDetails() { } } - sstream << "\nPID: " << multi_shm()->lastPID - << "\nUser: " << multi_shm()->lastUser - << "\nDate: " << multi_shm()->lastDate << std::endl; + sstream << "\nPID: " << detector_shm()->lastPID + << "\nUser: " << detector_shm()->lastUser + << "\nDate: " << detector_shm()->lastDate << std::endl; return sstream.str(); } bool DetectorImpl::getInitialChecks() const { - return multi_shm()->initialChecks; + return detector_shm()->initialChecks; } void DetectorImpl::setInitialChecks(const bool value) { - multi_shm()->initialChecks = value; + detector_shm()->initialChecks = value; } void DetectorImpl::initSharedMemory(bool verify) { - if (!multi_shm.IsExisting()) { - multi_shm.CreateSharedMemory(); + if (!detector_shm.IsExisting()) { + detector_shm.CreateSharedMemory(); initializeDetectorStructure(); } else { - multi_shm.OpenSharedMemory(); - if (verify && multi_shm()->shmversion != MULTI_SHMVERSION) { - LOG(logERROR) << "Multi shared memory (" << multiId + detector_shm.OpenSharedMemory(); + if (verify && detector_shm()->shmversion != DETECTOR_SHMVERSION) { + LOG(logERROR) << "Detector shared memory (" << detectorId << ") version mismatch " "(expected 0x" - << std::hex << MULTI_SHMVERSION << " but got 0x" - << multi_shm()->shmversion << std::dec + << std::hex << DETECTOR_SHMVERSION << " but got 0x" + << detector_shm()->shmversion << std::dec << ". Clear Shared memory to continue."; throw SharedMemoryError("Shared memory version mismatch!"); } @@ -148,16 +148,16 @@ void DetectorImpl::initSharedMemory(bool verify) { } void DetectorImpl::initializeDetectorStructure() { - multi_shm()->shmversion = MULTI_SHMVERSION; - multi_shm()->numberOfDetectors = 0; - multi_shm()->multiDetectorType = GENERIC; - multi_shm()->numberOfDetector.x = 0; - multi_shm()->numberOfDetector.y = 0; - multi_shm()->numberOfChannels.x = 0; - multi_shm()->numberOfChannels.y = 0; - multi_shm()->acquiringFlag = false; - multi_shm()->initialChecks = true; - multi_shm()->gapPixels = false; + detector_shm()->shmversion = DETECTOR_SHMVERSION; + detector_shm()->numberOfDetectors = 0; + detector_shm()->multiDetectorType = GENERIC; + detector_shm()->numberOfDetector.x = 0; + detector_shm()->numberOfDetector.y = 0; + detector_shm()->numberOfChannels.x = 0; + detector_shm()->numberOfChannels.y = 0; + detector_shm()->acquiringFlag = false; + detector_shm()->initialChecks = true; + detector_shm()->gapPixels = false; } void DetectorImpl::initializeMembers(bool verify) { @@ -165,10 +165,10 @@ void DetectorImpl::initializeMembers(bool verify) { zmqSocket.clear(); // get objects from single det shared memory (open) - for (int i = 0; i < multi_shm()->numberOfDetectors; i++) { + for (int i = 0; i < detector_shm()->numberOfDetectors; i++) { try { detectors.push_back( - sls::make_unique(multiId, i, verify)); + sls::make_unique(detectorId, i, verify)); } catch (...) { detectors.clear(); throw; @@ -177,27 +177,27 @@ void DetectorImpl::initializeMembers(bool verify) { } void DetectorImpl::updateUserdetails() { - multi_shm()->lastPID = getpid(); - memset(multi_shm()->lastUser, 0, sizeof(multi_shm()->lastUser)); - memset(multi_shm()->lastDate, 0, sizeof(multi_shm()->lastDate)); + detector_shm()->lastPID = getpid(); + memset(detector_shm()->lastUser, 0, sizeof(detector_shm()->lastUser)); + memset(detector_shm()->lastDate, 0, sizeof(detector_shm()->lastDate)); try { - sls::strcpy_safe(multi_shm()->lastUser, exec("whoami").c_str()); - sls::strcpy_safe(multi_shm()->lastDate, exec("date").c_str()); + sls::strcpy_safe(detector_shm()->lastUser, exec("whoami").c_str()); + sls::strcpy_safe(detector_shm()->lastDate, exec("date").c_str()); } catch (...) { - sls::strcpy_safe(multi_shm()->lastUser, "errorreading"); - sls::strcpy_safe(multi_shm()->lastDate, "errorreading"); + sls::strcpy_safe(detector_shm()->lastUser, "errorreading"); + sls::strcpy_safe(detector_shm()->lastDate, "errorreading"); } } bool DetectorImpl::isAcquireReady() { - if (multi_shm()->acquiringFlag) { + if (detector_shm()->acquiringFlag) { LOG(logWARNING) << "Acquire has already started. " "If previous acquisition terminated unexpectedly, " "reset busy flag to restart.(sls_detector_put clearbusy)"; return false; } - multi_shm()->acquiringFlag = true; + detector_shm()->acquiringFlag = true; return true; } @@ -232,14 +232,14 @@ void DetectorImpl::setVirtualDetectorServers(const int numdet, const int port) { void DetectorImpl::setHostname(const std::vector &name) { // this check is there only to allow the previous detsizechan command - if (multi_shm()->numberOfDetectors != 0) { + if (detector_shm()->numberOfDetectors != 0) { LOG(logWARNING) << "There are already detector(s) in shared memory." "Freeing Shared memory now."; - bool initialChecks = multi_shm()->initialChecks; + bool initialChecks = detector_shm()->initialChecks; freeSharedMemory(); - setupMultiDetector(); - multi_shm()->initialChecks = initialChecks; + setupDetector(); + detector_shm()->initialChecks = initialChecks; } for (const auto &hostname : name) { addSlsDetector(hostname); @@ -263,7 +263,7 @@ void DetectorImpl::addSlsDetector(const std::string &hostname) { if (d->getHostname() == host) { LOG(logWARNING) << "Detector " << host - << "already part of the multiDetector!" << std::endl + << "already part of the Detector!" << std::endl << "Remove it before adding it back in a new position!"; return; } @@ -274,13 +274,13 @@ void DetectorImpl::addSlsDetector(const std::string &hostname) { detectorType type = Module::getTypeFromDetector(host, port); auto pos = detectors.size(); detectors.emplace_back( - sls::make_unique(type, multiId, pos, false)); - multi_shm()->numberOfDetectors = detectors.size(); + sls::make_unique(type, detectorId, pos, false)); + detector_shm()->numberOfDetectors = detectors.size(); detectors[pos]->setControlPort(port); detectors[pos]->setStopPort(port + 1); - detectors[pos]->setHostname(host, multi_shm()->initialChecks); + detectors[pos]->setHostname(host, detector_shm()->initialChecks); // detector type updated by now - multi_shm()->multiDetectorType = + detector_shm()->multiDetectorType = Parallel(&Module::getDetectorType, {}) .tsquash("Inconsistent detector types."); // for moench and ctb @@ -288,11 +288,11 @@ void DetectorImpl::addSlsDetector(const std::string &hostname) { } void DetectorImpl::updateDetectorSize() { - LOG(logDEBUG) << "Updating Multi-Detector Size: " << size(); + LOG(logDEBUG) << "Updating Detector Size: " << size(); const slsDetectorDefs::xy det_size = detectors[0]->getNumberOfChannels(); - int maxy = multi_shm()->numberOfChannels.y; + int maxy = detector_shm()->numberOfChannels.y; if (maxy == 0) { maxy = det_size.y * size(); } @@ -303,33 +303,33 @@ void DetectorImpl::updateDetectorSize() { ++ndetx; } - multi_shm()->numberOfDetector.x = ndetx; - multi_shm()->numberOfDetector.y = ndety; - multi_shm()->numberOfChannels.x = det_size.x * ndetx; - multi_shm()->numberOfChannels.y = det_size.y * ndety; + detector_shm()->numberOfDetector.x = ndetx; + detector_shm()->numberOfDetector.y = ndety; + detector_shm()->numberOfChannels.x = det_size.x * ndetx; + detector_shm()->numberOfChannels.y = det_size.y * ndety; LOG(logDEBUG) << "\n\tNumber of Detectors in X direction:" - << multi_shm()->numberOfDetector.x + << detector_shm()->numberOfDetector.x << "\n\tNumber of Detectors in Y direction:" - << multi_shm()->numberOfDetector.y + << detector_shm()->numberOfDetector.y << "\n\tNumber of Channels in X direction:" - << multi_shm()->numberOfChannels.x + << detector_shm()->numberOfChannels.x << "\n\tNumber of Channels in Y direction:" - << multi_shm()->numberOfChannels.y; + << detector_shm()->numberOfChannels.y; for (auto &d : detectors) { - d->updateMultiSize(multi_shm()->numberOfDetector); + d->updateDetectorSize(detector_shm()->numberOfDetector); } } int DetectorImpl::size() const { return detectors.size(); } slsDetectorDefs::xy DetectorImpl::getNumberOfDetectors() const { - return multi_shm()->numberOfDetector; + return detector_shm()->numberOfDetector; } slsDetectorDefs::xy DetectorImpl::getNumberOfChannels() const { - return multi_shm()->numberOfChannels; + return detector_shm()->numberOfChannels; } void DetectorImpl::setNumberOfChannels(const slsDetectorDefs::xy c) { @@ -337,33 +337,33 @@ void DetectorImpl::setNumberOfChannels(const slsDetectorDefs::xy c) { throw RuntimeError( "Set the number of channels before setting hostname."); } - multi_shm()->numberOfChannels = c; + detector_shm()->numberOfChannels = c; } bool DetectorImpl::getGapPixelsinCallback() const { - return multi_shm()->gapPixels; + return detector_shm()->gapPixels; } void DetectorImpl::setGapPixelsinCallback(const bool enable) { if (enable) { - switch (multi_shm()->multiDetectorType) { + switch (detector_shm()->multiDetectorType) { case JUNGFRAU: break; case EIGER: if (size() && detectors[0]->getQuad()) { break; } - if (multi_shm()->numberOfDetector.y % 2 != 0) { + if (detector_shm()->numberOfDetector.y % 2 != 0) { throw RuntimeError("Gap pixels can only be used " "for full modules."); } break; default: throw RuntimeError("Gap Pixels is not implemented for " - + multi_shm()->multiDetectorType); + + detector_shm()->multiDetectorType); } } - multi_shm()->gapPixels = enable; + detector_shm()->gapPixels = enable; } int DetectorImpl::createReceivingDataSockets(const bool destroy) { @@ -383,7 +383,7 @@ int DetectorImpl::createReceivingDataSockets(const bool destroy) { size_t numSockets = detectors.size(); size_t numSocketsPerDetector = 1; - if (multi_shm()->multiDetectorType == EIGER) { + if (detector_shm()->multiDetectorType == EIGER) { numSocketsPerDetector = 2; } if (Parallel(&Module::getNumberofUDPInterfacesFromShm, {}).squash() == @@ -420,7 +420,7 @@ int DetectorImpl::createReceivingDataSockets(const bool destroy) { void DetectorImpl::readFrameFromReceiver() { - bool gapPixels = multi_shm()->gapPixels; + bool gapPixels = detector_shm()->gapPixels; LOG(logDEBUG) << "Gap pixels: " << gapPixels; int nX = 0; @@ -577,7 +577,7 @@ void DetectorImpl::readFrameFromReceiver() { uint32_t yoffset = coordY * nPixelsY; uint32_t singledetrowoffset = nPixelsX * bytesPerPixel; uint32_t rowoffset = nX * singledetrowoffset; - if (multi_shm()->multiDetectorType == CHIPTESTBOARD) { + if (detector_shm()->multiDetectorType == CHIPTESTBOARD) { singledetrowoffset = size; } LOG(logDEBUG1) @@ -755,7 +755,7 @@ int DetectorImpl::InsertGapPixels(char *image, char *&gpImage, // eiger requires inter chip gap pixels are halved // jungfrau prefers same inter chip gap pixels as the boundary pixels int divisionValue = 2; - slsDetectorDefs::detectorType detType = multi_shm()->multiDetectorType; + slsDetectorDefs::detectorType detType = detector_shm()->multiDetectorType; if (detType == JUNGFRAU) { divisionValue = 1; } @@ -1048,7 +1048,7 @@ int DetectorImpl::acquire() { // start and read all try { - if (multi_shm()->multiDetectorType == EIGER) { + if (detector_shm()->multiDetectorType == EIGER) { Parallel(&Module::prepareAcquisition, {}); } Parallel(&Module::startAndReadAll, {}); @@ -1172,7 +1172,7 @@ int DetectorImpl::kbhit() { std::vector DetectorImpl::readProgrammingFile(const std::string &fname) { // validate type of file bool isPof = false; - switch (multi_shm()->multiDetectorType) { + switch (detector_shm()->multiDetectorType) { case JUNGFRAU: case CHIPTESTBOARD: case MOENCH: diff --git a/slsDetectorSoftware/src/DetectorImpl.h b/slsDetectorSoftware/src/DetectorImpl.h index 21424c712..583cd3a8f 100755 --- a/slsDetectorSoftware/src/DetectorImpl.h +++ b/slsDetectorSoftware/src/DetectorImpl.h @@ -15,8 +15,8 @@ class detectorData; #include #include -#define MULTI_SHMAPIVERSION 0x190809 -#define MULTI_SHMVERSION 0x200319 +#define DETECTOR_SHMAPIVERSION 0x190809 +#define DETECTOR_SHMVERSION 0x200319 #define SHORT_STRING_LENGTH 50 #include @@ -30,7 +30,7 @@ class Module; * @short structure allocated in shared memory to store detector settings * for IPC and cache */ -struct sharedMultiSlsDetector { +struct sharedDetector { /* FIXED PATTERN FOR STATIC FUNCTIONS. DO NOT CHANGE, ONLY APPEND * ------*/ @@ -68,12 +68,12 @@ class DetectorImpl : public virtual slsDetectorDefs { public: /** * Constructor - * @param id multi detector id + * @param detector_id multi detector id * @param verify true to verify if shared memory version matches existing * one * @param update true to update last user pid, date etc */ - explicit DetectorImpl(int multi_id = 0, bool verify = true, + explicit DetectorImpl(int detector_id = 0, bool verify = true, bool update = true); /** @@ -192,11 +192,11 @@ class DetectorImpl : public virtual slsDetectorDefs { /** set acquiring flag in shared memory */ void setAcquiringFlag(bool flag); - /** return multi detector shared memory ID */ - int getMultiId() const; + /** return detector shared memory ID */ + int getDetectorId() const; /** Free specific shared memory from the command line without creating object */ - static void freeSharedMemory(int multiId, int detPos = -1); + static void freeSharedMemory(int detectorId, int detPos = -1); /** Free all modules from current multi Id shared memory and delete members */ void freeSharedMemory(); @@ -298,7 +298,7 @@ class DetectorImpl : public virtual slsDetectorDefs { * one * @param update true to update last user pid, date etc */ - void setupMultiDetector(bool verify = true, bool update = true); + void setupDetector(bool verify = true, bool update = true); /** * Creates shm and initializes shm structure OR @@ -377,10 +377,10 @@ class DetectorImpl : public virtual slsDetectorDefs { int kbhit(); /** Multi detector Id */ - const int multiId{0}; + const int detectorId{0}; /** Shared Memory object */ - sls::SharedMemory multi_shm{0, -1}; + sls::SharedMemory detector_shm{0, -1}; /** pointers to the Module structures */ std::vector> detectors; diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index d5772e282..684bf3f0b 100755 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -25,9 +25,9 @@ namespace sls{ // create shm -Module::Module(detectorType type, int multi_id, int det_id, +Module::Module(detectorType type, int detector_id, int module_id, bool verify) - : detId(det_id), shm(multi_id, det_id) { + : moduleId(module_id), shm(detector_id, module_id) { // ensure shared memory was not created before if (shm.IsExisting()) { @@ -37,22 +37,22 @@ Module::Module(detectorType type, int multi_id, int det_id, shm.RemoveSharedMemory(); } - initSharedMemory(type, multi_id, verify); + initSharedMemory(type, detector_id, verify); } // pick up from shm -Module::Module(int multi_id, int det_id, bool verify) - : detId(det_id), shm(multi_id, det_id) { +Module::Module(int detector_id, int module_id, bool verify) + : moduleId(module_id), shm(detector_id, module_id) { // getDetectorType From shm will check if it was already existing - detectorType type = getDetectorTypeFromShm(multi_id, verify); - initSharedMemory(type, multi_id, verify); + detectorType type = getDetectorTypeFromShm(detector_id, verify); + initSharedMemory(type, detector_id, verify); } Module::~Module() = default; bool Module::isFixedPatternSharedMemoryCompatible() { - return (shm()->shmversion >= SLS_SHMAPIVERSION); + return (shm()->shmversion >= MODULE_SHMAPIVERSION); } void Module::checkDetectorVersionCompatibility() { @@ -363,19 +363,19 @@ void Module::setHostname(const std::string &hostname, const bool initialChecks) std::string Module::getHostname() const { return shm()->hostname; } -void Module::initSharedMemory(detectorType type, int multi_id, +void Module::initSharedMemory(detectorType type, int detector_id, bool verify) { - shm = SharedMemory(multi_id, detId); + shm = SharedMemory(detector_id, moduleId); if (!shm.IsExisting()) { shm.CreateSharedMemory(); initializeDetectorStructure(type); } else { shm.OpenSharedMemory(); - if (verify && shm()->shmversion != SLS_SHMVERSION) { + if (verify && shm()->shmversion != MODULE_SHMVERSION) { std::ostringstream ss; - ss << "Single shared memory (" << multi_id << "-" << detId + ss << "Module shared memory (" << detector_id << "-" << moduleId << ":) version mismatch (expected 0x" << std::hex - << SLS_SHMVERSION << " but got 0x" << shm()->shmversion << ")" + << MODULE_SHMVERSION << " but got 0x" << shm()->shmversion << ")" << std::dec << ". Clear Shared memory to continue."; throw SharedMemoryError(ss.str()); } @@ -383,11 +383,11 @@ void Module::initSharedMemory(detectorType type, int multi_id, } void Module::initializeDetectorStructure(detectorType type) { - shm()->shmversion = SLS_SHMVERSION; + shm()->shmversion = MODULE_SHMVERSION; memset(shm()->hostname, 0, MAX_STR_LENGTH); shm()->myDetectorType = type; - shm()->multiSize.x = 0; - shm()->multiSize.y = 0; + shm()->detectorSize.x = 0; + shm()->detectorSize.y = 0; shm()->controlPort = DEFAULT_PORTNO; shm()->stopPort = DEFAULT_PORTNO + 1; sls::strcpy_safe(shm()->settingsDir, getenv("HOME")); @@ -395,7 +395,7 @@ void Module::initializeDetectorStructure(detectorType type) { shm()->rxTCPPort = DEFAULT_PORTNO + 2; shm()->useReceiverFlag = false; shm()->zmqport = DEFAULT_ZMQ_CL_PORTNO + - (detId * ((shm()->myDetectorType == EIGER) ? 2 : 1)); + (moduleId * ((shm()->myDetectorType == EIGER) ? 2 : 1)); shm()->zmqip = IpAddr{}; shm()->numUDPInterfaces = 1; shm()->stoppedFlag = false; @@ -489,19 +489,19 @@ int Module::receiveModule(sls_detector_module *myMod, return ts; } -slsDetectorDefs::detectorType Module::getDetectorTypeFromShm(int multi_id, +slsDetectorDefs::detectorType Module::getDetectorTypeFromShm(int detector_id, bool verify) { if (!shm.IsExisting()) { throw SharedMemoryError("Shared memory " + shm.GetName() + - "does not exist.\n Corrupted Multi Shared " + "does not exist.\n Corrupted Detector Shared " "memory. Please free shared memory."); } shm.OpenSharedMemory(); - if (verify && shm()->shmversion != SLS_SHMVERSION) { + if (verify && shm()->shmversion != MODULE_SHMVERSION) { std::ostringstream ss; - ss << "Single shared memory (" << multi_id << "-" << detId - << ":)version mismatch (expected 0x" << std::hex << SLS_SHMVERSION + ss << "Module shared memory (" << detector_id << "-" << moduleId + << ":)version mismatch (expected 0x" << std::hex << MODULE_SHMVERSION << " but got 0x" << shm()->shmversion << ")" << std::dec << ". Clear Shared memory to continue."; shm.UnmapSharedMemory(); @@ -585,9 +585,9 @@ int Module::getReadNLines() { return retval; } -void Module::updateMultiSize(slsDetectorDefs::xy det) { - shm()->multiSize = det; - int args[2] = {shm()->multiSize.y, detId}; +void Module::updateDetectorSize(slsDetectorDefs::xy det) { + shm()->detectorSize = det; + int args[2] = {shm()->detectorSize.y, moduleId}; sendToDetector(F_SET_POSITION, args, nullptr); } @@ -673,7 +673,7 @@ void Module::execCommand(const std::string &cmd) { LOG(logDEBUG1) << "Sending command to detector " << arg; sendToDetector(F_EXEC_COMMAND, arg, retval); if (strlen(retval) != 0U) { - LOG(logINFO) << "Detector " << detId << " returned:\n" << retval; + LOG(logINFO) << "Detector " << moduleId << " returned:\n" << retval; } } @@ -745,7 +745,7 @@ std::vector Module::getConfigFileCommands() { std::vector commands; for (const auto &cmd : base) { std::ostringstream os; - os << detId << ':' << cmd; + os << moduleId << ':' << cmd; commands.emplace_back(os.str()); } return commands; @@ -917,7 +917,7 @@ void Module::loadSettingsFile(const std::string &fname) { std::ostringstream ostfn; ostfn << fname; - // find specific file if it has detid in file name (.snxxx) + // find specific file if it has moduleId in file name (.snxxx) if (shm()->myDetectorType == EIGER) { if (fname.find(".sn") == std::string::npos && fname.find(".trim") == std::string::npos && @@ -936,7 +936,7 @@ void Module::saveSettingsFile(const std::string &fname) { std::ostringstream ostfn; ostfn << fname; - // find specific file if it has detid in file name (.snxxx) + // find specific file if it has moduleId in file name (.snxxx) if (shm()->myDetectorType == EIGER) { ostfn << ".sn" << std::setfill('0') << std::setw(3) << std::dec << getSerialNumber(); @@ -1521,17 +1521,17 @@ void Module::setReceiverHostname(const std::string &receiverIP) { // populate from shared memory retval.detType = shm()->myDetectorType; - retval.multiSize.x = shm()->multiSize.x; - retval.multiSize.y = shm()->multiSize.y; - retval.detId = detId; + retval.detectorSize.x = shm()->detectorSize.x; + retval.detectorSize.y = shm()->detectorSize.y; + retval.moduleId = moduleId; memset(retval.hostname, 0, sizeof(retval.hostname)); strcpy_safe(retval.hostname, shm()->hostname); LOG(logDEBUG1) << "detType:" << retval.detType << std::endl - << "multiSize.x:" << retval.multiSize.x << std::endl - << "multiSize.y:" << retval.multiSize.y << std::endl - << "detId:" << retval.detId << std::endl + << "detectorSize.x:" << retval.detectorSize.x << std::endl + << "detectorSize.y:" << retval.detectorSize.y << std::endl + << "moduleId:" << retval.moduleId << std::endl << "hostname:" << retval.hostname << std::endl << "udpInterfaces:" << retval.udpInterfaces << std::endl << "udp_dstport:" << retval.udp_dstport << std::endl @@ -1568,12 +1568,12 @@ void Module::setReceiverHostname(const std::string &receiverIP) { // update detectors with dest mac if (retval.udp_dstmac == 0 && retvals[0] != 0) { LOG(logINFO) << "Setting destination udp mac of " - "detector " << detId << " to " << retvals[0]; + "detector " << moduleId << " to " << retvals[0]; sendToDetector(F_SET_DEST_UDP_MAC, retvals[0], nullptr); } if (retval.udp_dstmac2 == 0 && retvals[1] != 0) { LOG(logINFO) << "Setting destination udp mac2 of " - "detector " << detId << " to " << retvals[1]; + "detector " << moduleId << " to " << retvals[1]; sendToDetector(F_SET_DEST_UDP_MAC2, retvals[1], nullptr); } @@ -1668,7 +1668,7 @@ void Module::setDestinationUDPIP(const IpAddr ip) { if (shm()->useReceiverFlag) { sls::MacAddr retval(0LU); sendToReceiver(F_SET_RECEIVER_UDP_IP, ip, retval); - LOG(logINFO) << "Setting destination udp mac of detector " << detId << " to " << retval; + LOG(logINFO) << "Setting destination udp mac of detector " << moduleId << " to " << retval; sendToDetector(F_SET_DEST_UDP_MAC, retval, nullptr); } } @@ -1691,7 +1691,7 @@ void Module::setDestinationUDPIP2(const IpAddr ip) { if (shm()->useReceiverFlag) { sls::MacAddr retval(0LU); sendToReceiver(F_SET_RECEIVER_UDP_IP2, ip, retval); - LOG(logINFO) << "Setting destination udp mac2 of detector " << detId << " to " << retval; + LOG(logINFO) << "Setting destination udp mac2 of detector " << moduleId << " to " << retval; sendToDetector(F_SET_DEST_UDP_MAC2, retval, nullptr); } } @@ -1865,7 +1865,7 @@ void Module::updateReceiverStreamingIP() { if (ip == 0) { ip = HostnameToIp(shm()->rxHostname); } - LOG(logINFO) << "Setting default receiver " << detId << " streaming zmq ip to " << ip; + LOG(logINFO) << "Setting default receiver " << moduleId << " streaming zmq ip to " << ip; } setReceiverStreamingIP(ip); } @@ -1953,7 +1953,7 @@ void Module::setAdditionalJsonHeader(const std::map &j if (ret == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); - throw RuntimeError("Receiver " + std::to_string(detId) + + throw RuntimeError("Receiver " + std::to_string(moduleId) + " returned error: " + std::string(mess)); } } @@ -1971,7 +1971,7 @@ std::map Module::getAdditionalJsonHeader() { if (ret == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); - throw RuntimeError("Receiver " + std::to_string(detId) + + throw RuntimeError("Receiver " + std::to_string(moduleId) + " returned error: " + std::string(mess)); } else { client.Receive(&size, sizeof(size)); @@ -2079,7 +2079,7 @@ std::vector Module::getVetoPhoton(const int chipIndex) { if (ret == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); - throw RuntimeError("Detector " + std::to_string(detId) + + throw RuntimeError("Detector " + std::to_string(moduleId) + " returned error: " + std::string(mess)); } else { int nch = -1; @@ -2174,7 +2174,7 @@ void Module::setVetoPhoton(const int chipIndex, const int numPhotons, const int if (ret == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); - throw RuntimeError("Detector " + std::to_string(detId) + + throw RuntimeError("Detector " + std::to_string(moduleId) + " returned error: " + std::string(mess)); } } @@ -2573,7 +2573,7 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { int fnum = F_PROGRAM_FPGA; int ret = FAIL; char mess[MAX_STR_LENGTH] = {0}; - LOG(logINFO) << "Sending programming binary (from pof) to detector " << detId + LOG(logINFO) << "Sending programming binary (from pof) to detector " << moduleId << " (" << shm()->hostname << ")"; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); @@ -2584,13 +2584,13 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { if (ret == FAIL) { client.Receive(mess, sizeof(mess)); std::ostringstream os; - os << "Detector " << detId << " (" << shm()->hostname << ")" + os << "Detector " << moduleId << " (" << shm()->hostname << ")" << " returned error: " << mess; throw RuntimeError(os.str()); } // erasing flash - LOG(logINFO) << "Erasing Flash for detector " << detId << " (" + LOG(logINFO) << "Erasing Flash for detector " << moduleId << " (" << shm()->hostname << ")"; printf("%d%%\r", 0); std::cout << std::flush; @@ -2608,7 +2608,7 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { std::cout << std::flush; } printf("\n"); - LOG(logINFO) << "Writing to Flash to detector " << detId << " (" + LOG(logINFO) << "Writing to Flash to detector " << moduleId << " (" << shm()->hostname << ")"; printf("%d%%\r", 0); std::cout << std::flush; @@ -2632,7 +2632,7 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { printf("\n"); client.Receive(mess, sizeof(mess)); std::ostringstream os; - os << "Detector " << detId << " (" << shm()->hostname << ")" + os << "Detector " << moduleId << " (" << shm()->hostname << ")" << " returned error: " << mess; throw RuntimeError(os.str()); } @@ -2656,7 +2656,7 @@ void Module::programFPGAviaNios(std::vector buffer) { int fnum = F_PROGRAM_FPGA; int ret = FAIL; char mess[MAX_STR_LENGTH] = {0}; - LOG(logINFO) << "Sending programming binary (from rbf) to detector " << detId + LOG(logINFO) << "Sending programming binary (from rbf) to detector " << moduleId << " (" << shm()->hostname << ")"; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); @@ -2667,7 +2667,7 @@ void Module::programFPGAviaNios(std::vector buffer) { if (ret == FAIL) { client.Receive(mess, sizeof(mess)); std::ostringstream os; - os << "Detector " << detId << " (" << shm()->hostname << ")" + os << "Detector " << moduleId << " (" << shm()->hostname << ")" << " returned error: " << mess; throw RuntimeError(os.str()); } @@ -2677,7 +2677,7 @@ void Module::programFPGAviaNios(std::vector buffer) { if (ret == FAIL) { client.Receive(mess, sizeof(mess)); std::ostringstream os; - os << "Detector " << detId << " (" << shm()->hostname << ")" + os << "Detector " << moduleId << " (" << shm()->hostname << ")" << " returned error: " << mess; throw RuntimeError(os.str()); } @@ -2739,7 +2739,7 @@ void Module::setModule(sls_detector_module &module, int tb) { if (ret == FAIL) { char mess[MAX_STR_LENGTH] = {0}; client.Receive(mess, sizeof(mess)); - throw RuntimeError("Detector " + std::to_string(detId) + + throw RuntimeError("Detector " + std::to_string(moduleId) + " returned error: " + mess); } client.Receive(&retval, sizeof(retval)); @@ -2781,7 +2781,7 @@ void Module::updateRateCorrection() { std::string Module::printReceiverConfiguration() { std::ostringstream os; - os << "\n\nDetector " << detId << "\nReceiver Hostname:\t" + os << "\n\nDetector " << moduleId << "\nReceiver Hostname:\t" << getReceiverHostname(); if (shm()->myDetectorType == JUNGFRAU) { @@ -2844,7 +2844,7 @@ void Module::execReceiverCommand(const std::string &cmd) { LOG(logDEBUG1) << "Sending command to receiver: " << arg; if (shm()->useReceiverFlag) { sendToReceiver(F_EXEC_RECEIVER_COMMAND, arg, retval); - LOG(logINFO) << "Receiver " << detId << " returned:\n" << retval; + LOG(logINFO) << "Receiver " << moduleId << " returned:\n" << retval; } } @@ -3019,7 +3019,7 @@ std::vector Module::getNumMissingPackets() const { if (ret == FAIL) { char mess[MAX_STR_LENGTH]{}; client.Receive(mess, MAX_STR_LENGTH); - throw RuntimeError("Receiver " + std::to_string(detId) + + throw RuntimeError("Receiver " + std::to_string(moduleId) + " returned error: " + std::string(mess)); } else { int nports = -1; @@ -3028,7 +3028,7 @@ std::vector Module::getNumMissingPackets() const { memset(mp, 0, sizeof(mp)); client.Receive(mp, sizeof(mp)); std::vector retval(mp, mp + nports); - LOG(logDEBUG1) << "Missing packets of Receiver" << detId << ": " << sls::ToString(retval); + LOG(logDEBUG1) << "Missing packets of Receiver" << moduleId << ": " << sls::ToString(retval); return retval; } } diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index 988429662..1401defe5 100755 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -13,8 +13,8 @@ class ServerInterface; -#define SLS_SHMAPIVERSION 0x190726 -#define SLS_SHMVERSION 0x200402 +#define MODULE_SHMAPIVERSION 0x190726 +#define MODULE_SHMVERSION 0x200402 namespace sls{ @@ -22,7 +22,7 @@ namespace sls{ * @short structure allocated in shared memory to store detector settings for * IPC and cache */ -struct sharedSlsDetector { +struct sharedModule { /* FIXED PATTERN FOR STATIC FUNCTIONS. DO NOT CHANGE, ONLY APPEND ------*/ @@ -39,7 +39,7 @@ struct sharedSlsDetector { /** END OF FIXED PATTERN -----------------------------------------------*/ /** Number of detectors in multi list in x dir and y dir */ - slsDetectorDefs::xy multiSize; + slsDetectorDefs::xy detectorSize; /** is the port used for control functions */ int controlPort; @@ -90,22 +90,22 @@ class Module : public virtual slsDetectorDefs { /** * Constructor called when creating new shared memory * @param type detector type - * @param multi_id multi detector shared memory id - * @param id sls detector id (position in detectors list) + * @param detector_id multi detector shared memory id + * @param module_id module id (position in detectors list) * @param verify true to verify if shared memory version matches existing * one */ - explicit Module(detectorType type, int multi_id = 0, int det_id = 0, + explicit Module(detectorType type, int detector_id = 0, int module_id = 0, bool verify = true); /** * Constructor called when opening existing shared memory - * @param multi_id multi detector shared memory id - * @param id sls detector id (position in detectors list) + * @param detector_id multi detector shared memory id + * @param module_id module id (position in detectors list) * @param verify true to verify if shared memory version matches existing * one */ - explicit Module(int multi_id = 0, int det_id = 0, bool verify = true); + explicit Module(int detector_id = 0, int module_id = 0, bool verify = true); /** * Destructor @@ -141,7 +141,7 @@ class Module : public virtual slsDetectorDefs { /** * Free shared memory and delete shared memory structure - * occupied by the sharedSlsDetector structure + * occupied by the sharedModule structure * Is only safe to call if one deletes the Module object afterward * and frees multi shared memory/updates * thisMultiDetector->numberOfDetectors @@ -213,12 +213,12 @@ class Module : public virtual slsDetectorDefs { * Set Detector offset in shared memory in dimension d * @param det detector size */ - void updateMultiSize(slsDetectorDefs::xy det); + void updateDetectorSize(slsDetectorDefs::xy det); int setControlPort(int port_number); /** - * Returns the detector TCP control port \sa sharedSlsDetector + * Returns the detector TCP control port * @returns the detector TCP control port */ int getControlPort() const; @@ -226,7 +226,7 @@ class Module : public virtual slsDetectorDefs { int setStopPort(int port_number); /** - * Returns the detector TCP stop port \sa sharedSlsDetector + * Returns the detector TCP stop port * @returns the detector TCP stop port */ int getStopPort() const; @@ -234,7 +234,7 @@ class Module : public virtual slsDetectorDefs { int setReceiverPort(int port_number); /** - * Returns the receiver TCP port \sa sharedSlsDetector + * Returns the receiver TCP port * @returns the receiver TCP port */ int getReceiverPort() const; @@ -306,13 +306,13 @@ class Module : public virtual slsDetectorDefs { int tb = 1); /** - * Returns the detector trimbit/settings directory \sa sharedSlsDetector + * Returns the detector trimbit/settings directory * @returns the trimbit/settings directory */ std::string getSettingsDir(); /** - * Sets the detector trimbit/settings directory \sa sharedSlsDetector + * Sets the detector trimbit/settings directory * @param s trimbits/settings directory * @returns the trimbit/settings directory */ @@ -645,7 +645,7 @@ class Module : public virtual slsDetectorDefs { void test(); /** - * Returns the receiver IP address\sa sharedSlsDetector + * Returns the receiver IP address * @returns the receiver IP address */ std::string getReceiverHostname() const; @@ -755,26 +755,26 @@ class Module : public virtual slsDetectorDefs { sls::MacAddr getDestinationUDPMAC2(); /** - * Sets the receiver UDP port\sa sharedSlsDetector + * Sets the receiver UDP port * @param udpport receiver UDP port */ void setDestinationUDPPort(int udpport); /** - * Returns the receiver UDP port\sa sharedSlsDetector + * Returns the receiver UDP port * @returns the receiver UDP port */ int getDestinationUDPPort(); /** - * Sets the receiver UDP port 2\sa sharedSlsDetector (Eiger and Jungfrau + * Sets the receiver UDP port 2 (Eiger and Jungfrau * only) * @param udpport receiver UDP port 2 */ void setDestinationUDPPort2(int udpport); /** - * Returns the receiver UDP port 2 of same interface\sa sharedSlsDetector + * Returns the receiver UDP port 2 of same interface * (Eiger and Jungfrau only) * @returns the receiver UDP port 2 of same interface */ @@ -814,49 +814,49 @@ class Module : public virtual slsDetectorDefs { int getSelectedUDPInterface(); /** - * Sets the client zmq port\sa sharedSlsDetector + * Sets the client zmq port * @param port client zmq port */ void setClientStreamingPort(int port); /** - * Returns the client zmq port \sa sharedSlsDetector + * Returns the client zmq port * @returns the client zmq port */ int getClientStreamingPort(); /** - * Sets the receiver zmq port\sa sharedSlsDetector + * Sets the receiver zmq port * @param port receiver zmq port */ void setReceiverStreamingPort(int port); /** - * Returns the receiver zmq port \sa sharedSlsDetector + * Returns the receiver zmq port * @returns the receiver zmq port */ int getReceiverStreamingPort(); /** - * Sets the client zmq ip\sa sharedSlsDetector + * Sets the client zmq ip * @param ip client zmq ip */ void setClientStreamingIP(const sls::IpAddr ip); /** - * Returns the client zmq ip \sa sharedSlsDetector + * Returns the client zmq ip * @returns the client zmq ip */ sls::IpAddr getClientStreamingIP(); /** - * Sets the receiver zmq ip\sa sharedSlsDetector + * Sets the receiver zmq ip * @param ip receiver zmq ip */ void setReceiverStreamingIP(const sls::IpAddr ip); /** - * Returns the receiver zmq ip \sa sharedSlsDetector + * Returns the receiver zmq ip * @returns the receiver zmq ip */ sls::IpAddr getReceiverStreamingIP(); @@ -921,13 +921,13 @@ class Module : public virtual slsDetectorDefs { int64_t setReceiverUDPSocketBufferSize(int64_t udpsockbufsize = -1); /** - * Returns the receiver UDP socket buffer size\sa sharedSlsDetector + * Returns the receiver UDP socket buffer size * @returns the receiver UDP socket buffer size */ int64_t getReceiverUDPSocketBufferSize(); /** - * Returns the receiver real UDP socket buffer size\sa sharedSlsDetector + * Returns the receiver real UDP socket buffer size * @returns the receiver real UDP socket buffer size */ int64_t getReceiverRealUDPSocketBufferSize() const; @@ -1129,7 +1129,6 @@ class Module : public virtual slsDetectorDefs { /** * Sets the number of trim energies and their value (Eiger) - * \sa sharedSlsDetector * @param nen number of energies * @param vector os trimmed energies * @returns number of trim energies @@ -1138,7 +1137,6 @@ class Module : public virtual slsDetectorDefs { /** * Returns a vector with the trimmed energies (Eiger) - * \sa sharedSlsDetector * @returns vector with the trimmed energies */ std::vector getTrimEn(); @@ -1676,21 +1674,21 @@ class Module : public virtual slsDetectorDefs { /** * Get Detector Type from Shared Memory (opening shm without verifying size) - * @param multi_id multi detector Id + * @param detector_id multi detector Id * @param verify true to verify if shm size matches existing one * @returns detector type */ - detectorType getDetectorTypeFromShm(int multi_id, bool verify = true); + detectorType getDetectorTypeFromShm(int detector_id, bool verify = true); /** * Initialize shared memory * @param created true if shared memory must be created, else false to open * @param type type of detector - * @param multi_id multi detector Id + * @param detector_id multi detector Id * @param verify true to verify if shm size matches existing one * @returns true if the shared memory was created now */ - void initSharedMemory(detectorType type, int multi_id, bool verify = true); + void initSharedMemory(detectorType type, int detector_id, bool verify = true); /** * Initialize detector structure to defaults @@ -1773,10 +1771,10 @@ class Module : public virtual slsDetectorDefs { std::vector getSettingsFileDacNames(); /** Module Id or position in the detectors list */ - const int detId; + const int moduleId; /** Shared Memory object */ - mutable sls::SharedMemory shm{0, 0}; + mutable sls::SharedMemory shm{0, 0}; }; }// sls \ No newline at end of file diff --git a/slsDetectorSoftware/src/Receiver.cpp b/slsDetectorSoftware/src/Receiver.cpp new file mode 100755 index 000000000..d3d4f55cf --- /dev/null +++ b/slsDetectorSoftware/src/Receiver.cpp @@ -0,0 +1,84 @@ +#include "Receiver.h" +#include "string_utils.h" + +namespace sls { + +size_t Receiver::NUM_RECEIVERS{0}; + +size_t Receiver::getNumReceivers() { + return NUM_RECEIVERS; +} + + +// create shm +Receiver::Receiver(int detector_id, int module_id, int receiver_id, + int tcp_port, std::string hostname) + : receiverId(receiver_id), shm(detector_id, module_id, receiver_id) { + + // ensure shared memory was not created before + if (shm.IsExisting()) { + LOG(logWARNING) << "This shared memory should have been " + "deleted before! " + << shm.GetName() << ". Freeing it again"; + shm.RemoveSharedMemory(); + } + shm = SharedMemory(detector_id, module_id, receiver_id); + ++NUM_RECEIVERS; + + // initalize receiver structure + shm()->shmversion = RECEIVER_SHMVERSION; + memset(shm()->hostname, 0, MAX_STR_LENGTH); + shm()->tcpPort = DEFAULT_RX_PORTNO + NUM_RECEIVERS - 1; + shm()->zmqPort = DEFAULT_ZMQ_RX_PORTNO + NUM_RECEIVERS - 1; + shm()->zmqIp = IpAddr{}; + + // copy port, hostname if given + if (tcp_port != 0) { + shm()->tcpPort = tcp_port; + } + if (!hostname.empty()) { + setHostname(hostname); + } +} + +// open shm +Receiver::Receiver(int detector_id, int module_id, int receiver_id, + bool verify) + : receiverId(receiver_id), shm(detector_id, module_id, receiver_id) { + shm.OpenSharedMemory(); + if (verify && shm()->shmversion != RECEIVER_SHMVERSION) { + std::ostringstream ss; + ss << "Receiver shared memory (" << detector_id << "-" << receiverId + << ":) version mismatch (expected 0x" << std::hex + << RECEIVER_SHMVERSION << " but got 0x" << shm()->shmversion << ")" + << std::dec << ". Clear Shared memory to continue."; + throw SharedMemoryError(ss.str()); + } +} + +Receiver::~Receiver() { + --NUM_RECEIVERS; +} + +void Receiver::setHostname(const std::string &ip_port) { + if (ip_port.empty()) { + throw RuntimeError("Invalid receiver hostname. Cannot be empty."); + } + // parse tcp port from this hostname:port string + std::string host = ip_port; + auto res = sls::split(host, ':'); + if (res.size() > 1) { + host = res[0]; + shm()->tcpPort = std::stoi(res[1]); + } + sls::strcpy_safe(shm()->hostname, host.c_str()); + + updateReceiver(); +} + +void Receiver::updateReceiver() { + //checkReceiverVersionCompatibility(); +} + + +} // namespace sls \ No newline at end of file diff --git a/slsDetectorSoftware/src/Receiver.h b/slsDetectorSoftware/src/Receiver.h new file mode 100755 index 000000000..7ffc18c54 --- /dev/null +++ b/slsDetectorSoftware/src/Receiver.h @@ -0,0 +1,46 @@ +#pragma once +#include "SharedMemory.h" +#include "logger.h" +#include "sls_detector_defs.h" +#include "network_utils.h" + +#define RECEIVER_SHMVERSION 0x200414 + +namespace sls { + struct sharedReceiver { + + /* FIXED PATTERN FOR STATIC FUNCTIONS. DO NOT CHANGE, ONLY APPEND ------*/ + + int shmversion; + char hostname[MAX_STR_LENGTH]; + int tcpPort; + + /** END OF FIXED PATTERN -----------------------------------------------*/ + + int zmqPort; + sls::IpAddr zmqIp; + + }; + + class Receiver : public virtual slsDetectorDefs { + public: + static size_t getNumReceivers(); + // create shm + explicit Receiver(int detector_id, int module_id, int receiver_id, + int tcp_port = 0, std::string hostname = ""); + // open shm + explicit Receiver(int detector_id, int module_id, int receiver_id, + bool verify); + + virtual ~Receiver(); + + void setHostname(const std::string &ip_port); + void updateReceiver(); + + private: + static size_t NUM_RECEIVERS; + const int receiverId{0}; + mutable sls::SharedMemory shm{0, 0, 0}; + }; + +} // sls \ No newline at end of file diff --git a/slsDetectorSoftware/src/SharedMemory.h b/slsDetectorSoftware/src/SharedMemory.h index 0130d4435..5d5c714a6 100755 --- a/slsDetectorSoftware/src/SharedMemory.h +++ b/slsDetectorSoftware/src/SharedMemory.h @@ -23,7 +23,8 @@ #include #define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_" -#define SHM_SLS_PREFIX "_sls_" +#define SHM_MODULE_PREFIX "_module_" +#define SHM_RECEIVER_PREFIX "_receiver_" #define SHM_ENV_NAME "SLSDETNAME" #include @@ -39,10 +40,11 @@ class SharedMemory { * Constructor * creates the single/multi detector shared memory name * @param multiId multi detector id - * @param slsId sls detector id, -1 if a multi detector shared memory + * @param moduleId module detector id, -1 if a multi detector shared memory + * @param receiverId receiver id, -1 if a multi detector or module shared memory */ - SharedMemory(int multiId, int slsId) { - name = ConstructSharedMemoryName(multiId, slsId); + SharedMemory(int multiId, int moduleId, int receiverId = -1) { + name = ConstructSharedMemoryName(multiId, moduleId, receiverId); } /** @@ -215,10 +217,11 @@ class SharedMemory { * Create Shared memory name * throws exception if name created is longer than required 255(manpages) * @param multiId multi detector id - * @param slsId sls detector id, -1 if a multi detector shared memory + * @param moduleId module detector id, -1 if a multi detector shared memory + * @param receiverId receiver id, -1 if a multi detector or module shared memory * @returns shared memory name */ - std::string ConstructSharedMemoryName(int multiId, int slsId) { + std::string ConstructSharedMemoryName(int multiId, int moduleId, int receiverId) { // using environment path std::string sEnvPath = ""; @@ -229,14 +232,20 @@ class SharedMemory { } std::stringstream ss; - if (slsId < 0) + if (moduleId < 0) ss << SHM_MULTI_PREFIX << multiId << sEnvPath; + else if (receiverId < 0) + ss << SHM_MULTI_PREFIX << multiId << SHM_MODULE_PREFIX << moduleId << sEnvPath; else - ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath; + ss << SHM_MULTI_PREFIX << multiId << SHM_MODULE_PREFIX << moduleId << + SHM_RECEIVER_PREFIX << receiverId << sEnvPath; std::string temp = ss.str(); if (temp.length() > NAME_MAX_LENGTH) { - std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n" + "Maximum is " + std::to_string(NAME_MAX_LENGTH) + ". Change the environment variable " + SHM_ENV_NAME; + std::string msg = "Shared memory initialization failed. " + + temp + " has " + std::to_string(temp.length()) + " characters. \n" + + "Maximum is " + std::to_string(NAME_MAX_LENGTH) + + ". Change the environment variable " + SHM_ENV_NAME; LOG(logERROR) << msg; throw SharedMemoryError(msg); } diff --git a/slsReceiverSoftware/src/ClientInterface.cpp b/slsReceiverSoftware/src/ClientInterface.cpp index c04d378bd..42801d1a8 100755 --- a/slsReceiverSoftware/src/ClientInterface.cpp +++ b/slsReceiverSoftware/src/ClientInterface.cpp @@ -337,9 +337,9 @@ int ClientInterface::setup_receiver(Interface &socket) { auto arg = socket.Receive(); LOG(logDEBUG1) << "detType:" << arg.detType << std::endl - << "multiSize.x:" << arg.multiSize.x << std::endl - << "multiSize.y:" << arg.multiSize.y << std::endl - << "detId:" << arg.detId << std::endl + << "detectorSize.x:" << arg.detectorSize.x << std::endl + << "detectorSize.y:" << arg.detectorSize.y << std::endl + << "moduleId:" << arg.moduleId << std::endl << "hostname:" << arg.hostname << std::endl << "udpInterfaces:" << arg.udpInterfaces << std::endl << "udp_dstport:" << arg.udp_dstport << std::endl @@ -380,10 +380,10 @@ int ClientInterface::setup_receiver(Interface &socket) { // basic setup setDetectorType(arg.detType); { - int msize[2] = {arg.multiSize.x, arg.multiSize.y}; - impl()->setMultiDetectorSize(msize); + int msize[2] = {arg.detectorSize.x, arg.detectorSize.y}; + impl()->setDetectorSize(msize); } - impl()->setDetectorPositionId(arg.detId); + impl()->setDetectorPositionId(arg.moduleId); impl()->setDetectorHostname(arg.hostname); // udp setup diff --git a/slsReceiverSoftware/src/Implementation.cpp b/slsReceiverSoftware/src/Implementation.cpp index 73dad4599..a8d9204bc 100755 --- a/slsReceiverSoftware/src/Implementation.cpp +++ b/slsReceiverSoftware/src/Implementation.cpp @@ -308,7 +308,7 @@ int *Implementation::getMultiDetectorSize() const { return (int *)numDet; } -void Implementation::setMultiDetectorSize(const int *size) { +void Implementation::setDetectorSize(const int *size) { LOG(logDEBUG3) << __SHORT_AT__ << " called"; std::string log_message = "Detector Size (ports): ("; for (int i = 0; i < MAX_DIMENSIONS; ++i) { @@ -1008,7 +1008,7 @@ void Implementation::setNumberofUDPInterfaces(const int n) { SetThreadPriorities(); // update (from 1 to 2 interface) & also for printout - setMultiDetectorSize(numDet); + setDetectorSize(numDet); // update row and column in dataprocessor setDetectorPositionId(detID); diff --git a/slsReceiverSoftware/src/Implementation.h b/slsReceiverSoftware/src/Implementation.h index a815b14b7..ae384dbfe 100755 --- a/slsReceiverSoftware/src/Implementation.h +++ b/slsReceiverSoftware/src/Implementation.h @@ -29,7 +29,7 @@ class Implementation : private virtual slsDetectorDefs { void setDetectorType(const detectorType d); int *getMultiDetectorSize() const; - void setMultiDetectorSize(const int *size); + void setDetectorSize(const int *size); int getDetectorPositionId() const; void setDetectorPositionId(const int id); std::string getDetectorHostname() const; diff --git a/slsSupportLib/include/sls_detector_defs.h b/slsSupportLib/include/sls_detector_defs.h index 9d17713a1..088434254 100755 --- a/slsSupportLib/include/sls_detector_defs.h +++ b/slsSupportLib/include/sls_detector_defs.h @@ -32,6 +32,7 @@ /** default ports */ #define DEFAULT_PORTNO 1952 +#define DEFAULT_RX_PORTNO 1954 #define DEFAULT_UDP_PORTNO 50001 #define DEFAULT_ZMQ_CL_PORTNO 30001 #define DEFAULT_ZMQ_RX_PORTNO 30001 @@ -466,8 +467,8 @@ class slsDetectorDefs { */ struct rxParameters { detectorType detType{GENERIC}; - xy multiSize; - int detId{0}; + xy detectorSize; + int moduleId{0}; char hostname[MAX_STR_LENGTH]; int udpInterfaces{1}; int udp_dstport{0};