gem-graph-server/src/model.c

164 lines
5.9 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-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-06-23 11:37:47 +02:00
#define MAX_MODEL_NUMBER 1
2021-06-23 14:16:45 +02:00
#define ARROW_NUMBER 2
#define SITE_NUMBER 2
#define MAX_CYCLES 10
#define MAX_THREAD 0
#define XMAX 30
#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;
static int loadedModelIndex;
2021-06-21 14:26:09 +02:00
2021-06-23 14:16:45 +02:00
static Model_t **knownModel;
static int knownModelIndex;
static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model);
static void ModelPrepareArrows(Space_t *globalDrawingSpace,
ArrowArray_t *arrowArray,
Model_t *model);
/* -------------------------------------------------------------------------- */
void ModelSystemInit(void)
{
loadedModel = (Model_t**) calloc(MAX_MODEL_NUMBER, sizeof(Model_t*));
// XXX read known models from files
knownModel = (Model_t**) calloc(MAX_MODEL_NUMBER, sizeof(Model_t));
}
void ModelSystemDestroy(void)
{
free(loadedModel);
free(knownModel);
}
void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots
{
knownModel =
(Model_t**) realloc(knownModel, ++knownModelIndex * sizeof(Model_t));
knownModel[knownModelIndex] = (Model_t*) calloc(1, sizeof(Model_t));
knownModel[knownModelIndex]->id = knownModelIndex;
*newModel = knownModel[knownModelIndex];
knownModel[knownModelIndex]->space_xMax = XMAX;
knownModel[knownModelIndex]->space_yMax = YMAX;
knownModel[knownModelIndex]->space_zMax = ZMAX;
knownModel[knownModelIndex]->nmaxThread = MAX_THREAD;
knownModel[knownModelIndex]->nmaxCycles = MAX_CYCLES;
knownModel[knownModelIndex]->siteNumber = SITE_NUMBER;
}
int ModelLoad(int id) // TODO unload !
{
// Creating structure for the Scheduler
knownModel[id]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t));
knownModel[id]->scheduler->globalDrawingSpace =
(Space_t*) calloc(1, sizeof(Space_t));
knownModel[id]->scheduler->nMaxThread = knownModel[id]->nmaxThread;
knownModel[id]->scheduler->nMaxCycles = knownModel[id]->nmaxCycles;
// Preparing global drawing space
ModelPrepareSpace(knownModel[id]->scheduler->globalDrawingSpace,
knownModel[id]);
knownModel[id]->scheduler->arrowArray =
(ArrowArray_t*) calloc(1, sizeof(ArrowArray_t));
ModelPrepareArrows(knownModel[id]->scheduler->globalDrawingSpace,
knownModel[id]->scheduler->arrowArray, knownModel[id]);
loadedModel[loadedModelIndex] = knownModel[id];
return loadedModelIndex++;
}
void ModelRun(int id)
{
// Creating structure for the Scheduler
2021-06-23 17:33:46 +02:00
SchedInit(loadedModel[id]->scheduler);
2021-06-23 14:16:45 +02:00
}
void ModelStop(int id)
{
// Creating structure for the Scheduler
2021-06-23 17:33:46 +02:00
loadedModel[id]->scheduler->pleaseStop = true;
2021-06-23 17:07:43 +02:00
printLog("Model %d stopped!\n", id);
2021-06-23 14:16:45 +02:00
}
void ModelDelete(Model_t *newModel)
{
return;
}
2021-06-23 17:07:43 +02:00
void ModelShutdown(void)
{
for (int i = 0; i < loadedModelIndex; i++) {
ModelStop(i);
}
}
2021-06-23 14:16:45 +02:00
/* -------------------------------------------------------------------------- */
static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model)
2021-06-21 14:26:09 +02:00
{
2021-06-23 14:16:45 +02:00
globalDrawingSpace->size =
(model->space_xMax+1) * (model->space_yMax+1) * (model->space_zMax+1);
globalDrawingSpace->xMax = model->space_xMax;
globalDrawingSpace->yMax = model->space_yMax;
globalDrawingSpace->zMax = model->space_zMax;
2021-06-21 14:26:09 +02:00
globalDrawingSpace->space =
(SpaceUnit_t*) calloc(globalDrawingSpace->size, sizeof(SpaceUnit_t));
for (int i = 0; i < globalDrawingSpace->size; i++) {
2021-06-23 14:16:45 +02:00
globalDrawingSpace->space[i].nSite = model->siteNumber;
globalDrawingSpace->space[i].sites =
(Site_t*) calloc(model->siteNumber, sizeof(Site_t));
2021-06-21 14:26:09 +02:00
}
}
2021-06-23 14:16:45 +02:00
static void ModelPrepareArrows(Space_t *globalDrawingSpace,
ArrowArray_t *arrowArray,
Model_t *model)
2021-06-21 14:26:09 +02:00
{
2021-06-23 14:16:45 +02:00
arrowArray->array = (Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t));
arrowArray->size = ARROW_NUMBER; //XXX hardcoded
2021-06-21 14:26:09 +02:00
2021-06-23 14:16:45 +02:00
// Creating some arrows XXX random walking
globalDrawingSpace->space[3].sites[1].nArrow = 1;
2021-06-23 09:13:46 +02:00
arrowArray->array[0].siteId = 1;
arrowArray->array[0].x = 3;
2021-06-21 14:26:09 +02:00
2021-06-23 14:16:45 +02:00
globalDrawingSpace->space[4].sites[0].nArrow = 1;
2021-06-23 09:13:46 +02:00
arrowArray->array[1].siteId = 0;
arrowArray->array[1].x = 4;
2021-06-21 14:26:09 +02:00
}