WIP: server communicator
This commit is contained in:
parent
3d56a4ec61
commit
8fe9e2125f
|
@ -124,6 +124,12 @@ struct {
|
||||||
bool pleaseStop;
|
bool pleaseStop;
|
||||||
} typedef Server_t;
|
} typedef Server_t;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
pthread_t *id;
|
||||||
|
bool pleaseStop;
|
||||||
|
int socketfd;
|
||||||
|
} typedef ServerCommunication_t;
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -23,19 +23,19 @@
|
||||||
#include "../include/base.h"
|
#include "../include/base.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *CmdHelp(char**, Server_t*);
|
//char *CmdHelp(char **, Server_t *);
|
||||||
char *CmdModel(char**, Server_t*);
|
char *CmdModel(char*, char**, Server_t*);
|
||||||
char *CmdShutdown(char**, Server_t*);
|
char *CmdShutdown(char*, char**, Server_t*);
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
const char *name;
|
const char *name;
|
||||||
char* (*execute)(char**, Server_t*);
|
char* (*execute)(char*, char**, Server_t*);
|
||||||
const char *help;
|
const char *help;
|
||||||
} typedef Command_t;
|
} typedef Command_t;
|
||||||
|
|
||||||
Command_t cmdList[] =
|
Command_t cmdList[] =
|
||||||
{
|
{
|
||||||
{"help", CmdHelp, "Help command"},
|
//{"help", CmdHelp, "Help command"},
|
||||||
{"model", CmdModel, "Model command"},
|
{"model", CmdModel, "Model command"},
|
||||||
{"shutdown", CmdShutdown, "Shutdown command"},
|
{"shutdown", CmdShutdown, "Shutdown command"},
|
||||||
};
|
};
|
||||||
|
|
13
src/cli.c
13
src/cli.c
|
@ -41,19 +41,20 @@ void connectedCommunication(int sockfd)
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Zeroing buffer
|
// Zeroing buffer
|
||||||
bzero(sendBuff, sizeof(sendBuff));
|
bzero(sendBuff, sizeof(sendBuff));
|
||||||
printf("\ngem-graph console> ");
|
printf("gem-graph console> ");
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
// Read command from terminal
|
// Read command from terminal
|
||||||
while (( curChar= getchar()) != '\n')
|
while (( curChar= getchar()) != '\n') {
|
||||||
|
// XXX ARROWS
|
||||||
sendBuff[i++] = curChar;
|
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
|
// Quit if asked
|
||||||
if (strcmp(exitCommand, sendBuff) == 0) {
|
if (strcmp(exitCommand, sendBuff) == 0) {
|
||||||
return;
|
return;
|
||||||
|
|
18
src/cmds.c
18
src/cmds.c
|
@ -24,23 +24,15 @@
|
||||||
#include "../include/model.h"
|
#include "../include/model.h"
|
||||||
|
|
||||||
#define LINE_LENGTH 80
|
#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;
|
int id, eid;
|
||||||
Model_t *newModel;
|
Model_t *newModel;
|
||||||
|
|
||||||
buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char));
|
|
||||||
|
|
||||||
// invalid use
|
// invalid use
|
||||||
if (!argv[1]) {
|
if (!argv[1]) {
|
||||||
strcat(buf, "{create | delete | load | run | list | info}\n");
|
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");
|
else strcat(buf, "{create | delete | load | run | list | info}\n");
|
||||||
|
|
||||||
CmdModelEnd:
|
CmdModelEnd:
|
||||||
printLog("Sent: %s\n", buf);
|
|
||||||
return 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();
|
ModelShutdown();
|
||||||
strcat(buf, "All model shutted down\n");
|
strcat(buf, "All model shutted down\n");
|
||||||
|
|
||||||
|
|
30
src/server.c
30
src/server.c
|
@ -41,23 +41,30 @@ void ServerInit(Server_t *server)
|
||||||
|
|
||||||
#define SEND_BUFFER_SIZE 80 * 24
|
#define SEND_BUFFER_SIZE 80 * 24
|
||||||
#define RECEIVE_BUFFER_SIZE 80
|
#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 **argv = NULL;
|
||||||
char receiveBuff[RECEIVE_BUFFER_SIZE];
|
char receiveBuff[RECEIVE_BUFFER_SIZE];
|
||||||
char sendBuff[SEND_BUFFER_SIZE];
|
char sendBuff[SEND_BUFFER_SIZE];
|
||||||
char *commandReturn;
|
|
||||||
int tokenIndex;
|
int tokenIndex;
|
||||||
|
|
||||||
|
args = (ServerCommunication_t*) serverCommunication;
|
||||||
|
printLog("Server communicator #%lu online\n", *args->id);
|
||||||
|
|
||||||
//Accept and incoming connection
|
//Accept and incoming connection
|
||||||
while(!args->pleaseStop) {
|
while(!args->pleaseStop) {
|
||||||
|
|
||||||
// Zeroing buffer
|
// Zeroing buffer
|
||||||
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
|
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 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
|
// Print buffer which contains the client request
|
||||||
printLog("Client request : '%s'\n", receiveBuff);
|
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
|
// Execute command by first arg in cmdList
|
||||||
for (int i = 0; i < LEN(cmdList); i++) {
|
for (int i = 0; i < LEN(cmdList); i++) {
|
||||||
if (strcmp(cmdList[i].name, argv[0]) == 0) {
|
if (strcmp(cmdList[i].name, argv[0]) == 0) {
|
||||||
commandReturn = cmdList[i].execute(argv, args);
|
cmdList[i].execute(sendBuff, argv, args);
|
||||||
|
|
||||||
// Copy server message in buffer
|
|
||||||
strcpy(sendBuff, commandReturn);
|
|
||||||
free(commandReturn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// and send that buffer to client
|
// and send that buffer to client
|
||||||
write(sockfd, sendBuff, sizeof(sendBuff));
|
write(args->sockfd, sendBuff, sizeof(sendBuff));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printLog("End of communications \n");
|
||||||
free(argv);
|
free(argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,9 +140,11 @@ static void *serverMain(void *server)
|
||||||
goto serverExiting;
|
goto serverExiting;
|
||||||
}
|
}
|
||||||
|
|
||||||
printLog("Server listening...\n");
|
|
||||||
len = sizeof(cli);
|
len = sizeof(cli);
|
||||||
|
|
||||||
|
while (!args->pleaseStop) {
|
||||||
|
printLog("Server listening...\n");
|
||||||
|
|
||||||
// Accept the data packet from client
|
// Accept the data packet from client
|
||||||
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
|
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
|
||||||
if (connfd < 0) {
|
if (connfd < 0) {
|
||||||
|
@ -149,7 +155,7 @@ static void *serverMain(void *server)
|
||||||
|
|
||||||
// Function for chatting between client and server
|
// Function for chatting between client and server
|
||||||
serverCommunicationInstance(args, connfd);
|
serverCommunicationInstance(args, connfd);
|
||||||
|
}
|
||||||
serverExiting:
|
serverExiting:
|
||||||
// After communication ended close the socket
|
// After communication ended close the socket
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
Loading…
Reference in New Issue