models create and run

This commit is contained in:
Adrien Bourmault 2021-07-07 19:46:23 +02:00
parent e0a7cfe0b3
commit 636758324f
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
3 changed files with 70 additions and 33 deletions

View File

@ -33,6 +33,8 @@ void ModelCreate(Model_t **newModel);
int ModelLoad(int id); int ModelLoad(int id);
void printModels(char *buf);
void ModelRun(int id); void ModelRun(int id);
void ModelStop(int id); void ModelStop(int id);

View File

@ -24,7 +24,7 @@
#include "../include/model.h" #include "../include/model.h"
#define LINE_LENGTH 80 #define LINE_LENGTH 80
#define LINE_NUMBER 15 #define LINE_NUMBER 50
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -95,7 +95,7 @@ char *CmdModel(char **argv, Server_t *args)
id = (int) strtol(argv[2] + 3, NULL, 10); id = (int) strtol(argv[2] + 3, NULL, 10);
if (id == 0 && (eid = ModelLoad(id)) <= 0) { if (id == 0 || (eid = ModelLoad(id)) <= 0) {
strcat(buf, "Failed to load model id"); strcat(buf, "Failed to load model id");
tempsize = sprintf(temp, " %d\n", id); tempsize = sprintf(temp, " %d\n", id);
strcat(buf, temp); strcat(buf, temp);
@ -103,7 +103,7 @@ char *CmdModel(char **argv, Server_t *args)
} }
strcat(buf, "Model id"); strcat(buf, "Model id");
tempsize = sprintf(temp, " %d ", id); tempsize = sprintf(temp, " %d ", id); //XXX
strcat(buf, temp); strcat(buf, temp);
strcat(buf, "loaded with eid "); strcat(buf, "loaded with eid ");
tempsize = sprintf(temp, " %d\n", eid); tempsize = sprintf(temp, " %d\n", eid);
@ -142,6 +142,7 @@ char *CmdModel(char **argv, Server_t *args)
else if (strcmp(argv[1], "list") == 0) { else if (strcmp(argv[1], "list") == 0) {
strcat(buf, "You asked for us to list models\n"); strcat(buf, "You asked for us to list models\n");
printModels(buf);
} }
else if (strcmp(argv[1], "info") == 0) { else if (strcmp(argv[1], "info") == 0) {
@ -160,6 +161,7 @@ char *CmdModel(char **argv, Server_t *args)
else strcat(buf, "{create | delete | load | run | list | info}\n"); else strcat(buf, "{create | delete | load | run | list | info}\n");
CmdModelEnd: CmdModelEnd:
printLog("Sent: %s\n", buf);
return buf; return buf;
} }

View File

@ -35,10 +35,13 @@
#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1) #define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1)
static Model_t **loadedModel; static Model_t **loadedModel;
static int loadedModelIndex; static int loadedModelSize; // begins to 1
static Model_t **knownModel; static Model_t **knownModel;
static int knownModelIndex; static int knownModelSize; // begins to 1
Model_t *lastModel;
Model_t **lastModelAddr;
static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model); static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model);
static void ModelPrepareArrows(Space_t *globalDrawingSpace, static void ModelPrepareArrows(Space_t *globalDrawingSpace,
@ -46,57 +49,83 @@ static void ModelPrepareArrows(Space_t *globalDrawingSpace,
Model_t *model); Model_t *model);
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
//XXX problem with index and struct
void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots
{ {
// increment index
knownModelSize++;
// create socket
knownModel = knownModel =
(Model_t**) realloc(knownModel, ++knownModelIndex * sizeof(Model_t*)); (Model_t**) realloc(knownModel, knownModelSize * sizeof(Model_t*));
knownModel[knownModelIndex] = (Model_t*) calloc(1, sizeof(Model_t)); // populate socket
knownModel[knownModelIndex]->id = knownModelIndex; knownModel[knownModelSize-1] = (Model_t*) calloc(1, sizeof(Model_t));
*newModel = knownModel[knownModelIndex]; // populate model
knownModel[knownModelSize-1]->id = knownModelSize;
// return value
*newModel = knownModel[knownModelSize-1];
lastModel = knownModel[knownModelSize-1];
lastModelAddr = &knownModel[knownModelSize-1];
knownModel[knownModelIndex]->name = // cont. model population
knownModel[knownModelSize-1]->name =
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
knownModel[knownModelIndex]->space_xMax = XMAX; knownModel[knownModelSize-1]->space_xMax = XMAX;
knownModel[knownModelIndex]->space_yMax = YMAX; knownModel[knownModelSize-1]->space_yMax = YMAX;
knownModel[knownModelIndex]->space_zMax = ZMAX; knownModel[knownModelSize-1]->space_zMax = ZMAX;
knownModel[knownModelIndex]->nmaxThread = MAX_THREAD; knownModel[knownModelSize-1]->nmaxThread = MAX_THREAD;
knownModel[knownModelIndex]->nmaxCycles = MAX_CYCLES; knownModel[knownModelSize-1]->nmaxCycles = MAX_CYCLES;
knownModel[knownModelIndex]->siteNumber = SITE_NUMBER; 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);
}
} }
int ModelLoad(int id) // TODO unload ! int ModelLoad(int id) // TODO unload !
{ {
if (id <= 0 || id >= knownModelIndex) { printLog("id: %d\n", id);
if (id <= 0 || id > knownModelSize) {
return -1; return -1;
} }
printLog("Loading model id %d (/%d models)...\n", id, knownModelIndex); printLog("Loading model id %d (/%d models)...\n", id, knownModelSize);
// Creating structure for the Scheduler // Creating structure for the Scheduler
knownModel[id]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); knownModel[id-1]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t));
knownModel[id]->scheduler->globalDrawingSpace = knownModel[id-1]->scheduler->globalDrawingSpace =
(Space_t*) calloc(1, sizeof(Space_t)); // TODO free this (Space_t*) calloc(1, sizeof(Space_t)); // TODO free this
knownModel[id]->scheduler->nMaxThread = knownModel[id]->nmaxThread; knownModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread;
knownModel[id]->scheduler->nMaxCycles = knownModel[id]->nmaxCycles; knownModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles;
// Preparing global drawing space // Preparing global drawing space
ModelPrepareSpace(knownModel[id]->scheduler->globalDrawingSpace, ModelPrepareSpace(knownModel[id-1]->scheduler->globalDrawingSpace,
knownModel[id]); knownModel[id-1]);
knownModel[id]->scheduler->arrowArray = knownModel[id-1]->scheduler->arrowArray =
(ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this
ModelPrepareArrows(knownModel[id]->scheduler->globalDrawingSpace, ModelPrepareArrows(knownModel[id-1]->scheduler->globalDrawingSpace,
knownModel[id]->scheduler->arrowArray, knownModel[id]); knownModel[id-1]->scheduler->arrowArray, knownModel[id-1]);
loadedModel = loadedModel =
(Model_t**) realloc(loadedModel, ++loadedModelIndex * sizeof(Model_t*)); (Model_t**) realloc(loadedModel, ++loadedModelSize * sizeof(Model_t*));
loadedModel[loadedModelIndex] = knownModel[id]; loadedModel =
return loadedModelIndex; (Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*));
loadedModel[loadedModelSize-1] = knownModel[id-1];
return loadedModelSize;
} }
void ModelRun(int id) void ModelRun(int id)
@ -121,7 +150,7 @@ void ModelDelete(int id)
void ModelShutdown(void) void ModelShutdown(void)
{ {
for (int i = 0; i < loadedModelIndex; i++) { for (int i = 0; i < loadedModelSize; i++) {
ModelStop(i); ModelStop(i);
} }
} }
@ -129,13 +158,17 @@ void ModelShutdown(void)
void ModelSystemInit(void) void ModelSystemInit(void)
{ {
loadedModel = (Model_t**) calloc(1, sizeof(Model_t*)); loadedModel = (Model_t**) calloc(1, sizeof(Model_t*));
// XXX read known models from files // XXX read known models from files
knownModel = (Model_t**) calloc(1, sizeof(Model_t*)); knownModel = (Model_t**) calloc(1, sizeof(Model_t*));
knownModelSize = 0;
loadedModelSize = 0;
} }
void ModelSystemDestroy(void) void ModelSystemDestroy(void)
{ {
for (int i = 0; i < loadedModelIndex; i++) { for (int i = 0; i < loadedModelSize; i++) {
ModelDelete(i); ModelDelete(i);
} }
free(loadedModel); free(loadedModel);