From 1d5463c1c55d805629c8e7e6972afe2d326d31c6 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 12 Jul 2021 17:09:38 +0200 Subject: [PATCH] Model can be run --- include/model.h | 4 ++-- src/cli.c | 14 +++++++------- src/cmds.c | 39 ++++++++++++++++++++++++++++++++++----- src/model.c | 36 +++++++++++++++++++++++++++--------- 4 files changed, 70 insertions(+), 23 deletions(-) diff --git a/include/model.h b/include/model.h index a42819e..49a3c02 100644 --- a/include/model.h +++ b/include/model.h @@ -35,8 +35,8 @@ int ModelLoad(int id); void printModels(char *buf); -void ModelRun(int id); +int ModelRun(int id); -void ModelStop(int id); +int ModelStop(int id); void ModelShutdown(void); diff --git a/src/cli.c b/src/cli.c index a5e2e18..1525b03 100644 --- a/src/cli.c +++ b/src/cli.c @@ -51,7 +51,7 @@ static char getch(void) fflush(stdout); if(tcgetattr(0, &old) < 0) { - printLog("Error getting terminal settings!\n"); + printLog("Error getting terminal settings! (%s)\n", strerror(errno)); return -1; } @@ -61,12 +61,12 @@ static char getch(void) old.c_cc[VTIME] = 0; if(tcsetattr(0, TCSANOW, &old) < 0) { - printLog("Error setting terminal settings!\n"); + printLog("Error setting terminal settings! (%s)\n", strerror(errno)); return -1; } if(read(0, &buf, 1) < 0) { - printLog("Error reading character!\n"); + printLog("Error reading character! (%s)\n", strerror(errno)); return -1; } @@ -74,7 +74,7 @@ static char getch(void) old.c_lflag |= ECHO; // set echo mode if(tcsetattr(0, TCSADRAIN, &old) < 0) { - printLog("Error resetting terminal settings!\n"); + printLog("Error resetting terminal settings! (%s)\n", strerror(errno)); return -1; } return buf; @@ -230,7 +230,7 @@ int main(void) // Socket creation sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { - printLog("Socket creation failed!\n"); + printLog("Socket creation failed! (%s)\n", strerror(errno)); return 1; } @@ -243,11 +243,11 @@ int main(void) // Connect to the server if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) { - printLog("Connection failed!\n"); + printLog("Connection failed! (%s)\n", strerror(errno)); return 2; } - printLog("Connected!\n"); + printLog("Connected!\n\n"); connectedCommunication(sockfd); diff --git a/src/cmds.c b/src/cmds.c index c7c2557..4100509 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -37,7 +37,7 @@ char *CmdModel(char *buf, char **argv, Server_t *args) // invalid use if (!argv[1]) { - strcat(buf, "{create | delete | load | run | list | info}\n"); + strcat(buf, "{create | delete | load | run | stop | list | info}\n"); goto CmdModelEnd; } @@ -89,8 +89,7 @@ char *CmdModel(char *buf, char **argv, Server_t *args) goto CmdModelEnd; } - sprintf(buf + strlen(buf), "Model id %d loaded with eid %d\n", - id, eid); //XXX + sprintf(buf + strlen(buf), "Model id %d loaded with eid %d\n", id, eid); goto CmdModelEnd; loadEnd: // invalid use @@ -115,7 +114,16 @@ char *CmdModel(char *buf, char **argv, Server_t *args) if (!argv[2]) { goto runEnd; } - // TODO run model + + id = (int) strtol(argv[2] + 3, NULL, 10); + + if (id == 0 || (!ModelRun(id))) { + sprintf(buf + strlen(buf), "Failed to run model id %d\n", id); + goto CmdModelEnd; + } + + sprintf(buf + strlen(buf), "Model id %d is running\n", id); + goto CmdModelEnd; runEnd: // invalid use strcat(buf, "Run a model simulation\n"); @@ -123,6 +131,27 @@ char *CmdModel(char *buf, char **argv, Server_t *args) goto CmdModelEnd; } + else if (strcmp(argv[1], "stop") == 0) { + if (!argv[2]) { + goto stopEnd; + } + + id = (int) strtol(argv[2] + 3, NULL, 10); + + if (id == 0 || (!ModelStop(id))) { + sprintf(buf + strlen(buf), "Failed to stop model id %d\n", id); + goto CmdModelEnd; + } + + sprintf(buf + strlen(buf), "Model id %d is running\n", id); + goto CmdModelEnd; + stopEnd: + // invalid use + strcat(buf, "Stop a model simulation\n"); + strcat(buf, "Usage: model stop id=ID\n"); + goto CmdModelEnd; + } + else if (strcmp(argv[1], "list") == 0) { strcat(buf, "You asked for us to list models\n"); printModels(buf); @@ -141,7 +170,7 @@ char *CmdModel(char *buf, char **argv, Server_t *args) } // invalid use - else strcat(buf, "{create | delete | load | run | list | info}\n"); + else strcat(buf, "{create | delete | load | run | stop | list | info}\n"); CmdModelEnd: return buf; diff --git a/src/model.c b/src/model.c index 808d28d..84ea909 100644 --- a/src/model.c +++ b/src/model.c @@ -93,7 +93,6 @@ void printModels(char *buf) int ModelLoad(int id) // TODO unload ! { - printLog("id: %d\n", id); if (id <= 0 || id > knownModelSize) { return -1; } @@ -128,18 +127,37 @@ int ModelLoad(int id) // TODO unload ! return loadedModelSize; } -void ModelRun(int id) +int ModelRun(int id) { - // Creating structure for the Scheduler - SchedInit(loadedModel[id]->scheduler); - printLog("Model %d launched!\n", id); + 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; } -void ModelStop(int id) +int ModelStop(int id) { - // Creating structure for the Scheduler - loadedModel[id]->scheduler->pleaseStop = true; - printLog("Model %d stopped!\n", id); + 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; } void ModelDelete(int id)