From 2804fb42af6e3022fb2382ae1247d262116bd1fc Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 16 Jun 2021 18:10:04 +0200 Subject: [PATCH] tcp server --- Makefile | 2 + include/base.h | 11 ++++ include/server.h | 20 ++++++ src/localworker.c | 2 +- src/main.c | 78 ++++++++++++++---------- src/scheduler.c | 4 +- src/server.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 232 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 4290f4e..cc6ecf9 100644 --- a/Makefile +++ b/Makefile @@ -61,9 +61,11 @@ tests: $(TESTS) # ---- Build debian package -------------------------------------------------- # $(BINDIR)/gem-graph-server.deb: all mkdir -p gem-graph-server/usr/bin + mkdir -p gem-graph-server/usr/share/doc/gem-graph-server mkdir -p gem-graph-server/DEBIAN cp $(DEBDIR)/Manifest gem-graph-server/DEBIAN/control 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 rm -rf gem-graph-server diff --git a/include/base.h b/include/base.h index 1c51d47..2a50586 100644 --- a/include/base.h +++ b/include/base.h @@ -106,3 +106,14 @@ struct { bool terminated; int returnValue; } typedef Worker_t; + +/* -------------------------------------------------------------------------- */ + +// +// Server +// + +struct { + pthread_t *id; + bool pleaseStop; +} typedef Server_t; diff --git a/include/server.h b/include/server.h index c36c3e8..7de0ea9 100644 --- a/include/server.h +++ b/include/server.h @@ -22,3 +22,23 @@ #ifndef BASE_H #include "../include/base.h" #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); diff --git a/src/localworker.c b/src/localworker.c index bccd8dd..7d5e467 100644 --- a/src/localworker.c +++ b/src/localworker.c @@ -30,7 +30,7 @@ static void *workerMain(void *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)) { printLog("Worker #%lu can't be initialized!\n", *(worker->id)); return NULL; diff --git a/src/main.c b/src/main.c index 22a1b61..c94159d 100644 --- a/src/main.c +++ b/src/main.c @@ -35,7 +35,8 @@ int main(int argc, char **argv) { time_t t; - Scheduler_t *scheduler0; + //Scheduler_t *scheduler0; + Server_t *server0; // Go! printLog("Starting gem-graph-server...\n"); @@ -49,51 +50,62 @@ int main(int argc, char **argv) // // Creating structure for the Scheduler // - scheduler0 = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); + /* scheduler0 = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); */ - scheduler0->globalDrawingSpace = - (Space_t*) calloc(1, sizeof(Space_t)); - scheduler0->globalDrawingSpace->space = - (SpaceUnit_t*) calloc(SPACE_SIZE, sizeof(SpaceUnit_t)); - scheduler0->globalDrawingSpace->size = SPACE_SIZE; - scheduler0->globalDrawingSpace->xmax = SPACE_SIZE; - scheduler0->globalDrawingSpace->ymax = SPACE_SIZE; - scheduler0->globalDrawingSpace->zmax = SPACE_SIZE; + /* scheduler0->globalDrawingSpace = */ + /* (Space_t*) calloc(1, sizeof(Space_t)); */ + /* scheduler0->globalDrawingSpace->space = */ + /* (SpaceUnit_t*) calloc(SPACE_SIZE, sizeof(SpaceUnit_t)); */ + /* scheduler0->globalDrawingSpace->size = SPACE_SIZE; */ + /* scheduler0->globalDrawingSpace->xmax = SPACE_SIZE; */ + /* scheduler0->globalDrawingSpace->ymax = SPACE_SIZE; */ + /* scheduler0->globalDrawingSpace->zmax = SPACE_SIZE; */ - scheduler0->arrowList = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); - scheduler0->arrowList->array = - (Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t)); - scheduler0->arrowList->size = ARROW_NUMBER; + /* scheduler0->arrowList = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); */ + /* scheduler0->arrowList->array = */ + /* (Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t)); */ + /* scheduler0->arrowList->size = ARROW_NUMBER; */ - printLog("Populating a random arrow list...\n"); - for (int i = 0; i < ARROW_NUMBER; i++) { + /* printLog("Populating a random arrow list...\n"); */ + /* for (int i = 0; i < ARROW_NUMBER; i++) { */ - if (scheduler0->globalDrawingSpace->xmax) - scheduler0->arrowList->array[i].x = - rand() % (scheduler0->globalDrawingSpace->xmax + 1); + /* if (scheduler0->globalDrawingSpace->xmax) */ + /* scheduler0->arrowList->array[i].x = */ + /* rand() % (scheduler0->globalDrawingSpace->xmax + 1); */ - if (scheduler0->globalDrawingSpace->ymax) - scheduler0->arrowList->array[i].y = - rand() % (scheduler0->globalDrawingSpace->ymax + 1); + /* if (scheduler0->globalDrawingSpace->ymax) */ + /* scheduler0->arrowList->array[i].y = */ + /* rand() % (scheduler0->globalDrawingSpace->ymax + 1); */ - if (scheduler0->globalDrawingSpace->zmax) - scheduler0->arrowList->array[i].z = - rand() % (scheduler0->globalDrawingSpace->zmax + 1); - } + /* if (scheduler0->globalDrawingSpace->zmax) */ + /* scheduler0->arrowList->array[i].z = */ + /* rand() % (scheduler0->globalDrawingSpace->zmax + 1); */ + /* } */ - scheduler0->nmaxThread = MAX_THREAD; - scheduler0->nmaxCycles = MAX_CYCLES; + /* scheduler0->nmaxThread = MAX_THREAD; */ + /* 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; } diff --git a/src/scheduler.c b/src/scheduler.c index d8a99c3..8a709fe 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -25,7 +25,7 @@ #include -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) { - scheduler->id = (pthread_t*) malloc(sizeof(pthread_t)); + scheduler->id = (pthread_t*) calloc(1, sizeof(pthread_t)); pthread_create(scheduler->id, NULL, schedulerMain, scheduler); return scheduler->id; } diff --git a/src/server.c b/src/server.c index e69de29..2dfa559 100644 --- a/src/server.c +++ b/src/server.c @@ -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 . // +//=-------------------------------------------------------------------------=// + +#include "../include/base.h" + +#include +#include +#include +#include +#include +#include + +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; +}