This commit is contained in:
2020-04-06 17:28:05 +02:00
parent 47b0e46f15
commit 80e55cd4da
7 changed files with 230 additions and 84 deletions

View File

@ -11,6 +11,13 @@ typedef enum{
OTHER
}intType;
// communciate with stop server
#ifdef VIRTUAL
#define FILE_STATUS "/tmp/Sls_virtual_server_status_"
#define FILE_STOP "/tmp/Sls_virtual_server_stop_"
#define FD_STATUS 0
#define FD_STOP 1
#endif
int bindSocket(unsigned short int port_number);
int acceptConnection(int socketDescriptor);

View File

@ -0,0 +1,14 @@
#pragma once
#ifdef VIRTUAL
// communciate between control and stop server
int ComVirtual_createFiles(const int port);
void ComVirtual_setFileNames(const int port);
int ComVirtual_writeStatus(int value);
int ComVirtual_readStatus(int* value);
int ComVirtual_writeStop(int value);
int ComVirtual_readStop(int* value);
int ComVirtual_writeToFile(int value, const char* fname, const char* serverName);
int ComVirtual_readFromFile(int* value, const char* fname, const char* serverName);
#endif

View File

@ -0,0 +1,107 @@
#ifdef VIRTUAL
#include "communication_virtual.h"
#include "clogger.h"
#include <string.h>
#define FILE_STATUS "/tmp/sls_virtual_server_status_"
#define FILE_STOP "/tmp/sls_virtual_server_stop_"
#define FD_STATUS 0
#define FD_STOP 1
#define FILE_NAME_LENGTH 1000
FILE* fd[2] = {NULL, NULL};
char fnameStatus[FILE_NAME_LENGTH];
char fnameStop[FILE_NAME_LENGTH];
int portNumber = 0;
int ComVirtual_createFiles(const int port) {
portNumber = port;
// control server writign status file
memset(fnameStatus, 0, FILE_NAME_LENGTH);
sprintf(fnameStatus, "%s%d", FILE_STATUS, port);
FILE* fd = NULL;
if (NULL == (fd = fopen(fnameStatus, "w"))) {
LOG(logERROR, ("Could not open the file %s for virtual communication\n",
fnameStatus));
return 0;
}
fclose(fd);
LOG(logINFOBLUE, ("Created status file %s\n", fnameStatus));
// stop server writing stop file
memset(fnameStop, 0, FILE_NAME_LENGTH);
sprintf(fnameStop, "%s%d", FILE_STOP, port);
if (NULL == (fd = fopen(fnameStop, "w"))) {
LOG(logERROR, ("Could not open the file %s for virtual communication\n",
fnameStop));
return 0;
}
fclose(fd);
LOG(logINFOBLUE, ("Created stop file %s\n", fnameStop));
return 1;
}
void ComVirtual_setFileNames(const int port) {
portNumber = port;
memset(fnameStatus, 0, FILE_NAME_LENGTH);
memset(fnameStop, 0, FILE_NAME_LENGTH);
sprintf(fnameStatus, "%s%d", FILE_STATUS, port);
sprintf(fnameStatus, "%s%d", FILE_STOP, port);
}
int ComVirtual_writeStatus(int value) {
return ComVirtual_writeToFile(value, FILE_STATUS, "Control");
}
int ComVirtual_readStatus(int* value) {
return ComVirtual_readFromFile(value, FILE_STATUS, "Control");
}
int ComVirtual_writeStop(int value) {
return ComVirtual_writeToFile(value, FILE_STOP, "Stop");
}
int ComVirtual_readStop(int* value) {
return ComVirtual_readFromFile(value, FILE_STOP, "Stop");
}
int ComVirtual_writeToFile(int value, const char* fname, const char* serverName) {
FILE* fd = NULL;
if (NULL == (fd = fopen(fname, "w"))) {
LOG(logERROR, ("Vritual %s Server [%d] could not open "
"the file %s for writing\n",
serverName, portNumber, fname));
return 0;
}
while (fwrite(&value, sizeof(value), 1, fd) < 1) {
LOG(logERROR, ("Vritual %s Server [%d] could not write "
"to file %s\n",
serverName, portNumber, fname));
return 0;
}
fclose(fd);
return 1;
}
int ComVirtual_readFromFile(int* value, const char* fname, const char* serverName) {
FILE* fd = NULL;
if (NULL == (fd = fopen(fname, "r"))) {
LOG(logERROR, ("Vritual %s Server [%d] could not open "
"the file %s for reading\n",
serverName, portNumber, fname));
return 0;
}
while (fread(value, sizeof(int), 1, fd) < 1) {
LOG(logERROR, ("Vritual %s Server [%d] could not read "
"from file %s\n",
serverName, portNumber, fname));
return 0;
}
fclose(fd);
return 1;
}
#endif

View File

@ -7,11 +7,13 @@
#include "slsDetectorServer_funcs.h"
#include "slsDetectorServer_defs.h"
#include "versionAPI.h"
#ifdef VIRTUAL
#include "communication_virtual.h"
#endif
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
// Global variables from communication_funcs
extern int isControlServer;
@ -24,9 +26,6 @@ extern int checkModuleFlag;
// Global variables from slsDetectorFunctionList
#ifdef VIRTUAL
//extern int pipeFDs[2];
#endif
#ifdef GOTTHARDD
extern int phaseShift;
#endif
@ -55,8 +54,9 @@ int main(int argc, char *argv[]) {
}
int portno = DEFAULT_PORTNO;
int retval = OK;
int fd = 0;
isControlServer = 1;
debugflag = 0;
checkModuleFlag = 1;
// if socket crash, ignores SISPIPE, prevents global signal handler
// subsequent read/write to socket gives error - must handle locally
@ -66,7 +66,11 @@ int main(int argc, char *argv[]) {
{
int i;
for (i = 1; i < argc; ++i) {
if(!strcasecmp(argv[i],"-devel")){
if(!strcasecmp(argv[i],"-stopserver")) {
LOG(logINFO, ("Detected stop server\n"));
isControlServer = 0;
}
else if(!strcasecmp(argv[i],"-devel")){
LOG(logINFO, ("Detected developer mode\n"));
debugflag = 1;
}
@ -83,7 +87,7 @@ int main(int argc, char *argv[]) {
LOG(logERROR, ("cannot decode port value %s. Exiting.\n", argv[i + 1]));
return -1;
}
LOG(logINFO, ("Detected control port: %d\n", portno));
LOG(logINFO, ("Detected port: %d\n", portno));
}
#ifdef GOTTHARDD
else if(!strcasecmp(argv[i],"-phaseshift")){
@ -101,62 +105,51 @@ int main(int argc, char *argv[]) {
}
}
// stop server
// control server
if (isControlServer) {
#ifdef STOP_SERVER
// create pipes to communicate with stop server
// start stop server process
char cmd[MAX_STR_LENGTH];
memset(cmd, 0, MAX_STR_LENGTH);
{
int i;
for (i = 0; i < argc; ++i) {
if (i > 0) {
strcat(cmd, " ");
}
strcat(cmd, argv[i]);
}
char temp[50];
memset(temp, 0, sizeof(temp));
sprintf(temp, " -stopserver -port %d &", portno + 1);
strcat(cmd, temp);
LOG(logDEBUG1, ("Command to start stop server:%s\n", cmd));
system(cmd);
}
LOG(logINFOBLUE, ("Control Server [%d]\n", portno));
#ifdef VIRTUAL
/*if (pipe(pipeFDs) < 0) {
LOG(logERROR, ("Could not create pipes to communicate with stop server\n"));
return -1;
}*/
// creating files for virtual servers to communicate with each other
if (!ComVirtual_createFiles(portno)) {
return -1;
}
#endif
// fork stop server
pid_t cpid = fork();
if (cpid < 0) {
LOG(logERROR, ("Could not create fork a stop server\n"));
return -1;
}
//fcntl(pipeFDs[PIPE_READ], F_SETFL, O_NONBLOCK);
//fcntl(pipeFDs[PIPE_WRITE], F_SETFL, O_NONBLOCK);
// stop server (child process)
if (cpid == 0) {
isControlServer = 0;
++portno;
LOG(logINFOBLUE, ("Stop server [%d]\n", portno));
// change name of stop server to distinguish
char stopServerSuffix[30];
memset(stopServerSuffix, 0, sizeof(stopServerSuffix));
sprintf(stopServerSuffix, " Stop_Server %d", portno);
//strcat(argv[0], stopServerSuffix);
#ifdef VIRTUAL
//close(pipeFDs[PIPE_WRITE]);
//fcntl(pipeFDs[PIPE_READ], F_SETFL, O_NONBLOCK);
#endif
} else {
isControlServer = 1;
LOG(logINFOBLUE, ("Control server [%d]\n", portno));
}
// stop server
else {
LOG(logINFOBLUE, ("Stop Server [%d]\n", portno));
#ifdef VIRTUAL
//close(pipeFDs[PIPE_READ]);
ComVirtual_setFileNames(portno);
#endif
}
#endif
init_detector();
{ // bind socket
sockfd = bindSocket(portno);
if (ret == FAIL)
return -1;
}
// bind socket
sockfd = bindSocket(portno);
if (ret == FAIL)
return -1;
// assign function table
function_table();
@ -167,8 +160,9 @@ int main(int argc, char *argv[]) {
}
// waits for connection
int retval = OK;
while(retval != GOODBYE && retval != REBOOT) {
fd = acceptConnection(sockfd);
int fd = acceptConnection(sockfd);
if (fd > 0) {
retval = decode_function(fd);
closeConnection(fd);