mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-21 03:10:02 +02:00
updated for framesynchronizer and added versions too
This commit is contained in:
parent
7844216812
commit
c1406efec6
@ -15,6 +15,7 @@
|
|||||||
#include <csignal> //SIGINT
|
#include <csignal> //SIGINT
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <getopt.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
@ -507,61 +508,108 @@ void GetDataCallback(slsDetectorDefs::sls_receiver_header &header,
|
|||||||
* - Default Start TCP port is 1954
|
* - 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;
|
||||||
// version
|
uint16_t numReceivers = 1;
|
||||||
if (argc == 2) {
|
|
||||||
std::string sargv1 = std::string(argv[1]);
|
|
||||||
if (sargv1 == "--version" || sargv1 == "-v") {
|
|
||||||
std::cout << "slsFrameSynchronizer Version: " << APIRECEIVER
|
|
||||||
<< std::endl;
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** - set default values */
|
|
||||||
int numReceivers = 1;
|
|
||||||
uint16_t startTCPPort = DEFAULT_TCP_RX_PORTNO;
|
|
||||||
bool printHeaders = false;
|
bool printHeaders = false;
|
||||||
|
uid_t userid = -1;
|
||||||
|
|
||||||
/** - get number of receivers and start tcp port from command line
|
std::string help_message =
|
||||||
* arguments */
|
"\nUsage: " + std::string(argv[0]) + " Options:\n" +
|
||||||
if (argc > 1) {
|
"\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;
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
|
case 'n':
|
||||||
try {
|
try {
|
||||||
if (argc == 3 || argc == 4) {
|
numReceivers = sls::StringTo<uint16_t>(optarg);
|
||||||
startTCPPort = sls::StringTo<uint16_t>(argv[1]);
|
if (numReceivers == 0 || numReceivers > 100) {
|
||||||
if (startTCPPort == 0) {
|
throw std::runtime_error("Invalid argument.");
|
||||||
throw std::runtime_error("Invalid start tcp port");
|
|
||||||
}
|
}
|
||||||
numReceivers = std::stoi(argv[2]);
|
} catch (...) {
|
||||||
if (numReceivers > 1024) {
|
throw sls::RuntimeError("Invalid number of receivers." +
|
||||||
cprintf(RED,
|
help_message);
|
||||||
"Did you mix up the order of the arguments?\n%s\n",
|
|
||||||
getHelpMessage().c_str());
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
if (numReceivers == 0) {
|
break;
|
||||||
cprintf(RED, "Invalid number of receivers.\n%s\n",
|
|
||||||
getHelpMessage().c_str());
|
case 'p':
|
||||||
return EXIT_FAILURE;
|
try {
|
||||||
|
startPort = sls::StringTo<uint16_t>(optarg);
|
||||||
|
} catch (...) {
|
||||||
|
throw sls::RuntimeError("Could not scan port number." +
|
||||||
|
help_message);
|
||||||
}
|
}
|
||||||
if (argc == 4) {
|
break;
|
||||||
printHeaders = sls::StringTo<bool>(argv[3]);
|
|
||||||
if (printHeaders) {
|
case 'c':
|
||||||
printHeadersLevel = sls::logINFOBLUE;
|
printHeaders = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'u':
|
||||||
|
try {
|
||||||
|
userid = sls::StringTo<uint32_t>(optarg);
|
||||||
|
} catch (...) {
|
||||||
|
throw sls::RuntimeError("Invalid uid." + help_message);
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
} else
|
|
||||||
throw std::runtime_error("Invalid number of arguments");
|
case 'h':
|
||||||
} catch (const std::exception &e) {
|
std::cout << help_message << std::endl;
|
||||||
cprintf(RED, "Error: %s\n%s\n", e.what(), getHelpMessage().c_str());
|
exit(EXIT_SUCCESS);
|
||||||
return EXIT_FAILURE;
|
default:
|
||||||
|
throw sls::RuntimeError(help_message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cprintf(RESET, "Number of Receivers: %d\n", numReceivers);
|
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
|
||||||
cprintf(RESET, "Start TCP Port: %hu\n", startTCPPort);
|
LOG(sls::logINFO) << "Number of Receivers: " << numReceivers;
|
||||||
cprintf(RESET, "Print Callback Headers: %s\n\n",
|
LOG(sls::logINFO) << "Start TCP Port: " << startPort;
|
||||||
(printHeaders ? "Enabled" : "Disabled"));
|
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;
|
||||||
@ -598,7 +646,7 @@ int main(int argc, char *argv[]) {
|
|||||||
sem_init(semaphore, 1, 0);
|
sem_init(semaphore, 1, 0);
|
||||||
semaphores.push_back(semaphore);
|
semaphores.push_back(semaphore);
|
||||||
|
|
||||||
uint16_t port = startTCPPort + i;
|
uint16_t port = startPort + 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,
|
||||||
|
@ -181,8 +181,8 @@ std::string getHelpMessage() {
|
|||||||
"\t-n, --num-receivers : Number of receivers.\n" +
|
"\t-n, --num-receivers : Number of receivers.\n" +
|
||||||
"\t-p, --port : TCP port to communicate with client for "
|
"\t-p, --port : TCP port to communicate with client for "
|
||||||
"configuration. Non-zero and 16 bit.\n" +
|
"configuration. Non-zero and 16 bit.\n" +
|
||||||
"\t-c, --callback : Enable dummy callbacks. Disabled (0) by "
|
"\t-c, --callback : Enable dummy callbacks for debugging. "
|
||||||
"default. Prints frame header for debugging.\n" +
|
"Disabled by default. \n" +
|
||||||
"\t-u, --uid : Set effective user id if receiver started "
|
"\t-u, --uid : Set effective user id if receiver started "
|
||||||
"with privileges. \n\n";
|
"with privileges. \n\n";
|
||||||
}
|
}
|
||||||
@ -203,22 +203,26 @@ int main(int argc, char *argv[]) {
|
|||||||
std::string help_message = getHelpMessage();
|
std::string help_message = getHelpMessage();
|
||||||
|
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
{"help", no_argument, nullptr, 'h'},
|
|
||||||
{"version", no_argument, nullptr, 'v'},
|
{"version", no_argument, nullptr, 'v'},
|
||||||
{"num-receivers", required_argument, nullptr, 'n'},
|
{"num-receivers", required_argument, nullptr, 'n'},
|
||||||
{"rx_tcpport", required_argument, nullptr, 't'},
|
{"rx_tcpport", required_argument, nullptr, 't'},
|
||||||
{"port", required_argument, nullptr, 'p'},
|
{"port", required_argument, nullptr, 'p'},
|
||||||
{"callback", required_argument, nullptr, 'c'},
|
{"callback", no_argument, nullptr, 'c'},
|
||||||
{"uid", required_argument, nullptr, 'u'},
|
{"uid", required_argument, nullptr, 'u'},
|
||||||
|
{"help", no_argument, nullptr, 'h'},
|
||||||
{nullptr, 0, nullptr, 0}};
|
{nullptr, 0, nullptr, 0}};
|
||||||
|
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
while (-1 != (opt = getopt_long(argc, argv, "hvn:t:p:u:c:", long_options,
|
while (-1 != (opt = getopt_long(argc, argv, "vn:t:p:cu:h", long_options,
|
||||||
&option_index))) {
|
&option_index))) {
|
||||||
|
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
|
|
||||||
|
case 'v':
|
||||||
|
std::cout << argv[0] << " Version: " << APIRECEIVER << std::endl;
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
case 'n':
|
case 'n':
|
||||||
try {
|
try {
|
||||||
numReceivers = sls::StringTo<uint16_t>(optarg);
|
numReceivers = sls::StringTo<uint16_t>(optarg);
|
||||||
@ -234,6 +238,7 @@ int main(int argc, char *argv[]) {
|
|||||||
case 't':
|
case 't':
|
||||||
LOG(sls::logWARNING)
|
LOG(sls::logWARNING)
|
||||||
<< "Deprecated option. Please use 'p' or '--port'.";
|
<< "Deprecated option. Please use 'p' or '--port'.";
|
||||||
|
//[[fallthrough]]; TODO: for when we update to c++17
|
||||||
case 'p':
|
case 'p':
|
||||||
try {
|
try {
|
||||||
startPort = sls::StringTo<uint16_t>(optarg);
|
startPort = sls::StringTo<uint16_t>(optarg);
|
||||||
@ -244,12 +249,7 @@ int main(int argc, char *argv[]) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
try {
|
callbackEnabled = true;
|
||||||
callbackEnabled = sls::StringTo<bool>(optarg);
|
|
||||||
} catch (...) {
|
|
||||||
throw sls::RuntimeError("Invalid callback enable." +
|
|
||||||
help_message);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'u':
|
case 'u':
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#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 <csignal> //SIGINT
|
#include <csignal> //SIGINT
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
@ -36,20 +37,24 @@ int main(int argc, char *argv[]) {
|
|||||||
"with privileges. \n\n";
|
"with privileges. \n\n";
|
||||||
|
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
{"help", no_argument, nullptr, 'h'},
|
|
||||||
{"version", no_argument, nullptr, 'v'},
|
{"version", no_argument, nullptr, 'v'},
|
||||||
{"rx_tcpport", required_argument, nullptr, 't'},
|
{"rx_tcpport", required_argument, nullptr, 't'},
|
||||||
{"port", required_argument, nullptr, 'p'},
|
{"port", required_argument, nullptr, 'p'},
|
||||||
{"uid", required_argument, nullptr, 'u'},
|
{"uid", required_argument, nullptr, 'u'},
|
||||||
|
{"help", no_argument, nullptr, 'h'},
|
||||||
{nullptr, 0, nullptr, 0}};
|
{nullptr, 0, nullptr, 0}};
|
||||||
|
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
while (-1 != (opt = getopt_long(argc, argv, "hvt:p:u:", long_options,
|
while (-1 != (opt = getopt_long(argc, argv, "vt:p:u:h", long_options,
|
||||||
&option_index))) {
|
&option_index))) {
|
||||||
|
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
|
|
||||||
|
case 'v':
|
||||||
|
std::cout << argv[0] << " Version: " << APIRECEIVER << std::endl;
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
LOG(sls::logWARNING)
|
LOG(sls::logWARNING)
|
||||||
<< "Deprecated option. Please use 'p' or '--port'.";
|
<< "Deprecated option. Please use 'p' or '--port'.";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user