Simplify error sending
This commit is contained in:
parent
5c06da2b6d
commit
56aab1cbec
1 changed files with 17 additions and 27 deletions
44
src/server.c
44
src/server.c
|
@ -503,9 +503,7 @@ void *Dispatcher(void *arg) {
|
|||
char *token;
|
||||
char *endptr = NULL;
|
||||
char *rest = message;
|
||||
char *error_message;
|
||||
char eot = EOT;
|
||||
size_t size_for_malloc;
|
||||
int n = 0, k = 0, sender_id, receiver_id;
|
||||
|
||||
/* Message should be: "<int sender_id> <char command> [<char* rest>]" */
|
||||
|
@ -528,9 +526,8 @@ void *Dispatcher(void *arg) {
|
|||
|
||||
/* Extract sender_id */
|
||||
if ((token = strtok_r(message, " ", &rest)) == NULL) {
|
||||
fprintf(stderr, "ERROR: strtok_r didn't return a token\n");
|
||||
error_message = "WARNING: Syntax error: missing arguments\n";
|
||||
cmd_send_message(SERVER_ID, sender_id, error_message);
|
||||
fprintf(stderr, "WARNING: strtok_r didn't return a token\n");
|
||||
cmd_send_message(SERVER_ID, sender_id, "WARNING: Syntax error: missing arguments\n");
|
||||
break;
|
||||
}
|
||||
sender_id = strtol(token, &endptr, 10);
|
||||
|
@ -541,9 +538,8 @@ void *Dispatcher(void *arg) {
|
|||
|
||||
/* Extract command */
|
||||
if ((token = strtok_r(rest, " ", &rest)) == NULL ) {
|
||||
fprintf(stderr, "ERROR: strtok_r didn't return a token\n");
|
||||
error_message = "WARNING: Syntax error: missing arguments\n";
|
||||
cmd_send_message(SERVER_ID, sender_id, error_message);
|
||||
fprintf(stderr, "WARNING: strtok_r didn't return a token\n");
|
||||
cmd_send_message(SERVER_ID, sender_id, "WARNING: Syntax error: missing arguments\n");
|
||||
break;
|
||||
}
|
||||
/* We don't check for string length, because only the first char will be used.
|
||||
|
@ -565,9 +561,8 @@ void *Dispatcher(void *arg) {
|
|||
case CMD_SET_NAME:
|
||||
printf("cmd_set_name (from %d)\n", sender_id);
|
||||
if ((token = strtok_r(rest, "\n", &rest)) == NULL) {
|
||||
fprintf(stderr, "ERROR: strtok_r didn't return a token\n");
|
||||
error_message = "WARNING: Syntax error: missing arguments\n";
|
||||
cmd_send_message(SERVER_ID, sender_id, error_message);
|
||||
fprintf(stderr, "WARNING: strtok_r didn't return a token\n");
|
||||
cmd_send_message(SERVER_ID, sender_id, "WARNING: Syntax error: missing arguments\n");
|
||||
break;
|
||||
}
|
||||
cmd_set_name(sender_id, token);
|
||||
|
@ -579,9 +574,8 @@ void *Dispatcher(void *arg) {
|
|||
case CMD_SET_GROUP:
|
||||
printf("cmd_set_group (from %d)\n", sender_id);
|
||||
if ((token = strtok_r(rest, "\n", &rest)) == NULL ) {
|
||||
fprintf(stderr, "ERROR: strtok_r didn't return a token\n");
|
||||
error_message = "WARNING: Syntax error: missing arguments\n";
|
||||
cmd_send_message(SERVER_ID, sender_id, error_message);
|
||||
fprintf(stderr, "WARNING: strtok_r didn't return a token\n");
|
||||
cmd_send_message(SERVER_ID, sender_id, "WARNING: Syntax error: missing arguments\n");
|
||||
break;
|
||||
}
|
||||
cmd_set_group(sender_id, token);
|
||||
|
@ -606,20 +600,17 @@ void *Dispatcher(void *arg) {
|
|||
|
||||
/* Extract receiver_id */
|
||||
if ((token = strtok_r(rest, " ", &rest)) == NULL) {
|
||||
fprintf(stderr, "ERROR: strtok_r didn't return a token\n");
|
||||
error_message = "WARNING: Syntax error: missing arguments\n";
|
||||
cmd_send_message(SERVER_ID, sender_id, error_message);
|
||||
fprintf(stderr, "WARNING: strtok_r didn't return a token\n");
|
||||
cmd_send_message(SERVER_ID, sender_id, "WARNING: Syntax error: missing arguments\n");
|
||||
break;
|
||||
}
|
||||
receiver_id = get_client_id_from_name(token);
|
||||
printf("token = %s\n", token);
|
||||
printf("receiver_id = %d\n", receiver_id);
|
||||
if (receiver_id == MAX_CLIENTS) {
|
||||
size_for_malloc = snprintf(NULL, 0, "WARNING: Receiver \"%s\" does not exist !", token);
|
||||
error_message = (char*) malloc(size_for_malloc + 1);
|
||||
snprintf(error_message, size_for_malloc + 1, "WARNING: Receiver \"%s\" does not exist !", token);
|
||||
fprintf(stderr, "%s", error_message);
|
||||
cmd_send_message(SERVER_ID, sender_id, error_message);
|
||||
sprintf(buf, "WARNING: Receiver \"%s\" does not exist !\n", token);
|
||||
fprintf(stderr, "%s", buf);
|
||||
cmd_send_message(SERVER_ID, sender_id, buf);
|
||||
break;
|
||||
}
|
||||
if (receiver_id < 0) {
|
||||
|
@ -630,11 +621,10 @@ void *Dispatcher(void *arg) {
|
|||
break;
|
||||
default:
|
||||
token = strtok_r(token, "\n", &rest);
|
||||
size_for_malloc = snprintf(NULL, 0, "WARNING: \"%s\" is not a command\n", token);
|
||||
error_message = (char*) malloc(size_for_malloc + 1);
|
||||
snprintf(error_message, size_for_malloc + 1, "WARNING: \"%s\" is not a command\n", token);
|
||||
fprintf(stderr, "%s", error_message);
|
||||
cmd_send_message(SERVER_ID, sender_id, error_message);
|
||||
sprintf(buf, "WARNING: \"%s\" is not a command\n", token);
|
||||
fprintf(stderr, "%s", buf);
|
||||
cmd_send_message(SERVER_ID, sender_id, buf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue