refactored directory structure

This commit is contained in:
2026-04-23 17:07:54 +02:00
parent 1422b41b0f
commit 38e2cc45aa
16 changed files with 1 additions and 1308 deletions
+1 -1
View File
@@ -426,7 +426,7 @@ endif()
# TODO refactor with simulators
if (SLS_USE_SERVER)
add_subdirectory(slsDetectorServers/slsDetectorServer)
add_subdirectory(slsDetectorServers/slsDetectorServer_cpp)
endif()
if (SLS_USE_RECEIVER)
@@ -1,36 +0,0 @@
# TODO: should be different executable if not simulators on !!
add_executable(matterhornDetectorServer_virtual
${CMAKE_CURRENT_SOURCE_DIR}/src/MatterhornApp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/VirtualMatterhornServer.cpp
#${CMAKE_CURRENT_SOURCE_DIR}/src/MatterhornServer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/CommandLineOptions.cpp
)
target_include_directories(matterhornDetectorServer_virtual
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../../slsSupportLib/include
${CMAKE_CURRENT_SOURCE_DIR}/../slsDetectorServer/include
)
target_compile_definitions(matterhornDetectorServer_virtual
PUBLIC VIRTUAL STOP_SERVER #what is this stop server should we really have a generic ServerAPP and pass compile options to create server e.g. MatterHorn?
)
target_link_libraries(matterhornDetectorServer_virtual
PUBLIC
slsSupportStatic
slsDetectorStatic
slsServerStatic
)
set_target_properties(matterhornDetectorServer_virtual PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
install(TARGETS matterhornDetectorServer_virtual
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
@@ -1,113 +0,0 @@
#pragma once
#include "DetectorServer.h"
#include "TCPInterface.h"
#include "communication_funcs.h"
#include "fmt/format.h"
#include "sls/logger.h"
#include "sls/network_utils.h"
#include "sls/sls_detector_defs.h"
#include "sls/versionAPI.h"
#include <array>
#include <cstring>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
namespace sls {
/// @brief Base class for Matterhorn Server, can be used to implement a virtual
/// server for testing and actual server
template <typename DerivedServer>
class BaseMatterhornServer
: public DetectorServer<BaseMatterhornServer<DerivedServer>> {
public:
/**
* Constructor
* Starts up a Matterhorn server.
* Assembles a Matterhorn server using TCP and UDP detector interfaces
* throws an exception in case of failure
* @param port TCP/IP port number
*/
explicit BaseMatterhornServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO)
: DetectorServer<BaseMatterhornServer<DerivedServer>>(port) {}
~BaseMatterhornServer() = default;
ReturnCode get_version(ServerInterface &socket);
ReturnCode get_detector_type(ServerInterface &socket);
ReturnCode initial_checks(ServerInterface &socket);
ReturnCode get_num_udp_interfaces(ServerInterface &socket) const;
/**
* @brief call function corresponding to the function ID received from the
* client and send back the result
* @param function_id the function ID received from the client
* @param socket the socket to send the result back to the client
*/
ReturnCode processFunction(const detFuncs function_id,
ServerInterface &socket);
private:
static std::string getMatterhornServerVersion();
static constexpr uint8_t numUDPInterfaces =
1; // only one udp per module for now
};
template <typename DerivedServer>
ReturnCode
BaseMatterhornServer<DerivedServer>::processFunction(const detFuncs function_id,
ServerInterface &socket) {
switch (function_id) {
default:
throw RuntimeError(
fmt::format("Function {} not implemented",
getFunctionNameFromEnum((enum detFuncs)function_id)));
}
}
template <typename DerivedServer>
ReturnCode BaseMatterhornServer<DerivedServer>::get_num_udp_interfaces(
ServerInterface &socket) const {
return static_cast<ReturnCode>(
socket.sendResult(static_cast<int>(numUDPInterfaces)));
}
template <typename DerivedServer>
ReturnCode
BaseMatterhornServer<DerivedServer>::get_version(ServerInterface &socket) {
auto version = getMatterhornServerVersion();
char version_cstr[MAX_STR_LENGTH]{};
strncpy(version_cstr, version.c_str(), version.size());
LOG(TLogLevel::logDEBUG) << "Matterhorn Server Version: " << version;
return static_cast<ReturnCode>(socket.sendResult(
version_cstr)); // TODO: check what would be possible return codes!!!
}
template <typename DerivedServer>
ReturnCode BaseMatterhornServer<DerivedServer>::get_detector_type(
ServerInterface &socket) {
int detectortype = slsDetectorDefs::detectorType::MATTERHORN;
return static_cast<ReturnCode>(socket.sendResult(detectortype));
}
template <typename DerivedServer>
std::string BaseMatterhornServer<DerivedServer>::getMatterhornServerVersion() {
return APIMATTERHORN;
}
template <typename DerivedServer>
ReturnCode
BaseMatterhornServer<DerivedServer>::initial_checks(ServerInterface &socket) {
return static_cast<DerivedServer *>(this)->initial_checks(socket);
}
} // namespace sls
@@ -1,67 +0,0 @@
#include "MatterhornServer.h"
#include "sls/sls_detector_defs.h"
#include <array>
#include <cstdint>
#include <getopt.h>
#include <string>
namespace sls {
struct DetectorServerOptions {
// TODO: careful changed for other detectors
uint16_t port{DEFAULT_TCP_CNTRL_PORTNO};
/// @brief ignore firmware version compatibility
bool ignoreFirmwareCompatibility{false};
/// @brief safe startup - skip initial detector setup and checks
bool safeStartup{false};
bool versionRequested{false};
bool helpRequested{false};
};
template <typename Server>
struct SpecificDetectorServerOptions : DetectorServerOptions {};
// template specialization
// template <>
// struct SpecificDetectorServerOptions<BaseMatterhornServer> {};
// TODO should be a general server specific class or even shared with
// CommandLIneOptions in Receiver
class CommandLineOptions {
public:
CommandLineOptions() = default;
~CommandLineOptions() = default;
DetectorServerOptions parse(int argc, char *argv[]);
std::string printOptions() const;
private:
std::string getHelpMessage(const std::string &executable) const;
uint16_t parsePort(const char *optarg) const;
void parse_deprecated(const int &opt, char *argv[]);
DetectorServerOptions detectorserveroptions{};
static constexpr std::array<option, 8> options{
{{"help", no_argument, nullptr, 'h'},
{"version", no_argument, nullptr, 'v'},
{"port", required_argument, nullptr, 'p'},
{"ignore_fw_compatibility", no_argument, nullptr,
'f'}, // ignore firmware compatibility check
{"safe_startup", no_argument, nullptr, 's'}, // safe startup
// deprecated options for backward compatibility
{"devel", no_argument, nullptr, 'd'}, // safe_startup mode
{"update", no_argument, nullptr, 'u'}, // firmware compatibility check
{nullptr, 0, nullptr, 0}}};
inline static const char optstring[] = "hvp:fs"
"du"; // second part is deprecated
};
} // namespace sls
@@ -1,27 +0,0 @@
#pragma once
#include "BaseMatterhornServer.h"
#include "TCPInterface.h"
#include "sls/sls_detector_defs.h"
#include <array>
#include <memory>
namespace sls {
class MatterhornServer : public BaseMatterhornServer<MatterhornServer> {
public:
/**
* Constructor
* Starts up a Matterhorn server.
* Assembles a Matterhorn server using TCP and UDP detector interfaces
* throws an exception in case of failure
* @param port TCP/IP port number
*/
explicit MatterhornServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO);
~MatterhornServer() = default;
ReturnCode initial_checks(ServerInterface &socket);
};
} // namespace sls
@@ -1,240 +0,0 @@
// clang-format off
#include "RegisterHelperStructs.hpp"
namespace sls {
/// @brief Enum for IP cores, value are adresses
constexpr enum class IPCore : uint32_t {
MH_RO_SM_AXI = 0, // dummy adresses for now
FHDR_AXI = 1,
AURORA_STATUS = 2,
AURORA_STATUS2 = 3,
PACKETIZERREG = 4,
UNKNOWN = 5
};
// Register definitions
constexpr Register CTRL_Reg{IPCore::UNKNOWN, 0x0};
constexpr Register Status_Reg{IPCore::UNKNOWN, 0x4};
constexpr Register FPGAVersionReg{IPCore::UNKNOWN, 0x8};
constexpr Register FPGA_GIT_HEAD{IPCore::UNKNOWN, 0xc};
constexpr Register FixedPatternReg{IPCore::UNKNOWN, 0x10};
constexpr Register ApiVersionReg{IPCore::UNKNOWN, 0x14};
constexpr Register Chip_ID_Reg{IPCore::UNKNOWN, 0x18};
constexpr Register MH_SM_Ctrl_Reg{IPCore::MH_RO_SM_AXI, 0x0};
constexpr Register MH_SM_Exposure_Reg{IPCore::MH_RO_SM_AXI, 0x4};
constexpr Register MH_SM_Period_Reg{IPCore::MH_RO_SM_AXI, 0x8};
constexpr Register MH_SM_Frames_Reg{IPCore::MH_RO_SM_AXI, 0xc};
constexpr Register MH_SM_StoreLength_Reg{IPCore::MH_RO_SM_AXI, 0x10};
constexpr Register MH_SM_ResetMHLength_Reg{IPCore::MH_RO_SM_AXI, 0x14};
constexpr Register Frame_HDR_Set_Reg{IPCore::FHDR_AXI, 0x0};
constexpr Register Frame_HDR_FrameNumLSB_Reg{IPCore::FHDR_AXI, 0x4};
constexpr Register Frame_HDR_FrameNumMSB_Reg{IPCore::FHDR_AXI, 0x8};
constexpr Register Frame_HDR_TimestampLSB_Reg{IPCore::FHDR_AXI, 0xc};
constexpr Register Frame_HDR_TimestampMSB_Reg{IPCore::FHDR_AXI, 0x10};
constexpr Register Frame_HDR_ModCoord_LSB_Reg{IPCore::FHDR_AXI, 0x14};
constexpr Register Frame_HDR_ModCoord_MSB_Reg{IPCore::FHDR_AXI, 0x18};
constexpr Register Frame_HDR_PktctrMax_Reg{IPCore::FHDR_AXI, 0x1c};
constexpr Register Aurora_Valid_DW_Reg{IPCore::AURORA_STATUS, 0x0};
constexpr Register Aurora_Valid_Bytes_Reg{IPCore::AURORA_STATUS, 0x4};
constexpr Register Aurora_Busy_Up_Cycles_Reg{IPCore::AURORA_STATUS, 0x8};
constexpr Register Aurora_Hard_Errors_Reg{IPCore::AURORA_STATUS, 0xc};
constexpr Register Aurora_Soft_Errors_Reg{IPCore::AURORA_STATUS, 0x10};
constexpr Register Aurora_Channel_n_Lanes_Up_Reg{IPCore::AURORA_STATUS, 0x14};
constexpr Register Aurora_GT_PLL_Lock_Reg{IPCore::AURORA_STATUS2, 0x0};
constexpr Register PktPacketLengthReg{IPCore::PACKETIZERREG, 0xa100};
constexpr Register PktNoPacketsReg{IPCore::PACKETIZERREG, 0xa104};
constexpr Register PktCtrlReg{IPCore::PACKETIZERREG, 0xa108};
constexpr Register PktCoordReg1{IPCore::PACKETIZERREG, 0xa10c};
constexpr Register PktCoordReg2{IPCore::PACKETIZERREG, 0xa110};
// Register fields
constexpr RegisterField Power_VIO{
CTRL_Reg, 0, 0x1};
constexpr RegisterField Power_Vcc_A{
CTRL_Reg, 1, 0x1};
constexpr RegisterField Power_Vcc_B{
CTRL_Reg, 2, 0x1};
constexpr RegisterField Power_Vcc_C{
CTRL_Reg, 3, 0x1};
constexpr RegisterField Power_Vcc_D{
CTRL_Reg, 4, 0x1};
constexpr RegisterField MH_Enable_Enable{
CTRL_Reg, 5, 0x1};
constexpr RegisterField MH_Clk_Enable{
CTRL_Reg, 6, 0x1};
constexpr RegisterField sm_busy{
Status_Reg, 0, 0x1};
constexpr RegisterField FPGACompDate{
FPGAVersionReg, 0, 0xffffff};
constexpr RegisterField FPGADetType{
FPGAVersionReg, 24, 0xff};
constexpr RegisterField FPGA_GIT_HEAD{
FPGA_GIT_HEAD, 0, 0xffffffff};
constexpr RegisterField FixedPattern{
FixedPatternReg, 0, 0xffffffff};
constexpr RegisterField ApiCompDate{
ApiVersionReg, 0, 0xffffff};
constexpr RegisterField ApiDetType{
ApiVersionReg, 24, 0xff};
constexpr RegisterField ChipID{
Chip_ID_Reg, 0, 0x7};
constexpr RegisterField Start_Acquistion{
MH_SM_Ctrl_Reg, 0, 0x1};
constexpr RegisterField Stop_Acquistion{
MH_SM_Ctrl_Reg, 1, 0x1};
constexpr RegisterField MH_Readout_Exposure_Time{
MH_SM_Exposure_Reg, 0, 0xffffffff};
constexpr RegisterField MH_Readout_Period_Time{
MH_SM_Period_Reg, 0, 0xffffffff};
constexpr RegisterField MH_Readout_Frames{
MH_SM_Frames_Reg, 0, 0xffffffff};
constexpr RegisterField MH_SM_StoreLength{
MH_SM_StoreLength_Reg, 0, 0xffffffff};
constexpr RegisterField MH_SM_ResetMHLength{
MH_SM_ResetMHLength_Reg, 0, 0xffffffff};
constexpr RegisterField Frame_Hdr_Set_Framenumber{
Frame_HDR_Set_Reg, 0, 0x1};
constexpr RegisterField Frame_Hdr_Set_Timestamp{
Frame_HDR_Set_Reg, 1, 0x1};
constexpr RegisterField Frame_Hdr_Framenumber_LSB{
Frame_HDR_FrameNumLSB_Reg, 0, 0xffffffff};
constexpr RegisterField Frame_Hdr_Framenumber_MSB{
Frame_HDR_FrameNumMSB_Reg, 0, 0xffffffff};
constexpr RegisterField Frame_Hdr_Timestamp_LSB{
Frame_HDR_TimestampLSB_Reg, 0, 0xffffffff};
constexpr RegisterField Frame_Hdr_Timestamp_MSB{
Frame_HDR_TimestampMSB_Reg, 0, 0xffffffff};
constexpr RegisterField Frame_HDR_ModCoord_LSB{
Frame_HDR_ModCoord_LSB_Reg, 0, 0xffffffff};
constexpr RegisterField Frame_HDR_ModCoord_MSB{
Frame_HDR_ModCoord_MSB_Reg, 0, 0xffffffff};
constexpr RegisterField Frame_HDR_PktctrMax{
Frame_HDR_PktctrMax_Reg, 0, 0xff};
constexpr RegisterField Aurora_Number_Valid_DW{
Aurora_Valid_DW_Reg, 0, 0xffffffff};
constexpr RegisterField Aurora_Valid_Bytes{
Aurora_Valid_Bytes_Reg, 0, 0xffffffff};
constexpr RegisterField Aurora_Busy_Up_Cycles{
Aurora_Busy_Up_Cycles_Reg, 0, 0xffffffff};
constexpr RegisterField Aurora_Hard_Errors{
Aurora_Hard_Errors_Reg, 0, 0xffffffff};
constexpr RegisterField Aurora_Soft_Errors{
Aurora_Soft_Errors_Reg, 0, 0xffffffff};
constexpr RegisterField Aurora_Lanes_Up{
Aurora_Channel_n_Lanes_Up_Reg, 0, 0xf};
constexpr RegisterField Aurora_Channel_Up{
Aurora_Channel_n_Lanes_Up_Reg, 4, 0x1};
constexpr RegisterField Aurora_GT_PLL_Lock{
Aurora_GT_PLL_Lock_Reg, 0, 0x1};
constexpr RegisterField Aurora_GT_PLL_Lock_Counter{
Aurora_GT_PLL_Lock_Reg, 4, 0x1ffffff};
constexpr RegisterField PacketLength1G{
PktPacketLengthReg, 0, 0xffff};
constexpr RegisterField PacketLength10G{
PktPacketLengthReg, 16, 0xffff};
constexpr RegisterField NoPackets1G{
PktNoPacketsReg, 0, 0x3f};
constexpr RegisterField NoPackets10G{
PktNoPacketsReg, 16, 0x3f};
constexpr RegisterField NoServers{
PktCtrlReg, 0, 0x3f};
constexpr RegisterField ServerStart{
PktCtrlReg, 8, 0x1f};
constexpr RegisterField EthInterf{
PktCtrlReg, 16, 0x1};
constexpr RegisterField Coordx{
PktCoordReg1, 0, 0xffff};
constexpr RegisterField Coordy{
PktCoordReg1, 16, 0xffff};
constexpr RegisterField Coordz{
PktCoordReg2, 0, 0xffff};
} // namespace sls
// clang-format on
@@ -1,29 +0,0 @@
#include <cstdint>
#include <string_view>
namespace sls {
enum class IPCore : uint32_t; // forward declaration of IPCore enum class
struct Register {
/// @brief IP core address space
const IPCore ip_core{}; // TODO replace by enum type
/// @brief Offset of the register in bytes from the base address of the IP
/// core
const uint32_t offset_in_bytes{};
};
struct RegisterField {
/// @brief Register to which the field belongs
const Register register_{};
/// @brief Bit position of the least significant bit of the field in the
/// register
const uint32_t bit_position{};
/// @brief Bitmask for the field
const uint32_t bitmask{};
};
} // namespace sls
@@ -1,24 +0,0 @@
#include "BaseMatterhornServer.h"
namespace sls {
class VirtualMatterhornServer
: public BaseMatterhornServer<VirtualMatterhornServer> {
public:
/**
* Constructor
* Starts up a virtual Matterhorn server.
* Assembles a virtual Matterhorn server using TCP and UDP detector
* interfaces throws an exception in case of failure
* @param port TCP/IP port number
*/
explicit VirtualMatterhornServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO);
~VirtualMatterhornServer() = default;
ReturnCode initial_checks(ServerInterface &socket);
};
} // namespace sls
@@ -1,116 +0,0 @@
#include "CommandLineOptions.h"
#include "sls/ToString.h"
#include "sls/sls_detector_exceptions.h"
#include <cstdint>
#include <fmt/format.h>
#include <getopt.h>
#include <iostream>
namespace sls {
uint16_t CommandLineOptions::parsePort(const char *optarg) const {
uint16_t val = 0; // TODO: in c code its unsigned int
try {
val = sls::StringTo<uint16_t>(optarg);
} catch (const std::exception &e) {
throw sls::RuntimeError(fmt::format(
"Could not parse port number {}. {}", optarg, e.what()));
}
if (val < 1024) {
throw sls::RuntimeError(
"Invalid/ privileged port number parsed. Min: 1024.");
}
return val;
}
std::string
CommandLineOptions::getHelpMessage(const std::string &executable) const {
// TODO: update if we keep it Matterhonr specific - refactor a bit better -
// e.g. if compiled with detector macro
std::string helpmessage = fmt::format(
"Usage: {}"
" [arguments]\n"
"Possible arguments are:\n"
"\t-v, --version : Software version\n"
"\t-p, --port <port> : TCP communication port with client. "
"\n"
"\t-s, --safe_startup : Safe startup mode. Skips initial "
"detector setup and checks. \n"
"\t-f, --ignore_fw_compatibility : Ignore firmware compatibility "
"check. \n",
executable);
return helpmessage;
}
void CommandLineOptions::parse_deprecated(const int &opt, char *argv[]) {
switch (opt) {
case 'd':
std::cout << "Warning: -d/--devel option is deprecated. Use "
"-l/--safe_startup instead."
<< std::endl;
detectorserveroptions.safeStartup = true;
break;
case 'u':
std::cout << "Warning: -u/--update option is deprecated. Use "
"-f/--ignore_fw_compatibility instead."
<< std::endl;
detectorserveroptions.ignoreFirmwareCompatibility = true;
break;
default:
throw std::runtime_error(fmt::format("Wrong command line arguments. {}",
getHelpMessage(argv[0])));
}
}
std::string CommandLineOptions::printOptions() const {
std::string msg = "setting up detector server";
if (detectorserveroptions.ignoreFirmwareCompatibility) {
msg += " skipping firmware compatibility checks";
msg += detectorserveroptions.safeStartup ? " and" : "";
}
if (detectorserveroptions.safeStartup) {
msg += " in safe startup mode e.g. skipping any initial detector setup "
"and checks";
}
return msg;
}
DetectorServerOptions CommandLineOptions::parse(int argc, char *argv[]) {
int opt, option_index = 0;
while ((opt = getopt_long(argc, argv, optstring, options.data(),
&option_index)) != -1) {
switch (opt) {
case 'h':
std::cout << getHelpMessage(argv[0]) << std::endl;
detectorserveroptions.helpRequested = true; // to exit in main
break;
case 'v':
detectorserveroptions.versionRequested = true; // to exit in main
break;
case 'p':
detectorserveroptions.port = parsePort(optarg);
break;
case 'f':
detectorserveroptions.ignoreFirmwareCompatibility = true;
break;
case 's':
detectorserveroptions.safeStartup = true;
break;
default:
parse_deprecated(opt, argv); // to handle deprecated options and
// throw error for wrong options
}
}
return detectorserveroptions;
}
} // namespace sls
@@ -1,123 +0,0 @@
#include "CommandLineOptions.h"
#include "VirtualMatterhornServer.h"
#include "sls/logger.h"
#include "sls/sls_detector_exceptions.h"
#include "sls/versionAPI.h"
#include <semaphore.h>
#include <csignal>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
using namespace sls;
pid_t pid = -1;
volatile bool interruption = false;
/**
* Control+C Interrupt Handler
* to let all the other process know to exit properly
*/
void sigInterruptHandler(int signal) {
(void)signal; // suppress unused warning if needed
/*
if (pid > 0) {
kill(pid, SIGTERM); // tell child to exit
}
*/
interruption = true; // tell parent to exit
}
void sigterm_handler(int) { interruption = true; }
// TODO: should be a generic ServerApp for all detectors
int main(int argc, char *argv[]) {
CommandLineOptions cli;
DetectorServerOptions opts{};
try {
opts = cli.parse(argc, argv);
} catch (sls::RuntimeError &e) {
return EXIT_FAILURE;
}
if (opts.versionRequested) {
std::cout << fmt::format("MatterhornServer Version: {}", APIMATTERHORN)
<< std::endl; // might go back to costum CommandLIneOptions
// getVersion
return EXIT_SUCCESS;
}
if (opts.helpRequested) {
return EXIT_SUCCESS;
}
LOG(TLogLevel::logINFOMAGENTA) << cli.printOptions();
// Register Ctrl+C handler
std::signal(SIGINT, sigInterruptHandler);
// handle locally on socket crash
signal(SIGPIPE, SIG_IGN);
// handle locally on socket crash
// sls::setupSignalHandler(SIGPIPE, SIG_IGN); / what is this?
pid = fork(); // fork process for control and stop server
if (pid == 0) {
// Stop server Process
signal(SIGTERM, sigterm_handler);
LOG(TLogLevel::logINFOBLUE) << "Stop Server [" << opts.port + 1 << "]";
try {
VirtualMatterhornServer stopServer(opts.port + 1);
while (!interruption) {
sleep(1);
}
} catch (...) {
LOG(TLogLevel::logINFOBLUE)
<< "Exiting Stop Server [ Tid: " << gettid() << " ]";
// TODO: maybe also terminate the control server !!!!
return EXIT_FAILURE;
}
LOG(TLogLevel::logINFOBLUE)
<< "Exiting Stop Server [ Tid: " << gettid() << " ]";
LOG(sls::logINFO) << "Exiting Stop Server";
return EXIT_SUCCESS;
} else if (pid > 0) {
// parent
// Control Server Process
LOG(TLogLevel::logINFOBLUE) << "Control Server [" << opts.port << "]\n";
try {
VirtualMatterhornServer server(
opts.port); // TODO use virtual if compiled with virtual
// simulators on
while (!interruption) {
sleep(1);
}
} catch (...) {
kill(0, SIGTERM); // tell child to exit
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
return EXIT_FAILURE;
}
waitpid(0, nullptr, 0); // wait for child to exit
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
LOG(sls::logINFO) << "Exiting Detector Server";
} else {
LOG(sls::logERROR)
<< "Failed to fork process for control and stop server";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
@@ -1,24 +0,0 @@
#include "MatterhornServer.h"
namespace sls {
MatterhornServer::MatterhornServer(uint16_t port)
: BaseMatterhornServer<MatterhornServer>(port) {
// TODO: when do i set the udp mac and ip ?
// should maybe be part of the constructor?
tcpInterface->startTCPServer();
// need a function to setup detector - e.g. set all registers etc.
}
ReturnCode MatterhornServer::initial_checks(ServerInterface &socket) {
// TODO: add more checks here, for now just return true to be able to test
// the should check firmware -client compatibility
bool initial_checks_passed = true;
return static_cast<ReturnCode>(socket.sendResult(initial_checks_passed));
}
} // namespace sls
@@ -1,24 +0,0 @@
#include "VirtualMatterhornServer.h"
namespace sls {
VirtualMatterhornServer::VirtualMatterhornServer(uint16_t port)
: BaseMatterhornServer<VirtualMatterhornServer>(port) {
udpDetails[0].srcip = LOCALHOSTIP_INT;
// should maybe be part of the constructor?
tcpInterface->startTCPServer();
// need a function to setup detector - e.g. set all registers etc.
}
ReturnCode VirtualMatterhornServer::initial_checks(ServerInterface &socket) {
// TODO: add more checks here, for now just return true to be able to test
// the should check firmware -client compatibility
bool initial_checks_passed = true;
return static_cast<ReturnCode>(socket.sendResult(initial_checks_passed));
}
} // namespace sls
@@ -1,63 +0,0 @@
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/TCPInterface.cpp
)
add_library(slsServerObject OBJECT
${SOURCES}
)
target_include_directories(slsServerObject PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_link_libraries(slsServerObject
PUBLIC
slsProjectOptions
slsSupportStatic
PRIVATE
slsProjectWarnings
)
set(DETECTOR_LIBRARY_TARGETS slsServerObject)
set(PUBLICHEADERS
${CMAKE_CURRENT_SOURCE_DIR}/include/TCPInterface.h
)
#Shared library
if(SLS_BUILD_SHARED_LIBRARIES)
add_library(slsServerShared SHARED $<TARGET_OBJECTS:slsServerObject>)
target_link_libraries(slsServerShared PUBLIC slsServerObject)
set_target_properties(slsServerShared PROPERTIES
VERSION ${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH}
SOVERSION ${PACKAGE_VERSION_MAJOR}
LIBRARY_OUTPUT_NAME SlsServer
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
PUBLIC_HEADER "${PUBLICHEADERS}"
)
list(APPEND DETECTOR_LIBRARY_TARGETS slsServerShared)
endif(SLS_BUILD_SHARED_LIBRARIES)
#Static library
add_library(slsServerStatic STATIC $<TARGET_OBJECTS:slsServerObject>)
target_link_libraries(slsServerStatic PUBLIC slsServerObject)
set_target_properties(slsServerStatic PROPERTIES
ARCHIVE_OUTPUT_NAME SlsServerStatic
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
PUBLIC_HEADER "${PUBLICHEADERS}"
)
list(APPEND DETECTOR_LIBRARY_TARGETS slsServerStatic)
if((CMAKE_BUILD_TYPE STREQUAL "Release") AND SLS_LTO_AVAILABLE)
set_property(TARGET ${DETECTOR_LIBRARY_TARGETS} PROPERTY INTERPROCEDURAL_OPTIMIZATION True)
endif()
install(TARGETS ${DETECTOR_LIBRARY_TARGETS}
EXPORT "${TARGETS_EXPORT_NAME}"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sls
)
@@ -1,269 +0,0 @@
#pragma once
#include "TCPInterface.h"
#include "communication_funcs.h"
#include "sls/logger.h"
#include "sls/network_utils.h"
#include "sls/sls_detector_defs.h"
#include "sls/versionAPI.h"
#include <array>
#include <cstring>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
namespace sls {
// TODO move to defs?
/// @brief struct saving udp details (one UDP port per module)
struct UDPInfo {
uint16_t srcport{};
uint16_t dstport{};
uint64_t srcmac{};
uint64_t dstmac{};
uint32_t srcip{};
uint32_t dstip{};
};
template <typename DerivedDetectorServer> class DetectorServer {
public:
/**
* Constructor
* Creates a detector server.
* Assembles a detector server using TCP and UDP detector interfaces
* throws an exception in case of failure
* @param port TCP/IP port number
*/
explicit DetectorServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO);
protected:
/// @brief TCP/IP interface for communication with the client
std::unique_ptr<TCPInterface> tcpInterface;
std::array<UDPInfo, 1>
udpDetails{}; // TODO: for now only one receiver per module
/// @brief TODO what is this?
bool updateMode{true};
private:
ReturnCode processFunction(const detFuncs function_id,
ServerInterface &socket);
// TODO dont know what this does?
ReturnCode get_update_mode(ServerInterface &socket) const;
ReturnCode get_source_udp_mac(ServerInterface &socket) const;
ReturnCode set_source_udp_mac(ServerInterface &socket);
ReturnCode get_source_udp_ip(ServerInterface &socket) const;
ReturnCode set_source_udp_ip(ServerInterface &socket);
ReturnCode get_source_udp_port(ServerInterface &socket) const;
ReturnCode set_destination_udp_mac(ServerInterface &socket);
ReturnCode get_destination_udp_mac(ServerInterface &socket) const;
ReturnCode set_destination_udp_ip(ServerInterface &socket);
ReturnCode get_destination_udp_ip(ServerInterface &socket) const;
ReturnCode set_destination_udp_port(ServerInterface &socket);
ReturnCode get_destination_udp_port(ServerInterface &socket) const;
};
template <typename DerivedDetectorServer>
DetectorServer<DerivedDetectorServer>::DetectorServer(uint16_t port) {
validatePortNumber(port);
udpDetails[0].srcport = DEFAULT_UDP_SRC_PORTNO;
udpDetails[0].dstport = DEFAULT_UDP_DST_PORTNO;
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);
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::processFunction(
const detFuncs function_id, ServerInterface &socket) {
switch (function_id) {
case detFuncs::F_GET_SERVER_VERSION:
return static_cast<DerivedDetectorServer *>(this)->get_version(socket);
case detFuncs::F_GET_DETECTOR_TYPE:
return static_cast<DerivedDetectorServer *>(this)->get_detector_type(
socket);
case detFuncs::F_INITIAL_CHECKS:
return static_cast<DerivedDetectorServer *>(this)->initial_checks(
socket);
case detFuncs::F_GET_NUM_INTERFACES:
return static_cast<DerivedDetectorServer *>(this)
->get_num_udp_interfaces(socket);
case detFuncs::F_GET_UPDATE_MODE:
return get_update_mode(socket);
case detFuncs::F_SET_SOURCE_UDP_MAC:
return set_source_udp_mac(socket);
case detFuncs::F_GET_SOURCE_UDP_MAC:
return get_source_udp_mac(socket);
case detFuncs::F_SET_SOURCE_UDP_IP:
return set_source_udp_ip(socket);
case detFuncs::F_GET_SOURCE_UDP_IP:
return get_source_udp_ip(socket);
case detFuncs::F_SET_DEST_UDP_MAC:
return set_destination_udp_mac(socket);
case detFuncs::F_GET_DEST_UDP_MAC:
return get_destination_udp_mac(socket);
case detFuncs::F_SET_DEST_UDP_IP:
return set_destination_udp_ip(socket);
case detFuncs::F_GET_DEST_UDP_IP:
return get_destination_udp_ip(socket);
case detFuncs::F_SET_DEST_UDP_PORT:
return set_destination_udp_port(socket);
case detFuncs::F_GET_DEST_UDP_PORT:
return get_destination_udp_port(socket);
default:
LOG(logDEBUG) << "Checking specific server functions for function ID: "
<< function_id;
// process detector specific functions
static_cast<DerivedDetectorServer *>(this)->processFunction(function_id,
socket);
}
return ReturnCode::FAIL;
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::get_update_mode(
ServerInterface &socket) const {
return static_cast<ReturnCode>(
socket.sendResult(static_cast<int>(updateMode)));
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::set_source_udp_mac(
ServerInterface &socket) {
uint64_t newsrcudpMac;
try {
int ret = socket.Receive<uint64_t>(newsrcudpMac);
} catch (const SocketError &e) {
LOG(logERROR) << "Failed to receive new source UDP MAC address: "
<< e.what();
return ReturnCode::FAIL;
}
udpDetails[0].srcmac = newsrcudpMac;
// TODO: configuremac, check unicast address
return ReturnCode::OK;
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::get_source_udp_mac(
ServerInterface &socket) const {
return static_cast<ReturnCode>(socket.sendResult(udpDetails[0].srcmac));
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::set_source_udp_ip(
ServerInterface &socket) {
uint32_t newSrcIp;
try {
int ret = socket.Receive(newSrcIp);
} catch (const SocketError &e) {
LOG(logERROR) << "Failed to receive new source UDP IP address: "
<< e.what();
return ReturnCode::FAIL;
}
udpDetails[0].srcip = newSrcIp;
return ReturnCode::OK;
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::get_source_udp_ip(
ServerInterface &socket) const {
return static_cast<ReturnCode>(socket.sendResult(udpDetails[0].srcip));
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::set_destination_udp_mac(
ServerInterface &socket) {
uint64_t newDstMac;
try {
int ret = socket.Receive<uint64_t>(newDstMac);
} catch (const SocketError &e) {
LOG(logERROR) << "Failed to receive new destination UDP MAC address: "
<< e.what();
return ReturnCode::FAIL;
}
udpDetails[0].dstmac = newDstMac;
// TODO: configuremac, check unicast address
return ReturnCode::OK;
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::get_destination_udp_mac(
ServerInterface &socket) const {
return static_cast<ReturnCode>(socket.sendResult(udpDetails[0].dstmac));
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::set_destination_udp_ip(
ServerInterface &socket) {
uint32_t newDstIp;
try {
int ret = socket.Receive(newDstIp);
} catch (const SocketError &e) {
LOG(logERROR) << "Failed to receive new destination UDP IP address: "
<< e.what();
return ReturnCode::FAIL;
}
udpDetails[0].dstip = newDstIp;
return ReturnCode::OK;
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::get_destination_udp_ip(
ServerInterface &socket) const {
return static_cast<ReturnCode>(socket.sendResult(udpDetails[0].dstip));
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::set_destination_udp_port(
ServerInterface &socket) {
uint16_t newDstPort;
try {
int ret = socket.Receive(newDstPort);
} catch (const SocketError &e) {
LOG(logERROR) << "Failed to receive new destination UDP port number: "
<< e.what();
return ReturnCode::FAIL;
}
udpDetails[0].dstport = newDstPort;
return ReturnCode::OK;
}
template <typename DerivedDetectorServer>
ReturnCode DetectorServer<DerivedDetectorServer>::get_destination_udp_port(
ServerInterface &socket) const {
return static_cast<ReturnCode>(socket.sendResult(udpDetails[0].dstport));
};
} // namespace sls
@@ -1,60 +0,0 @@
#pragma once
#include "sls/ServerSocket.h"
#include "sls/sls_detector_defs.h"
#include "sls/sls_detector_funcs.h"
#include <atomic>
#include <functional>
#include <thread>
#include <unordered_map>
namespace sls {
/**
* @brief TCPInterface class handles communication and processing of commands
* from Client to Server.
*/
class TCPInterface {
public:
~TCPInterface();
TCPInterface(std::function<ReturnCode(const detFuncs &, ServerInterface &)>
&processFunction_,
const uint16_t portNumber = DEFAULT_TCP_CNTRL_PORTNO);
/// @brief creates tcp thread
void startTCPServer();
std::atomic<bool> killTcpThread{false};
private:
/**
* @brief starts the TCP/IP server to listen for client commands and process
* them
*/
void startTCPServerClientConnection();
/**
* @brief decodes the received command and calls the corresponding function
* @param function_id The ID of the function recived by the server and to
* be executed
*/
ReturnCode processReceivedData(const detFuncs function_id,
ServerInterface &socket);
/// @brief map of function IDs and corresponding functions
std::function<ReturnCode(const detFuncs &, ServerInterface &)>
processFunction;
/// @brief TCP/IP port number for the detector server
uint16_t portNumber{};
/// @brief socket for TCP/IP communication with the client
ServerSocket server;
/// @brief thread for running the TCP/IP server
std::unique_ptr<std::thread> tcpThread;
};
} // namespace sls
@@ -1,92 +0,0 @@
#include "TCPInterface.h"
#include "fmt/format.h"
#include "sls/logger.h"
#include "sls/string_utils.h"
#include <unistd.h>
namespace sls {
TCPInterface::TCPInterface(
std::function<ReturnCode(const detFuncs &, ServerInterface &)>
&processFunction_,
const uint16_t portNumber_)
: processFunction(processFunction_), portNumber(portNumber_),
server(portNumber_) {
// validatePortNumber(portNumber); TODO: where to validate?
}
TCPInterface::~TCPInterface() {
killTcpThread = true; // mmh but if the receiver is stuck in a function,
// this will be of no help
LOG(logINFO) << "Shutting down TCP Socket on port " << portNumber;
server.shutdown();
LOG(logDEBUG) << "TCP Socket closed on port " << portNumber;
tcpThread->join();
}
void TCPInterface::startTCPServer() {
tcpThread = std::make_unique<std::thread>(
&TCPInterface::startTCPServerClientConnection, this);
}
void TCPInterface::startTCPServerClientConnection() {
LOG(logINFO) << "SLS Server starting TCP Server on port " << portNumber
<< '\n';
int function_id{}; // TODO should it be an enum type
while (!killTcpThread) {
try {
auto socket = server.accept();
try {
socket.Receive(function_id);
if (function_id < 0 || function_id >= NUM_DET_FUNCTIONS) {
throw RuntimeError(fmt::format(
"{}:{}", UNRECOGNIZED_FNUM_ENUM,
getFunctionNameFromEnum((enum detFuncs)function_id)));
}
auto returncode = processReceivedData(
static_cast<detFuncs>(function_id), socket);
if (returncode == FAIL) {
throw RuntimeError(fmt::format(
"Error processing command with fnum: {}",
getFunctionNameFromEnum((enum detFuncs)function_id)));
}
} catch (const RuntimeError &e) {
// We had an error needs to be sent to client
char mess[MAX_STR_LENGTH]{};
LOG(logERROR) << "Error processing command: " << e.what();
strcpy_safe(mess, e.what());
socket.Send(slsDetectorDefs::FAIL);
socket.Send(mess);
}
// TODO handle exiting server if tcp command was to exit server
} catch (const RuntimeError &e) {
LOG(logERROR) << "Accept failed: " << e.what();
}
}
LOG(logINFOBLUE) << "Exiting TCP Server";
}
ReturnCode TCPInterface::processReceivedData(const detFuncs function_id,
ServerInterface &socket) {
LOG(logDEBUG1) << "calling function fnum: " << function_id << " ("
<< getFunctionNameFromEnum((enum detFuncs)function_id)
<< ")";
ReturnCode returncode = processFunction(function_id, socket);
LOG(logDEBUG1) << "Function "
<< getFunctionNameFromEnum((enum detFuncs)function_id)
<< " finished";
return returncode;
}
} // namespace sls