Implemented hearbeat

This commit is contained in:
2026-01-04 20:56:32 +01:00
parent 36ae15c320
commit 4a089ed368
3 changed files with 127 additions and 40 deletions
+35 -18
View File
@@ -1,16 +1,28 @@
#include "commands.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <unistd.h>
#include "commands.h"
#define send(queue_id, msgbuf_ptr) \
msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
static void heartbeat_loop(int server_queue_id, const char *client_id) {
msgbuf_t msg = {.mtype = Hearbeat, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
while (1) {
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(heartbeat) failed");
break;
}
sleep(HEARTBEAT_INTERVAL);
}
}
static void cleanup_queue(int qid) {
if (qid != -1) {
msgctl(qid, IPC_RMID, NULL);
@@ -33,21 +45,22 @@ int main(int argc, char *argv[]) {
int server_queue_id = -1;
const char *id_path = "/home/piotr/server_queue_id";
FILE *f = fopen(id_path, "rb");
int fd = open(id_path, O_RDONLY);
if (fd == -1) {
perror("Failed to open server queue id file");
cleanup_queue(client_queue_id);
return 1;
}
if (fread(&server_queue_id, sizeof(int), 1, f) != 1) {
if (read(fd, &server_queue_id, sizeof(int)) == -1) {
fprintf(stderr, "Failed to read server queue id from %s\n", id_path);
server_queue_id = -1;
}
fclose(f);
close(fd);
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);
msgbuf_t msg = {.mtype = Login, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
snprintf(msg.command, COMMAND_LENGTH, "%d", client_queue_id);
if (msgsnd(server_queue_id, &msg, sizeof(msg) - sizeof(long), 0) == -1) {
perror("msgsnd(login) failed");
@@ -73,11 +86,14 @@ int main(int argc, char *argv[]) {
printf("Login accepted for id '%s'\n", client_id);
if (fork() == 0) {
heartbeat_loop(server_queue_id, client_id);
return 0;
}
/* Send a test message to the server */
msgbuf_t test = {
.mtype = Message,
.sender = "the_only_one",
};
msgbuf_t test = {.mtype = Message, .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) {
@@ -90,7 +106,8 @@ int main(int argc, char *argv[]) {
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. */
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;
@@ -102,7 +119,7 @@ int main(int argc, char *argv[]) {
sizeof(incoming) - sizeof(long), 0, 0);
perror("Got a message damn");
if (got == -1) {
perror("msgrcv(incoming) failed");
perror("msgrcv(incoming) failed");
if (errno == EINTR) {
continue;