gem-graph-server/src/main.c

152 lines
4.9 KiB
C
Raw Normal View History

2021-06-09 11:35:47 +02:00
//=-------------------------------------------------------------------------=//
// Main //
// //
// Copyright © 2021 The Gem-graph Project //
// //
// This file is part of gem-graph. //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//=-------------------------------------------------------------------------=//
2021-06-09 14:40:23 +02:00
#include "../include/base.h"
2021-06-09 11:54:58 +02:00
#include "../include/server.h"
2021-06-23 17:07:43 +02:00
#include "../include/model.h"
2021-06-09 11:35:47 +02:00
/* -------------------------------------------------------------------------- */
static Server_t *server;
2021-06-09 11:35:47 +02:00
2021-08-24 18:25:44 +02:00
static void SigIntTermHandler(int signum)
{
server->pleaseStop = true;
printLog("Server stopping\n");
ModelShutdown();
printLog("All model shutted down\n");
}
// TODO parse args to discover :
// - config files directory
// - models & users directories
2021-06-09 11:35:47 +02:00
int main(int argc, char **argv)
{
2021-08-24 18:25:44 +02:00
int options;
2021-06-14 18:33:13 +02:00
time_t t;
2021-07-09 20:42:44 +02:00
int returnValue = 0;
2021-08-24 18:25:44 +02:00
Parameters_t *parameters;
2021-06-14 18:33:13 +02:00
// Get parameters TODO from args
2021-08-24 18:25:44 +02:00
parameters = (Parameters_t*) calloc(sizeof(Parameters_t), 1);
while ((options = getopt(argc, argv, ":C:M:U:")) != -1) {
switch (options) {
case 'C':
printLog("Config : %s\n", optarg);
parameters->configDir = (char*) malloc(strlen(optarg) + 1);
strcpy(parameters->configDir, optarg);
break;
case 'M':
printLog("Models : %s\n", optarg);
parameters->modelDir = (char*) malloc(strlen(optarg) + 1);
strcpy(parameters->modelDir, optarg);
break;
case 'U':
printLog("User : %s\n", optarg);
parameters->userDir = (char*) malloc(strlen(optarg) + 1);
strcpy(parameters->userDir, optarg);
break;
case ':':
printLog("Option missing argument : %c\n", optopt);
if (parameters->configDir)
free(parameters->configDir);
if (parameters->modelDir)
free(parameters->modelDir);
if (parameters->userDir)
free(parameters->userDir);
free(parameters);
return ENOSYS;
break;
case '?':
printLog("Unknown option : %c\n", optopt);
if (parameters->configDir)
free(parameters->configDir);
if (parameters->modelDir)
free(parameters->modelDir);
if (parameters->userDir)
free(parameters->userDir);
free(parameters);
return ENOSYS;
break;
}
}
if (!parameters->configDir | !parameters->modelDir | !parameters->userDir) {
printLog("Missing arguments\n");
if (parameters->configDir)
free(parameters->configDir);
if (parameters->modelDir)
free(parameters->modelDir);
if (parameters->userDir)
free(parameters->userDir);
free(parameters);
return ENOSYS;
}
2021-06-14 18:33:13 +02:00
// Go!
2021-06-11 15:11:06 +02:00
printLog("Starting gem-graph-server...\n");
// Register new interrupt handler
2021-08-24 18:25:44 +02:00
signal(SIGINT, SigIntTermHandler);
signal(SIGTERM, SigIntTermHandler);
2021-06-23 17:07:43 +02:00
// Initializing random generator
2021-06-14 18:33:13 +02:00
t = time(&t);
2021-06-11 17:39:34 +02:00
srand((unsigned) t);
server = (Server_t*) calloc(1, sizeof(Server_t));
2021-06-09 23:56:42 +02:00
2021-06-23 17:07:43 +02:00
// Initializing model system
ModelSystemInit(parameters);
2021-06-23 17:07:43 +02:00
2021-06-21 10:34:11 +02:00
// Launching server
ServerInit(server);
2021-06-09 14:40:23 +02:00
2021-06-23 17:07:43 +02:00
// Waiting for termination
ServerWait(server);
2021-06-09 14:40:23 +02:00
2021-06-21 10:34:11 +02:00
// Exiting
returnValue |= server->returnValue;
2021-06-23 17:07:43 +02:00
ModelSystemDestroy();
free(server);
server = NULL;
2021-06-11 12:23:16 +02:00
2021-08-20 16:41:58 +02:00
free(parameters->userDir);
free(parameters->modelDir);
free(parameters->configDir);
free(parameters);
parameters = NULL;
2021-07-09 20:42:44 +02:00
return returnValue;
2021-06-11 12:23:16 +02:00
}