diff --git a/include/base.h b/include/base.h index fd7f6b5..2f86ad2 100644 --- a/include/base.h +++ b/include/base.h @@ -127,7 +127,7 @@ struct { /* -------------------------------------------------------------------------- */ // -// Server +// Supervisor // struct { pthread_t *id; diff --git a/include/cmds.h b/include/cmds.h index 741d765..146b18b 100644 --- a/include/cmds.h +++ b/include/cmds.h @@ -23,15 +23,19 @@ #include "../include/base.h" #endif -void *launchScheduler(void*); +char *CmdHelp(char**, Server_t*); +char *CmdModel(char**, Server_t*); +char *CmdShutdown(char**, Server_t*); struct { const char *name; - void* (*execute)(void*); + char* (*execute)(char**, Server_t*); const char *help; } typedef Command_t; Command_t cmdList[] = { - {"launch scheduler", launchScheduler, "Launches a scheduler instance"}, + {"help", CmdHelp, "Help command"}, + {"model", CmdModel, "Model command"}, + {"shutdown", CmdShutdown, "Shutdown command"}, }; diff --git a/include/model.h b/include/model.h index 29b4458..1975b32 100644 --- a/include/model.h +++ b/include/model.h @@ -32,3 +32,9 @@ int ModelLoad(int id); void ModelCreate(Model_t **newModel); int ModelLoad(int id); + +void ModelRun(int id); + +void ModelStop(int id); + +void ModelShutdown(void); diff --git a/include/server.h b/include/server.h index b2e4b3d..ce08e27 100644 --- a/include/server.h +++ b/include/server.h @@ -33,7 +33,7 @@ void ServerInit(Server_t *server); // -------------------------------------------------------------------------- // static inline void ServerDestroy(Server_t *server) { - ; + free(server->id); } // -------------------------------------------------------------------------- // diff --git a/src/cli.c b/src/cli.c index 1492d78..26382f1 100644 --- a/src/cli.c +++ b/src/cli.c @@ -102,8 +102,7 @@ static inline int receiveSocket(int effectiveSocket, char *serverAnswer, int main(int argc, char **argv) { - int socketDescriptor, nextSocketDescriptor, readSize; - struct sockaddr_in server; + int socketDescriptor; char clientMessage[255] = {0}; char serverReply[255] = {0}; @@ -142,7 +141,7 @@ int main(int argc, char **argv) sendSocket(socketDescriptor, clientMessage, strlen(clientMessage)); //Received the data from the server - readSize = receiveSocket(socketDescriptor, serverReply, 200); + receiveSocket(socketDescriptor, serverReply, 200); printf("%s\n", serverReply); memset(serverReply, 0, 255); diff --git a/src/cmds.c b/src/cmds.c index 862ddd8..0e11406 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -23,9 +23,58 @@ #include "../include/scheduler.h" #include "../include/model.h" +#define LINE_LENGTH 80 +#define LINE_NUMBER 15 + /* -------------------------------------------------------------------------- */ -void *CmdModel(void *args) +char *CmdHelp(char **argv, Server_t *args) { - + return "OK\n"; +} + +char *CmdModel(char **argv, Server_t *args) +{ + char *buf; + buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char)); + + if (!argv[1]) { + strcat(buf, "{create | delete | load | run}\n"); + goto CmdModelEnd; + } + + if (strcmp(argv[1], "create") == 0) { + strcat(buf, "You asked for us to create a model\n"); + } + + if (strcmp(argv[1], "delete") == 0) { + strcat(buf, "You asked for us to delete a model\n"); + } + + if (strcmp(argv[1], "load") == 0) { + strcat(buf, "You asked for us to load a model\n"); + } + + if (strcmp(argv[1], "run") == 0) { + strcat(buf, "You asked for us to run a model\n"); + } + +CmdModelEnd: + return buf; +} + +char *CmdShutdown(char **argv, Server_t *args) +{ + char *buf; + buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char)); + + ModelShutdown(); + strcat(buf, "All model shutted down\n"); + + args->pleaseStop = true; + strcat(buf, "Server stopping\n"); + + +CmdShutdownEnd: + return buf; } diff --git a/src/main.c b/src/main.c index d816b26..50b5c8f 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,7 @@ #include "../include/base.h" #include "../include/server.h" +#include "../include/model.h" int main(int argc, char **argv) @@ -31,19 +32,25 @@ int main(int argc, char **argv) // Go! printLog("Starting gem-graph-server...\n"); - // random generator + // Initializing random generator t = time(&t); srand((unsigned) t); server0 = (Server_t*) calloc(1, sizeof(Server_t)); + // Initializing model system + ModelSystemInit(); + // Launching server ServerInit(server0); + // Waiting for termination ServerWait(server0); // Exiting ServerDestroy(server0); + ModelSystemDestroy(); + free(server0); return 0; } diff --git a/src/model.c b/src/model.c index 88fd7c7..1d8ca74 100644 --- a/src/model.c +++ b/src/model.c @@ -110,6 +110,7 @@ void ModelStop(int id) { // Creating structure for the Scheduler knownModel[id]->scheduler->pleaseStop = true; + printLog("Model %d stopped!\n", id); } void ModelDelete(Model_t *newModel) @@ -117,6 +118,13 @@ void ModelDelete(Model_t *newModel) return; } +void ModelShutdown(void) +{ + for (int i = 0; i < loadedModelIndex; i++) { + ModelStop(i); + } +} + /* -------------------------------------------------------------------------- */ static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model) diff --git a/src/server.c b/src/server.c index ac55834..3298488 100644 --- a/src/server.c +++ b/src/server.c @@ -79,6 +79,9 @@ static void *serverMain(void *server) struct sockaddr_in client; char clientRequest[255]= {0}; char serverAnswer[255] = {0}; + char **argv = NULL; + char *commandReturn; + int tokenIndex; // Get args args = (Server_t*) server; @@ -131,10 +134,22 @@ static void *serverMain(void *server) printLog("Client request : %s\n",clientRequest); - for (int i = 0; i < LEN(cmdList); i++) { // TODO extract first command name - if (strcmp(cmdList[i].name, clientRequest) == 0) { - cmdList[i].execute(NULL); - strcpy(serverAnswer,"OK\0"); + // get args in an array + tokenIndex = 0; + argv = (char**) realloc(argv, 1 * sizeof(char*)); + argv[0] = strtok(clientRequest, " "); + while (argv[tokenIndex]) { + tokenIndex++; + argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*)); + argv[tokenIndex] = strtok(NULL, " "); + } + + // test first arg to find command in cmdList + for (int i = 0; i < LEN(cmdList); i++) { + if (strcmp(cmdList[i].name, argv[0]) == 0) { + commandReturn = cmdList[i].execute(argv, args); + strcpy(serverAnswer, commandReturn); + free(commandReturn); } } @@ -147,6 +162,7 @@ static void *serverMain(void *server) close(effectiveSocket); } + free(argv); printLog("Server #%lu offline\n", *args->id); return NULL;