mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-07-13 11:21:50 +02:00
wip
This commit is contained in:
@ -7,11 +7,15 @@
|
|||||||
#include "sls/ToString.h"
|
#include "sls/ToString.h"
|
||||||
#include "sls/logger.h"
|
#include "sls/logger.h"
|
||||||
|
|
||||||
|
#define MAX_RECEIVERS 1024
|
||||||
|
|
||||||
ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
|
ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
|
||||||
CommonOptions base;
|
CommonOptions base;
|
||||||
base.port = DEFAULT_TCP_RX_PORTNO;
|
base.port = DEFAULT_TCP_RX_PORTNO;
|
||||||
MultiReceiverOptions multi;
|
MultiReceiverOptions multi;
|
||||||
FrameSyncOptions frame;
|
FrameSyncOptions frame;
|
||||||
|
uint16_t numReceivers = 1;
|
||||||
|
bool optionalArg = false;
|
||||||
|
|
||||||
int opt;
|
int opt;
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
@ -31,6 +35,7 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
|
|||||||
|
|
||||||
static struct option multi_opts[] = {
|
static struct option multi_opts[] = {
|
||||||
{"callback", no_argument, nullptr, 'c'},
|
{"callback", no_argument, nullptr, 'c'},
|
||||||
|
{"rx_tcpport", required_argument, nullptr, 't'},
|
||||||
{"num-receivers", required_argument, nullptr, 'n'},
|
{"num-receivers", required_argument, nullptr, 'n'},
|
||||||
{nullptr, 0, nullptr, 0}
|
{nullptr, 0, nullptr, 0}
|
||||||
};
|
};
|
||||||
@ -44,7 +49,7 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
|
|||||||
std::vector<option> options;
|
std::vector<option> options;
|
||||||
options.insert(options.end(), std::begin(common_opts), std::end(common_opts) - 1);
|
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);
|
options.insert(options.end(), std::begin(single_opts), std::end(single_opts) - 1);
|
||||||
} else if (app == AppType::MultiReceiver) {
|
} else if (app == AppType::MultiReceiver) {
|
||||||
options.insert(options.end(), std::begin(multi_opts), std::end(multi_opts) - 1);
|
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";
|
std::string optstring = "vp:u:h";
|
||||||
if (app == AppType::SingleReceiver) {
|
if (app == AppType::SingleReceiver) {
|
||||||
optstring += "t:";
|
optstring += "t:";
|
||||||
}
|
} else if (app == AppType::MultiReceiver) {
|
||||||
if (app == AppType::MultiReceiver || app == AppType::FrameSynchronizer) {
|
optstring += "cn:t:";
|
||||||
|
} else if (app == AppType::FrameSynchronizer) {
|
||||||
optstring += "cn:";
|
optstring += "cn:";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,17 +103,28 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'n':
|
case 'n':
|
||||||
if (app == AppType::MultiReceiver)
|
try {
|
||||||
multi.numReceivers = sls::StringTo<uint16_t>(optarg);
|
if (app == AppType::MultiReceiver)
|
||||||
else if (app == AppType::FrameSynchronizer)
|
multi.numReceivers = sls::StringTo<uint16_t>(optarg);
|
||||||
frame.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;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
if (app == AppType::MultiReceiver)
|
optionalArg = true;
|
||||||
|
if (app == AppType::MultiReceiver) {
|
||||||
multi.callbackEnabled = true;
|
multi.callbackEnabled = true;
|
||||||
else if (app == AppType::FrameSynchronizer)
|
} else if (app == AppType::FrameSynchronizer) {
|
||||||
frame.printHeaders = true;
|
frame.printHeaders = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -116,15 +133,31 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
// remaining arguments
|
// remaining arguments
|
||||||
if (optind < argc) {
|
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) {
|
switch (app) {
|
||||||
case AppType::SingleReceiver: return base;
|
case AppType::SingleReceiver:
|
||||||
|
return base;
|
||||||
case AppType::MultiReceiver:
|
case AppType::MultiReceiver:
|
||||||
|
LOG(sls::logINFO) << "Call back enable: " << multi.callbackEnabled;
|
||||||
static_cast<CommonOptions&>(multi) = base;
|
static_cast<CommonOptions&>(multi) = base;
|
||||||
return multi;
|
return multi;
|
||||||
case AppType::FrameSynchronizer:
|
case AppType::FrameSynchronizer:
|
||||||
|
LOG(sls::logINFO) << "Print headers: " << frame.printHeaders;
|
||||||
static_cast<CommonOptions&>(frame) = base;
|
static_cast<CommonOptions&>(frame) = base;
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
@ -132,6 +165,41 @@ ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]) {
|
|||||||
throw std::logic_error("Unknown AppType");
|
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) {
|
void setEffectiveUID(uid_t uid) {
|
||||||
if (geteuid() == uid) {
|
if (geteuid() == uid) {
|
||||||
LOG(sls::logINFO)
|
LOG(sls::logINFO)
|
||||||
|
@ -36,7 +36,9 @@ struct FrameSyncOptions : CommonOptions {
|
|||||||
using ParsedOptions = std::variant<CommonOptions, MultiReceiverOptions, FrameSyncOptions>;
|
using ParsedOptions = std::variant<CommonOptions, MultiReceiverOptions, FrameSyncOptions>;
|
||||||
|
|
||||||
ParsedOptions parseCommandLine(AppType app, int argc, char* argv[]);
|
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);
|
void setEffectiveUID(uid_t uid);
|
||||||
|
std::string getTypeString(const AppType app);
|
||||||
std::string getVersion(AppType app);
|
std::string getVersion(AppType app);
|
||||||
std::string getHelpMessage(AppType app);
|
std::string getHelpMessage(AppType app);
|
||||||
|
|
||||||
|
@ -10,13 +10,11 @@
|
|||||||
#include "sls/container_utils.h"
|
#include "sls/container_utils.h"
|
||||||
#include "sls/logger.h"
|
#include "sls/logger.h"
|
||||||
#include "sls/sls_detector_defs.h"
|
#include "sls/sls_detector_defs.h"
|
||||||
#include "sls/versionAPI.h"
|
#include "CommandLineOptions.h"
|
||||||
|
|
||||||
#include <csignal> //SIGINT
|
#include <csignal> //SIGINT
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <getopt.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
@ -501,123 +499,14 @@ void GetDataCallback(slsDetectorDefs::sls_receiver_header &header,
|
|||||||
sem_post(&stat->available);
|
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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
uint16_t startPort = DEFAULT_TCP_RX_PORTNO;
|
auto opts = parseCommandLine(AppType::MultiReceiver, argc, argv);
|
||||||
uint16_t numReceivers = 1;
|
auto& o = std::get<CommonOptions>(opts);
|
||||||
bool printHeaders = false;
|
if (o.versionRequested || o.helpRequested) {
|
||||||
uid_t userid = -1;
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
|
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 */
|
/** - Catch signal SIGINT to close files and call destructors properly */
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
@ -641,7 +530,8 @@ int main(int argc, char *argv[]) {
|
|||||||
cprintf(RED, "Could not set handler function for SIGPIPE\n");
|
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
|
// store pointer for signal handler
|
||||||
global_frame_status = &stat;
|
global_frame_status = &stat;
|
||||||
|
|
||||||
@ -649,12 +539,12 @@ int main(int argc, char *argv[]) {
|
|||||||
void *user_data = static_cast<void *>(&stat);
|
void *user_data = static_cast<void *>(&stat);
|
||||||
std::thread combinerThread(Correlate, &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_t *semaphore = new sem_t;
|
||||||
sem_init(semaphore, 1, 0);
|
sem_init(semaphore, 1, 0);
|
||||||
semaphores.push_back(semaphore);
|
semaphores.push_back(semaphore);
|
||||||
|
|
||||||
uint16_t port = startPort + i;
|
uint16_t port = o.port + i;
|
||||||
threads.emplace_back([i, semaphore, port, user_data]() {
|
threads.emplace_back([i, semaphore, port, user_data]() {
|
||||||
sls::Receiver receiver(port);
|
sls::Receiver receiver(port);
|
||||||
receiver.registerCallBackStartAcquisition(StartAcquisitionCallback,
|
receiver.registerCallBackStartAcquisition(StartAcquisitionCallback,
|
||||||
|
@ -7,13 +7,10 @@
|
|||||||
#include "sls/container_utils.h"
|
#include "sls/container_utils.h"
|
||||||
#include "sls/logger.h"
|
#include "sls/logger.h"
|
||||||
#include "sls/sls_detector_defs.h"
|
#include "sls/sls_detector_defs.h"
|
||||||
#include "sls/sls_detector_exceptions.h"
|
#include "CommandLineOptions.h"
|
||||||
#include "sls/versionAPI.h"
|
|
||||||
|
|
||||||
#include <csignal> //SIGINT
|
#include <csignal> //SIGINT
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <getopt.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
#include <sys/wait.h> //wait
|
#include <sys/wait.h> //wait
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -140,171 +137,17 @@ void GetData(slsDetectorDefs::sls_receiver_header &header,
|
|||||||
*/
|
*/
|
||||||
void sigInterruptHandler(int p) { sem_post(&semaphore); }
|
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[]) {
|
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();
|
auto opts = parseCommandLine(AppType::MultiReceiver, argc, argv);
|
||||||
|
auto& o = std::get<CommonOptions>(opts);
|
||||||
static struct option long_options[] = {
|
if (o.versionRequested || o.helpRequested) {
|
||||||
{"version", no_argument, nullptr, 'v'},
|
return EXIT_SUCCESS;
|
||||||
{"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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
|
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 */
|
/** - Catch signal SIGINT to close files and call destructors properly */
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
@ -330,7 +173,8 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
/** - loop over number of receivers */
|
/** - loop over number of receivers */
|
||||||
sem_init(&semaphore, 1, 0);
|
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 */
|
/** - fork process to create child process */
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
@ -349,13 +193,13 @@ int main(int argc, char *argv[]) {
|
|||||||
<< "Child process " << i << " [ Tid: " << gettid() << ']';
|
<< "Child process " << i << " [ Tid: " << gettid() << ']';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
uint16_t port = startPort + i;
|
uint16_t port = o.port + i;
|
||||||
sls::Receiver receiver(port);
|
sls::Receiver receiver(port);
|
||||||
|
|
||||||
/** - register callbacks. remember to set file write enable
|
/** - register callbacks. remember to set file write enable
|
||||||
* to 0 (using the client) if we should not write files and you
|
* to 0 (using the client) if we should not write files and you
|
||||||
* will write data using the callbacks */
|
* will write data using the callbacks */
|
||||||
if (callbackEnabled) {
|
if (m.callbackEnabled) {
|
||||||
|
|
||||||
/** - Call back for start acquisition */
|
/** - Call back for start acquisition */
|
||||||
LOG(sls::logINFOBLUE) << "Registering StartAcq()";
|
LOG(sls::logINFOBLUE) << "Registering StartAcq()";
|
||||||
|
@ -6,11 +6,9 @@
|
|||||||
#include "sls/container_utils.h"
|
#include "sls/container_utils.h"
|
||||||
#include "sls/logger.h"
|
#include "sls/logger.h"
|
||||||
#include "sls/sls_detector_defs.h"
|
#include "sls/sls_detector_defs.h"
|
||||||
#include "sls/versionAPI.h"
|
|
||||||
#include "CommandLineOptions.h"
|
#include "CommandLineOptions.h"
|
||||||
|
|
||||||
#include <csignal> //SIGINT
|
#include <csignal> //SIGINT
|
||||||
#include <getopt.h>
|
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@ -33,8 +31,6 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]";
|
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]";
|
||||||
LOG(sls::logINFO) << "Port: " << o.port;
|
|
||||||
|
|
||||||
|
|
||||||
// Catch signal SIGINT to close files and call destructors properly
|
// Catch signal SIGINT to close files and call destructors properly
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
Reference in New Issue
Block a user