Added client listing
This commit is contained in:
@@ -81,12 +81,25 @@ static void input_loop(int server_queue_id, const char *client_id) {
|
||||
if (send(server_queue_id, &msg) == -1) {
|
||||
perror("msgsnd(/msg) failed");
|
||||
}
|
||||
} else if (strncmp(buffer, "/list", 5) == 0) {
|
||||
// handle list
|
||||
} else if (strncmp(buffer, "/list ", 6) == 0) {
|
||||
char *offset = buffer + 6;
|
||||
|
||||
if (strcmp(offset, "active") != 0 && strcmp(offset, "all") != 0) {
|
||||
printf("Invalid list type. Use /list active or /list all\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
msgbuf_t msg = {.mtype = List_clients, .sender = ""};
|
||||
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
|
||||
strncpy(msg.command, offset, COMMAND_LENGTH - 1);
|
||||
if (send(server_queue_id, &msg) == -1) {
|
||||
perror("msgsnd(/list) failed");
|
||||
}
|
||||
} else if (strncmp(buffer, "/mute ", 6) == 0) {
|
||||
// handle mute
|
||||
} else if (strcmp(buffer, "/quit") == 0) {
|
||||
// exit
|
||||
// TODO: Would be nice to have a way to gracefully logout and not just
|
||||
// depend on heartbeats, maybe
|
||||
} else {
|
||||
printf("Unknown command\n");
|
||||
}
|
||||
@@ -160,18 +173,6 @@ int main(int argc, char *argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// /* Send a test message to the server */
|
||||
// msgbuf_t test = {.mtype = Message, .command = "u:test2", .sender = ""};
|
||||
// strncpy(test.sender, client_id, COMMAND_LENGTH - 1);
|
||||
// snprintf(test.message, MESSAGE_LENGTH, "Hello from %s", client_id);
|
||||
|
||||
// if (msgsnd(server_queue_id, &test, sizeof(test) - sizeof(long), 0) == -1) {
|
||||
// printf("DEBUG: test.mtype = %u\n", test.mtype);
|
||||
// perror("failed while sending a test message");
|
||||
// cleanup_queue(client_queue_id);
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
while (1) {
|
||||
msgbuf_t incoming;
|
||||
ssize_t got = msgrcv(client_queue_id, &incoming,
|
||||
@@ -190,12 +191,33 @@ int main(int argc, char *argv[]) {
|
||||
case Message:
|
||||
printf("%s: %s\n", incoming.sender, incoming.message);
|
||||
break;
|
||||
case List_clients: {
|
||||
// Check the command field for the list type
|
||||
if (strncmp(incoming.command, "active", COMMAND_LENGTH) == 0) {
|
||||
printf("Active clients:\n%s\n", incoming.message);
|
||||
} else {
|
||||
printf("All clients:\n%s\n", incoming.message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Signal:
|
||||
// Tell the user if the previous message was accepted and/or delivered
|
||||
if (incoming.stype == ACK_ACCEPTED) {
|
||||
printf("Message sent successfully.\n");
|
||||
} else if (incoming.stype == ACK_DELIVERED) {
|
||||
printf("Message delivered to the %s.\n", incoming.command);
|
||||
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
|
||||
printf("Your message could not be delivered: User not found.\n");
|
||||
} else {
|
||||
// Display whatever if I forgot to handle something
|
||||
printf("Your message could not be delivered: Error code %d.\n",
|
||||
incoming.stype);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
printf("Received unknown mtype=%ld stype=%d\n", (long)incoming.mtype,
|
||||
incoming.stype);
|
||||
printf("Received unknown command of mtype=%ld stype=%d\n",
|
||||
(long)incoming.mtype, incoming.stype);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user