From 97868beb393c3e17ce32c98a2c45ef1b50ac4041 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 30 Jun 2021 15:46:18 +0200 Subject: [PATCH] Model create + load cmd --- include/base.h | 1 + src/cli.c | 3 ++- src/cmds.c | 15 ++++++++++++++- src/model.c | 40 ++++++++++++++++++++++++---------------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/include/base.h b/include/base.h index 2f86ad2..0c01749 100644 --- a/include/base.h +++ b/include/base.h @@ -142,6 +142,7 @@ struct { // struct { int id; + char *name; int space_xMax; int space_yMax; int space_zMax; diff --git a/src/cli.c b/src/cli.c index 26382f1..ad2938f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -25,6 +25,7 @@ #include #define SERVER_IP_ADDR "127.0.0.1" +#define SERVER_PORT 90190 // -------------------------------------------------------------------------- // // Socket init function // @@ -42,7 +43,7 @@ static inline int createSocket(void) static inline int connectSocket(int newSocket) { int effectiveSocket = -1; - int serverPort = 90190; + int serverPort = SERVER_PORT; struct sockaddr_in remote = {0}; remote.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); diff --git a/src/cmds.c b/src/cmds.c index 9230b8b..3740758 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -55,7 +55,6 @@ char *CmdModel(char **argv, Server_t *args) } if (strncmp(argv[2], "name=", 5) == 0) { - // TODO get the model name if (strncmp(argv[3], "file=", 5) == 0) { // TODO get the file content (sent by the client) from args @@ -65,6 +64,8 @@ char *CmdModel(char **argv, Server_t *args) // TODO modify model according to things in file + strcpy(argv[2] + 5, newModel->name); + strcat(buf, "Model created with id"); tempsize = sprintf(temp, " %d\n", newModel->id); strcat(buf, temp); @@ -84,6 +85,18 @@ char *CmdModel(char **argv, Server_t *args) goto CmdModelEnd; } + else if (strcmp(argv[1], "load") == 0) { + if (!argv[2]) { + goto loadEnd; + } + // TODO get ID + loadEnd: + // invalid use + strcat(buf, "Loads a model structure\n"); + strcat(buf, "Usage: model load id=ID\n"); + goto CmdModelEnd; + } + else if (strcmp(argv[1], "delete") == 0) { if (!argv[2]) { goto deleteEnd; diff --git a/src/model.c b/src/model.c index 4e86d8d..713b61a 100644 --- a/src/model.c +++ b/src/model.c @@ -24,6 +24,7 @@ #include "../include/scheduler.h" #define MAX_MODEL_NUMBER 1 +#define MAX_MODEL_NAME_SIZE 255 #define ARROW_NUMBER 6 #define SITE_NUMBER 2 #define MAX_CYCLES 10 @@ -46,27 +47,16 @@ static void ModelPrepareArrows(Space_t *globalDrawingSpace, /* -------------------------------------------------------------------------- */ -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)); + (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]->name = + (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); knownModel[knownModelIndex]->space_xMax = XMAX; knownModel[knownModelIndex]->space_yMax = YMAX; knownModel[knownModelIndex]->space_zMax = ZMAX; @@ -104,6 +94,7 @@ void ModelRun(int id) { // Creating structure for the Scheduler SchedInit(loadedModel[id]->scheduler); + printLog("Model %d launched!\n", id); } void ModelStop(int id) @@ -113,9 +104,10 @@ void ModelStop(int id) printLog("Model %d stopped!\n", id); } -void ModelDelete(Model_t *newModel) +void ModelDelete(int id) { - return; + free(knownModel[id]->name); + free(knownModel[id]); } void ModelShutdown(void) @@ -125,6 +117,22 @@ void ModelShutdown(void) } } +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) +{ + for (int i = 0; i < loadedModelIndex; i++) { + ModelDelete(i); + } + free(loadedModel); + free(knownModel); +} + /* -------------------------------------------------------------------------- */ static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model)