gem-graph-server/src/model.c

404 lines
12 KiB
C
Raw Normal View History

2021-06-21 14:26:09 +02:00
//=-------------------------------------------------------------------------=//
// Model 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/base.h"
2021-08-20 16:41:58 +02:00
#include "../include/xml.h"
2021-06-23 09:13:46 +02:00
#include "../include/arrows.h"
2021-06-23 14:16:45 +02:00
#include "../include/scheduler.h"
2021-06-23 09:13:46 +02:00
2021-08-20 16:10:18 +02:00
#include <dirent.h>
#include <signal.h>
2021-06-23 11:37:47 +02:00
#define MAX_MODEL_NUMBER 1
2021-08-24 19:39:53 +02:00
#define MAX_MODEL_NAME_SIZE 48
#define MAX_MODEL_FILENAME_SIZE 48
#define MAX_AUTHOR_NAME_SIZE 48
#define MAX_VERSION_SIZE 16
2021-06-23 20:23:37 +02:00
#define ARROW_NUMBER 6
2021-06-23 14:16:45 +02:00
#define SITE_NUMBER 2
#define MAX_CYCLES 10
#define MAX_THREAD 0
2021-06-23 20:23:37 +02:00
#define XMAX 39
2021-06-23 14:16:45 +02:00
#define YMAX 0
#define ZMAX 0
#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1)
2021-06-23 11:37:47 +02:00
2021-06-23 14:16:45 +02:00
static Model_t **loadedModel;
2021-07-07 19:46:23 +02:00
static int loadedModelSize; // begins to 1
2021-06-21 14:26:09 +02:00
2021-06-23 14:16:45 +02:00
static Model_t **knownModel;
2021-07-07 19:46:23 +02:00
static int knownModelSize; // begins to 1
Model_t *lastModel;
Model_t **lastModelAddr;
2021-06-23 14:16:45 +02:00
2021-08-20 16:41:58 +02:00
2021-08-24 18:25:44 +02:00
static inline int ModelParseFile(Model_t *model);
2021-08-20 16:10:18 +02:00
void ModelSystemDestroy(void);
2021-06-23 14:16:45 +02:00
/* -------------------------------------------------------------------------- */
2021-07-07 19:46:23 +02:00
void printModels(char *buf)
{
sprintf(buf + strlen(buf),"Known models\n");
for (int i = 0; i <= knownModelSize-1; i++) {
2021-08-24 19:39:53 +02:00
sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s, date: %lu, "
"author: %s\n",
knownModel[i]->id,
knownModel[i],
knownModel[i]->name,
knownModel[i]->date,
knownModel[i]->author
);
2021-07-07 19:46:23 +02:00
}
sprintf(buf + strlen(buf), "\nLoaded models\n");
for (int i = 0; i <= loadedModelSize-1; i++) {
sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s\n",
loadedModel[i]->id, loadedModel[i], loadedModel[i]->name);
}
2021-06-23 14:16:45 +02:00
}
2021-08-24 19:39:53 +02:00
int ModelLoad(int id)
2021-06-23 14:16:45 +02:00
{
2021-07-07 19:46:23 +02:00
if (id <= 0 || id > knownModelSize) {
2021-07-02 15:06:11 +02:00
return -1;
}
2021-07-07 19:46:23 +02:00
printLog("Loading model id %d (/%d models)...\n", id, knownModelSize);
2021-06-30 16:14:18 +02:00
2021-06-23 14:16:45 +02:00
// Creating structure for the Scheduler
2021-07-07 19:46:23 +02:00
knownModel[id-1]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t));
2021-06-23 14:16:45 +02:00
2021-08-24 19:39:53 +02:00
loadedModelSize++;
2021-07-02 15:06:11 +02:00
2021-07-07 19:46:23 +02:00
loadedModel =
(Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*));
2021-08-24 19:39:53 +02:00
2021-07-07 19:46:23 +02:00
loadedModel[loadedModelSize-1] = knownModel[id-1];
return loadedModelSize;
2021-06-23 14:16:45 +02:00
}
2021-08-24 19:39:53 +02:00
void ModelUnload(int id)
{
// Destroy scheduler
SchedDestroy(loadedModel[id-1]->scheduler);
loadedModel[id-1]->scheduler = NULL;
memmove(&loadedModel[id-1],
&loadedModel[id-1] + sizeof(Model_t*),
loadedModelSize - (id-1));
// Decrement loaded model index
loadedModelSize--;
// Resize loaded model list
loadedModel =
(Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*));
}
2021-08-24 18:25:44 +02:00
/* -------------------------------------------------------------------------- */
2021-07-12 17:09:38 +02:00
int ModelRun(int id)
2021-06-23 14:16:45 +02:00
{
2021-07-12 17:54:57 +02:00
if (id <= 0 || id > loadedModelSize)
2021-07-12 17:09:38 +02:00
return 0;
2021-07-12 17:54:57 +02:00
if (!loadedModel[id-1]->scheduler)
2021-07-12 17:09:38 +02:00
return 0;
2021-07-12 17:54:57 +02:00
if (loadedModel[id-1]->isRunning)
return 0;
2021-08-24 19:39:53 +02:00
//
2021-07-12 17:54:57 +02:00
loadedModel[id-1]->scheduler->globalDrawingSpace =
2021-08-24 19:39:53 +02:00
(Space_t*) calloc(1, sizeof(Space_t));
2021-07-12 17:54:57 +02:00
loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread;
loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles;
// Preparing global drawing space
2021-08-20 16:41:58 +02:00
// TODO Load space
2021-07-12 17:54:57 +02:00
loadedModel[id-1]->scheduler->arrowArray =
2021-08-24 19:39:53 +02:00
(ArrowArray_t*) calloc(1, sizeof(ArrowArray_t));
2021-07-12 17:54:57 +02:00
2021-08-20 16:41:58 +02:00
// TODO Load arrows
2021-07-12 17:54:57 +02:00
loadedModel[id-1]->scheduler->pleaseStop = false;
loadedModel[id-1]->isRunning = true;
2021-07-12 17:09:38 +02:00
SchedInit(loadedModel[id-1]->scheduler);
2021-07-12 17:54:57 +02:00
2021-07-12 17:09:38 +02:00
printLog("Model %d launched\n", id);
return 1;
2021-06-23 14:16:45 +02:00
}
2021-07-12 17:09:38 +02:00
int ModelStop(int id)
2021-06-23 14:16:45 +02:00
{
2021-07-12 17:09:38 +02:00
if (id <= 0 || id > loadedModelSize) {
return 0;
}
if (!loadedModel[id-1]->scheduler) {
return 0;
}
2021-08-24 19:39:53 +02:00
// Stop model scheduler
2021-07-12 17:09:38 +02:00
loadedModel[id-1]->scheduler->pleaseStop = true;
printLog("Model %d stop bit set\n", id);
2021-08-24 19:39:53 +02:00
// Wait for Shceduler to stop
2021-07-12 17:09:38 +02:00
SchedWait(loadedModel[id-1]->scheduler);
2021-08-24 19:39:53 +02:00
SchedContentDestroy(loadedModel[id-1]->scheduler);
2021-07-12 17:09:38 +02:00
2021-08-24 19:39:53 +02:00
// Disable running bit
2021-07-12 17:54:57 +02:00
loadedModel[id-1]->isRunning = false;
2021-07-12 17:09:38 +02:00
return 1;
2021-06-23 14:16:45 +02:00
}
2021-08-24 18:25:44 +02:00
/* -------------------------------------------------------------------------- */
2021-09-14 11:20:33 +02:00
void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots
2021-08-24 18:25:44 +02:00
{
// increment index
knownModelSize++;
// create socket
knownModel =
(Model_t**) realloc(knownModel, knownModelSize * sizeof(Model_t*));
// populate socket
knownModel[knownModelSize-1] = (Model_t*) calloc(1, sizeof(Model_t));
// populate model
knownModel[knownModelSize-1]->id = knownModelSize;
// return value
*newModel = knownModel[knownModelSize-1];
lastModel = knownModel[knownModelSize-1];
lastModelAddr = &knownModel[knownModelSize-1];
2021-08-24 19:39:53 +02:00
// continue. model population
2021-08-24 18:25:44 +02:00
knownModel[knownModelSize-1]->name =
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
knownModel[knownModelSize-1]->filename =
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
knownModel[knownModelSize-1]->author =
2021-08-24 19:39:53 +02:00
(char *) calloc(1, sizeof(char) * MAX_AUTHOR_NAME_SIZE);
2021-08-24 18:25:44 +02:00
knownModel[knownModelSize-1]->version =
2021-08-24 19:39:53 +02:00
(char *) calloc(1, sizeof(char) * MAX_VERSION_SIZE);
2021-08-24 18:25:44 +02:00
knownModel[knownModelSize-1]->space_xMax = XMAX;
knownModel[knownModelSize-1]->space_yMax = YMAX;
knownModel[knownModelSize-1]->space_zMax = ZMAX;
knownModel[knownModelSize-1]->nmaxThread = MAX_THREAD;
knownModel[knownModelSize-1]->nmaxCycles = MAX_CYCLES;
knownModel[knownModelSize-1]->siteNumber = 0;
}
2021-08-20 16:41:58 +02:00
void ModelDelete(int id) //XXX
2021-06-23 14:16:45 +02:00
{
2021-08-24 19:39:53 +02:00
// Free a model structure
2021-08-20 16:41:58 +02:00
free(knownModel[id-1]->name);
knownModel[id-1]->name = NULL;
2021-08-24 12:09:54 +02:00
2021-08-20 16:41:58 +02:00
free(knownModel[id-1]->filename);
2021-08-24 12:09:54 +02:00
knownModel[id-1]->filename = NULL;
free(knownModel[id-1]->author);
knownModel[id-1]->author = NULL;
free(knownModel[id-1]->version);
knownModel[id-1]->version = NULL;
2021-08-20 16:41:58 +02:00
free(knownModel[id-1]);
knownModel[id-1] = NULL;
2021-08-24 12:09:54 +02:00
2021-08-24 01:14:48 +02:00
knownModelSize--;
2021-06-23 14:16:45 +02:00
}
2021-06-23 17:07:43 +02:00
void ModelShutdown(void)
{
2021-08-24 19:39:53 +02:00
// Stop each model from running
2021-07-07 19:46:23 +02:00
for (int i = 0; i < loadedModelSize; i++) {
2021-06-23 17:07:43 +02:00
ModelStop(i);
2021-08-24 19:39:53 +02:00
ModelUnload(i);
2021-06-23 17:07:43 +02:00
}
}
2021-08-24 18:25:44 +02:00
/* -------------------------------------------------------------------------- */
void ModelSystemInit(Parameters_t *parameters)
2021-06-30 15:46:18 +02:00
{
2021-08-20 16:10:18 +02:00
struct dirent *modelDirEntry = NULL;
DIR *modelDir = NULL;
Model_t *newModel;
char *extensionPosition;
2021-07-02 15:06:11 +02:00
loadedModel = (Model_t**) calloc(1, sizeof(Model_t*));
2021-07-07 19:46:23 +02:00
2021-07-02 15:06:11 +02:00
knownModel = (Model_t**) calloc(1, sizeof(Model_t*));
2021-07-07 19:46:23 +02:00
knownModelSize = 0;
loadedModelSize = 0;
printLog("Model system initiated with folder : %s\n", parameters->modelDir);
2021-08-20 16:10:18 +02:00
2021-08-24 19:39:53 +02:00
// Open model directory
2021-08-20 16:10:18 +02:00
if ((modelDir = opendir(parameters->modelDir)) <= 0) {
printLog("Could not open %s\n", parameters->modelDir);
ModelSystemDestroy();
kill(getpid(), SIGTERM);
return;
}
while ((modelDirEntry = readdir(modelDir)) != NULL) {
if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) {
// Creating model
2021-09-14 11:20:33 +02:00
ModelAddToKnown(&newModel);
2021-08-20 16:10:18 +02:00
2021-08-24 19:39:53 +02:00
// Write file path in filename
2021-08-24 00:41:49 +02:00
strncpy(newModel->filename, parameters->modelDir,
strlen(parameters->modelDir));
2021-08-24 19:39:53 +02:00
// Add a / separator
2021-08-24 00:41:49 +02:00
strcat(newModel->filename
+ strlen(parameters->modelDir),
"/");
2021-08-24 19:39:53 +02:00
// Add the file relative name
2021-08-24 00:41:49 +02:00
strncpy(newModel->filename
+ strlen(parameters->modelDir)
+ 1,
modelDirEntry->d_name,
2021-08-20 16:10:18 +02:00
strlen(modelDirEntry->d_name));
2021-08-24 19:39:53 +02:00
// Write model name
2021-08-20 16:10:18 +02:00
strncpy(newModel->name, modelDirEntry->d_name,
extensionPosition - modelDirEntry->d_name);
2021-08-24 19:39:53 +02:00
// Ask to parse the new model
2021-08-24 01:14:48 +02:00
if (ModelParseFile(newModel) != 0) {
ModelDelete(newModel->id);
continue;
};
2021-08-20 16:41:58 +02:00
// Check model is valid and/or parsed
2021-08-24 01:14:48 +02:00
if (newModel->validated == false) {
2021-08-20 16:41:58 +02:00
ModelDelete(newModel->id);
continue;
}
2021-08-24 19:39:53 +02:00
// Succeeded !
2021-08-24 00:41:49 +02:00
printLog("Loaded model %s\n", newModel->name);
2021-08-20 16:10:18 +02:00
}
}
2021-06-30 15:46:18 +02:00
}
void ModelSystemDestroy(void)
{
2021-07-07 19:46:23 +02:00
for (int i = 0; i < loadedModelSize; i++) {
2021-06-30 15:46:18 +02:00
ModelDelete(i);
}
free(loadedModel);
2021-07-12 17:54:57 +02:00
loadedModel = NULL;
2021-06-30 15:46:18 +02:00
free(knownModel);
2021-07-12 17:54:57 +02:00
knownModel = NULL;
2021-06-30 15:46:18 +02:00
}
2021-06-23 14:16:45 +02:00
/* -------------------------------------------------------------------------- */
2021-08-31 01:03:02 +02:00
static inline void CreateField(ModelField_t *modelField,
const char *tag,
const bool mandatory,
2021-09-14 11:20:33 +02:00
char *value,
size_t valueSize,
ModelField_t *son,
ModelField_t *next)
2021-08-31 01:03:02 +02:00
{
strncpy(modelField->tag, tag, 25);
modelField->mandatory = mandatory;
2021-09-14 11:20:33 +02:00
modelField->destination = value;
modelField->size = valueSize;
2021-08-31 01:03:02 +02:00
modelField->son = son;
modelField->next = next;
}
2021-08-24 18:25:44 +02:00
static inline int ModelParseFile(Model_t *model)
2021-06-21 14:26:09 +02:00
{
2021-08-31 01:03:02 +02:00
char date[25];
2021-08-24 00:41:49 +02:00
ModelField_t identityField;
2021-08-31 01:03:02 +02:00
ModelField_t identityNameField;
ModelField_t identityAuthorField;
ModelField_t identityDateField;
ModelField_t identityVersionField;
ModelField_t parametersField;
ModelField_t parametersModelizationField;
ModelField_t parametersSpaceField;
2021-08-24 00:41:49 +02:00
2021-08-31 01:03:02 +02:00
CreateField(&identityField,
"identity", false, NULL, 0, &identityNameField, NULL);
CreateField(&identityNameField,
"name", true, model->name, MAX_MODEL_NAME_SIZE,
NULL, &identityAuthorField);
CreateField(&identityAuthorField,
"author", true, model->author, MAX_AUTHOR_NAME_SIZE,
NULL, &identityDateField);
CreateField(&identityDateField,
"date", true, date, 25,
NULL, &identityVersionField);
CreateField(&identityVersionField,
"version", true, model->version, MAX_VERSION_SIZE, NULL, NULL);
CreateField(&parametersField,
2021-09-14 11:20:33 +02:00
"parameters", false, NULL, 0, NULL, NULL);
2021-08-31 01:03:02 +02:00
CreateField(&parametersModelizationField,
2021-09-14 11:20:33 +02:00
"modelization", false, NULL, 0, NULL, NULL);
2021-08-31 01:03:02 +02:00
CreateField(&parametersSpaceField,
2021-09-14 11:20:33 +02:00
"space", false, NULL, 0, NULL, NULL);
2021-08-24 12:09:54 +02:00
2021-08-24 00:41:49 +02:00
2021-08-20 16:41:58 +02:00
// TODO modify model according to things in file
2021-08-24 00:41:49 +02:00
printLog("Parsing model %s\n", model->name);
2021-09-14 11:20:33 +02:00
XmlSetNewDocument(model->filename);
2021-08-24 00:41:49 +02:00
2021-09-14 11:20:33 +02:00
XmlResetWorkingRoot();
2021-08-24 19:39:53 +02:00
2021-09-14 11:20:33 +02:00
XmlCloseCurrentDocument();
if (XmlParseTree(&identityField) != 0) {
2021-08-24 01:14:48 +02:00
printLog("Invalid document, parsing can't succeed!\n");
return EBADF;
}
2021-08-24 00:41:49 +02:00
2021-08-24 19:39:53 +02:00
// Interpret what needs to be
model->date = strtol(date, NULL, 0);
// validate when we're finished
2021-08-24 01:14:48 +02:00
model->validated = true;
return 0;
2021-06-21 14:26:09 +02:00
}