server arguments like linux

This commit is contained in:
maliakal_d 2020-09-08 08:18:48 +02:00
parent 1a90c58d9e
commit e82e531fb1
2 changed files with 123 additions and 58 deletions

View File

@ -9,6 +9,7 @@
#include "sls_detector_defs.h" #include "sls_detector_defs.h"
#include "versionAPI.h" #include "versionAPI.h"
#include <getopt.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -36,76 +37,128 @@ void sigInterruptHandler(int p) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// print version // options
if (argc > 1 && !strcasecmp(argv[1], "-version")) {
int version = 0;
#ifdef GOTTHARDD
version = APIGOTTHARD;
#elif EIGERD
version = APIEIGER;
#elif JUNGFRAUD
version = APIJUNGFRAU;
#elif CHIPTESTBOARDD
version = APICTB;
#elif MOENCHD
version = APIMOENCH;
#endif
LOG(logINFO, ("SLS Detector Server %s (0x%x)\n", GITBRANCH, version));
}
int portno = DEFAULT_PORTNO; int portno = DEFAULT_PORTNO;
isControlServer = 1; isControlServer = 1;
debugflag = 0; debugflag = 0;
checkModuleFlag = 1; checkModuleFlag = 1;
int version = 0;
// if socket crash, ignores SISPIPE, prevents global signal handler // help message
// subsequent read/write to socket gives error - must handle locally char helpMessage[MAX_STR_LENGTH];
signal(SIGPIPE, SIG_IGN); memset(helpMessage, 0, MAX_STR_LENGTH);
sprintf(
helpMessage,
"\n\n"
"Usage: %s [arguments]\n"
"Possible arguments are:\n"
"\t-v, --version : Software version\n"
"\t-p, --port <port> : TCP communication port with client. \n"
"\t-d, --devel : Developer mode. Skips firmware checks. \n"
"\t-g, --nomodule : [Mythen3][Gotthard2] Generic or No "
"Module mode. Skips detector type checks. \n"
"\t-f, --phaseshift <value> : [Gotthard] only. Sets phase shift. \n"
"\t-s, --stopserver : Stop server. Do not use as created by "
"control server \n\n",
argv[0]);
// circumvent the basic tests // parse command line for config
for (int i = 1; i < argc; ++i) { static struct option long_options[] = {
if (!strcasecmp(argv[i], "-stopserver")) { // These options set a flag.
LOG(logINFO, ("Detected stop server\n")); // These options dont set a flag. We distinguish them by their indices.
isControlServer = 0; {"help", no_argument, NULL, 'h'},
} else if (!strcasecmp(argv[i], "-devel")) { {"version", no_argument, NULL, 'v'},
LOG(logINFO, ("Detected developer mode\n")); {"port", required_argument, NULL, 'p'},
debugflag = 1; {"devel", no_argument, NULL, 'd'},
} else if (!strcasecmp(argv[i], "-nomodule")) { {"phaseshift", required_argument, NULL, 'f'},
LOG(logINFO, ("Detected No Module mode\n")); {"nomodule", no_argument, NULL, 'g'}, // generic
checkModuleFlag = 0; {"stopserver", no_argument, NULL, 's'},
} else if (!strcasecmp(argv[i], "-port")) { {NULL, 0, NULL, 0}};
if ((i + 1) >= argc) {
LOG(logERROR, ("no port value given. Exiting.\n")); optind = 1;
return -1; // getopt_long stores the option index here
} int option_index = 0;
if (sscanf(argv[i + 1], "%d", &portno) == 0) { int c = 0;
LOG(logERROR,
("cannot decode port value %s. Exiting.\n", argv[i + 1])); while (c != -1) {
return -1; c = getopt_long(argc, argv, "hvp:df:gs", long_options, &option_index);
// Detect the end of the options
if (c == -1)
break;
switch (c) {
case 'v':
#ifdef GOTTHARDD
version = APIGOTTHARD;
#elif EIGERD
version = APIEIGER;
#elif JUNGFRAUD
version = APIJUNGFRAU;
#elif CHIPTESTBOARDD
version = APICTB;
#elif MOENCHD
version = APIMOENCH;
#elif MYTHEN3D
version = APIMYTHEN3;
#elif GOTTHARD2D
version = APIGOTTHARD2;
#endif
LOG(logINFOBLUE, ("SLS Detector Server Version: %s (0x%x)\n",
GITBRANCH, version));
exit(EXIT_SUCCESS);
case 'p':
if (sscanf(optarg, "%d", &portno) != 1) {
LOG(logERROR, ("Cannot scan port argument\n%s", helpMessage));
exit(EXIT_FAILURE);
} }
LOG(logINFO, ("Detected port: %d\n", portno)); LOG(logINFO, ("Detected port: %d\n", portno));
} break;
#ifdef GOTTHARDD
else if (!strcasecmp(argv[i], "-phaseshift")) { case 'd':
if ((i + 1) >= argc) { LOG(logINFO, ("Detected developer mode\n"));
LOG(logERROR, ("no phase shift value given. Exiting.\n")); debugflag = 1;
return -1; break;
case 'f':
#ifndef GOTTHARDD
LOG(logERROR,
("Phase shift argument not implemented for this detector\n"));
exit(EXIT_FAILURE);
#else
if (sscanf(optarg, "%d", &phaseShift) != 1) {
LOG(logERROR,
("Cannot scan phase shift argument\n%s", helpMessage));
exit(EXIT_FAILURE);
} }
if (sscanf(argv[i + 1], "%d", &phaseShift) == 0) { LOG(logINFO, ("Detected phase shift: %d\n", phaseShift));
LOG(logERROR, ("cannot decode phase shift value %s. Exiting.\n",
argv[i + 1]));
return -1;
}
LOG(logINFO, ("Detected phase shift of %d\n", phaseShift));
}
#endif #endif
break;
case 'g':
LOG(logINFO, ("Detected generic mode (no module)\n"));
checkModuleFlag = 0;
break;
case 's':
LOG(logINFO, ("Detected stop server\n"));
isControlServer = 0;
break;
case 'h':
default:
LOG(logERROR, (helpMessage));
exit(EXIT_FAILURE);
}
} }
// control server // control server
if (isControlServer) { if (isControlServer) {
LOG(logINFOBLUE, ("Control Server [%d]\n", portno)); LOG(logINFOBLUE, ("Control Server [%d]\n", portno));
// Catch signal SIGINT to destroy shm properly // Catch signal SIGINT (Ctrl + c) to destroy shm properly
struct sigaction sa; struct sigaction sa;
sa.sa_flags = 0; // no flags sa.sa_flags = 0; // no flags
sa.sa_handler = sigInterruptHandler; // handler function sa.sa_handler = sigInterruptHandler; // handler function
@ -122,9 +175,18 @@ int main(int argc, char *argv[]) {
// start stop server process // start stop server process
char cmd[MAX_STR_LENGTH]; char cmd[MAX_STR_LENGTH];
memset(cmd, 0, MAX_STR_LENGTH); memset(cmd, 0, MAX_STR_LENGTH);
char portCmd[256];
memset(portCmd, 0, 256);
sprintf(portCmd, "-p%d", portno);
for (int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i) {
if (!strcasecmp(argv[i], "-port")) { LOG(logINFOBLUE, ("i:%d argv[i]:%s\n", i, argv[i]));
i += 2; // remove port argument (--port) and [value]
if (!strcasecmp(argv[i], "--port")) {
++i;
continue;
}
// remove port argument (-p[value])
if (!strcasecmp(argv[i], portCmd)) {
continue; continue;
} }
if (i > 0) { if (i > 0) {
@ -134,7 +196,7 @@ int main(int argc, char *argv[]) {
} }
char temp[50]; char temp[50];
memset(temp, 0, sizeof(temp)); memset(temp, 0, sizeof(temp));
sprintf(temp, " -stopserver -port %d &", portno + 1); sprintf(temp, " --stopserver --port %d &", portno + 1);
strcat(cmd, temp); strcat(cmd, temp);
LOG(logDEBUG1, ("Command to start stop server:%s\n", cmd)); LOG(logDEBUG1, ("Command to start stop server:%s\n", cmd));
@ -149,6 +211,10 @@ int main(int argc, char *argv[]) {
} }
} }
// if socket crash, ignores SISPIPE, prevents global signal handler
// subsequent read/write to socket gives error - must handle locally
signal(SIGPIPE, SIG_IGN);
init_detector(); init_detector();
// bind socket // bind socket
sockfd = bindSocket(portno); sockfd = bindSocket(portno);

View File

@ -20,7 +20,6 @@ Receiver::~Receiver() = default;
Receiver::Receiver(int argc, char *argv[]) : tcpipInterface(nullptr) { Receiver::Receiver(int argc, char *argv[]) : tcpipInterface(nullptr) {
// options // options
std::map<std::string, std::string> configuration_map;
int tcpip_port_no = 1954; int tcpip_port_no = 1954;
uid_t userid = -1; uid_t userid = -1;