WIP: server communicator

This commit is contained in:
Adrien Bourmault 2021-07-08 18:14:46 +02:00
parent 3d56a4ec61
commit 8fe9e2125f
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
5 changed files with 49 additions and 48 deletions

View File

@ -124,6 +124,12 @@ struct {
bool pleaseStop;
} typedef Server_t;
struct {
pthread_t *id;
bool pleaseStop;
int socketfd;
} typedef ServerCommunication_t;
/* -------------------------------------------------------------------------- */
//

View File

@ -23,19 +23,19 @@
#include "../include/base.h"
#endif
char *CmdHelp(char**, Server_t*);
char *CmdModel(char**, Server_t*);
char *CmdShutdown(char**, Server_t*);
//char *CmdHelp(char **, Server_t *);
char *CmdModel(char*, char**, Server_t*);
char *CmdShutdown(char*, char**, Server_t*);
struct {
const char *name;
char* (*execute)(char**, Server_t*);
char* (*execute)(char*, char**, Server_t*);
const char *help;
} typedef Command_t;
Command_t cmdList[] =
{
{"help", CmdHelp, "Help command"},
//{"help", CmdHelp, "Help command"},
{"model", CmdModel, "Model command"},
{"shutdown", CmdShutdown, "Shutdown command"},
};

View File

@ -41,19 +41,20 @@ void connectedCommunication(int sockfd)
for (;;) {
// Zeroing buffer
bzero(sendBuff, sizeof(sendBuff));
printf("\ngem-graph console> ");
printf("gem-graph console> ");
i = 0;
// Read command from terminal
while (( curChar= getchar()) != '\n')
while (( curChar= getchar()) != '\n') {
// XXX ARROWS
sendBuff[i++] = curChar;
// Check null-sized string
if (sendBuff[0] == 0) {
continue;
}
// Do not send null-string
if (sendBuff[0] == 0)
continue;
// Quit if asked
if (strcmp(exitCommand, sendBuff) == 0) {
return;

View File

@ -24,23 +24,15 @@
#include "../include/model.h"
#define LINE_LENGTH 80
#define LINE_NUMBER 50
#define LINE_NUMBER 24
/* -------------------------------------------------------------------------- */
char *CmdHelp(char **argv, Server_t *args)
char *CmdModel(char *buf, char **argv, Server_t *args)
{
return "OK\n";
}
char *CmdModel(char **argv, Server_t *args)
{
char *buf;
int id, eid;
Model_t *newModel;
buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char));
// invalid use
if (!argv[1]) {
strcat(buf, "{create | delete | load | run | list | info}\n");
@ -150,15 +142,11 @@ char *CmdModel(char **argv, Server_t *args)
else strcat(buf, "{create | delete | load | run | list | info}\n");
CmdModelEnd:
printLog("Sent: %s\n", buf);
return buf;
}
char *CmdShutdown(char **argv, Server_t *args)
char *CmdShutdown(char *buf, char **argv, Server_t *args)
{
char *buf;
buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char));
ModelShutdown();
strcat(buf, "All model shutted down\n");

View File

@ -31,7 +31,7 @@ static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Server init function //
// Server init function //
// -------------------------------------------------------------------------- //
void ServerInit(Server_t *server)
{
@ -41,23 +41,30 @@ void ServerInit(Server_t *server)
#define SEND_BUFFER_SIZE 80 * 24
#define RECEIVE_BUFFER_SIZE 80
static void serverCommunicationInstance(Server_t *args, int sockfd)
static void serverCommunicationInstance(void *serverCommunication)
{
ServerCommunication_t *args;
char **argv = NULL;
char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE];
char *commandReturn;
int tokenIndex;
args = (ServerCommunication_t*) serverCommunication;
printLog("Server communicator #%lu online\n", *args->id);
//Accept and incoming connection
while(!args->pleaseStop) {
// Zeroing buffer
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
printLog("Waiting for incoming connections...\n");
printLog("Waiting for commands...\n");
// Read the message from client and copy it in buffer
read(sockfd, receiveBuff, sizeof(receiveBuff));
read(args->sockfd, receiveBuff, sizeof(receiveBuff));
// Ignore null-sized request
if (receiveBuff[0] == 0)
break;
// Print buffer which contains the client request
printLog("Client request : '%s'\n", receiveBuff);
@ -78,18 +85,15 @@ static void serverCommunicationInstance(Server_t *args, int sockfd)
// Execute command by first arg in cmdList
for (int i = 0; i < LEN(cmdList); i++) {
if (strcmp(cmdList[i].name, argv[0]) == 0) {
commandReturn = cmdList[i].execute(argv, args);
// Copy server message in buffer
strcpy(sendBuff, commandReturn);
free(commandReturn);
cmdList[i].execute(sendBuff, argv, args);
}
}
// and send that buffer to client
write(sockfd, sendBuff, sizeof(sendBuff));
write(args->sockfd, sendBuff, sizeof(sendBuff));
}
printLog("End of communications \n");
free(argv);
}
@ -136,20 +140,22 @@ static void *serverMain(void *server)
goto serverExiting;
}
printLog("Server listening...\n");
len = sizeof(cli);
// Accept the data packet from client
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) {
printLog("Server acccept failed!\n");
goto serverExiting;
while (!args->pleaseStop) {
printLog("Server listening...\n");
// 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");
// Function for chatting between client and server
serverCommunicationInstance(args, connfd);
}
printLog("Client accepted\n");
// Function for chatting between client and server
serverCommunicationInstance(args, connfd);
serverExiting:
// After communication ended close the socket
close(sockfd);