//=-------------------------------------------------------------------------=//
// 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
#include
#include
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Server init function //
// -------------------------------------------------------------------------- //
void ServerInit(Server_t *server)
{
pthread_create(&server->id, NULL, serverMain, server);
}
#define SEND_BUFFER_SIZE 80 * 24
#define RECEIVE_BUFFER_SIZE 80
void *serverCommunicationInstance(void *server)
{
ServerCommunication_t *args;
char **argv = NULL;
char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE];
int tokenIndex;
args = (ServerCommunication_t*) server;
//Accept and incoming connection
while(!args->pleaseStop) {
// Zeroing buffer
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
printLog("Waiting for commands...\n");
// Read the message from client and copy it in buffer
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);
// 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
bzero(sendBuff, RECEIVE_BUFFER_SIZE);
// Execute command by first arg in cmdList
for (int i = 0; i < LEN(cmdList); i++) {
if (strcmp(cmdList[i].name, argv[0]) == 0) {
cmdList[i].execute(sendBuff, argv, args->parent);
}
}
// and send that buffer to client
write(args->sockfd, sendBuff, sizeof(sendBuff));
}
close(args->sockfd);
printLog("End of communications \n");
free(argv);
return NULL;
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
#define PORT 9000
static void *serverMain(void *server)
{
Server_t *args;
ServerCommunication_t serverSlots[50] = {0};
int connfd, flags, serverSlotIndex = 0;
uint len;
struct sockaddr_in servaddr, cli;
// Get args
args = (Server_t*) server;
printLog("Server #%lu online\n", args->id);
// Create socket
args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (args->sockfd == -1) {
printLog("Socket creation failed!\n");
goto serverExiting;
}
flags = fcntl(args->sockfd, F_GETFL);
if (flags == -1) {
printLog("Socket parameters getting failed!\n");
goto serverExiting;
}
if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
printLog("Socket non-blocking setting failed!\n");
goto serverExiting;
}
// 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(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
printLog("Socket bind failed!\n");
close(args->sockfd);
goto serverExiting;
}
// Now server is ready to listen and verification
if ((listen(args->sockfd, 5)) != 0) {
printLog("Socket listening failed!\n");
close(args->sockfd);
goto serverExiting;
}
len = sizeof(cli);
printLog("Server listening...\n");
while (!args->pleaseStop) {
// Accept the data packet from client
connfd = accept(args->sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) {
if (errno != EWOULDBLOCK) {
printLog("Server acccept failed!\n");
goto serverExiting;
}
} else {
printLog("Client accepted\n");
serverSlots[serverSlotIndex].sockfd = connfd;
serverSlots[serverSlotIndex].parent = args;
pthread_create(&serverSlots[serverSlotIndex].id,
NULL,
serverCommunicationInstance,
(void*)&serverSlots[serverSlotIndex]);
printLog("Server listening...\n");
}
}
serverExiting:
printLog("Server #%lu offline\n", args->id);
return NULL;
}