Model can be run
This commit is contained in:
parent
442c76e96c
commit
1d5463c1c5
|
@ -35,8 +35,8 @@ int ModelLoad(int id);
|
||||||
|
|
||||||
void printModels(char *buf);
|
void printModels(char *buf);
|
||||||
|
|
||||||
void ModelRun(int id);
|
int ModelRun(int id);
|
||||||
|
|
||||||
void ModelStop(int id);
|
int ModelStop(int id);
|
||||||
|
|
||||||
void ModelShutdown(void);
|
void ModelShutdown(void);
|
||||||
|
|
14
src/cli.c
14
src/cli.c
|
@ -51,7 +51,7 @@ static char getch(void)
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
if(tcgetattr(0, &old) < 0) {
|
if(tcgetattr(0, &old) < 0) {
|
||||||
printLog("Error getting terminal settings!\n");
|
printLog("Error getting terminal settings! (%s)\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,12 +61,12 @@ static char getch(void)
|
||||||
old.c_cc[VTIME] = 0;
|
old.c_cc[VTIME] = 0;
|
||||||
|
|
||||||
if(tcsetattr(0, TCSANOW, &old) < 0) {
|
if(tcsetattr(0, TCSANOW, &old) < 0) {
|
||||||
printLog("Error setting terminal settings!\n");
|
printLog("Error setting terminal settings! (%s)\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(read(0, &buf, 1) < 0) {
|
if(read(0, &buf, 1) < 0) {
|
||||||
printLog("Error reading character!\n");
|
printLog("Error reading character! (%s)\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ static char getch(void)
|
||||||
old.c_lflag |= ECHO; // set echo mode
|
old.c_lflag |= ECHO; // set echo mode
|
||||||
|
|
||||||
if(tcsetattr(0, TCSADRAIN, &old) < 0) {
|
if(tcsetattr(0, TCSADRAIN, &old) < 0) {
|
||||||
printLog("Error resetting terminal settings!\n");
|
printLog("Error resetting terminal settings! (%s)\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
|
@ -230,7 +230,7 @@ int main(void)
|
||||||
// Socket creation
|
// Socket creation
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd == -1) {
|
if (sockfd == -1) {
|
||||||
printLog("Socket creation failed!\n");
|
printLog("Socket creation failed! (%s)\n", strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,11 +243,11 @@ int main(void)
|
||||||
|
|
||||||
// Connect to the server
|
// Connect to the server
|
||||||
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
|
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
|
||||||
printLog("Connection failed!\n");
|
printLog("Connection failed! (%s)\n", strerror(errno));
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
printLog("Connected!\n");
|
printLog("Connected!\n\n");
|
||||||
|
|
||||||
connectedCommunication(sockfd);
|
connectedCommunication(sockfd);
|
||||||
|
|
||||||
|
|
39
src/cmds.c
39
src/cmds.c
|
@ -37,7 +37,7 @@ char *CmdModel(char *buf, char **argv, Server_t *args)
|
||||||
|
|
||||||
// invalid use
|
// invalid use
|
||||||
if (!argv[1]) {
|
if (!argv[1]) {
|
||||||
strcat(buf, "{create | delete | load | run | list | info}\n");
|
strcat(buf, "{create | delete | load | run | stop | list | info}\n");
|
||||||
goto CmdModelEnd;
|
goto CmdModelEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,8 +89,7 @@ char *CmdModel(char *buf, char **argv, Server_t *args)
|
||||||
goto CmdModelEnd;
|
goto CmdModelEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(buf + strlen(buf), "Model id %d loaded with eid %d\n",
|
sprintf(buf + strlen(buf), "Model id %d loaded with eid %d\n", id, eid);
|
||||||
id, eid); //XXX
|
|
||||||
goto CmdModelEnd;
|
goto CmdModelEnd;
|
||||||
loadEnd:
|
loadEnd:
|
||||||
// invalid use
|
// invalid use
|
||||||
|
@ -115,7 +114,16 @@ char *CmdModel(char *buf, char **argv, Server_t *args)
|
||||||
if (!argv[2]) {
|
if (!argv[2]) {
|
||||||
goto runEnd;
|
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:
|
runEnd:
|
||||||
// invalid use
|
// invalid use
|
||||||
strcat(buf, "Run a model simulation\n");
|
strcat(buf, "Run a model simulation\n");
|
||||||
|
@ -123,6 +131,27 @@ char *CmdModel(char *buf, char **argv, Server_t *args)
|
||||||
goto CmdModelEnd;
|
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) {
|
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);
|
printModels(buf);
|
||||||
|
@ -141,7 +170,7 @@ char *CmdModel(char *buf, char **argv, Server_t *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// invalid use
|
// invalid use
|
||||||
else strcat(buf, "{create | delete | load | run | list | info}\n");
|
else strcat(buf, "{create | delete | load | run | stop | list | info}\n");
|
||||||
|
|
||||||
CmdModelEnd:
|
CmdModelEnd:
|
||||||
return buf;
|
return buf;
|
||||||
|
|
36
src/model.c
36
src/model.c
|
@ -93,7 +93,6 @@ void printModels(char *buf)
|
||||||
|
|
||||||
int ModelLoad(int id) // TODO unload !
|
int ModelLoad(int id) // TODO unload !
|
||||||
{
|
{
|
||||||
printLog("id: %d\n", id);
|
|
||||||
if (id <= 0 || id > knownModelSize) {
|
if (id <= 0 || id > knownModelSize) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -128,18 +127,37 @@ int ModelLoad(int id) // TODO unload !
|
||||||
return loadedModelSize;
|
return loadedModelSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelRun(int id)
|
int ModelRun(int id)
|
||||||
{
|
{
|
||||||
// Creating structure for the Scheduler
|
if (id <= 0 || id > loadedModelSize) {
|
||||||
SchedInit(loadedModel[id]->scheduler);
|
return 0;
|
||||||
printLog("Model %d launched!\n", id);
|
}
|
||||||
|
if (!loadedModel[id-1]->scheduler) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelStop(int id)
|
SchedInit(loadedModel[id-1]->scheduler);
|
||||||
|
printLog("Model %d launched\n", id);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ModelStop(int id)
|
||||||
{
|
{
|
||||||
// Creating structure for the Scheduler
|
if (id <= 0 || id > loadedModelSize) {
|
||||||
loadedModel[id]->scheduler->pleaseStop = true;
|
return 0;
|
||||||
printLog("Model %d stopped!\n", id);
|
}
|
||||||
|
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)
|
void ModelDelete(int id)
|
||||||
|
|
Loading…
Reference in New Issue