Server config load and graceful client exit

This commit is contained in:
2026-01-26 01:25:16 +01:00
parent 904efbd9b9
commit fe703b6ddd
7 changed files with 252 additions and 26 deletions
+55 -19
View File
@@ -12,7 +12,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "commands.h"
// commands.h already is included by util.h
#include "util/util.h"
#define send(queue_id, msgbuf_ptr) msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
@@ -68,32 +68,57 @@ static void handle_hearbeat_timeouts(Client_t *clients, int client_count, int se
}
int main(int argc, char **argv) {
// TODO: Load config file with clients and groups
int client_count = 2;
int group_count = 3;
server_config_t config;
// If we are given a path, try to load the config from there
if (argc >= 2) {
load_config(&config, argv[1]);
} else {
fprintf(stderr, "No config file provided, using default location example.conf\n");
load_config(&config, "./example.conf");
}
int client_count = config.client_count;
int group_count = config.group_count;
int clients_memory_id = shmget(IPC_PRIVATE, sizeof(Client_t) * client_count, IPC_CREAT | 0666);
Client_t *clients = shmat(clients_memory_id, NULL, 0);
Group_t *groups = malloc(sizeof(Group_t) * 3);
Group_t *groups = malloc(sizeof(Group_t) * group_count);
int semaphore_id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
semctl(semaphore_id, 0, SETVAL, 1);
clients[0] =
(Client_t){.id = "test1", .logged_in = false, .muted_clients = NULL, .nickname = "Kregielnia", .last_seen = 0};
clients[1] =
(Client_t){.id = "test2", .logged_in = false, .muted_clients = NULL, .nickname = "Bajzel", .last_seen = 0};
for (int i = 0; i < client_count; i++) {
strncpy(clients[i].id, config.clients[i].client_id, COMMAND_LENGTH - 1);
clients[i].logged_in = false;
clients[i].muted_clients = NULL;
strncpy(clients[i].nickname, config.clients[i].nickname, COMMAND_LENGTH - 1);
clients[i].queue_id = -1;
clients[i].last_seen = 0;
clients[i].saved_message_count = 0;
}
groups[0] = (Group_t){.name = "HelloChat", .client_count = 2, .members = NULL};
groups[0].members = malloc(sizeof(Client_t *) * client_count);
groups[0].members[0] = &clients[0];
groups[0].members[1] = &clients[1];
for (int i = 0; i < group_count; i++) {
strncpy(groups[i].name, config.groups[i].name, COMMAND_LENGTH - 2);
groups[i].client_count = config.groups[i].member_count;
groups[i].members = malloc(sizeof(Client_t *) * config.groups[i].member_count);
for (int j = 0; j < config.groups[i].member_count; j++) {
// Find the client with the given id
for (int k = 0; k < client_count; k++) {
if (strncmp(clients[k].id, config.groups[i].member_ids[j], COMMAND_LENGTH) == 0) {
groups[i].members[j] = &clients[k];
break;
}
}
}
}
groups[1] = (Group_t){.name = "Macarena", .client_count = 0, .members = NULL};
groups[1].members = malloc(sizeof(Client_t *) * client_count);
groups[2] = (Group_t){.name = "Bananownia", .client_count = 1, .members = NULL};
groups[2].members = malloc(sizeof(Client_t *) * client_count);
groups[2].members[0] = &clients[1];
// No longer useful after copying the data over
for (int i = 0; i < group_count; i++) {
for (int j = 0; j < config.groups[i].member_count; j++) {
free(config.groups[i].member_ids[j]);
}
}
free(config.clients);
free(config.groups);
int server_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
@@ -119,6 +144,8 @@ int main(int argc, char **argv) {
return 0;
}
printf("Server ready.\n");
msgbuf_t msgbuf;
while (1) {
// Read from the ipc, then handle the command
@@ -305,6 +332,15 @@ int main(int argc, char **argv) {
break;
}
case Logout: {
for (int i = 0; i < client_count; i++) {
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0 && clients[i].logged_in) {
clients[i].logged_in = false;
clients[i].queue_id = -1;
printf("Client %s logged out\n", msgbuf.sender);
break;
}
}
break;
}
case Hearbeat:
for (int i = 0; i < client_count; i++) {