Implemented hearbeat
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
#include "commands.h"
|
||||
#include "bits/types/struct_timeval.h"
|
||||
#include "sys/ipc.h"
|
||||
#include "sys/shm.h"
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/msg.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "commands.h"
|
||||
|
||||
#define send(queue_id, msgbuf_ptr) \
|
||||
msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
|
||||
|
||||
@@ -18,6 +23,7 @@ typedef struct {
|
||||
char *muted_clients;
|
||||
char nickname[16];
|
||||
int queue_id;
|
||||
long last_seen;
|
||||
} Client;
|
||||
|
||||
typedef struct {
|
||||
@@ -26,21 +32,56 @@ typedef struct {
|
||||
int client_count;
|
||||
} Group;
|
||||
|
||||
static void handle_hearbeat_timeouts(Client *clients, int client_count,
|
||||
int semaphore_id) {
|
||||
struct sembuf sem_op = {
|
||||
.sem_num = 0,
|
||||
.sem_op = 1,
|
||||
.sem_flg = 0,
|
||||
};
|
||||
while (1) {
|
||||
sem_op.sem_op = 1;
|
||||
semop(semaphore_id, &sem_op, 1);
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
for (int i = 0; i < client_count; i++) {
|
||||
if (clients[i].logged_in &&
|
||||
(tv.tv_sec - clients[i].last_seen) > HEARTBEAT_TIMEOUT) {
|
||||
printf("Client %s has timed out due to missed heartbeats\n",
|
||||
clients[i].id);
|
||||
clients[i].logged_in = false;
|
||||
clients[i].queue_id = -1;
|
||||
}
|
||||
}
|
||||
sem_op.sem_op = -1;
|
||||
semop(semaphore_id, &sem_op, 1);
|
||||
sleep(HEARTBEAT_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// TODO: Load config file with clients and groups
|
||||
int client_count = 1;
|
||||
int client_count = 2;
|
||||
int group_count = 3;
|
||||
Client *clients = malloc(sizeof(Client) * 9);
|
||||
int clients_memory_id =
|
||||
shmget(IPC_PRIVATE, sizeof(Client) * client_count, IPC_CREAT | 0666);
|
||||
Client *clients = shmat(clients_memory_id, NULL, 0);
|
||||
Group *groups = malloc(sizeof(Group) * 3);
|
||||
|
||||
clients[0] = (Client){
|
||||
.id = "the_only_one",
|
||||
.logged_in = false,
|
||||
.muted_clients = NULL,
|
||||
.nickname = "Kregielnia",
|
||||
};
|
||||
int semaphore_id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
|
||||
semctl(semaphore_id, 0, SETVAL, 1);
|
||||
|
||||
clients[0] = (Client){.id = "test1",
|
||||
.logged_in = false,
|
||||
.muted_clients = NULL,
|
||||
.nickname = "Kregielnia",
|
||||
.last_seen = 0};
|
||||
clients[1] = (Client){.id = "test2",
|
||||
.logged_in = false,
|
||||
.muted_clients = NULL,
|
||||
.nickname = "Bajzel",
|
||||
.last_seen = 0};
|
||||
|
||||
// key_t key = ftok(argv[0], 555);
|
||||
int server_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
|
||||
|
||||
if (server_queue_id == -1) {
|
||||
@@ -61,7 +102,11 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
close(id_file_handle);
|
||||
|
||||
// TODO: Fork here, for the heartbeat process than can sleep
|
||||
if (fork() == 0) {
|
||||
handle_hearbeat_timeouts(clients, client_count, semaphore_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
msgbuf_t msgbuf;
|
||||
while (1) {
|
||||
// Read from the ipc, then handle the command
|
||||
@@ -70,19 +115,19 @@ int main(int argc, char **argv) {
|
||||
|
||||
switch (msgbuf.mtype) {
|
||||
case Login:
|
||||
printf("Received login request for id: %s\n", msgbuf.command);
|
||||
printf("Received login request for id: %s\n", msgbuf.sender);
|
||||
Client *client = NULL;
|
||||
for (int i = 0; i < 1; i++) {
|
||||
if (strncmp(clients[i].id, msgbuf.command, COMMAND_LENGTH) == 0) {
|
||||
if (strncmp(clients[i].id, msgbuf.sender, 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);
|
||||
int msg_queue_id = atoi(msgbuf.command);
|
||||
if (client == NULL) {
|
||||
printf("User not found: %s\n", msgbuf.command);
|
||||
printf("User not found: %s\n", msgbuf.sender);
|
||||
msgbuf_t response = {
|
||||
.mtype = Login,
|
||||
.stype = ERR_USER_NOT_FOUND,
|
||||
@@ -93,7 +138,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
if (client->logged_in) {
|
||||
printf("User already logged in: %s\n", msgbuf.command);
|
||||
printf("User already logged in: %s\n", msgbuf.sender);
|
||||
msgbuf_t response = {
|
||||
.mtype = Login,
|
||||
.stype = ERR_ALREADY_LOGGED_IN,
|
||||
@@ -105,24 +150,26 @@ int main(int argc, char **argv) {
|
||||
|
||||
client->logged_in = true;
|
||||
client->queue_id = msg_queue_id;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
client->last_seen = tv.tv_sec;
|
||||
msgbuf_t response = {
|
||||
.mtype = Login,
|
||||
.stype = ACK_ACCEPTED,
|
||||
};
|
||||
printf("User accepted: %s\n", msgbuf.command);
|
||||
printf("User accepted: %s\n", msgbuf.sender);
|
||||
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;
|
||||
}
|
||||
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,
|
||||
@@ -161,6 +208,26 @@ int main(int argc, char **argv) {
|
||||
break;
|
||||
case Logout:
|
||||
case Hearbeat:
|
||||
for (int i = 0; i < client_count; i++) {
|
||||
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0 &&
|
||||
clients[i].logged_in) {
|
||||
// Take the semaphore to update last_seen
|
||||
struct sembuf sem_op = {
|
||||
.sem_num = 0,
|
||||
.sem_op = -1,
|
||||
.sem_flg = 0,
|
||||
};
|
||||
semop(semaphore_id, &sem_op, 1);
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
clients[i].last_seen = tv.tv_sec;
|
||||
|
||||
// Free the semaphore
|
||||
sem_op.sem_op = 1;
|
||||
semop(semaphore_id, &sem_op, 1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case List_clients:
|
||||
break;
|
||||
case Mute:
|
||||
|
||||
Reference in New Issue
Block a user