mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-08-05 13:02:26 +02:00
added shared memory for aqcuisition status
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<DerivedServer>::initial_checks(ServerInterface &socket) {
|
||||
return static_cast<DerivedServer *>(this)->initial_checks(socket);
|
||||
}
|
||||
|
||||
template <typename DerivedServer>
|
||||
ReturnCode BaseMatterhornServer<DerivedServer>::get_run_status(
|
||||
ServerInterface &socket) const {
|
||||
|
||||
return static_cast<const DerivedServer *>(this)->get_run_status(socket);
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
@@ -19,6 +19,8 @@ class VirtualMatterhornServer
|
||||
~VirtualMatterhornServer() = default;
|
||||
|
||||
ReturnCode initial_checks(ServerInterface &socket);
|
||||
|
||||
ReturnCode get_run_status(ServerInterface &socket) const;
|
||||
};
|
||||
|
||||
} // namespace sls
|
||||
@@ -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<ReturnCode>(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<ReturnCode>(socket.sendResult(scanstatus));
|
||||
}
|
||||
|
||||
LOG(logINFO) << fmt::format("Status: {}\n", ToString(status));
|
||||
return static_cast<ReturnCode>(socket.sendResult(status));
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
@@ -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<slsDetectorDefs::runStatus> scanStatus{
|
||||
slsDetectorDefs::runStatus::IDLE}; // idle, running or error
|
||||
std::atomic<bool> scanStop{false};
|
||||
|
||||
// TODO: only neccessary for virtual, maybe have two shared memory
|
||||
// structures, one for virtual
|
||||
std::atomic<slsDetectorDefs::runStatus> status{
|
||||
slsDetectorDefs::runStatus::IDLE};
|
||||
std::atomic<bool> stop{false};
|
||||
};
|
||||
|
||||
template <typename DerivedDetectorServer> class DetectorServer {
|
||||
|
||||
@@ -49,7 +67,14 @@ template <typename DerivedDetectorServer> class DetectorServer {
|
||||
/// @brief TODO what is this?
|
||||
bool updateMode{true};
|
||||
|
||||
/// @brief
|
||||
mutable SharedMemory<acquisitionStatus> 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<DerivedDetectorServer>::DetectorServer(uint16_t port) {
|
||||
udpDetails[0].srcport = DEFAULT_UDP_SRC_PORTNO;
|
||||
udpDetails[0].dstport = DEFAULT_UDP_DST_PORTNO;
|
||||
|
||||
createSharedMemory();
|
||||
|
||||
std::function<ReturnCode(const detFuncs &, ServerInterface &)> fn =
|
||||
[this](const detFuncs &function_id, ServerInterface &socket) {
|
||||
return this->processFunction(function_id, socket);
|
||||
};
|
||||
|
||||
tcpInterface = std::make_unique<TCPInterface>(fn, port);
|
||||
}
|
||||
|
||||
@@ -131,6 +159,9 @@ ReturnCode DetectorServer<DerivedDetectorServer>::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<DerivedDetectorServer *>(this)->get_run_status(
|
||||
socket);
|
||||
|
||||
default:
|
||||
LOG(logDEBUG) << "Checking specific server functions for function ID: "
|
||||
@@ -143,6 +174,19 @@ ReturnCode DetectorServer<DerivedDetectorServer>::processFunction(
|
||||
return ReturnCode::FAIL;
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
void DetectorServer<DerivedDetectorServer>::createSharedMemory() {
|
||||
|
||||
shm = SharedMemory<acquisitionStatus>(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 <typename DerivedDetectorServer>
|
||||
ReturnCode DetectorServer<DerivedDetectorServer>::get_update_mode(
|
||||
ServerInterface &socket) const {
|
||||
@@ -166,7 +210,7 @@ ReturnCode DetectorServer<DerivedDetectorServer>::set_source_udp_mac(
|
||||
|
||||
udpDetails[0].srcmac = newsrcudpMac;
|
||||
// TODO: configuremac, check unicast address
|
||||
return ReturnCode::OK;
|
||||
return static_cast<ReturnCode>(socket.Send(ReturnCode::OK));
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -178,6 +222,7 @@ ReturnCode DetectorServer<DerivedDetectorServer>::get_source_udp_mac(
|
||||
template <typename DerivedDetectorServer>
|
||||
ReturnCode DetectorServer<DerivedDetectorServer>::set_source_udp_ip(
|
||||
ServerInterface &socket) {
|
||||
|
||||
uint32_t newSrcIp;
|
||||
|
||||
try {
|
||||
@@ -189,7 +234,7 @@ ReturnCode DetectorServer<DerivedDetectorServer>::set_source_udp_ip(
|
||||
}
|
||||
|
||||
udpDetails[0].srcip = newSrcIp;
|
||||
return ReturnCode::OK;
|
||||
return static_cast<ReturnCode>(socket.Send(ReturnCode::OK));
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -213,7 +258,7 @@ ReturnCode DetectorServer<DerivedDetectorServer>::set_destination_udp_mac(
|
||||
|
||||
udpDetails[0].dstmac = newDstMac;
|
||||
// TODO: configuremac, check unicast address
|
||||
return ReturnCode::OK;
|
||||
return static_cast<ReturnCode>(socket.Send(ReturnCode::OK));
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -236,7 +281,7 @@ ReturnCode DetectorServer<DerivedDetectorServer>::set_destination_udp_ip(
|
||||
}
|
||||
|
||||
udpDetails[0].dstip = newDstIp;
|
||||
return ReturnCode::OK;
|
||||
return static_cast<ReturnCode>(socket.Send(ReturnCode::OK));
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -259,7 +304,7 @@ ReturnCode DetectorServer<DerivedDetectorServer>::set_destination_udp_port(
|
||||
}
|
||||
|
||||
udpDetails[0].dstport = newDstPort;
|
||||
return ReturnCode::OK;
|
||||
return static_cast<ReturnCode>(socket.Send(ReturnCode::OK));
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
|
||||
@@ -193,6 +193,8 @@ template <typename T> class SharedMemory {
|
||||
if (verifySize)
|
||||
checkSize(fd);
|
||||
shared_struct = mapSharedMemory(fd);
|
||||
|
||||
LOG(logINFO) << "Shared memory " << name << " opened";
|
||||
}
|
||||
|
||||
void unmapSharedMemory() {
|
||||
|
||||
Reference in New Issue
Block a user