diff --git a/include/base.h b/include/base.h index 0c01749..6e59cac 100644 --- a/include/base.h +++ b/include/base.h @@ -124,6 +124,12 @@ struct { bool pleaseStop; } typedef Server_t; +struct { + pthread_t *id; + bool pleaseStop; + int socketfd; +} typedef ServerCommunication_t; + /* -------------------------------------------------------------------------- */ // diff --git a/include/cmds.h b/include/cmds.h index 146b18b..57d8db5 100644 --- a/include/cmds.h +++ b/include/cmds.h @@ -23,19 +23,19 @@ #include "../include/base.h" #endif -char *CmdHelp(char**, Server_t*); -char *CmdModel(char**, Server_t*); -char *CmdShutdown(char**, Server_t*); +//char *CmdHelp(char **, Server_t *); +char *CmdModel(char*, char**, Server_t*); +char *CmdShutdown(char*, char**, Server_t*); struct { const char *name; - char* (*execute)(char**, Server_t*); + char* (*execute)(char*, char**, Server_t*); const char *help; } typedef Command_t; Command_t cmdList[] = { - {"help", CmdHelp, "Help command"}, + //{"help", CmdHelp, "Help command"}, {"model", CmdModel, "Model command"}, {"shutdown", CmdShutdown, "Shutdown command"}, }; diff --git a/src/cli.c b/src/cli.c index 0eebee1..22d97b0 100644 --- a/src/cli.c +++ b/src/cli.c @@ -41,19 +41,20 @@ void connectedCommunication(int sockfd) for (;;) { // Zeroing buffer bzero(sendBuff, sizeof(sendBuff)); - printf("\ngem-graph console> "); + printf("gem-graph console> "); i = 0; // Read command from terminal - while (( curChar= getchar()) != '\n') + while (( curChar= getchar()) != '\n') { + // XXX ARROWS sendBuff[i++] = curChar; - - // Check null-sized string - if (sendBuff[0] == 0) { - continue; } + // Do not send null-string + if (sendBuff[0] == 0) + continue; + // Quit if asked if (strcmp(exitCommand, sendBuff) == 0) { return; diff --git a/src/cmds.c b/src/cmds.c index 17ec6e1..dafd60c 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -24,23 +24,15 @@ #include "../include/model.h" #define LINE_LENGTH 80 -#define LINE_NUMBER 50 +#define LINE_NUMBER 24 /* -------------------------------------------------------------------------- */ -char *CmdHelp(char **argv, Server_t *args) +char *CmdModel(char *buf, char **argv, Server_t *args) { - return "OK\n"; -} - -char *CmdModel(char **argv, Server_t *args) -{ - char *buf; int id, eid; Model_t *newModel; - buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char)); - // invalid use if (!argv[1]) { strcat(buf, "{create | delete | load | run | list | info}\n"); @@ -150,15 +142,11 @@ 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; } -char *CmdShutdown(char **argv, Server_t *args) +char *CmdShutdown(char *buf, char **argv, Server_t *args) { - char *buf; - buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char)); - ModelShutdown(); strcat(buf, "All model shutted down\n"); diff --git a/src/server.c b/src/server.c index f9ce48a..85a04b0 100644 --- a/src/server.c +++ b/src/server.c @@ -31,7 +31,7 @@ static void *serverMain(void *server); /* -------------------------------------------------------------------------- */ // -------------------------------------------------------------------------- // -// Server init function // +// Server init function // // -------------------------------------------------------------------------- // void ServerInit(Server_t *server) { @@ -41,23 +41,30 @@ void ServerInit(Server_t *server) #define SEND_BUFFER_SIZE 80 * 24 #define RECEIVE_BUFFER_SIZE 80 -static void serverCommunicationInstance(Server_t *args, int sockfd) +static void serverCommunicationInstance(void *serverCommunication) { + ServerCommunication_t *args; char **argv = NULL; char receiveBuff[RECEIVE_BUFFER_SIZE]; char sendBuff[SEND_BUFFER_SIZE]; - char *commandReturn; int tokenIndex; + args = (ServerCommunication_t*) serverCommunication; + printLog("Server communicator #%lu online\n", *args->id); + //Accept and incoming connection while(!args->pleaseStop) { // Zeroing buffer bzero(receiveBuff, RECEIVE_BUFFER_SIZE); - printLog("Waiting for incoming connections...\n"); + printLog("Waiting for commands...\n"); // Read the message from client and copy it in buffer - read(sockfd, receiveBuff, sizeof(receiveBuff)); + read(args->sockfd, receiveBuff, sizeof(receiveBuff)); + + // Ignore null-sized request + if (receiveBuff[0] == 0) + break; // Print buffer which contains the client request printLog("Client request : '%s'\n", receiveBuff); @@ -78,18 +85,15 @@ static void serverCommunicationInstance(Server_t *args, int sockfd) // Execute command by first arg in cmdList for (int i = 0; i < LEN(cmdList); i++) { if (strcmp(cmdList[i].name, argv[0]) == 0) { - commandReturn = cmdList[i].execute(argv, args); - - // Copy server message in buffer - strcpy(sendBuff, commandReturn); - free(commandReturn); + cmdList[i].execute(sendBuff, argv, args); } } // and send that buffer to client - write(sockfd, sendBuff, sizeof(sendBuff)); + write(args->sockfd, sendBuff, sizeof(sendBuff)); } + printLog("End of communications \n"); free(argv); } @@ -136,20 +140,22 @@ static void *serverMain(void *server) goto serverExiting; } - printLog("Server listening...\n"); len = sizeof(cli); - // Accept the data packet from client - connfd = accept(sockfd, (struct sockaddr*)&cli, &len); - if (connfd < 0) { - printLog("Server acccept failed!\n"); - goto serverExiting; + while (!args->pleaseStop) { + printLog("Server listening...\n"); + + // Accept the data packet from client + connfd = accept(sockfd, (struct sockaddr*)&cli, &len); + if (connfd < 0) { + printLog("Server acccept failed!\n"); + goto serverExiting; + } + printLog("Client accepted\n"); + + // Function for chatting between client and server + serverCommunicationInstance(args, connfd); } - printLog("Client accepted\n"); - - // Function for chatting between client and server - serverCommunicationInstance(args, connfd); - serverExiting: // After communication ended close the socket close(sockfd);