tcp server

This commit is contained in:
Adrien Bourmault 2021-06-16 18:10:04 +02:00
parent c5d0568578
commit 2804fb42af
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
7 changed files with 232 additions and 36 deletions

View File

@ -61,9 +61,11 @@ tests: $(TESTS)
# ---- Build debian package -------------------------------------------------- # # ---- Build debian package -------------------------------------------------- #
$(BINDIR)/gem-graph-server.deb: all $(BINDIR)/gem-graph-server.deb: all
mkdir -p gem-graph-server/usr/bin mkdir -p gem-graph-server/usr/bin
mkdir -p gem-graph-server/usr/share/doc/gem-graph-server
mkdir -p gem-graph-server/DEBIAN mkdir -p gem-graph-server/DEBIAN
cp $(DEBDIR)/Manifest gem-graph-server/DEBIAN/control cp $(DEBDIR)/Manifest gem-graph-server/DEBIAN/control
cp $(BINDIR)/gem-graph-server gem-graph-server/usr/bin cp $(BINDIR)/gem-graph-server gem-graph-server/usr/bin
cp $(DEBDIR)/copyright gem-graph-server/usr/share/doc/gem-graph-server/copyright
dpkg-deb --build gem-graph-server dpkg-deb --build gem-graph-server
rm -rf gem-graph-server rm -rf gem-graph-server

View File

@ -106,3 +106,14 @@ struct {
bool terminated; bool terminated;
int returnValue; int returnValue;
} typedef Worker_t; } typedef Worker_t;
/* -------------------------------------------------------------------------- */
//
// Server
//
struct {
pthread_t *id;
bool pleaseStop;
} typedef Server_t;

View File

@ -22,3 +22,23 @@
#ifndef BASE_H #ifndef BASE_H
#include "../include/base.h" #include "../include/base.h"
#endif #endif
// -------------------------------------------------------------------------- //
// Server init function //
// -------------------------------------------------------------------------- //
pthread_t *ServerInit(Server_t *server);
// -------------------------------------------------------------------------- //
// Server destructor function //
// -------------------------------------------------------------------------- //
static inline void ServerDestroy(Server_t *server)
{
;
}
// -------------------------------------------------------------------------- //
// Server wait function //
// -------------------------------------------------------------------------- //
static inline void ServerWait(Server_t *server)
{
pthread_join(*server->id, NULL);

View File

@ -30,7 +30,7 @@ static void *workerMain(void *worker);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
pthread_t *WorkerInit(Worker_t *worker) pthread_t *WorkerInit(Worker_t *worker)
{ {
worker->id = (pthread_t*) malloc(sizeof(pthread_t)); worker->id = (pthread_t*) calloc(1, sizeof(pthread_t));
if (pthread_create(worker->id, NULL, workerMain, worker)) { if (pthread_create(worker->id, NULL, workerMain, worker)) {
printLog("Worker #%lu can't be initialized!\n", *(worker->id)); printLog("Worker #%lu can't be initialized!\n", *(worker->id));
return NULL; return NULL;

View File

@ -35,7 +35,8 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
time_t t; time_t t;
Scheduler_t *scheduler0; //Scheduler_t *scheduler0;
Server_t *server0;
// Go! // Go!
printLog("Starting gem-graph-server...\n"); printLog("Starting gem-graph-server...\n");
@ -49,51 +50,62 @@ int main(int argc, char **argv)
// //
// Creating structure for the Scheduler // Creating structure for the Scheduler
// //
scheduler0 = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); /* scheduler0 = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); */
scheduler0->globalDrawingSpace = /* scheduler0->globalDrawingSpace = */
(Space_t*) calloc(1, sizeof(Space_t)); /* (Space_t*) calloc(1, sizeof(Space_t)); */
scheduler0->globalDrawingSpace->space = /* scheduler0->globalDrawingSpace->space = */
(SpaceUnit_t*) calloc(SPACE_SIZE, sizeof(SpaceUnit_t)); /* (SpaceUnit_t*) calloc(SPACE_SIZE, sizeof(SpaceUnit_t)); */
scheduler0->globalDrawingSpace->size = SPACE_SIZE; /* scheduler0->globalDrawingSpace->size = SPACE_SIZE; */
scheduler0->globalDrawingSpace->xmax = SPACE_SIZE; /* scheduler0->globalDrawingSpace->xmax = SPACE_SIZE; */
scheduler0->globalDrawingSpace->ymax = SPACE_SIZE; /* scheduler0->globalDrawingSpace->ymax = SPACE_SIZE; */
scheduler0->globalDrawingSpace->zmax = SPACE_SIZE; /* scheduler0->globalDrawingSpace->zmax = SPACE_SIZE; */
scheduler0->arrowList = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); /* scheduler0->arrowList = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); */
scheduler0->arrowList->array = /* scheduler0->arrowList->array = */
(Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t)); /* (Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t)); */
scheduler0->arrowList->size = ARROW_NUMBER; /* scheduler0->arrowList->size = ARROW_NUMBER; */
printLog("Populating a random arrow list...\n"); /* printLog("Populating a random arrow list...\n"); */
for (int i = 0; i < ARROW_NUMBER; i++) { /* for (int i = 0; i < ARROW_NUMBER; i++) { */
if (scheduler0->globalDrawingSpace->xmax) /* if (scheduler0->globalDrawingSpace->xmax) */
scheduler0->arrowList->array[i].x = /* scheduler0->arrowList->array[i].x = */
rand() % (scheduler0->globalDrawingSpace->xmax + 1); /* rand() % (scheduler0->globalDrawingSpace->xmax + 1); */
if (scheduler0->globalDrawingSpace->ymax) /* if (scheduler0->globalDrawingSpace->ymax) */
scheduler0->arrowList->array[i].y = /* scheduler0->arrowList->array[i].y = */
rand() % (scheduler0->globalDrawingSpace->ymax + 1); /* rand() % (scheduler0->globalDrawingSpace->ymax + 1); */
if (scheduler0->globalDrawingSpace->zmax) /* if (scheduler0->globalDrawingSpace->zmax) */
scheduler0->arrowList->array[i].z = /* scheduler0->arrowList->array[i].z = */
rand() % (scheduler0->globalDrawingSpace->zmax + 1); /* rand() % (scheduler0->globalDrawingSpace->zmax + 1); */
} /* } */
scheduler0->nmaxThread = MAX_THREAD; /* scheduler0->nmaxThread = MAX_THREAD; */
scheduler0->nmaxCycles = MAX_CYCLES; /* scheduler0->nmaxCycles = MAX_CYCLES; */
/* // */
/* // Creating the Scheduler thread */
/* // */
/* SchedInit(scheduler0); */
/* SchedWait(scheduler0); */
/* SchedDestroy(scheduler0); */
/* free(scheduler0); */
// //
// Creating the Scheduler thread // Creating structure for the server
// //
SchedInit(scheduler0); server0 = (Server_t*) calloc(1, sizeof(Server_t));
SchedWait(scheduler0); ServerInit(server0);
SchedDestroy(scheduler0); ServerWait(server0);
free(scheduler0); ServerDestroy(server0);
return 0; return 0;
} }

View File

@ -25,7 +25,7 @@
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
static void *schedulerMain(void *parameters); static void *schedulerMain(void *scheduler);
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -34,7 +34,7 @@ static void *schedulerMain(void *parameters);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
pthread_t *SchedInit(Scheduler_t *scheduler) pthread_t *SchedInit(Scheduler_t *scheduler)
{ {
scheduler->id = (pthread_t*) malloc(sizeof(pthread_t)); scheduler->id = (pthread_t*) calloc(1, sizeof(pthread_t));
pthread_create(scheduler->id, NULL, schedulerMain, scheduler); pthread_create(scheduler->id, NULL, schedulerMain, scheduler);
return scheduler->id; return scheduler->id;
} }

View File

@ -0,0 +1,151 @@
//=-------------------------------------------------------------------------=//
// 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"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Scheduler init function //
// -------------------------------------------------------------------------- //
pthread_t *ServerInit(Server_t *scheduler)
{
scheduler->id = (pthread_t*) calloc(1, sizeof(pthread_t));
pthread_create(scheduler->id, NULL, serverMain, scheduler);
return scheduler->id;
}
// -------------------------------------------------------------------------- //
// Socket init function //
// -------------------------------------------------------------------------- //
static inline int createSocket(void)
{
int hSocket;
printLog("Create the socket\n");
hSocket = socket(AF_INET, SOCK_STREAM, 0);
return hSocket;
}
// -------------------------------------------------------------------------- //
// Soecket binding function //
// -------------------------------------------------------------------------- //
static inline int bindSocket(int hSocket)
{
int iRetval=-1;
int ClientPort = 90190;
struct sockaddr_in remote= {0};
/* Internet address family */
remote.sin_family = AF_INET;
/* Any incoming interface */
remote.sin_addr.s_addr = htonl(INADDR_ANY);
remote.sin_port = htons(ClientPort); /* Local port */
iRetval = bind(hSocket,(struct sockaddr *)&remote,sizeof(remote));
return iRetval;
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
static void *serverMain(void *server)
{
Server_t *args;
int socketDescriptor, sock, clientLen, readSize;
struct sockaddr_in client;
char clientMessage[255]= {0};
char message[255] = {0};
const char *pMessage = "hello world!\n";
// Get args
args = (Server_t*) server;
//Create socket
socketDescriptor = createSocket();
if (socketDescriptor)
{
printLog("Could not create socket\n");
return NULL;
}
printLog("Socket created\n");
//Bind
if( bindSocket(socketDescriptor) < 0)
{
//print the error message
printLog("bind failed.\n");
return NULL;
}
printLog("bind done\n");
//Listen
listen(socketDescriptor, 3);
//Accept and incoming connection
while(!args->pleaseStop)
{
printLog("Waiting for incoming connections...\n");
clientLen = sizeof(struct sockaddr_in);
//accept connection from an incoming client
sock = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientLen);
if (sock < 0)
{
printLog("accept failed");
return NULL;
}
printLog("Connection accepted\n");
memset(clientMessage, '\0', sizeof clientMessage);
memset(message, '\0', sizeof message);
//Receive a reply from the client
if( recv(sock, clientMessage, 200, 0) < 0)
{
printLog("recv failed");
break;
}
printLog("Client reply : %s\n",clientMessage);
if(strcmp(pMessage,clientMessage)==0)
{
strcpy(message,"Hi there !");
}
else
{
strcpy(message,"Invalid Message !");
}
// Send some data
if( send(sock, message, strlen(message), 0) < 0)
{
printLog("Send failed");
return NULL;
}
close(sock);
}
return 0;
}