gem-graph-server/src/server.c

170 lines
6.1 KiB
C
Raw Normal View History

2021-06-16 18:10:04 +02:00
//=-------------------------------------------------------------------------=//
// 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 <https://www.gnu.org/licenses/>. //
//=-------------------------------------------------------------------------=//
#include "../include/base.h"
2021-06-17 12:05:03 +02:00
#include "../include/cmds.h"
2021-06-16 18:10:04 +02:00
#include <sys/socket.h>
#include <arpa/inet.h>
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Scheduler init function //
// -------------------------------------------------------------------------- //
2021-06-21 10:34:11 +02:00
void ServerInit(Server_t *scheduler)
2021-06-16 18:10:04 +02:00
{
scheduler->id = (pthread_t*) calloc(1, sizeof(pthread_t));
pthread_create(scheduler->id, NULL, serverMain, scheduler);
}
// -------------------------------------------------------------------------- //
// Socket init function //
// -------------------------------------------------------------------------- //
static inline int createSocket(void)
{
2021-06-16 19:44:48 +02:00
int newSocket;
2021-06-16 18:10:04 +02:00
printLog("Create the socket\n");
2021-06-16 19:44:48 +02:00
newSocket = socket(AF_INET, SOCK_STREAM, 0);
return newSocket;
2021-06-16 18:10:04 +02:00
}
// -------------------------------------------------------------------------- //
// Soecket binding function //
// -------------------------------------------------------------------------- //
2021-06-16 19:44:48 +02:00
static inline int bindSocket(int newSocket)
2021-06-16 18:10:04 +02:00
{
2021-06-16 19:44:48 +02:00
int effectiveSocket = -1;
int clientPort = 90190;
struct sockaddr_in remote = {0};
// Internet address family
2021-06-16 18:10:04 +02:00
remote.sin_family = AF_INET;
2021-06-16 19:44:48 +02:00
// Any incoming interface
2021-06-16 18:10:04 +02:00
remote.sin_addr.s_addr = htonl(INADDR_ANY);
2021-06-16 19:44:48 +02:00
remote.sin_port = htons(clientPort); // Local port
effectiveSocket = bind(newSocket,(struct sockaddr*)&remote,sizeof(remote));
return effectiveSocket;
2021-06-16 18:10:04 +02:00
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
static void *serverMain(void *server)
{
Server_t *args;
2021-06-21 10:57:58 +02:00
int socketDescriptor, effectiveSocket, clientRequestLength;
2021-06-16 18:10:04 +02:00
struct sockaddr_in client;
2021-06-16 19:44:48 +02:00
char clientRequest[255]= {0};
char serverAnswer[255] = {0};
2021-06-23 17:07:43 +02:00
char **argv = NULL;
char *commandReturn;
int tokenIndex;
2021-06-17 12:05:03 +02:00
2021-06-16 18:10:04 +02:00
// Get args
args = (Server_t*) server;
2021-06-16 19:44:48 +02:00
printLog("Server #%lu online\n", *args->id);
2021-06-16 18:10:04 +02:00
//Create socket
socketDescriptor = createSocket();
2021-06-17 21:34:50 +02:00
if (socketDescriptor < 0) {
2021-06-16 18:10:04 +02:00
printLog("Could not create socket\n");
return NULL;
}
printLog("Socket created\n");
//Bind
2021-06-17 21:34:50 +02:00
if( bindSocket(socketDescriptor) < 0) {
2021-06-16 19:44:48 +02:00
//print the error serverAnswer
printLog("Socket bind failed\n");
2021-06-16 18:10:04 +02:00
return NULL;
}
2021-06-16 19:44:48 +02:00
printLog("Socket bind succeeded\n");
2021-06-16 18:10:04 +02:00
//Listen
listen(socketDescriptor, 3);
//Accept and incoming connection
2021-06-17 21:34:50 +02:00
while(!args->pleaseStop) {
2021-06-16 18:10:04 +02:00
printLog("Waiting for incoming connections...\n");
2021-06-21 10:57:58 +02:00
clientRequestLength = sizeof(struct sockaddr_in);
2021-06-16 19:44:48 +02:00
2021-06-16 18:10:04 +02:00
//accept connection from an incoming client
2021-06-21 10:57:58 +02:00
effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientRequestLength);
2021-06-16 19:44:48 +02:00
2021-06-17 21:34:50 +02:00
if (effectiveSocket < 0) {
2021-06-16 19:44:48 +02:00
printLog("Acceptation failed\n");
2021-06-16 18:10:04 +02:00
return NULL;
}
2021-06-16 19:44:48 +02:00
2021-06-16 18:10:04 +02:00
printLog("Connection accepted\n");
2021-06-16 19:44:48 +02:00
memset(clientRequest, '\0', sizeof(clientRequest));
2021-06-17 21:34:50 +02:00
strcpy(serverAnswer,"Invalid command\0");
2021-06-16 19:44:48 +02:00
2021-06-17 21:34:50 +02:00
//Receive REQUEST from client
if (recv(effectiveSocket, clientRequest, 200, 0) < 0) {
2021-06-16 19:44:48 +02:00
printLog("Reception failed");
2021-06-16 18:10:04 +02:00
break;
}
2021-06-17 21:34:50 +02:00
printLog("Client request : %s\n",clientRequest);
2021-06-23 17:07:43 +02:00
// 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);
2021-06-17 21:34:50 +02:00
}
2021-06-16 18:10:04 +02:00
}
2021-06-17 21:34:50 +02:00
// REPLY to client
if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) {
2021-06-16 19:44:48 +02:00
printLog("Send failed\n");
2021-06-16 18:10:04 +02:00
return NULL;
}
2021-06-16 19:44:48 +02:00
close(effectiveSocket);
2021-06-16 18:10:04 +02:00
}
2021-06-16 19:44:48 +02:00
2021-06-23 17:07:43 +02:00
free(argv);
2021-06-16 19:44:48 +02:00
printLog("Server #%lu offline\n", *args->id);
return NULL;
2021-06-16 18:10:04 +02:00
}