mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-08-10 21:30:29 +02:00
Code Review 3
This commit is contained in:
@@ -453,6 +453,7 @@ if (SLS_USE_GUI)
|
||||
endif (SLS_USE_GUI)
|
||||
|
||||
if (SLS_USE_MATTERHORN)
|
||||
# TODO cant use add_subdirectory twice - can only build matterhorn server or simulator
|
||||
add_subdirectory(slsDetectorServers/matterhornServer)
|
||||
endif()
|
||||
|
||||
|
||||
@@ -49,12 +49,8 @@ class BaseMatterhornServer
|
||||
ProcessedResult processFunction(const detFuncs function_id,
|
||||
ServerInterface &socket);
|
||||
|
||||
using ImplType = typename implementation_typetrait<DerivedServer>::ImplType;
|
||||
|
||||
protected:
|
||||
auto *getImpl() { return this->getDerivedImpl(); }
|
||||
|
||||
const auto *getImpl() const { return this->getDerivedImpl(); }
|
||||
using ImplType =
|
||||
typename implementation_type_trait<DerivedServer>::ImplType;
|
||||
|
||||
private:
|
||||
DerivedServer *getDerived() { return static_cast<DerivedServer *>(this); }
|
||||
@@ -94,7 +90,8 @@ BaseMatterhornServer<DerivedServer>::set_counter_mask(ServerInterface &socket) {
|
||||
}
|
||||
|
||||
try {
|
||||
getImpl()->set_counter_mask(counter_mask);
|
||||
DetectorServer<BaseMatterhornServer<DerivedServer>>::getImpl()
|
||||
->set_counter_mask(counter_mask);
|
||||
} catch (const std::exception &e) {
|
||||
return_fail("Failed to set counter mask: " + std::string(e.what()));
|
||||
}
|
||||
@@ -109,13 +106,14 @@ ProcessedResult BaseMatterhornServer<DerivedServer>::get_counter_mask(
|
||||
uint32_t counter_mask{};
|
||||
|
||||
try {
|
||||
counter_mask = getImpl()->get_counter_mask();
|
||||
counter_mask =
|
||||
DetectorServer<BaseMatterhornServer<DerivedServer>>::getImpl()
|
||||
->get_counter_mask();
|
||||
} catch (const std::exception &e) {
|
||||
return_fail("Failed to get counter mask: " + std::string(e.what()));
|
||||
}
|
||||
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(counter_mask))};
|
||||
return send_result(socket, counter_mask);
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
@@ -86,8 +86,11 @@ template <typename DerivedMatterhornServerImpl>
|
||||
void BaseMatterhornServerImpl<DerivedMatterhornServerImpl>::setupDetector() {
|
||||
// TODO: extend
|
||||
try {
|
||||
// TODO: for a stop server command it should not talk to the board ->
|
||||
// add flag.
|
||||
set_num_frames(1);
|
||||
set_num_triggers(1);
|
||||
set_counter_mask(0xF); // enable counter all counters by default
|
||||
} catch (const std::exception &e) {
|
||||
LOG(logERROR) << "Failed to setup detector: " << e.what();
|
||||
detectorSetupStatus.error_message = std::string(e.what());
|
||||
@@ -141,12 +144,18 @@ void BaseMatterhornServerImpl<DerivedMatterhornServerImpl>::set_num_frames(
|
||||
try {
|
||||
busCommunication.writeRegister(Reg::MH_SM_Frames_Reg,
|
||||
static_cast<uint32_t>(num_frames));
|
||||
auto written_num_frames = busCommunication.readRegister(
|
||||
Reg::MH_SM_Frames_Reg); // check if write was successful
|
||||
|
||||
if (num_frames != static_cast<uint64_t>(written_num_frames)) {
|
||||
throw std::runtime_error(
|
||||
fmt::format("Requested {} frames, but set {}", num_frames,
|
||||
static_cast<uint64_t>(written_num_frames)));
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
LOG(logERROR) << "Failed to set number of frames: " << e.what();
|
||||
throw;
|
||||
}
|
||||
// TODO: maybe always check that the value is correctly set by reading back
|
||||
// the register and comparing
|
||||
}
|
||||
|
||||
template <typename DerivedMatterhornServerImpl>
|
||||
@@ -155,6 +164,13 @@ void BaseMatterhornServerImpl<DerivedMatterhornServerImpl>::set_num_triggers(
|
||||
|
||||
try {
|
||||
busCommunication.writeRegister(Reg::MH_SM_Triggers_Reg, num_triggers);
|
||||
auto written_num_triggers = busCommunication.readRegister(
|
||||
Reg::MH_SM_Triggers_Reg); // check if write was successful
|
||||
if (num_triggers != written_num_triggers) {
|
||||
throw std::runtime_error(
|
||||
fmt::format("Requested {} triggers, but set {}", num_triggers,
|
||||
written_num_triggers));
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
LOG(logERROR) << "Failed to set number of triggers: " << e.what();
|
||||
throw;
|
||||
@@ -273,8 +289,24 @@ void BaseMatterhornServerImpl<DerivedMatterhornServerImpl>::set_module_position(
|
||||
try {
|
||||
busCommunication.writeRegister(Reg::Frame_HDR_ModCoord_LSB_Reg,
|
||||
register_value_LSB);
|
||||
|
||||
auto written_register_value_LSB = busCommunication.readRegister(
|
||||
Reg::Frame_HDR_ModCoord_LSB_Reg); // check if write was successful
|
||||
|
||||
busCommunication.writeRegister(Reg::Frame_HDR_ModCoord_MSB_Reg,
|
||||
register_value_MSB);
|
||||
|
||||
auto written_register_value_MSB = busCommunication.readRegister(
|
||||
Reg::Frame_HDR_ModCoord_MSB_Reg); // check if write was successful
|
||||
|
||||
if (register_value_LSB != written_register_value_LSB ||
|
||||
register_value_MSB != written_register_value_MSB) {
|
||||
throw std::runtime_error(
|
||||
fmt::format("LSB: requested {}, but set {}. "
|
||||
"MSB: requested {}, but set {}",
|
||||
register_value_LSB, written_register_value_LSB,
|
||||
register_value_MSB, written_register_value_MSB));
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
LOG(logERROR) << "Failed to write module position register: "
|
||||
<< e.what();
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "CommandLineOptions.hpp"
|
||||
#include MATTERHORN_SERVER_HEADER
|
||||
#include "helpers/Helpers.hpp"
|
||||
#include "sls/logger.h"
|
||||
#include "sls/sls_detector_exceptions.h"
|
||||
#include "sls/versionAPI.h"
|
||||
#include <semaphore.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <fmt/format.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
@@ -62,6 +64,9 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
LOG(TLogLevel::logINFOMAGENTA) << cli.printOptions();
|
||||
|
||||
// free shared memory from previous run (not removed if detector crashed)
|
||||
freeSharedMemory();
|
||||
|
||||
// Register Ctrl+C handler
|
||||
std::signal(SIGINT, sigInterruptHandler);
|
||||
|
||||
|
||||
@@ -11,8 +11,6 @@ namespace sls {
|
||||
class MatterhornServer : public BaseMatterhornServer<MatterhornServer> {
|
||||
|
||||
public:
|
||||
using ImplType = MatterhornServerImpl;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Starts up a Matterhorn server.
|
||||
@@ -23,11 +21,6 @@ class MatterhornServer : public BaseMatterhornServer<MatterhornServer> {
|
||||
explicit MatterhornServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO);
|
||||
|
||||
~MatterhornServer() = default;
|
||||
|
||||
private:
|
||||
ImplType *getImpl() { return this->getDerivedImpl(); }
|
||||
|
||||
const ImplType *getImpl() const { return this->getDerivedImpl(); }
|
||||
};
|
||||
|
||||
} // namespace sls
|
||||
@@ -4,6 +4,9 @@ namespace sls {
|
||||
|
||||
slsDetectorDefs::runStatus MatterhornServerImpl::get_run_status() const {
|
||||
|
||||
// TODO: will also have a scanStatus - scanStatus should be in base
|
||||
// implementation and shared between virtual and actual detector - split
|
||||
// this function into two.
|
||||
return slsDetectorDefs::runStatus::IDLE; // TODO: implement
|
||||
}
|
||||
|
||||
@@ -13,6 +16,9 @@ void MatterhornServerImpl::set_module_position_and_update_srcudpmac(
|
||||
// position_info = [num_modules_in_y, module_index]
|
||||
|
||||
const size_t module_row = position_info[1] % position_info[0];
|
||||
if (position_info[0] <= 0) {
|
||||
throw RuntimeError("Number of modules in y direction cannot be 0.");
|
||||
}
|
||||
const size_t module_col = position_info[1] / position_info[0];
|
||||
|
||||
try {
|
||||
|
||||
@@ -17,9 +17,6 @@ VirtualMatterhornServer::VirtualMatterhornServer(uint16_t port)
|
||||
// TODO: no init_server function for now is it neccessary to set the init
|
||||
// flag
|
||||
getImpl()->setupDetector();
|
||||
|
||||
getImpl()->set_source_udp_ip(
|
||||
LOCALHOSTIP_INT); // TODO: should this be done in setupDetector?
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
@@ -8,7 +8,6 @@ class VirtualMatterhornServer
|
||||
: public BaseMatterhornServer<VirtualMatterhornServer> {
|
||||
|
||||
public:
|
||||
using ImplType = VirtualMatterhornServerImpl;
|
||||
/**
|
||||
* Constructor
|
||||
* Starts up a virtual Matterhorn server.
|
||||
@@ -19,11 +18,6 @@ class VirtualMatterhornServer
|
||||
explicit VirtualMatterhornServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO);
|
||||
|
||||
~VirtualMatterhornServer() = default;
|
||||
|
||||
private:
|
||||
ImplType *getImpl() { return this->getDerivedImpl(); }
|
||||
|
||||
const ImplType *getImpl() const { return this->getDerivedImpl(); }
|
||||
};
|
||||
|
||||
} // namespace sls
|
||||
@@ -6,6 +6,10 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
VirtualMatterhornServerImpl::VirtualMatterhornServerImpl() {
|
||||
this->set_source_udp_ip(LOCALHOSTIP_INT);
|
||||
}
|
||||
|
||||
slsDetectorDefs::runStatus VirtualMatterhornServerImpl::get_run_status() const {
|
||||
|
||||
slsDetectorDefs::runStatus scanstatus{};
|
||||
@@ -42,7 +46,7 @@ void VirtualMatterhornServerImpl::set_module_position_and_update_srcudpmac(
|
||||
if (this->udpDetails[0].srcmac ==
|
||||
0) { // only configure if source mac address is not set already
|
||||
uint64_t newSrcMac =
|
||||
generate_mac_address_from_module_position(module_row, module_col);
|
||||
generateMacAddressfromModulePosition(module_row, module_col);
|
||||
|
||||
this->updateSrcMacAddress(newSrcMac);
|
||||
}
|
||||
@@ -51,8 +55,7 @@ void VirtualMatterhornServerImpl::set_module_position_and_update_srcudpmac(
|
||||
void VirtualMatterhornServerImpl::set_source_udp_mac(
|
||||
const uint64_t newsrcudpMac) {
|
||||
|
||||
if ((newsrcudpMac << INDIVIDUAL_GROUP_BIT_OFFSET) == 0 &&
|
||||
(newsrcudpMac << UNIVERSAL_LOCAL_BIT_OFFSET) == 1) {
|
||||
if (!isValidMac(newsrcudpMac)) {
|
||||
throw RuntimeError("Invalid source MAC address: unicast bit or local "
|
||||
"administration bit is not set");
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ class VirtualMatterhornServerImpl
|
||||
: public BaseMatterhornServerImpl<VirtualMatterhornServerImpl> {
|
||||
|
||||
public:
|
||||
VirtualMatterhornServerImpl() = default;
|
||||
VirtualMatterhornServerImpl();
|
||||
~VirtualMatterhornServerImpl() = default;
|
||||
|
||||
slsDetectorDefs::runStatus get_run_status() const;
|
||||
|
||||
@@ -9,13 +9,13 @@ class VirtualMatterhornServer;
|
||||
class MatterhornServerImpl;
|
||||
class VirtualMatterhornServerImpl;
|
||||
|
||||
template <typename DetectorServer> struct implementation_typetrait;
|
||||
template <typename DetectorServer> struct implementation_type_trait;
|
||||
|
||||
template <> struct implementation_typetrait<MatterhornServer> {
|
||||
template <> struct implementation_type_trait<MatterhornServer> {
|
||||
using ImplType = MatterhornServerImpl;
|
||||
};
|
||||
|
||||
template <> struct implementation_typetrait<VirtualMatterhornServer> {
|
||||
template <> struct implementation_type_trait<VirtualMatterhornServer> {
|
||||
using ImplType = VirtualMatterhornServerImpl;
|
||||
};
|
||||
|
||||
|
||||
@@ -38,12 +38,12 @@ template <typename DerivedDetectorServer> class DetectorServer {
|
||||
|
||||
std::unique_ptr<DetectorServerImpl> impl;
|
||||
|
||||
auto *getDerivedImpl() {
|
||||
auto *getImpl() {
|
||||
return static_cast<typename DerivedDetectorServer::ImplType *>(
|
||||
impl.get());
|
||||
}
|
||||
|
||||
const auto *getDerivedImpl() const {
|
||||
const auto *getImpl() const {
|
||||
return static_cast<const typename DerivedDetectorServer::ImplType *>(
|
||||
impl.get());
|
||||
}
|
||||
@@ -191,8 +191,7 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_update_mode(
|
||||
|
||||
// TODO: catch the socket error during Send and add error message to the
|
||||
// ProcessedResult but DatSocket shared with receiver - some refactoring
|
||||
return ProcessedResult{static_cast<ReturnCode>(
|
||||
socket.sendResult(static_cast<int>(updateMode)))};
|
||||
return send_result(socket, static_cast<uint32_t>(updateMode));
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -200,8 +199,7 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_source_udp_mac(
|
||||
ServerInterface &socket) const {
|
||||
auto srcUdpMac = impl->get_source_udp_mac();
|
||||
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(srcUdpMac))};
|
||||
return send_result(socket, srcUdpMac);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -220,7 +218,7 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::set_source_udp_mac(
|
||||
}
|
||||
|
||||
try {
|
||||
getDerivedImpl()->set_source_udp_mac(newsrcudpMac);
|
||||
getImpl()->set_source_udp_mac(newsrcudpMac);
|
||||
} catch (const std::exception &e) {
|
||||
LOG(logERROR) << "Failed to set source UDP MAC address: " << e.what();
|
||||
return_fail("Failed to set source UDP MAC address: " +
|
||||
@@ -254,8 +252,7 @@ template <typename DerivedDetectorServer>
|
||||
ProcessedResult DetectorServer<DerivedDetectorServer>::get_source_udp_ip(
|
||||
ServerInterface &socket) const {
|
||||
uint32_t src_UdpIp = impl->get_source_udp_ip();
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(src_UdpIp))};
|
||||
return send_result(socket, src_UdpIp);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -283,8 +280,7 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_destination_udp_mac(
|
||||
ServerInterface &socket) const {
|
||||
|
||||
auto dstUdpMac = impl->get_destination_udp_mac();
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(dstUdpMac))};
|
||||
return send_result(socket, dstUdpMac);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -312,8 +308,7 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_destination_udp_ip(
|
||||
ServerInterface &socket) const {
|
||||
|
||||
uint32_t dstUdpIp = impl->get_destination_udp_ip();
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(dstUdpIp))};
|
||||
return send_result(socket, dstUdpIp);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -340,8 +335,7 @@ template <typename DerivedDetectorServer>
|
||||
ProcessedResult DetectorServer<DerivedDetectorServer>::get_destination_udp_port(
|
||||
ServerInterface &socket) const {
|
||||
uint16_t dstUdpPort = impl->get_destination_udp_port();
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(dstUdpPort))};
|
||||
return send_result(socket, dstUdpPort);
|
||||
};
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -349,15 +343,14 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_num_frames(
|
||||
ServerInterface &socket) const {
|
||||
uint64_t num_frames{};
|
||||
try {
|
||||
num_frames = getDerivedImpl()->get_num_frames();
|
||||
num_frames = getImpl()->get_num_frames();
|
||||
} catch (const std::exception &e) {
|
||||
auto error_message =
|
||||
"Failed to get number of frames: " + std::string(e.what());
|
||||
LOG(logERROR) << error_message;
|
||||
return return_fail(error_message);
|
||||
}
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(num_frames))};
|
||||
return send_result(socket, num_frames);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -373,7 +366,7 @@ DetectorServer<DerivedDetectorServer>::set_num_frames(ServerInterface &socket) {
|
||||
return return_fail(error_message);
|
||||
}
|
||||
try {
|
||||
getDerivedImpl()->set_num_frames(num_frames);
|
||||
getImpl()->set_num_frames(num_frames);
|
||||
} catch (const std::exception &e) {
|
||||
auto error_message =
|
||||
"Failed to set number of frames: " + std::string(e.what());
|
||||
@@ -388,16 +381,14 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_num_triggers(
|
||||
ServerInterface &socket) const {
|
||||
uint64_t num_triggers{};
|
||||
try {
|
||||
num_triggers =
|
||||
static_cast<uint64_t>(getDerivedImpl()->get_num_triggers());
|
||||
num_triggers = static_cast<uint64_t>(getImpl()->get_num_triggers());
|
||||
} catch (const std::exception &e) {
|
||||
auto error_message =
|
||||
"Failed to get number of triggers: " + std::string(e.what());
|
||||
LOG(logERROR) << error_message;
|
||||
return return_fail(error_message);
|
||||
}
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(num_triggers))};
|
||||
return send_result(socket, num_triggers);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -413,7 +404,7 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::set_num_triggers(
|
||||
return return_fail(error_message);
|
||||
}
|
||||
try {
|
||||
getDerivedImpl()->set_num_triggers(num_triggers);
|
||||
getImpl()->set_num_triggers(num_triggers);
|
||||
} catch (const std::exception &e) {
|
||||
auto error_message =
|
||||
"Failed to set number of triggers: " + std::string(e.what());
|
||||
@@ -428,32 +419,30 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_version(
|
||||
ServerInterface &socket) const {
|
||||
|
||||
auto version =
|
||||
getDerivedImpl()
|
||||
->get_server_version(); // TODO: get Impl from derived server
|
||||
getImpl()->get_server_version(); // TODO: get Impl from derived server
|
||||
|
||||
char version_cstr[MAX_STR_LENGTH]{};
|
||||
std::snprintf(version_cstr, sizeof(version_cstr), "%s",
|
||||
version.c_str()); // ensures temination
|
||||
LOG(TLogLevel::logDEBUG) << "Server Version: " << version;
|
||||
return ProcessedResult{static_cast<ReturnCode>(socket.sendResult(
|
||||
version_cstr))}; // TODO: check what would be possible return codes!!!
|
||||
return send_result(
|
||||
socket,
|
||||
version_cstr); // TODO: check what would be possible return codes!!!
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
ProcessedResult DetectorServer<DerivedDetectorServer>::get_num_udp_interfaces(
|
||||
ServerInterface &socket) const {
|
||||
auto num_udp_interfaces = getDerivedImpl()->get_num_udp_interfaces();
|
||||
int num_udp_interfaces = getImpl()->get_num_udp_interfaces();
|
||||
|
||||
return ProcessedResult{static_cast<ReturnCode>(
|
||||
socket.sendResult(static_cast<int>(num_udp_interfaces)))};
|
||||
return send_result(socket, num_udp_interfaces);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
ProcessedResult DetectorServer<DerivedDetectorServer>::get_detector_type(
|
||||
ServerInterface &socket) const {
|
||||
uint32_t detectortype = getDerivedImpl()->get_detector_type();
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(detectortype))};
|
||||
uint32_t detectortype = getImpl()->get_detector_type();
|
||||
return send_result(socket, detectortype);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
@@ -461,39 +450,37 @@ ProcessedResult DetectorServer<DerivedDetectorServer>::get_receiver_parameters(
|
||||
ServerInterface &socket) const {
|
||||
|
||||
slsDetectorDefs::rxParameters rx_params =
|
||||
getDerivedImpl()->get_receiver_parameters();
|
||||
getImpl()->get_receiver_parameters();
|
||||
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(rx_params))};
|
||||
return send_result(socket, rx_params);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
ProcessedResult DetectorServer<DerivedDetectorServer>::get_run_status(
|
||||
ServerInterface &socket) const {
|
||||
|
||||
slsDetectorDefs::runStatus status = getDerivedImpl()->get_run_status();
|
||||
slsDetectorDefs::runStatus status = getImpl()->get_run_status();
|
||||
|
||||
return ProcessedResult{static_cast<ReturnCode>(socket.sendResult(status))};
|
||||
return send_result(socket, status);
|
||||
}
|
||||
|
||||
template <typename DerivedDetectorServer>
|
||||
ProcessedResult DetectorServer<DerivedDetectorServer>::initial_checks(
|
||||
ServerInterface &socket) const {
|
||||
auto detectorsetupstatus = getDerivedImpl()->initial_checks();
|
||||
auto detectorsetupstatus = getImpl()->get_detector_setup_status();
|
||||
|
||||
// TODO: should there be a time limit?
|
||||
while (detectorsetupstatus.setup_status ==
|
||||
detector_setup_status::NOT_SETUP) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
detectorsetupstatus = getDerivedImpl()->initial_checks();
|
||||
detectorsetupstatus = getImpl()->get_detector_setup_status();
|
||||
}
|
||||
if (detectorsetupstatus.setup_status ==
|
||||
detector_setup_status::FAILED_SETUP) {
|
||||
return return_fail("Initial checks failed: " +
|
||||
detectorsetupstatus.error_message);
|
||||
} else {
|
||||
return ProcessedResult{
|
||||
static_cast<ReturnCode>(socket.sendResult(true))};
|
||||
return send_result<bool>(socket, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,8 +503,7 @@ DetectorServer<DerivedDetectorServer>::set_module_position_and_update_srcudpmac(
|
||||
}
|
||||
|
||||
try {
|
||||
getDerivedImpl()->set_module_position_and_update_srcudpmac(
|
||||
position_info);
|
||||
getImpl()->set_module_position_and_update_srcudpmac(position_info);
|
||||
} catch (const std::exception &e) {
|
||||
return_fail("Failed to set module position: " + std::string(e.what()));
|
||||
}
|
||||
|
||||
@@ -62,12 +62,12 @@ class DetectorServerImpl {
|
||||
|
||||
void set_source_udp_ip(const uint32_t srcip);
|
||||
|
||||
uint32_t get_source_udp_ip() const;
|
||||
|
||||
void set_destination_udp_ip(const uint32_t dstip);
|
||||
|
||||
uint32_t get_destination_udp_ip() const;
|
||||
|
||||
uint32_t get_source_udp_ip() const;
|
||||
|
||||
void set_destination_udp_mac(const uint64_t dstmac);
|
||||
|
||||
uint64_t get_destination_udp_mac() const;
|
||||
@@ -76,7 +76,7 @@ class DetectorServerImpl {
|
||||
|
||||
uint16_t get_destination_udp_port() const;
|
||||
|
||||
detector_setup_status initial_checks() const;
|
||||
detector_setup_status get_detector_setup_status() const;
|
||||
|
||||
protected:
|
||||
std::array<UDPInfo, 1>
|
||||
|
||||
@@ -30,6 +30,11 @@ inline ProcessedResult send_ok(ServerInterface &socket) {
|
||||
static_cast<ReturnCode>(socket.Send(ReturnCode::OK))};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline ProcessedResult send_result(ServerInterface &socket, const T &value) {
|
||||
return ProcessedResult{static_cast<ReturnCode>(socket.sendResult(value))};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TCPInterface class handles communication and processing of commands
|
||||
* from Client to Server.
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#pragma once
|
||||
#include "Defs.hpp"
|
||||
#include "DetectorServerImpl.hpp"
|
||||
#include "sls/SharedMemory.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
@@ -6,13 +9,13 @@
|
||||
namespace sls {
|
||||
|
||||
constexpr uint64_t mac_mask = 0xffffffffffff0000;
|
||||
constexpr uint8_t offset_row_position_in_mac = 16; // given in bits
|
||||
constexpr uint8_t offset_col_position_in_mac = 0; // given in bits
|
||||
constexpr uint8_t offset_row_position_in_mac = 8; // given in bits
|
||||
constexpr uint8_t offset_col_position_in_mac = 0; // given in bits
|
||||
|
||||
/// @brief generates a random locally administered unicast MAC address for the
|
||||
/// source UDP
|
||||
/// @return generated MAC address
|
||||
inline uint64_t generaterandomMacAddress() {
|
||||
inline uint64_t generateRandomMacAddress() {
|
||||
uint64_t mac =
|
||||
0xAA0000000000; // locally administered unicast address (0xA: 0b1010) //
|
||||
// TODO maybe 0x02000000000 better?
|
||||
@@ -28,11 +31,10 @@ inline uint64_t generaterandomMacAddress() {
|
||||
/// @param module_row
|
||||
/// @param module_col
|
||||
/// @return generated MAC address
|
||||
inline uint64_t
|
||||
generate_mac_address_from_module_position(const size_t module_row,
|
||||
const size_t module_col) {
|
||||
inline uint64_t generateMacAddressfromModulePosition(const uint8_t module_row,
|
||||
const uint8_t module_col) {
|
||||
|
||||
uint64_t newSrcMac = generaterandomMacAddress();
|
||||
uint64_t newSrcMac = generateRandomMacAddress();
|
||||
newSrcMac = (newSrcMac & mac_mask) |
|
||||
(module_row << offset_row_position_in_mac) |
|
||||
(module_col << offset_col_position_in_mac);
|
||||
@@ -40,4 +42,23 @@ generate_mac_address_from_module_position(const size_t module_row,
|
||||
return newSrcMac;
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
/// @brief check that mac is unicast and locally administered
|
||||
/// @param mac
|
||||
/// @return true if mac is valid, false otherwise
|
||||
inline bool isValidMac(const uint64_t mac) {
|
||||
if ((mac << INDIVIDUAL_GROUP_BIT_OFFSET) == 0 &&
|
||||
(mac << UNIVERSAL_LOCAL_BIT_OFFSET) == 1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline void freeSharedMemory() {
|
||||
SharedMemory<acquisitionStatus> shm(0, -1, "server");
|
||||
|
||||
if (shm.exists()) {
|
||||
shm.removeSharedMemory();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "DetectorServerImpl.hpp"
|
||||
#include "sls/logger.h"
|
||||
#include "sls/sls_detector_exceptions.h"
|
||||
#include <fmt/format.h>
|
||||
|
||||
namespace sls {
|
||||
@@ -20,7 +21,12 @@ void DetectorServerImpl::createSharedMemory() {
|
||||
shm.openSharedMemory(true); // stop server
|
||||
} else {
|
||||
LOG(logINFOBLUE) << "Creating shared memory for acquisition status";
|
||||
shm.createSharedMemory();
|
||||
try {
|
||||
shm.createSharedMemory();
|
||||
} catch (const SharedMemoryAlreadyExistsError &e) {
|
||||
shm.openSharedMemory(true); // potential race conditions between
|
||||
// stop and control server
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +83,7 @@ uint16_t DetectorServerImpl::get_destination_udp_port() const {
|
||||
return udpDetails[0].dstport;
|
||||
}
|
||||
|
||||
detector_setup_status DetectorServerImpl::initial_checks() const {
|
||||
detector_setup_status DetectorServerImpl::get_detector_setup_status() const {
|
||||
return detectorSetupStatus;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,9 +46,9 @@ void HardwareMemoryModel::unmapMemory() {
|
||||
if (munmap(reinterpret_cast<void *>(
|
||||
const_cast<uint32_t *>(mapped_memory_ptr)),
|
||||
size_memory_space) < 0) {
|
||||
throw RuntimeError(
|
||||
fmt::format("Failed to unmap memory for IP core: {}",
|
||||
IPCore_base_address)); // TODO: needs ToString
|
||||
LOG(logWARNING)
|
||||
<< fmt::format("Failed to unmap memory for IP core: {}",
|
||||
IPCore_base_address); // TODO: needs ToString
|
||||
}
|
||||
mapped_memory_ptr = nullptr;
|
||||
}
|
||||
|
||||
@@ -483,7 +483,6 @@ class MatterhornData : public GeneralData {
|
||||
dynamicRange = 16; // default
|
||||
SetCounterMask(0xf); // default all 4 counters enabled
|
||||
UpdateImageSize();
|
||||
CalculatefifoDepth();
|
||||
};
|
||||
|
||||
void SetDynamicRange(int dr) {
|
||||
@@ -513,6 +512,8 @@ class MatterhornData : public GeneralData {
|
||||
LOG(logINFO) << "imageSize: " << imageSize;
|
||||
actualImageSize = imageSize;
|
||||
|
||||
CalculatefifoDepth();
|
||||
|
||||
packetsPerFrame = imageSize / dataSize;
|
||||
|
||||
LOG(logINFO) << "Packets Per Frame: " << packetsPerFrame;
|
||||
|
||||
@@ -270,8 +270,6 @@ void Implementation::setModulePositionId(const int id) {
|
||||
streamingPort = DEFAULT_ZMQ_RX_PORTNO + modulePos * portGeometry.x;
|
||||
|
||||
if (numModules.y == 0) {
|
||||
LOG(logERROR) << "Number of modules in y direction is 0. Cannot set "
|
||||
"module position.";
|
||||
throw RuntimeError("Number of modules in y direction is 0. Cannot set "
|
||||
"module position.");
|
||||
}
|
||||
|
||||
@@ -168,7 +168,11 @@ template <typename T> class SharedMemory {
|
||||
if (fd < 0) {
|
||||
std::string msg =
|
||||
"Create shared memory " + name + " failed: " + strerror(errno);
|
||||
throw SharedMemoryError(msg);
|
||||
if (errno == EEXIST) {
|
||||
throw SharedMemoryAlreadyExistsError(msg);
|
||||
} else {
|
||||
throw SharedMemoryError(msg);
|
||||
}
|
||||
}
|
||||
|
||||
if (ftruncate(fd, sizeof(T)) < 0) {
|
||||
|
||||
@@ -17,6 +17,11 @@ class SharedMemoryError : public RuntimeError {
|
||||
explicit SharedMemoryError(const std::string &msg);
|
||||
};
|
||||
|
||||
class SharedMemoryAlreadyExistsError : public SharedMemoryError {
|
||||
public:
|
||||
explicit SharedMemoryAlreadyExistsError(const std::string &msg);
|
||||
};
|
||||
|
||||
class SocketError : public RuntimeError {
|
||||
public:
|
||||
explicit SocketError(const std::string &msg);
|
||||
|
||||
@@ -14,6 +14,11 @@ RuntimeError::RuntimeError(const char *msg) : runtime_error(msg) {
|
||||
}
|
||||
SharedMemoryError::SharedMemoryError(const std::string &msg)
|
||||
: RuntimeError(msg) {}
|
||||
|
||||
SharedMemoryAlreadyExistsError::SharedMemoryAlreadyExistsError(
|
||||
const std::string &msg)
|
||||
: SharedMemoryError(msg) {}
|
||||
|
||||
SocketError::SocketError(const std::string &msg) : RuntimeError(msg) {}
|
||||
ZmqSocketError::ZmqSocketError(const std::string &msg) : RuntimeError(msg) {}
|
||||
NotImplementedError::NotImplementedError(const std::string &msg)
|
||||
|
||||
Reference in New Issue
Block a user