339 lines
10 KiB
C
339 lines
10 KiB
C
//=-------------------------------------------------------------------------=//
|
|
// 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"
|
|
#include "../include/parsing.h"
|
|
#include "../include/arrows.h"
|
|
#include "../include/scheduler.h"
|
|
|
|
#include <dirent.h>
|
|
#include <signal.h>
|
|
|
|
#define MAX_MODEL_NUMBER 1
|
|
#define MAX_MODEL_NAME_SIZE 48
|
|
#define MAX_MODEL_FILENAME_SIZE 48
|
|
#define MAX_AUTHOR_NAME_SIZE 48
|
|
#define MAX_VERSION_SIZE 16
|
|
#define ARROW_NUMBER 6
|
|
#define SITE_NUMBER 2
|
|
#define MAX_CYCLES 10
|
|
#define MAX_THREAD 0
|
|
#define XMAX 39
|
|
#define YMAX 0
|
|
#define ZMAX 0
|
|
#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1)
|
|
|
|
static Model_t **loadedModel;
|
|
static int loadedModelSize; // begins to 1
|
|
|
|
static Model_t **knownModel;
|
|
static int knownModelSize; // begins to 1
|
|
|
|
Model_t *lastModel;
|
|
Model_t **lastModelAddr;
|
|
|
|
void ModelSystemDestroy(void);
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
void printModels(char *buf)
|
|
{
|
|
sprintf(buf + strlen(buf),"Known models\n");
|
|
for (int i = 0; i <= knownModelSize-1; i++) {
|
|
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
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
int ModelLoad(int id)
|
|
{
|
|
if (id <= 0 || id > knownModelSize) {
|
|
return -1;
|
|
}
|
|
|
|
printLog("Loading model id %d (/%d models)...\n", id, knownModelSize);
|
|
|
|
// Creating structure for the Scheduler
|
|
knownModel[id-1]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t));
|
|
|
|
loadedModelSize++;
|
|
|
|
loadedModel =
|
|
(Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*));
|
|
|
|
loadedModel[loadedModelSize-1] = knownModel[id-1];
|
|
return loadedModelSize;
|
|
}
|
|
|
|
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*));
|
|
}
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
int ModelRun(int id)
|
|
{
|
|
if (id <= 0 || id > loadedModelSize)
|
|
return 0;
|
|
|
|
if (!loadedModel[id-1]->scheduler)
|
|
return 0;
|
|
|
|
if (loadedModel[id-1]->isRunning)
|
|
return 0;
|
|
|
|
//
|
|
loadedModel[id-1]->scheduler->globalDrawingSpace =
|
|
(Space_t*) calloc(1, sizeof(Space_t));
|
|
|
|
loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread;
|
|
loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles;
|
|
|
|
// Preparing global drawing space
|
|
// TODO Load space
|
|
|
|
loadedModel[id-1]->scheduler->arrowArray =
|
|
(ArrowArray_t*) calloc(1, sizeof(ArrowArray_t));
|
|
|
|
// TODO Load arrows
|
|
|
|
loadedModel[id-1]->scheduler->pleaseStop = false;
|
|
loadedModel[id-1]->isRunning = true;
|
|
|
|
SchedInit(loadedModel[id-1]->scheduler);
|
|
|
|
printLog("Model %d launched\n", id);
|
|
return 1;
|
|
}
|
|
|
|
int ModelStop(int id)
|
|
{
|
|
if (id <= 0 || id > loadedModelSize) {
|
|
return 0;
|
|
}
|
|
if (!loadedModel[id-1]->scheduler) {
|
|
return 0;
|
|
}
|
|
|
|
// Stop model scheduler
|
|
loadedModel[id-1]->scheduler->pleaseStop = true;
|
|
printLog("Model %d stop bit set\n", id);
|
|
|
|
// Wait for Shceduler to stop
|
|
SchedWait(loadedModel[id-1]->scheduler);
|
|
SchedContentDestroy(loadedModel[id-1]->scheduler);
|
|
|
|
// Disable running bit
|
|
loadedModel[id-1]->isRunning = false;
|
|
return 1;
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots
|
|
{
|
|
// 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];
|
|
|
|
// continue. model population
|
|
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 =
|
|
(char *) calloc(1, sizeof(char) * MAX_AUTHOR_NAME_SIZE);
|
|
|
|
knownModel[knownModelSize-1]->version =
|
|
(char *) calloc(1, sizeof(char) * MAX_VERSION_SIZE);
|
|
|
|
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;
|
|
}
|
|
|
|
void ModelDelete(int id) //XXX
|
|
{
|
|
// Free a model structure
|
|
free(knownModel[id-1]->name);
|
|
knownModel[id-1]->name = NULL;
|
|
|
|
free(knownModel[id-1]->filename);
|
|
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;
|
|
|
|
free(knownModel[id-1]);
|
|
knownModel[id-1] = NULL;
|
|
|
|
knownModelSize--;
|
|
}
|
|
|
|
void ModelShutdown(void)
|
|
{
|
|
// Stop each model from running
|
|
for (int i = 0; i < loadedModelSize; i++) {
|
|
ModelStop(i);
|
|
ModelUnload(i);
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
void ModelSystemInit(Parameters_t *parameters)
|
|
{
|
|
struct dirent *modelDirEntry = NULL;
|
|
DIR *modelDir = NULL;
|
|
Model_t *newModel;
|
|
char *extensionPosition;
|
|
|
|
loadedModel = (Model_t**) calloc(1, sizeof(Model_t*));
|
|
|
|
knownModel = (Model_t**) calloc(1, sizeof(Model_t*));
|
|
|
|
knownModelSize = 0;
|
|
loadedModelSize = 0;
|
|
|
|
printLog("Model system initiated with folder : %s\n", parameters->modelDir);
|
|
|
|
// Open model directory
|
|
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
|
|
ModelAddToKnown(&newModel);
|
|
|
|
// Write file path in filename
|
|
strncpy(newModel->filename, parameters->modelDir,
|
|
strlen(parameters->modelDir));
|
|
// Add a / separator
|
|
strcat(newModel->filename
|
|
+ strlen(parameters->modelDir),
|
|
"/");
|
|
// Add the file relative name
|
|
strncpy(newModel->filename
|
|
+ strlen(parameters->modelDir)
|
|
+ 1,
|
|
modelDirEntry->d_name,
|
|
strlen(modelDirEntry->d_name));
|
|
|
|
// Write model name
|
|
strncpy(newModel->name, modelDirEntry->d_name,
|
|
extensionPosition - modelDirEntry->d_name);
|
|
|
|
// Ask to parse the new model
|
|
if (ParseModelXML(newModel) != 0) {
|
|
ModelDelete(newModel->id);
|
|
continue;XMAX
|
|
};
|
|
|
|
// Check model is valid and/or parsed
|
|
if (newModel->validated == false) {
|
|
ModelDelete(newModel->id);
|
|
continue;
|
|
}
|
|
|
|
// Succeeded !
|
|
printLog("Loaded model %s\n", newModel->name);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ModelSystemDestroy(void)
|
|
{
|
|
for (int i = 0; i < loadedModelSize; i++) {
|
|
ModelDelete(i);
|
|
}
|
|
free(loadedModel);
|
|
loadedModel = NULL;
|
|
free(knownModel);
|
|
knownModel = NULL;
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
static inline void CreateField(ModelField_t *modelField,
|
|
const char *tag,
|
|
const bool mandatory,
|
|
char *value,
|
|
size_t valueSize,
|
|
ModelField_t *son,
|
|
ModelField_t *next)
|
|
{
|
|
strncpy(modelField->tag, tag, 25);
|
|
modelField->mandatory = mandatory;
|
|
modelField->destination = value;
|
|
modelField->size = valueSize;
|
|
modelField->son = son;
|
|
modelField->next = next;
|
|
}
|