diff --git a/include/base.h b/include/base.h index 60dab17..17d70f6 100644 --- a/include/base.h +++ b/include/base.h @@ -69,6 +69,8 @@ struct { #define LOGMSG "[%s]" #define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__) +#define LEN(x) (sizeof(x) / sizeof((x)[0])) + /* -------------------------------------------------------------------------- */ // @@ -114,12 +116,6 @@ struct { // Server // -struct { - const char *name; - void* (*func)(void*); - const char *help; -} typedef Command_t; - struct { pthread_t *id; bool pleaseStop; diff --git a/include/server.h b/include/server.h index 6efa0a1..7c16fe2 100644 --- a/include/server.h +++ b/include/server.h @@ -24,12 +24,12 @@ #endif // -------------------------------------------------------------------------- // -// Server init function // +// Server init function // // -------------------------------------------------------------------------- // pthread_t *ServerInit(Server_t *server); // -------------------------------------------------------------------------- // -// Server destructor function // +// Server destructor function // // -------------------------------------------------------------------------- // static inline void ServerDestroy(Server_t *server) { @@ -37,7 +37,7 @@ static inline void ServerDestroy(Server_t *server) } // -------------------------------------------------------------------------- // -// Server wait function // +// Server wait function // // -------------------------------------------------------------------------- // static inline void ServerWait(Server_t *server) { diff --git a/src/server.c b/src/server.c index 8987615..70fa6d9 100644 --- a/src/server.c +++ b/src/server.c @@ -70,11 +70,6 @@ static inline int bindSocket(int newSocket) return effectiveSocket; } -// -// Commands understood by the server -// -// TODO dict of commands - // -------------------------------------------------------------------------- // // Server main function // // -------------------------------------------------------------------------- // @@ -86,8 +81,6 @@ static void *serverMain(void *server) char clientRequest[255]= {0}; char serverAnswer[255] = {0}; - const char *pMessage = "hello"; - // Get args args = (Server_t*) server; printLog("Server #%lu online\n", *args->id); @@ -95,16 +88,14 @@ static void *serverMain(void *server) //Create socket socketDescriptor = createSocket(); - if (socketDescriptor < 0) - { + if (socketDescriptor < 0) { printLog("Could not create socket\n"); return NULL; } printLog("Socket created\n"); //Bind - if( bindSocket(socketDescriptor) < 0) - { + if( bindSocket(socketDescriptor) < 0) { //print the error serverAnswer printLog("Socket bind failed\n"); return NULL; @@ -116,16 +107,14 @@ static void *serverMain(void *server) listen(socketDescriptor, 3); //Accept and incoming connection - while(!args->pleaseStop) - { + while(!args->pleaseStop) { printLog("Waiting for incoming connections...\n"); clientLen = sizeof(struct sockaddr_in); //accept connection from an incoming client effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientLen); - if (effectiveSocket < 0) - { + if (effectiveSocket < 0) { printLog("Acceptation failed\n"); return NULL; } @@ -133,26 +122,25 @@ static void *serverMain(void *server) printLog("Connection accepted\n"); memset(clientRequest, '\0', sizeof(clientRequest)); - memset(serverAnswer, '\0', sizeof(serverAnswer)); + strcpy(serverAnswer,"Invalid command\0"); - //Receive a reply from the client - if (recv(effectiveSocket, clientRequest, 200, 0) < 0) - { + //Receive REQUEST from client + if (recv(effectiveSocket, clientRequest, 200, 0) < 0) { printLog("Reception failed"); break; } - printLog("Client reply : %s\n",clientRequest); - if (strcmp(pMessage,clientRequest) == 0) - { - strcpy(serverAnswer,"Hello there."); + + printLog("Client request : %s\n",clientRequest); + + for (int i = 0; i < LEN(cmdList); i++) { + if (strcmp(cmdList[i].name, clientRequest) == 0) { + cmdList[i].execute(NULL); + strcpy(serverAnswer,"OK\0"); + } } - else - { - strcpy(serverAnswer,"Invalid command"); - } - // Send some data - if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) - { + + // REPLY to client + if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) { printLog("Send failed\n"); return NULL; }