gem-graph-server/src/model.c

249 lines
8.3 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-30 15:46:18 +02:00
#define MAX_MODEL_NAME_SIZE 255
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
static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model);
static void ModelPrepareArrows(Space_t *globalDrawingSpace,
ArrowArray_t *arrowArray,
Model_t *model);
/* -------------------------------------------------------------------------- */
void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots
{
2021-07-07 19:46:23 +02:00
// increment index
knownModelSize++;
// create socket
2021-06-23 14:16:45 +02:00
knownModel =
2021-07-07 19:46:23 +02:00
(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];
// cont. model population
knownModel[knownModelSize-1]->name =
2021-06-30 15:46:18 +02:00
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
2021-07-07 19:46:23 +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 = SITE_NUMBER;
}
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\n",
knownModel[i]->id, knownModel[i], knownModel[i]->name);
}
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
}
int ModelLoad(int id) // TODO unload !
{
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-07-07 19:46:23 +02:00
knownModel[id-1]->scheduler->globalDrawingSpace =
2021-07-02 15:06:11 +02:00
(Space_t*) calloc(1, sizeof(Space_t)); // TODO free this
2021-06-23 14:16:45 +02:00
2021-07-07 19:46:23 +02:00
knownModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread;
knownModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles;
2021-06-23 14:16:45 +02:00
// Preparing global drawing space
2021-07-07 19:46:23 +02:00
ModelPrepareSpace(knownModel[id-1]->scheduler->globalDrawingSpace,
knownModel[id-1]);
2021-06-23 14:16:45 +02:00
2021-07-07 19:46:23 +02:00
knownModel[id-1]->scheduler->arrowArray =
2021-07-02 15:06:11 +02:00
(ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this
2021-06-23 14:16:45 +02:00
2021-07-07 19:46:23 +02:00
ModelPrepareArrows(knownModel[id-1]->scheduler->globalDrawingSpace,
knownModel[id-1]->scheduler->arrowArray, knownModel[id-1]);
2021-06-23 14:16:45 +02:00
2021-07-02 15:06:11 +02:00
loadedModel =
2021-07-07 19:46:23 +02:00
(Model_t**) realloc(loadedModel, ++loadedModelSize * sizeof(Model_t*));
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*));
loadedModel[loadedModelSize-1] = knownModel[id-1];
return loadedModelSize;
2021-06-23 14:16:45 +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:09:38 +02:00
if (id <= 0 || id > loadedModelSize) {
return 0;
}
if (!loadedModel[id-1]->scheduler) {
return 0;
}
SchedInit(loadedModel[id-1]->scheduler);
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;
}
loadedModel[id-1]->scheduler->pleaseStop = true;
printLog("Model %d stop bit set\n", id);
SchedWait(loadedModel[id-1]->scheduler);
SchedDestroy(loadedModel[id-1]->scheduler);
loadedModel[id-1]->scheduler->pleaseStop = false;
return 1;
2021-06-23 14:16:45 +02:00
}
2021-06-30 15:46:18 +02:00
void ModelDelete(int id)
2021-06-23 14:16:45 +02:00
{
2021-06-30 15:46:18 +02:00
free(knownModel[id]->name);
free(knownModel[id]);
2021-06-23 14:16:45 +02:00
}
2021-06-23 17:07:43 +02:00
void ModelShutdown(void)
{
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-06-30 15:46:18 +02:00
void ModelSystemInit(void)
{
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-06-30 15:46:18 +02:00
// XXX read known models from files
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;
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);
free(knownModel);
}
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
2021-06-23 20:23:37 +02:00
/* In each cell: the West (left) site is 0, the East (right) site is 1 */
globalDrawingSpace->space[1].sites[1].nArrow = 1;
2021-06-23 09:13:46 +02:00
arrowArray->array[0].siteId = 1;
2021-06-23 20:23:37 +02:00
arrowArray->array[0].x = 1;
2021-06-21 14:26:09 +02:00
2021-06-23 20:23:37 +02:00
globalDrawingSpace->space[2].sites[0].nArrow = 1;
2021-06-23 09:13:46 +02:00
arrowArray->array[1].siteId = 0;
2021-06-23 20:23:37 +02:00
arrowArray->array[1].x = 2;
globalDrawingSpace->space[3].sites[1].nArrow = 1;
arrowArray->array[2].siteId = 1;
arrowArray->array[2].x = 3;
globalDrawingSpace->space[4].sites[0].nArrow = 1;
arrowArray->array[3].siteId = 0;
arrowArray->array[3].x = 4;
globalDrawingSpace->space[10].sites[1].nArrow = 1;
arrowArray->array[4].siteId = 1;
arrowArray->array[4].x = 10;
globalDrawingSpace->space[11].sites[0].nArrow = 1;
arrowArray->array[5].siteId = 0;
arrowArray->array[5].x = 11;
2021-06-21 14:26:09 +02:00
}