Move receiver_id parsing to Dispatcher

This commit is contained in:
flyingscorpio@clevo 2022-11-01 12:56:13 +01:00
parent 3c655cd491
commit 669574616d
2 changed files with 16 additions and 21 deletions

View file

@ -240,27 +240,10 @@ int cmd_list_connected(int sender_id) {
}
/* Function for CMD_SEND_MESSAGE */
int cmd_send_message(char *message, int sender_id) {
int receiver_id;
char *token;
char *endptr = NULL;
char *message_header;
int cmd_send_message(int sender_id, int receiver_id, char* message) {
char *formated_message;
size_t size_for_malloc;
/* Message should be: "<int receiver_id> <char* message>" */
/* Extract receiver_id */
token = strtok_r(message, " ", &message);
receiver_id = strtol(token, &endptr, 10);
if ((receiver_id < 0) || ((receiver_id == 0) && (endptr == token))) {
fprintf(stderr, "Invalid receiver ID \"%s\"\n", token);
return -1;
}
if (clients[receiver_id].id < 0) {
fprintf(stderr, "Receiver \"%d\" does not exist !\n", receiver_id);
}
printf("Message Sender : %d\n", sender_id);
printf("Message Receiver : %d\n", receiver_id);
printf("Message to send : %s\n", message);
@ -285,7 +268,7 @@ void *Dispatcher(void *arg) {
char *token;
char *endptr = NULL;
char *rest = message;
int n = 0, k = 0, sender_id;
int n = 0, k = 0, sender_id, receiver_id;
/* Message should be: "<int sender_id> <char command> [<char* rest>]" */
@ -324,7 +307,19 @@ void *Dispatcher(void *arg) {
break;
case 's':
printf("cmd_send_message (from %d)\n", sender_id);
cmd_send_message(rest, sender_id);
/* Extract receiver_id */
token = strtok_r(rest, " ", &rest);
receiver_id = strtol(token, &endptr, 10);
if ((receiver_id < 0) || ((receiver_id == 0) && (endptr == token))) {
fprintf(stderr, "Invalid receiver ID \"%s\"\n", token);
break;
}
if (clients[receiver_id].id < 0) {
fprintf(stderr, "Receiver \"%d\" does not exist !\n", receiver_id);
break;
}
cmd_send_message(sender_id, receiver_id, rest);
break;
default:
fprintf(stderr, "WARNING: %s is not a command\n", token);

View file

@ -26,7 +26,7 @@ int disconnect_client(int);
int cmd_list_connected(int);
/* Function for CMD_SEND_MESSAGE */
int cmd_send_message(char*, int);
int cmd_send_message(int, int, char*);
/* Thread, pop message from the message_buffer pipe, parse it and send it to the good ClientSender */
void *Dispatcher(void*);