From 36ae15c320d4f50924e45aafb86c047b55de95c2 Mon Sep 17 00:00:00 2001 From: Piotr Kozak Date: Sun, 4 Jan 2026 16:24:36 +0100 Subject: [PATCH] A very messy demo of a broadcast --- .gitignore | 1 + Makefile | 15 +++++ client.c | 143 +++++++++++++++++++++++++++++++++++++++++++ commands.h | 33 ++++++++++ server.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 369 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 client.c create mode 100644 commands.h create mode 100644 server.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a58ee25 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CC = gcc +CFLAGS = -Wall -I. + +all: client server + +client: + mkdir -p build + $(CC) $(CFLAGS) -o build/client client.c + +server: + mkdir -p build + $(CC) $(CFLAGS) -o build/server server.c + +clean: + rm -f client server diff --git a/client.c b/client.c new file mode 100644 index 0000000..4d1492f --- /dev/null +++ b/client.c @@ -0,0 +1,143 @@ +#include "commands.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#define send(queue_id, msgbuf_ptr) \ + msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0) + +static void cleanup_queue(int qid) { + if (qid != -1) { + msgctl(qid, IPC_RMID, NULL); + } +} + +int main(int argc, char *argv[]) { + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + char *client_id = argv[1]; + + int client_queue_id = msgget(IPC_PRIVATE, IPC_CREAT | 0666); + if (client_queue_id == -1) { + perror("msgget(client) failed"); + return 1; + } + + int server_queue_id = -1; + const char *id_path = "/home/piotr/server_queue_id"; + FILE *f = fopen(id_path, "rb"); + + if (fread(&server_queue_id, sizeof(int), 1, f) != 1) { + fprintf(stderr, "Failed to read server queue id from %s\n", id_path); + server_queue_id = -1; + } + fclose(f); + + msgbuf_t msg; + memset(&msg, 0, sizeof(msgbuf_t)); + msg.mtype = Login; + /* command field holds client id for login */ + strncpy(msg.command, client_id, COMMAND_LENGTH - 1); + /* message field holds the client's queue id as string */ + snprintf(msg.message, MESSAGE_LENGTH, "%d", client_queue_id); + + if (msgsnd(server_queue_id, &msg, sizeof(msg) - sizeof(long), 0) == -1) { + perror("msgsnd(login) failed"); + cleanup_queue(client_queue_id); + return 1; + } + + /* Wait for login response (server replies with mtype = Login) */ + msgbuf_t resp; + ssize_t r = + msgrcv(client_queue_id, &resp, sizeof(resp) - sizeof(long), Login, 0); + if (r == -1) { + perror("msgrcv(login response) failed"); + cleanup_queue(client_queue_id); + return 1; + } + + if (resp.stype != ACK_ACCEPTED) { + fprintf(stderr, "Login rejected: code=%d\n", resp.stype); + cleanup_queue(client_queue_id); + return 1; + } + + printf("Login accepted for id '%s'\n", client_id); + + /* Send a test message to the server */ + msgbuf_t test = { + .mtype = Message, + .sender = "the_only_one", + }; + 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; + } + + printf("Sent test message to server: \"%s\"\n", test.message); + + /* Now wait for server forwarded message(s) and a signal ack. + We'll wait until we've seen both a Message and a Signal ack or timeout. */ + int seen_message = 0; + int seen_ack = 0; + const int MAX_ITER = 10; + + // for (int i = 0; i < MAX_ITER && !(seen_message && seen_ack); ++i) { + while (1) { + msgbuf_t incoming; + ssize_t got = msgrcv(client_queue_id, &incoming, + sizeof(incoming) - sizeof(long), 0, 0); + perror("Got a message damn"); + if (got == -1) { + perror("msgrcv(incoming) failed"); + + if (errno == EINTR) { + continue; + } + perror("msgrcv(incoming) failed"); + break; + } + + switch (incoming.mtype) { + case Message: + printf("Received Message: \"%s\"\n", incoming.message); + seen_message = 1; + break; + case Signal: + printf("Received Signal: code=%d\n", incoming.stype); + if (incoming.stype == ACK_ACCEPTED || incoming.stype == ACK_DELIVERED) { + seen_ack = 1; + } + break; + default: + printf("Received unknown mtype=%ld stype=%d\n", (long)incoming.mtype, + incoming.stype); + break; + } + } + + if (!seen_message) { + fprintf(stderr, "Did not receive forwarded message from server\n"); + } + if (!seen_ack) { + fprintf(stderr, "Did not receive ack from server\n"); + } + + /* Logout/cleanup: remove our queue */ + cleanup_queue(client_queue_id); + printf("Client exiting\n"); + return 0; +} diff --git a/commands.h b/commands.h new file mode 100644 index 0000000..910f05e --- /dev/null +++ b/commands.h @@ -0,0 +1,33 @@ +#define MESSAGE_LENGTH 256 +#define COMMAND_LENGTH 16 + +typedef enum { + Message = 1, + Login, + Logout, + Hearbeat, + List_clients, + Mute, + Unmute, + Signal +} CommandType; + +typedef enum { + ACK_ACCEPTED = 0, + ACK_DELIVERED, + ERR_UNKNOWN_COMMAND, + ERR_NOT_LOGGED_IN, + ERR_ALREADY_LOGGED_IN, + ERR_USER_NOT_FOUND, + ERR_USER_MUTED, + ERR_INVALID_FORMAT +} Signal_t; + +typedef struct { + CommandType mtype; + Signal_t stype; // Used with mtype = signal. Responses from the server - to specify errors or acks + char sender[COMMAND_LENGTH]; // Who sent the message + char command[COMMAND_LENGTH]; // Functions as an attribute to mtype - + // nicknames, id to send/mute to + char message[MESSAGE_LENGTH]; // Actual message content +} msgbuf_t; diff --git a/server.c b/server.c new file mode 100644 index 0000000..c5093a4 --- /dev/null +++ b/server.c @@ -0,0 +1,177 @@ +#include "commands.h" +#include "sys/ipc.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#define send(queue_id, msgbuf_ptr) \ + msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0) + +typedef struct { + char *id; + bool logged_in; + char *muted_clients; + char nickname[16]; + int queue_id; +} Client; + +typedef struct { + Client **clients; + char name[16]; + int client_count; +} Group; + +int main(int argc, char **argv) { + // TODO: Load config file with clients and groups + int client_count = 1; + int group_count = 3; + Client *clients = malloc(sizeof(Client) * 9); + Group *groups = malloc(sizeof(Group) * 3); + + clients[0] = (Client){ + .id = "the_only_one", + .logged_in = false, + .muted_clients = NULL, + .nickname = "Kregielnia", + }; + + // key_t key = ftok(argv[0], 555); + int server_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT); + + if (server_queue_id == -1) { + perror("msgget failed"); + return 1; + } + + int id_file_handle = + open("/home/piotr/server_queue_id", O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (!id_file_handle) { + perror("Failed to open file"); + return 1; + } + + if (write(id_file_handle, &server_queue_id, sizeof(int)) == -1) { + perror("Failed to write server queue id to file"); + return 1; + } + close(id_file_handle); + + // TODO: Fork here, for the heartbeat process than can sleep + msgbuf_t msgbuf; + while (1) { + // Read from the ipc, then handle the command + int read_status = + msgrcv(server_queue_id, &msgbuf, sizeof(msgbuf) - sizeof(long), 0, 0); + + switch (msgbuf.mtype) { + case Login: + printf("Received login request for id: %s\n", msgbuf.command); + Client *client = NULL; + for (int i = 0; i < 1; i++) { + if (strncmp(clients[i].id, msgbuf.command, COMMAND_LENGTH) == 0) { + client = &clients[i]; + break; + } + } + + // Reuse the buffer to get the client's message queue id + int msg_queue_id = atoi(msgbuf.message); + if (client == NULL) { + printf("User not found: %s\n", msgbuf.command); + msgbuf_t response = { + .mtype = Login, + .stype = ERR_USER_NOT_FOUND, + }; + + send(msg_queue_id, &response); + continue; + } + + if (client->logged_in) { + printf("User already logged in: %s\n", msgbuf.command); + msgbuf_t response = { + .mtype = Login, + .stype = ERR_ALREADY_LOGGED_IN, + }; + + send(msg_queue_id, &response); + continue; + } + + client->logged_in = true; + client->queue_id = msg_queue_id; + msgbuf_t response = { + .mtype = Login, + .stype = ACK_ACCEPTED, + }; + printf("User accepted: %s\n", msgbuf.command); + send(msg_queue_id, &response); + break; + case Message: + + printf("Recieved message, checking which client\n"); + // Find the client that sent the message then forward it to all the + // specified recipients + client = NULL; + for (int i = 0; i < client_count; i++) { + if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) { + client = &clients[i]; + break; + } + } + + printf("Received message from client id %s: %s\n", msgbuf.sender, + msgbuf.message); + + if (client == NULL || !client->logged_in) { + // Client not found or not logged in + printf("Client not found or not logged in"); + msgbuf_t response = { + .mtype = Message, + .stype = ERR_NOT_LOGGED_IN, + }; + send(read_status, &response); + continue; + } + + printf("Forwarding message: %s\n", msgbuf.message); + + // TODO: Find the actual recipients, for now broadcast + msgbuf_t forward_msg = { + .mtype = Message, + }; + strncpy(forward_msg.message, msgbuf.message, MESSAGE_LENGTH); + // Yes, the client sending the message also receives it + for (int i = 0; i < client_count; i++) { + if (clients[i].logged_in) { + send(clients[i].queue_id, &forward_msg); + } + } + + forward_msg.stype = ACK_ACCEPTED; + forward_msg.mtype = Signal; + send(client->queue_id, &forward_msg); + + // TODO: Accepted and Delivered ack + break; + case Logout: + case Hearbeat: + case List_clients: + break; + case Mute: + case Unmute: + break; + case Signal: + // Signals are sent from server to client, not the other way around + // so we ignore them here lol + break; + } + } + + return 0; +}