Model can be run

This commit is contained in:
Adrien Bourmault 2021-07-12 17:09:38 +02:00
parent 442c76e96c
commit 1d5463c1c5
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
4 changed files with 70 additions and 23 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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)