Add parser and send_message

This commit is contained in:
debian 2022-10-27 15:07:44 +02:00
parent 283f4f2009
commit 69d0b56607

View file

@ -216,6 +216,21 @@ int disconnect_client(int client_id) {
return 0;
}
int send_message(char *message){
char *token;
if ( (token = strtok(message, " ")) != NULL ){
for(int i = 0; i < strlen(token); i++){
if (token[i] < 48 || token[i] > 57) {
fprintf(stderr, "Error: Receiver ID \"%s\" is not a number !\n", token);
return -1;
}
}
}
printf("Message to send : %s\n",message);
return 0;
}
/* Thread, parses and handles messages */
/* Thread, Dispatcher : pop message from the message_buffer pipe, parse it and send it to the good ClientSender */
void *Dispatcher(void *arg) {
@ -223,6 +238,9 @@ void *Dispatcher(void *arg) {
char message[1024]; // TODO: replace by malloc
int n = 0 ;
int k = 0 ;
int c = 0 ;
char *token;
while (1) {
/* Read 1 bytes at a time, because we don't know how long the message will be */
@ -232,13 +250,37 @@ void *Dispatcher(void *arg) {
message[k++] = '\0';
k = 0;
printf("Message received : %s", message);
/* TODO: parse(*message); Do we do that in a separate thread ? */
/* parse(message); Do we do that in a separate thread ? */
c = 0;
token = strtok(message, " ");
while (token != NULL){
if (c == 0){
switch (token[0]){
case 'l':
printf("Command list\n");
// TODO list function
break;
case 's':
printf("Command send\n");
send_message(message);
// TODO send function
break;
default:
fprintf(stderr, "WARNING: %s is not a command\n", token);
}
}
token = strtok(NULL, "");
printf("Here %s\n", token);
c++;
}
}
/* TODO: Writing Message into pipe */
}
if (n < 0) {
fprintf(stderr, "ERROR: Dispatcher failed to read from message_buffer\n");
}
if (n < 0) {
fprintf(stderr, "ERROR: Dispatcher failed to read from message_buffer\n");
break;
}
}
}
pthread_exit(0);