From 55b5511485138fdfbee3e8d83cce660f4c2d51c9 Mon Sep 17 00:00:00 2001 From: Alice Date: Wed, 29 Apr 2026 19:12:10 +0200 Subject: [PATCH] added shared memory for aqcuisition status --- .../matterhornServer/CMakeLists.txt | 3 +- .../include/BaseMatterhornServer.h | 9 +++ .../include/VirtualMatterhornServer.h | 2 + .../src/VirtualMatterhornServer.cpp | 21 +++++++ .../include/DetectorServer.h | 57 +++++++++++++++++-- slsDetectorSoftware/src/SharedMemory.h | 2 + 6 files changed, 87 insertions(+), 7 deletions(-) diff --git a/slsDetectorServers/matterhornServer/CMakeLists.txt b/slsDetectorServers/matterhornServer/CMakeLists.txt index e4d237e41..e147cc6b8 100644 --- a/slsDetectorServers/matterhornServer/CMakeLists.txt +++ b/slsDetectorServers/matterhornServer/CMakeLists.txt @@ -12,7 +12,8 @@ if(SLS_USE_SIMULATOR) target_include_directories(matterhornDetectorServer_virtual PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/../../slsSupportLib/include - ${CMAKE_CURRENT_SOURCE_DIR}/../slsDetectorServer_cpp/include) + ${CMAKE_CURRENT_SOURCE_DIR}/../slsDetectorServer_cpp/include + ${CMAKE_SOURCE_DIR}/slsDetectorSoftware/src) # because of SharedMemory TODO: should be in slsSupportLib? target_link_libraries(matterhornDetectorServer_virtual PUBLIC diff --git a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h index 4ca5e46cc..176cfac9a 100644 --- a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h @@ -43,6 +43,8 @@ class BaseMatterhornServer ReturnCode get_num_udp_interfaces(ServerInterface &socket) const; + ReturnCode get_run_status(ServerInterface &socket) const; + /** * @brief call function corresponding to the function ID received from the * client and send back the result @@ -110,4 +112,11 @@ BaseMatterhornServer::initial_checks(ServerInterface &socket) { return static_cast(this)->initial_checks(socket); } +template +ReturnCode BaseMatterhornServer::get_run_status( + ServerInterface &socket) const { + + return static_cast(this)->get_run_status(socket); +} + } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h b/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h index 61cb31bda..00377bf50 100644 --- a/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h @@ -19,6 +19,8 @@ class VirtualMatterhornServer ~VirtualMatterhornServer() = default; ReturnCode initial_checks(ServerInterface &socket); + + ReturnCode get_run_status(ServerInterface &socket) const; }; } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp index 8c82da20c..ab4068c26 100644 --- a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp +++ b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp @@ -1,4 +1,5 @@ #include "VirtualMatterhornServer.h" +#include "sls/ToString.h" namespace sls { @@ -21,4 +22,24 @@ ReturnCode VirtualMatterhornServer::initial_checks(ServerInterface &socket) { return static_cast(socket.sendResult(initial_checks_passed)); } +ReturnCode +VirtualMatterhornServer::get_run_status(ServerInterface &socket) const { + + slsDetectorDefs::runStatus scanstatus{}; + slsDetectorDefs::runStatus status{}; + + scanstatus = shm()->scanStatus; + status = shm()->status; + + // TODO: why only error and running? what about other states? + if (scanstatus == slsDetectorDefs::runStatus::ERROR || + scanstatus == slsDetectorDefs::runStatus::RUNNING) { + LOG(logINFO) << fmt::format("Scan status: {}\n", ToString(scanstatus)); + return static_cast(socket.sendResult(scanstatus)); + } + + LOG(logINFO) << fmt::format("Status: {}\n", ToString(status)); + return static_cast(socket.sendResult(status)); +} + } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h index 6773f17ba..28781ef6c 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h +++ b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h @@ -1,6 +1,6 @@ #pragma once +#include "SharedMemory.h" #include "TCPInterface.h" -// #include "communication_funcs.h" #include "sls/logger.h" #include "sls/network_utils.h" #include "sls/sls_detector_defs.h" @@ -26,6 +26,24 @@ struct UDPInfo { }; using ReturnCode = slsDetectorDefs::ReturnCode; +/// @brief Shared memory structure for stop server to store run status +struct acquisitionStatus { + + /* FIXED PATTERN FOR STATIC FUNCTIONS. DO NOT CHANGE, ONLY APPEND ------*/ + int shmversion; + + bool isValid{true}; // false if freed to block access from python or c++ api + + std::atomic scanStatus{ + slsDetectorDefs::runStatus::IDLE}; // idle, running or error + std::atomic scanStop{false}; + + // TODO: only neccessary for virtual, maybe have two shared memory + // structures, one for virtual + std::atomic status{ + slsDetectorDefs::runStatus::IDLE}; + std::atomic stop{false}; +}; template class DetectorServer { @@ -49,7 +67,14 @@ template class DetectorServer { /// @brief TODO what is this? bool updateMode{true}; + /// @brief + mutable SharedMemory shm{ + 0, 0}; // TODO: is mutable really neccessary? + private: + /// @brief creates and maps shared memory + void createSharedMemory(); + ReturnCode processFunction(const detFuncs function_id, ServerInterface &socket); @@ -86,10 +111,13 @@ DetectorServer::DetectorServer(uint16_t port) { udpDetails[0].srcport = DEFAULT_UDP_SRC_PORTNO; udpDetails[0].dstport = DEFAULT_UDP_DST_PORTNO; + createSharedMemory(); + std::function fn = [this](const detFuncs &function_id, ServerInterface &socket) { return this->processFunction(function_id, socket); }; + tcpInterface = std::make_unique(fn, port); } @@ -131,6 +159,9 @@ ReturnCode DetectorServer::processFunction( return set_destination_udp_port(socket); case detFuncs::F_GET_DEST_UDP_PORT: return get_destination_udp_port(socket); + case detFuncs::F_GET_RUN_STATUS: + return static_cast(this)->get_run_status( + socket); default: LOG(logDEBUG) << "Checking specific server functions for function ID: " @@ -143,6 +174,19 @@ ReturnCode DetectorServer::processFunction( return ReturnCode::FAIL; } +template +void DetectorServer::createSharedMemory() { + + shm = SharedMemory(0, -1, "server"); + + if (shm.exists()) { + shm.openSharedMemory(true); // stop server TODO: should I verify size + } else { + LOG(logINFOBLUE) << "Creating shared memory for acquisition status"; + shm.createSharedMemory(); + } +} + template ReturnCode DetectorServer::get_update_mode( ServerInterface &socket) const { @@ -166,7 +210,7 @@ ReturnCode DetectorServer::set_source_udp_mac( udpDetails[0].srcmac = newsrcudpMac; // TODO: configuremac, check unicast address - return ReturnCode::OK; + return static_cast(socket.Send(ReturnCode::OK)); } template @@ -178,6 +222,7 @@ ReturnCode DetectorServer::get_source_udp_mac( template ReturnCode DetectorServer::set_source_udp_ip( ServerInterface &socket) { + uint32_t newSrcIp; try { @@ -189,7 +234,7 @@ ReturnCode DetectorServer::set_source_udp_ip( } udpDetails[0].srcip = newSrcIp; - return ReturnCode::OK; + return static_cast(socket.Send(ReturnCode::OK)); } template @@ -213,7 +258,7 @@ ReturnCode DetectorServer::set_destination_udp_mac( udpDetails[0].dstmac = newDstMac; // TODO: configuremac, check unicast address - return ReturnCode::OK; + return static_cast(socket.Send(ReturnCode::OK)); } template @@ -236,7 +281,7 @@ ReturnCode DetectorServer::set_destination_udp_ip( } udpDetails[0].dstip = newDstIp; - return ReturnCode::OK; + return static_cast(socket.Send(ReturnCode::OK)); } template @@ -259,7 +304,7 @@ ReturnCode DetectorServer::set_destination_udp_port( } udpDetails[0].dstport = newDstPort; - return ReturnCode::OK; + return static_cast(socket.Send(ReturnCode::OK)); } template diff --git a/slsDetectorSoftware/src/SharedMemory.h b/slsDetectorSoftware/src/SharedMemory.h index bbdcfdd66..fa20fd355 100644 --- a/slsDetectorSoftware/src/SharedMemory.h +++ b/slsDetectorSoftware/src/SharedMemory.h @@ -193,6 +193,8 @@ template class SharedMemory { if (verifySize) checkSize(fd); shared_struct = mapSharedMemory(fd); + + LOG(logINFO) << "Shared memory " << name << " opened"; } void unmapSharedMemory() {