diff --git a/include/model.h b/include/model.h index 1975b32..a42819e 100644 --- a/include/model.h +++ b/include/model.h @@ -33,6 +33,8 @@ void ModelCreate(Model_t **newModel); int ModelLoad(int id); +void printModels(char *buf); + void ModelRun(int id); void ModelStop(int id); diff --git a/src/cmds.c b/src/cmds.c index 2df88f7..d7e99fa 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -24,7 +24,7 @@ #include "../include/model.h" #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); - if (id == 0 && (eid = ModelLoad(id)) <= 0) { + if (id == 0 || (eid = ModelLoad(id)) <= 0) { strcat(buf, "Failed to load model id"); tempsize = sprintf(temp, " %d\n", id); strcat(buf, temp); @@ -103,7 +103,7 @@ char *CmdModel(char **argv, Server_t *args) } strcat(buf, "Model id"); - tempsize = sprintf(temp, " %d ", id); + tempsize = sprintf(temp, " %d ", id); //XXX strcat(buf, temp); strcat(buf, "loaded with 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) { strcat(buf, "You asked for us to list models\n"); + printModels(buf); } 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"); CmdModelEnd: + printLog("Sent: %s\n", buf); return buf; } diff --git a/src/model.c b/src/model.c index 14ffb23..808d28d 100644 --- a/src/model.c +++ b/src/model.c @@ -35,10 +35,13 @@ #define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1) static Model_t **loadedModel; -static int loadedModelIndex; +static int loadedModelSize; // begins to 1 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 ModelPrepareArrows(Space_t *globalDrawingSpace, @@ -46,57 +49,83 @@ static void ModelPrepareArrows(Space_t *globalDrawingSpace, Model_t *model); /* -------------------------------------------------------------------------- */ -//XXX problem with index and struct void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots { + // increment index + knownModelSize++; + // create socket 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]; + (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]; - knownModel[knownModelIndex]->name = + // cont. model population + knownModel[knownModelSize-1]->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; - knownModel[knownModelIndex]->nmaxThread = MAX_THREAD; - knownModel[knownModelIndex]->nmaxCycles = MAX_CYCLES; - knownModel[knownModelIndex]->siteNumber = SITE_NUMBER; + 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); + } } int ModelLoad(int id) // TODO unload ! { - if (id <= 0 || id >= knownModelIndex) { + printLog("id: %d\n", id); + if (id <= 0 || id > knownModelSize) { 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 - 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 - knownModel[id]->scheduler->nMaxThread = knownModel[id]->nmaxThread; - knownModel[id]->scheduler->nMaxCycles = knownModel[id]->nmaxCycles; + knownModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; + knownModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; // Preparing global drawing space - ModelPrepareSpace(knownModel[id]->scheduler->globalDrawingSpace, - knownModel[id]); + ModelPrepareSpace(knownModel[id-1]->scheduler->globalDrawingSpace, + knownModel[id-1]); - knownModel[id]->scheduler->arrowArray = + knownModel[id-1]->scheduler->arrowArray = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this - ModelPrepareArrows(knownModel[id]->scheduler->globalDrawingSpace, - knownModel[id]->scheduler->arrowArray, knownModel[id]); + ModelPrepareArrows(knownModel[id-1]->scheduler->globalDrawingSpace, + knownModel[id-1]->scheduler->arrowArray, knownModel[id-1]); loadedModel = - (Model_t**) realloc(loadedModel, ++loadedModelIndex * sizeof(Model_t*)); + (Model_t**) realloc(loadedModel, ++loadedModelSize * sizeof(Model_t*)); - loadedModel[loadedModelIndex] = knownModel[id]; - return loadedModelIndex; + loadedModel = + (Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*)); + loadedModel[loadedModelSize-1] = knownModel[id-1]; + return loadedModelSize; } void ModelRun(int id) @@ -121,7 +150,7 @@ void ModelDelete(int id) void ModelShutdown(void) { - for (int i = 0; i < loadedModelIndex; i++) { + for (int i = 0; i < loadedModelSize; i++) { ModelStop(i); } } @@ -129,13 +158,17 @@ void ModelShutdown(void) void ModelSystemInit(void) { loadedModel = (Model_t**) calloc(1, sizeof(Model_t*)); + // XXX read known models from files knownModel = (Model_t**) calloc(1, sizeof(Model_t*)); + + knownModelSize = 0; + loadedModelSize = 0; } void ModelSystemDestroy(void) { - for (int i = 0; i < loadedModelIndex; i++) { + for (int i = 0; i < loadedModelSize; i++) { ModelDelete(i); } free(loadedModel);