gem-graph-server/src/server.c

191 lines
6.4 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
2021-07-08 00:10:36 +02:00
#include <netdb.h>
#include <netinet/in.h>
2021-06-16 18:10:04 +02:00
#include <sys/socket.h>
2021-07-09 02:53:54 +02:00
#include <fcntl.h>
#include <errno.h>
2021-06-16 18:10:04 +02:00
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
2021-07-08 18:35:14 +02:00
// Server init function //
2021-06-16 18:10:04 +02:00
// -------------------------------------------------------------------------- //
void ServerInit(Server_t *server)
2021-06-16 18:10:04 +02:00
{
pthread_create(&server->id, NULL, serverMain, server);
2021-06-16 18:10:04 +02:00
}
2021-07-08 00:35:22 +02:00
#define SEND_BUFFER_SIZE 80 * 24
2021-07-08 00:28:34 +02:00
#define RECEIVE_BUFFER_SIZE 80
2021-07-09 02:53:54 +02:00
void *serverCommunicationInstance(void *server)
2021-06-16 18:10:04 +02:00
{
2021-07-09 02:53:54 +02:00
ServerCommunication_t *args;
2021-06-23 17:07:43 +02:00
char **argv = NULL;
2021-07-08 00:28:34 +02:00
char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE];
2021-06-23 17:07:43 +02:00
int tokenIndex;
2021-06-17 12:05:03 +02:00
2021-07-09 02:53:54 +02:00
args = (ServerCommunication_t*) server;
2021-06-16 18:10:04 +02:00
//Accept and incoming connection
2021-06-17 21:34:50 +02:00
while(!args->pleaseStop) {
2021-06-16 19:44:48 +02:00
2021-07-08 00:10:36 +02:00
// Zeroing buffer
2021-07-08 00:28:34 +02:00
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
2021-07-08 18:14:46 +02:00
printLog("Waiting for commands...\n");
2021-06-16 19:44:48 +02:00
2021-07-08 00:10:36 +02:00
// Read the message from client and copy it in buffer
2021-07-09 02:53:54 +02:00
read(args->sockfd, receiveBuff, sizeof(receiveBuff));
2021-07-08 18:14:46 +02:00
// Ignore null-sized request
if (receiveBuff[0] == 0)
break;
2021-06-16 19:44:48 +02:00
2021-07-08 00:10:36 +02:00
// Print buffer which contains the client request
2021-07-08 00:28:34 +02:00
printLog("Client request : '%s'\n", receiveBuff);
2021-06-17 21:34:50 +02:00
2021-06-23 17:07:43 +02:00
// get args in an array
tokenIndex = 0;
argv = (char**) realloc(argv, 1 * sizeof(char*));
2021-07-08 00:28:34 +02:00
argv[0] = strtok(receiveBuff, " ");
2021-06-23 17:07:43 +02:00
while (argv[tokenIndex]) {
tokenIndex++;
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
argv[tokenIndex] = strtok(NULL, " ");
}
2021-07-08 00:10:36 +02:00
// Zeroing buffer
2021-07-08 18:49:27 +02:00
bzero(sendBuff, RECEIVE_BUFFER_SIZE);
2021-07-08 00:10:36 +02:00
// Execute command by first arg in cmdList
2021-06-23 17:07:43 +02:00
for (int i = 0; i < LEN(cmdList); i++) {
if (strcmp(cmdList[i].name, argv[0]) == 0) {
2021-07-09 02:53:54 +02:00
cmdList[i].execute(sendBuff, argv, args->parent);
2021-06-17 21:34:50 +02:00
}
2021-06-16 18:10:04 +02:00
}
2021-06-17 21:34:50 +02:00
2021-07-08 00:10:36 +02:00
// and send that buffer to client
2021-07-09 02:53:54 +02:00
write(args->sockfd, sendBuff, sizeof(sendBuff));
2021-06-16 18:10:04 +02:00
}
2021-06-16 19:44:48 +02:00
2021-07-09 02:53:54 +02:00
close(args->sockfd);
2021-07-08 18:14:46 +02:00
printLog("End of communications \n");
2021-06-23 17:07:43 +02:00
free(argv);
2021-07-09 02:53:54 +02:00
return NULL;
2021-07-08 00:10:36 +02:00
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
#define PORT 9000
static void *serverMain(void *server)
{
Server_t *args;
2021-07-09 02:53:54 +02:00
ServerCommunication_t serverSlots[50] = {0};
int connfd, flags, serverSlotIndex = 0;
2021-07-08 00:10:36 +02:00
uint len;
struct sockaddr_in servaddr, cli;
// Get args
args = (Server_t*) server;
printLog("Server #%lu online\n", args->id);
2021-07-08 00:10:36 +02:00
// Create socket
2021-07-09 02:53:54 +02:00
args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (args->sockfd == -1) {
2021-07-08 00:10:36 +02:00
printLog("Socket creation failed!\n");
2021-07-08 17:26:13 +02:00
goto serverExiting;
2021-07-08 00:10:36 +02:00
}
2021-07-09 02:53:54 +02:00
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;
}
2021-07-08 00:10:36 +02:00
// 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
2021-07-09 02:53:54 +02:00
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
2021-07-08 00:10:36 +02:00
printLog("Socket bind failed!\n");
2021-07-09 02:53:54 +02:00
close(args->sockfd);
goto serverExiting;
2021-07-08 00:10:36 +02:00
}
// Now server is ready to listen and verification
2021-07-09 02:53:54 +02:00
if ((listen(args->sockfd, 5)) != 0) {
2021-07-08 00:10:36 +02:00
printLog("Socket listening failed!\n");
2021-07-09 02:53:54 +02:00
close(args->sockfd);
2021-07-08 17:26:13 +02:00
goto serverExiting;
2021-07-08 00:10:36 +02:00
}
len = sizeof(cli);
2021-07-09 02:53:54 +02:00
printLog("Server listening...\n");
2021-07-08 18:14:46 +02:00
while (!args->pleaseStop) {
2021-07-08 00:10:36 +02:00
2021-07-08 18:14:46 +02:00
// Accept the data packet from client
2021-07-09 02:53:54 +02:00
connfd = accept(args->sockfd, (struct sockaddr*)&cli, &len);
2021-07-08 18:14:46 +02:00
if (connfd < 0) {
2021-07-09 02:53:54 +02:00
if (errno != EWOULDBLOCK) {
printLog("Server acccept failed!\n");
goto serverExiting;
}
} else {
printLog("Client accepted\n");
serverSlots[serverSlotIndex].sockfd = connfd;
serverSlots[serverSlotIndex].parent = args;
2021-07-08 00:10:36 +02:00
2021-07-09 02:53:54 +02:00
pthread_create(&serverSlots[serverSlotIndex].id,
NULL,
serverCommunicationInstance,
(void*)&serverSlots[serverSlotIndex]);
printLog("Server listening...\n");
}
2021-07-08 18:14:46 +02:00
}
2021-07-08 00:10:36 +02:00
2021-07-09 02:53:54 +02:00
serverExiting:
printLog("Server #%lu offline\n", args->id);
2021-06-16 19:44:48 +02:00
return NULL;
2021-06-16 18:10:04 +02:00
}