198 lines
6.0 KiB
C
198 lines
6.0 KiB
C
//=-------------------------------------------------------------------------=//
|
|
// Server management module //
|
|
// //
|
|
// 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/>. //
|
|
//=-------------------------------------------------------------------------=//
|
|
|
|
#include "../include/cmds.h"
|
|
#include "../include/scheduler.h"
|
|
#include "../include/model.h"
|
|
|
|
#include <sys/socket.h>
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
char *CmdModel(char *buf, char **argv, Server_t *args)
|
|
{
|
|
int id, eid;
|
|
Model_t *newModel;
|
|
|
|
// invalid use
|
|
if (!argv[1]) {
|
|
strcat(buf, "{create | delete | load | run | stop | list | info}\n");
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
if (strcmp(argv[1], "create") == 0) {
|
|
|
|
if (!argv[2] || !argv[3]) {
|
|
goto createEnd;
|
|
}
|
|
|
|
if (strncmp(argv[2], "name=", 5) == 0) {
|
|
|
|
if (strncmp(argv[3], "file=", 5) == 0) {
|
|
// TODO get the file content (sent by the client) from args
|
|
|
|
// Creating model
|
|
ModelAddToKnown(&newModel);
|
|
|
|
// TODO modify model according to things in file
|
|
|
|
// Write name
|
|
strcpy(newModel->name, argv[2] + 5);
|
|
|
|
// Write filename
|
|
strcpy(newModel->filename, argv[3] + 5);
|
|
|
|
sprintf(buf + strlen(buf), "Model %s created with id %d\n",
|
|
newModel->name, newModel->id);
|
|
goto CmdModelEnd;
|
|
|
|
} else {
|
|
goto createEnd;
|
|
}
|
|
} else {
|
|
goto createEnd;
|
|
}
|
|
|
|
createEnd:
|
|
// invalid use
|
|
strcat(buf, "Creates a model structure\n");
|
|
strcat(buf, "Usage: model create name=NAME file=FILENAME\n");
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
else if (strcmp(argv[1], "load") == 0) {
|
|
if (!argv[2]) {
|
|
goto loadEnd;
|
|
}
|
|
|
|
id = (int) strtol(argv[2] + 3, NULL, 10);
|
|
|
|
if (id == 0 || (eid = ModelLoad(id)) <= 0) {
|
|
sprintf(buf + strlen(buf), "Failed to load model id %d\n", id);
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
sprintf(buf + strlen(buf), "Model id %d loaded with effective "
|
|
"id %d\n", id, eid);
|
|
goto CmdModelEnd;
|
|
loadEnd:
|
|
// invalid use
|
|
strcat(buf, "Loads a model structure\n");
|
|
strcat(buf, "Usage: model load id=ID\n");
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
else if (strcmp(argv[1], "delete") == 0) {
|
|
if (!argv[2]) {
|
|
goto deleteEnd;
|
|
}
|
|
// TODO delete model
|
|
deleteEnd:
|
|
// invalid use
|
|
strcat(buf, "Deletes a model structure\n");
|
|
strcat(buf, "Usage: model delete id=ID\n");
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
else if (strcmp(argv[1], "run") == 0) {
|
|
if (!argv[2]) {
|
|
goto runEnd;
|
|
}
|
|
|
|
id = (int) strtol(argv[2] + 3, NULL, 10);
|
|
|
|
if (id == 0 || (!ModelRun(id))) {
|
|
sprintf(buf + strlen(buf), "Failed to run model id %d\n", id);
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
sprintf(buf + strlen(buf), "Model id %d is running\n", id);
|
|
goto CmdModelEnd;
|
|
runEnd:
|
|
// invalid use
|
|
strcat(buf, "Run a model simulation\n");
|
|
strcat(buf, "Usage: model run id=ID\n");
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
else if (strcmp(argv[1], "stop") == 0) {
|
|
if (!argv[2]) {
|
|
goto stopEnd;
|
|
}
|
|
|
|
id = (int) strtol(argv[2] + 3, NULL, 10);
|
|
|
|
if (id == 0 || (!ModelStop(id))) {
|
|
sprintf(buf + strlen(buf), "Failed to stop model id %d\n", id);
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
sprintf(buf + strlen(buf), "Model id %d is running\n", id);
|
|
goto CmdModelEnd;
|
|
stopEnd:
|
|
// invalid use
|
|
strcat(buf, "Stop a model simulation\n");
|
|
strcat(buf, "Usage: model stop id=ID\n");
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
else if (strcmp(argv[1], "list") == 0) {
|
|
strcat(buf, "You asked for us to list models\n");
|
|
printModels(buf);
|
|
}
|
|
|
|
else if (strcmp(argv[1], "info") == 0) {
|
|
if (!argv[2]) {
|
|
goto infoEnd;
|
|
}
|
|
// TODO info model
|
|
infoEnd:
|
|
// invalid use
|
|
strcat(buf, "Print info about a model\n");
|
|
strcat(buf, "Usage: model info id=ID\n");
|
|
goto CmdModelEnd;
|
|
}
|
|
|
|
// invalid use
|
|
else strcat(buf, "{create | delete | load | run | stop | list | info}\n");
|
|
|
|
CmdModelEnd:
|
|
return buf;
|
|
}
|
|
|
|
char *CmdShutdown(char *buf, char **argv, Server_t *args)
|
|
{
|
|
args->pleaseStop = true;
|
|
strcat(buf, "Server stopping\n");
|
|
|
|
ModelShutdown();
|
|
strcat(buf, "All model shutted down\n");
|
|
|
|
return buf;
|
|
}
|
|
|
|
char *CmdHelp(char *buf, char **argv, Server_t *args)
|
|
{
|
|
strcat(buf, "List of known commands:\n");
|
|
|
|
return buf;
|
|
}
|