//=-------------------------------------------------------------------------=// // Server management module // // // // Copyright © 2021 The Gem-graph Project // // // // This file is part of gem-graph. // // // // This program is free software: you can redistribute it and/or modify // // it under the terms of the GNU Affero General Public License as // // published by the Free Software Foundation, either version 3 of the // // License, or (at your option) any later version. // // // // This program is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU Affero General Public License for more details. // // // // You should have received a copy of the GNU Affero General Public License // // along with this program. If not, see . // //=-------------------------------------------------------------------------=// #include "../include/base.h" #include "../include/cmds.h" #include #include #include static void *serverMain(void *server); /* -------------------------------------------------------------------------- */ // -------------------------------------------------------------------------- // // Server init function // // -------------------------------------------------------------------------- // void ServerInit(Server_t *server) { server->id = (pthread_t*) calloc(1, sizeof(pthread_t)); pthread_create(server->id, NULL, serverMain, server); } #define SEND_BUFFER_SIZE 80 #define RECEIVE_BUFFER_SIZE 80 static void serverCommunicationInstance(Server_t *args, int sockfd) { char **argv = NULL; char receiveBuff[RECEIVE_BUFFER_SIZE]; char sendBuff[SEND_BUFFER_SIZE]; char *commandReturn; int tokenIndex; //Accept and incoming connection while(!args->pleaseStop) { // Zeroing buffer bzero(receiveBuff, RECEIVE_BUFFER_SIZE); printLog("Waiting for incoming connections...\n"); // Read the message from client and copy it in buffer read(sockfd, receiveBuff, sizeof(receiveBuff)); // Print buffer which contains the client request printLog("Client request : '%s'\n", receiveBuff); // get args in an array tokenIndex = 0; argv = (char**) realloc(argv, 1 * sizeof(char*)); argv[0] = strtok(receiveBuff, " "); while (argv[tokenIndex]) { tokenIndex++; argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*)); argv[tokenIndex] = strtok(NULL, " "); } // Zeroing buffer snprintf(sendBuff, SEND_BUFFER_SIZE, "%s", "Invalid command!"); // 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); } } // and send that buffer to client write(sockfd, sendBuff, sizeof(sendBuff)); } free(argv); } // -------------------------------------------------------------------------- // // Server main function // // -------------------------------------------------------------------------- // #define PORT 9000 static void *serverMain(void *server) { Server_t *args; int sockfd, connfd; uint len; struct sockaddr_in servaddr, cli; // Get args args = (Server_t*) server; printLog("Server #%lu online\n", *args->id); // Create socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printLog("Socket creation failed!\n"); return NULL; } // Prepare binding structure bzero(&servaddr, sizeof(servaddr)); // Assign IP and PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); // Binding newly created socket if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) { printLog("Socket bind failed!\n"); return NULL; } // Now server is ready to listen and verification if ((listen(sockfd, 5)) != 0) { printLog("Socket listening failed!\n"); return NULL; } 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"); return NULL; } printLog("Client accepted\n"); // Function for chatting between client and server serverCommunicationInstance(args, connfd); // After communication ended close the socket close(sockfd); printLog("Server #%lu offline\n", *args->id); return NULL; }