This commit is contained in:
2025-07-07 00:11:01 +02:00
parent e0810d973d
commit 4ff29161d4
5 changed files with 99 additions and 299 deletions

View File

@ -7,11 +7,15 @@
#include "sls/ToString.h"
#include "sls/logger.h"
#define MAX_RECEIVERS 1024
ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
CommonOptions base;
base.port = DEFAULT_TCP_RX_PORTNO;
MultiReceiverOptions multi;
FrameSyncOptions frame;
uint16_t numReceivers = 1;
bool optionalArg = false;
int opt;
int option_index = 0;
@ -31,6 +35,7 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
static struct option multi_opts[] = {
{"callback", no_argument, nullptr, 'c'},
{"rx_tcpport", required_argument, nullptr, 't'},
{"num-receivers", required_argument, nullptr, 'n'},
{nullptr, 0, nullptr, 0}
};
@ -44,7 +49,7 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
std::vector<option> options;
options.insert(options.end(), std::begin(common_opts), std::end(common_opts) - 1);
if (app == AppType::SingleReceiver) {
if (app == AppType::SingleReceiver) {
options.insert(options.end(), std::begin(single_opts), std::end(single_opts) - 1);
} else if (app == AppType::MultiReceiver) {
options.insert(options.end(), std::begin(multi_opts), std::end(multi_opts) - 1);
@ -55,8 +60,9 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
std::string optstring = "vp:u:h";
if (app == AppType::SingleReceiver) {
optstring += "t:";
}
if (app == AppType::MultiReceiver || app == AppType::FrameSynchronizer) {
} else if (app == AppType::MultiReceiver) {
optstring += "cn:t:";
} else if (app == AppType::FrameSynchronizer) {
optstring += "cn:";
}
@ -97,17 +103,28 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
break;
case 'n':
if (app == AppType::MultiReceiver)
multi.numReceivers = sls::StringTo<uint16_t>(optarg);
else if (app == AppType::FrameSynchronizer)
frame.numReceivers = sls::StringTo<uint16_t>(optarg);
try {
if (app == AppType::MultiReceiver)
multi.numReceivers = sls::StringTo<uint16_t>(optarg);
else if (app == AppType::FrameSynchronizer)
frame.numReceivers = sls::StringTo<uint16_t>(optarg);
if (numReceivers < 0 || numReceivers > MAX_RECEIVERS) {
throw sls::RuntimeError("Invalid number of receivers. Max: " + std::to_string(MAX_RECEIVERS));
}
multi.numReceivers = numReceivers;
frame.numReceivers = numReceivers;
} catch (...) {
throw sls::RuntimeError("Invalid number of receivers parsed." + std::to_string(numReceivers));
}
break;
case 'c':
if (app == AppType::MultiReceiver)
optionalArg = true;
if (app == AppType::MultiReceiver) {
multi.callbackEnabled = true;
else if (app == AppType::FrameSynchronizer)
} else if (app == AppType::FrameSynchronizer) {
frame.printHeaders = true;
}
break;
default:
@ -116,15 +133,31 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
}
// remaining arguments
if (optind < argc) {
throw(sls::RuntimeError(std::string("Invalid arguments\n") + getHelpMessage(app)));
// maintain backward compatibility of [start port] [num receivers] [optional arg] ( for multi receiver and frame synchronizer )
if (app != AppType::SingleReceiver && slsDetectorDefs::OK == GetDeprecatedCommandLineOptions(argc, argv, base.port, numReceivers, optionalArg)) {
if (app == AppType::MultiReceiver) {
multi.numReceivers = numReceivers;
multi.callbackEnabled = optionalArg;
} else if (app == AppType::FrameSynchronizer) {
frame.numReceivers = numReceivers;
frame.printHeaders = optionalArg;
}
} else {
throw sls::RuntimeError("Invalid arguments." + getHelpMessage(app));
}
}
LOG(sls::logINFO) << "Number of receivers: " << numReceivers;
LOG(sls::logINFO) << "TCP Port: " << base.port;
switch (app) {
case AppType::SingleReceiver: return base;
case AppType::SingleReceiver:
return base;
case AppType::MultiReceiver:
LOG(sls::logINFO) << "Call back enable: " << multi.callbackEnabled;
static_cast<CommonOptions&>(multi) = base;
return multi;
case AppType::FrameSynchronizer:
LOG(sls::logINFO) << "Print headers: " << frame.printHeaders;
static_cast<CommonOptions&>(frame) = base;
return frame;
}
@ -132,6 +165,41 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
throw std::logic_error("Unknown AppType");
}
int GetDeprecatedCommandLineOptions(int argc, char *argv[], uint16_t &startPort, uint16_t &numReceivers, bool &optionalArg) {
std::string deprecatedMessage =
"Detected deprecated Options. Please update.\n";
if (argc > 1) {
try {
if (argc == 3 || argc == 4) {
startPort = sls::StringTo<uint16_t>(argv[1]);
numReceivers = sls::StringTo<uint16_t>(argv[2]);
if (numReceivers > MAX_RECEIVERS) {
LOG(sls::logWARNING) << deprecatedMessage;
LOG(sls::logERROR)
<< "Did you mix up the order of the arguments? Max "
"number of recievers: " << MAX_RECEIVERS;
return slsDetectorDefs::FAIL;
}
if (numReceivers == 0) {
LOG(sls::logWARNING) << deprecatedMessage;
LOG(sls::logERROR) << "Invalid number of receivers. Options: 1 - " << MAX_RECEIVERS;
return slsDetectorDefs::FAIL;
}
if (argc == 4) {
optionalArg = sls::StringTo<bool>(argv[3]);
}
} else
throw std::runtime_error("Invalid number of arguments");
} catch (const std::exception &e) {
LOG(sls::logWARNING) << deprecatedMessage;
LOG(sls::logERROR) << e.what();
return slsDetectorDefs::FAIL;
}
}
return slsDetectorDefs::OK;
}
void setEffectiveUID(uid_t uid) {
if (geteuid() == uid) {
LOG(sls::logINFO)

View File

@ -36,7 +36,9 @@ struct FrameSyncOptions : CommonOptions {
using ParsedOptions = std::variant<CommonOptions, MultiReceiverOptions, FrameSyncOptions>;
ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]);
int GetDeprecatedCommandLineOptions(int argc, char *argv[], uint16_t &startPort, uint16_t &numReceivers, bool &optionalArg);
void setEffectiveUID(uid_t uid);
std::string getTypeString(const AppType app);
std::string getVersion(AppType app);
std::string getHelpMessage(AppType app);

View File

@ -10,13 +10,11 @@
#include "sls/container_utils.h"
#include "sls/logger.h"
#include "sls/sls_detector_defs.h"
#include "sls/versionAPI.h"
#include "CommandLineOptions.h"
#include <csignal> //SIGINT
#include <cstdio>
#include <cstring>
#include <getopt.h>
#include <iostream>
#include <mutex>
#include <ostream>
#include <semaphore.h>
@ -501,123 +499,14 @@ void GetDataCallback(slsDetectorDefs::sls_receiver_header &header,
sem_post(&stat->available);
}
/**
* Example of main program using the Receiver class
*
* - Defines in file for:
* - Default Number of receivers is 1
* - Default Start TCP port is 1954
*/
int main(int argc, char *argv[]) {
uint16_t startPort = DEFAULT_TCP_RX_PORTNO;
uint16_t numReceivers = 1;
bool printHeaders = false;
uid_t userid = -1;
std::string help_message =
"\nUsage: " + std::string(argv[0]) + " Options:\n" +
"\t-v, --version : Version of " + std::string(argv[0]) + ".\n" +
"\t-n, --num-receivers : Number of receivers.\n" +
"\t-p, --port : TCP port to communicate with client for "
"configuration. Non-zero and 16 bit.\n" +
"\t-c, --print-headers : Print callback headers for debugging. "
"Disabled by default.\n" +
"\t-u, --uid : Set effective user id if receiver started "
"with privileges. \n\n";
static struct option long_options[] = {
{"version", no_argument, nullptr, 'v'},
{"num-receivers", required_argument, nullptr, 'n'},
{"port", required_argument, nullptr, 'p'},
{"print-headers", no_argument, nullptr, 'c'},
{"uid", required_argument, nullptr, 'u'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}};
int option_index = 0;
int opt = 0;
while (-1 != (opt = getopt_long(argc, argv, "vn:p:cu:h", long_options,
&option_index))) {
switch (opt) {
case 'v':
std::cout << argv[0] << " Version: " << APIRECEIVER << std::endl;
return (EXIT_SUCCESS);
case 'n':
try {
numReceivers = sls::StringTo<uint16_t>(optarg);
if (numReceivers == 0 || numReceivers > 100) {
throw std::runtime_error("Invalid argument.");
}
} catch (...) {
throw sls::RuntimeError(
"Invalid number of receivers. Max: 100." + help_message);
}
break;
case 'p':
try {
startPort = sls::StringTo<uint16_t>(optarg);
} catch (...) {
throw sls::RuntimeError("Could not scan port number." +
help_message);
}
break;
case 'c':
printHeaders = true;
break;
case 'u':
try {
userid = sls::StringTo<uint32_t>(optarg);
} catch (...) {
throw sls::RuntimeError("Invalid uid." + help_message);
}
break;
case 'h':
std::cout << help_message << std::endl;
return (EXIT_SUCCESS);
default:
LOG(sls::logERROR) << help_message;
return (EXIT_FAILURE);
}
}
// remaining arguments
if (optind < argc) {
LOG(sls::logERROR) << "Invalid arguments\n" << help_message;
return (EXIT_FAILURE);
auto opts = parseCommandLine(AppType::MultiReceiver, argc, argv);
auto& o = std::get<CommonOptions>(opts);
if (o.versionRequested || o.helpRequested) {
return EXIT_SUCCESS;
}
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
LOG(sls::logINFO) << "Number of Receivers: " << numReceivers;
LOG(sls::logINFO) << "Start TCP Port: " << startPort;
LOG(sls::logINFO) << "Print Callback Headers: " << printHeaders;
// set effective id if provided
if (userid != static_cast<uid_t>(-1)) {
if (geteuid() == userid) {
LOG(sls::logINFO)
<< "Process already has the same Effective UID " << userid;
} else {
if (seteuid(userid) != 0) {
std::ostringstream oss;
oss << "Could not set Effective UID to " << userid;
throw sls::RuntimeError(oss.str());
}
if (geteuid() != userid) {
std::ostringstream oss;
oss << "Could not set Effective UID to " << userid << ". Got "
<< geteuid();
throw sls::RuntimeError(oss.str());
}
LOG(sls::logINFO) << "Process Effective UID changed to " << userid;
}
}
/** - Catch signal SIGINT to close files and call destructors properly */
struct sigaction sa;
@ -641,7 +530,8 @@ int main(int argc, char *argv[]) {
cprintf(RED, "Could not set handler function for SIGPIPE\n");
}
FrameStatus stat{true, false, numReceivers};
auto& f = std::get<FrameSyncOptions>(opts);
FrameStatus stat{true, false, f.numReceivers};
// store pointer for signal handler
global_frame_status = &stat;
@ -649,12 +539,12 @@ int main(int argc, char *argv[]) {
void *user_data = static_cast<void *>(&stat);
std::thread combinerThread(Correlate, &stat);
for (int i = 0; i != numReceivers; ++i) {
for (int i = 0; i != f.numReceivers; ++i) {
sem_t *semaphore = new sem_t;
sem_init(semaphore, 1, 0);
semaphores.push_back(semaphore);
uint16_t port = startPort + i;
uint16_t port = o.port + i;
threads.emplace_back([i, semaphore, port, user_data]() {
sls::Receiver receiver(port);
receiver.registerCallBackStartAcquisition(StartAcquisitionCallback,

View File

@ -7,13 +7,10 @@
#include "sls/container_utils.h"
#include "sls/logger.h"
#include "sls/sls_detector_defs.h"
#include "sls/sls_detector_exceptions.h"
#include "sls/versionAPI.h"
#include "CommandLineOptions.h"
#include <csignal> //SIGINT
#include <cstring>
#include <getopt.h>
#include <iostream>
#include <semaphore.h>
#include <sys/wait.h> //wait
#include <unistd.h>
@ -140,171 +137,17 @@ void GetData(slsDetectorDefs::sls_receiver_header &header,
*/
void sigInterruptHandler(int p) { sem_post(&semaphore); }
int GetDeprecatedCommandLineOptions(int argc, char *argv[], uint16_t &startPort,
uint16_t &numReceivers,
bool &callbackEnabled) {
std::string deprecatedMessage =
"Detected deprecated Options. Please update.\n";
if (argc > 1) {
try {
if (argc == 3 || argc == 4) {
startPort = sls::StringTo<uint16_t>(argv[1]);
numReceivers = sls::StringTo<uint16_t>(argv[2]);
if (numReceivers > 100) {
LOG(sls::logWARNING) << deprecatedMessage;
LOG(sls::logERROR)
<< "Did you mix up the order of the arguments? Max "
"number of recievers: 100";
return slsDetectorDefs::FAIL;
}
if (numReceivers == 0) {
LOG(sls::logWARNING) << deprecatedMessage;
LOG(sls::logERROR) << "Invalid number of receivers.";
return slsDetectorDefs::FAIL;
}
if (argc == 4) {
callbackEnabled = sls::StringTo<bool>(argv[3]);
}
} else
throw std::runtime_error("Invalid number of arguments");
} catch (const std::exception &e) {
LOG(sls::logWARNING) << deprecatedMessage;
LOG(sls::logERROR) << e.what();
return slsDetectorDefs::FAIL;
}
}
return slsDetectorDefs::OK;
}
std::string getHelpMessage() {
std::string name = "slsMultiReceiver";
return "\nUsage: " + name + " Options:\n" +
"\t-v, --version : Version of " + name + ".\n" +
"\t-n, --num-receivers : Number of receivers.\n" +
"\t-p, --port : TCP port to communicate with client for "
"configuration. Non-zero and 16 bit.\n" +
"\t-c, --callback : Enable dummy callbacks for debugging. "
"Disabled by default. \n" +
"\t-u, --uid : Set effective user id if receiver started "
"with privileges. \n\n";
}
/**
* Example of main program using the Receiver class
*
* - Defines in file for:
* - Default Number of receivers is 1
* - Default Start TCP port is 1954
*/
int main(int argc, char *argv[]) {
uint16_t startPort = DEFAULT_TCP_RX_PORTNO;
uint16_t numReceivers = 1;
bool callbackEnabled = false;
uid_t userid = -1;
std::string help_message = getHelpMessage();
static struct option long_options[] = {
{"version", no_argument, nullptr, 'v'},
{"num-receivers", required_argument, nullptr, 'n'},
{"rx_tcpport", required_argument, nullptr, 't'},
{"port", required_argument, nullptr, 'p'},
{"callback", no_argument, nullptr, 'c'},
{"uid", required_argument, nullptr, 'u'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}};
int option_index = 0;
int opt = 0;
while (-1 != (opt = getopt_long(argc, argv, "vn:t:p:cu:h", long_options,
&option_index))) {
switch (opt) {
case 'v':
std::cout << argv[0] << " Version: " << APIRECEIVER << std::endl;
return EXIT_SUCCESS;
case 'n':
try {
numReceivers = sls::StringTo<uint16_t>(optarg);
if (numReceivers == 0 || numReceivers > 100) {
throw std::runtime_error("Invalid argument.");
}
} catch (...) {
throw sls::RuntimeError("Invalid number of receivers." +
help_message);
}
break;
case 't':
LOG(sls::logWARNING)
<< "Deprecated option. Please use 'p' or '--port'.";
[[fallthrough]];
case 'p':
try {
startPort = sls::StringTo<uint16_t>(optarg);
} catch (...) {
throw sls::RuntimeError("Could not scan port number." +
help_message);
}
break;
case 'c':
callbackEnabled = true;
break;
case 'u':
try {
userid = sls::StringTo<uint32_t>(optarg);
} catch (...) {
throw sls::RuntimeError("Invalid uid." + help_message);
}
break;
case 'h':
std::cout << help_message << std::endl;
return EXIT_SUCCESS;
default:
LOG(sls::logERROR) << help_message;
return EXIT_FAILURE;
}
}
// if remaining arguments, maintain backward compatibility of [startport]
// [num-receivers] [callback]
if (optind < argc) {
if (slsDetectorDefs::FAIL ==
GetDeprecatedCommandLineOptions(argc, argv, startPort, numReceivers,
callbackEnabled))
return EXIT_FAILURE;
auto opts = parseCommandLine(AppType::MultiReceiver, argc, argv);
auto& o = std::get<CommonOptions>(opts);
if (o.versionRequested || o.helpRequested) {
return EXIT_SUCCESS;
}
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
LOG(sls::logINFO) << "Number of Receivers: " << numReceivers;
LOG(sls::logINFO) << "Start TCP Port: " << startPort;
LOG(sls::logINFO) << "Callback Enable: " << callbackEnabled;
// set effective id if provided
if (userid != static_cast<uid_t>(-1)) {
if (geteuid() == userid) {
LOG(sls::logINFO)
<< "Process already has the same Effective UID " << userid;
} else {
if (seteuid(userid) != 0) {
std::ostringstream oss;
oss << "Could not set Effective UID to " << userid;
throw sls::RuntimeError(oss.str());
}
if (geteuid() != userid) {
std::ostringstream oss;
oss << "Could not set Effective UID to " << userid << ". Got "
<< geteuid();
throw sls::RuntimeError(oss.str());
}
LOG(sls::logINFO) << "Process Effective UID changed to " << userid;
}
}
/** - Catch signal SIGINT to close files and call destructors properly */
struct sigaction sa;
@ -330,7 +173,8 @@ int main(int argc, char *argv[]) {
/** - loop over number of receivers */
sem_init(&semaphore, 1, 0);
for (int i = 0; i < numReceivers; ++i) {
auto& m = std::get<MultiReceiverOptions>(opts);
for (int i = 0; i < m.numReceivers; ++i) {
/** - fork process to create child process */
pid_t pid = fork();
@ -349,13 +193,13 @@ int main(int argc, char *argv[]) {
<< "Child process " << i << " [ Tid: " << gettid() << ']';
try {
uint16_t port = startPort + i;
uint16_t port = o.port + i;
sls::Receiver receiver(port);
/** - register callbacks. remember to set file write enable
* to 0 (using the client) if we should not write files and you
* will write data using the callbacks */
if (callbackEnabled) {
if (m.callbackEnabled) {
/** - Call back for start acquisition */
LOG(sls::logINFOBLUE) << "Registering StartAcq()";

View File

@ -6,11 +6,9 @@
#include "sls/container_utils.h"
#include "sls/logger.h"
#include "sls/sls_detector_defs.h"
#include "sls/versionAPI.h"
#include "CommandLineOptions.h"
#include <csignal> //SIGINT
#include <getopt.h>
#include <semaphore.h>
#include <unistd.h>
@ -33,8 +31,6 @@ int main(int argc, char *argv[]) {
}
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]";
LOG(sls::logINFO) << "Port: " << o.port;
// Catch signal SIGINT to close files and call destructors properly
struct sigaction sa;