gem-graph-server/src/server.c

167 lines
5.8 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>
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
2021-07-08 18:14:46 +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
{
server->id = (pthread_t*) calloc(1, sizeof(pthread_t));
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-08 18:14:46 +02:00
static void serverCommunicationInstance(void *serverCommunication)
2021-06-16 18:10:04 +02:00
{
2021-07-08 18:14:46 +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-08 18:14:46 +02:00
args = (ServerCommunication_t*) serverCommunication;
printLog("Server communicator #%lu online\n", *args->id);
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-08 18:14:46 +02:00
read(args->sockfd, receiveBuff, sizeof(receiveBuff));
// 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 00:28:34 +02:00
snprintf(sendBuff, SEND_BUFFER_SIZE, "%s", "Invalid command!");
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-08 18:14:46 +02:00
cmdList[i].execute(sendBuff, argv, args);
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-08 18:14:46 +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-08 18:14:46 +02:00
printLog("End of communications \n");
2021-06-23 17:07:43 +02:00
free(argv);
2021-07-08 00:10:36 +02:00
}
// -------------------------------------------------------------------------- //
// 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");
2021-07-08 17:26:13 +02:00
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
if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
printLog("Socket bind failed!\n");
2021-07-08 17:26:13 +02:00
goto serverEarlyExiting;
2021-07-08 00:10:36 +02:00
}
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printLog("Socket listening failed!\n");
2021-07-08 17:26:13 +02:00
goto serverExiting;
2021-07-08 00:10:36 +02:00
}
len = sizeof(cli);
2021-07-08 18:14:46 +02:00
while (!args->pleaseStop) {
printLog("Server listening...\n");
2021-07-08 00:10:36 +02:00
2021-07-08 18:14:46 +02:00
// Accept the data packet from client
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) {
printLog("Server acccept failed!\n");
goto serverExiting;
}
printLog("Client accepted\n");
2021-07-08 00:10:36 +02:00
2021-07-08 18:14:46 +02:00
// Function for chatting between client and server
serverCommunicationInstance(args, connfd);
}
2021-07-08 17:26:13 +02:00
serverExiting:
2021-07-08 00:10:36 +02:00
// After communication ended close the socket
close(sockfd);
2021-07-08 17:26:13 +02:00
serverEarlyExiting:
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
}