Client can now actually send messages via command
Client can now specify the recipient of a message and type it. Messages for offline users are saved, unaccesible for now.
This commit is contained in:
@@ -13,10 +13,18 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "commands.h"
|
||||
#include "util/util.h"
|
||||
|
||||
#define send(queue_id, msgbuf_ptr) \
|
||||
msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
|
||||
|
||||
typedef struct {
|
||||
char sender[COMMAND_LENGTH];
|
||||
char command[COMMAND_LENGTH];
|
||||
char message[MESSAGE_LENGTH];
|
||||
int message_id;
|
||||
} saved_message_t;
|
||||
|
||||
typedef struct {
|
||||
char *id;
|
||||
bool logged_in;
|
||||
@@ -24,6 +32,9 @@ typedef struct {
|
||||
char nickname[16];
|
||||
int queue_id;
|
||||
long last_seen;
|
||||
// Things for keeping track of the saved messages
|
||||
saved_message_t saved_messages[20];
|
||||
int saved_message_count;
|
||||
} Client;
|
||||
|
||||
typedef struct {
|
||||
@@ -36,7 +47,7 @@ static void handle_hearbeat_timeouts(Client *clients, int client_count,
|
||||
int semaphore_id) {
|
||||
struct sembuf sem_op = {
|
||||
.sem_num = 0,
|
||||
.sem_op = 1,
|
||||
.sem_op = -1,
|
||||
.sem_flg = 0,
|
||||
};
|
||||
while (1) {
|
||||
@@ -53,7 +64,7 @@ static void handle_hearbeat_timeouts(Client *clients, int client_count,
|
||||
clients[i].queue_id = -1;
|
||||
}
|
||||
}
|
||||
sem_op.sem_op = -1;
|
||||
sem_op.sem_op = 1;
|
||||
semop(semaphore_id, &sem_op, 1);
|
||||
sleep(HEARTBEAT_TIMEOUT);
|
||||
}
|
||||
@@ -117,7 +128,7 @@ int main(int argc, char **argv) {
|
||||
case Login:
|
||||
printf("Received login request for id: %s\n", msgbuf.sender);
|
||||
Client *client = NULL;
|
||||
for (int i = 0; i < 1; i++) {
|
||||
for (int i = 0; i < client_count; i++) {
|
||||
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
||||
client = &clients[i];
|
||||
break;
|
||||
@@ -157,6 +168,7 @@ int main(int argc, char **argv) {
|
||||
.mtype = Login,
|
||||
.stype = ACK_ACCEPTED,
|
||||
};
|
||||
|
||||
printf("User accepted: %s\n", msgbuf.sender);
|
||||
send(msg_queue_id, &response);
|
||||
break;
|
||||
@@ -186,24 +198,102 @@ int main(int argc, char **argv) {
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Forwarding message: %s\n", msgbuf.message);
|
||||
// Now we have to decode if we are to forward this message to a user or a
|
||||
// group first character of command indicates that - u or g
|
||||
char target_type = msgbuf.command[0];
|
||||
char target_id[COMMAND_LENGTH];
|
||||
// Skip the first char and a separtor (:)
|
||||
strncpy(target_id, msgbuf.command + 2, COMMAND_LENGTH - 2);
|
||||
|
||||
// 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);
|
||||
if (target_type == 'u') {
|
||||
Client *target_client = NULL;
|
||||
for (int i = 0; i < client_count; i++) {
|
||||
if (strncmp(clients[i].id, target_id, COMMAND_LENGTH) == 0) {
|
||||
target_client = &clients[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (target_client == NULL) {
|
||||
// Target client not found or not logged in
|
||||
printf("Target client %s not found or not logged in\n", target_id);
|
||||
msgbuf_t response = {
|
||||
.mtype = Message,
|
||||
.stype = ERR_USER_NOT_FOUND,
|
||||
};
|
||||
send(client->queue_id, &response);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if the target is online or not, if they are not, store the
|
||||
// message
|
||||
if (!target_client->logged_in) {
|
||||
if (target_client->saved_message_count >= 20) {
|
||||
// Discard the oldest one and shift the rest to make room
|
||||
for (int i = 1; i < target_client->saved_message_count; i++) {
|
||||
target_client->saved_messages[i - 1] =
|
||||
target_client->saved_messages[i];
|
||||
}
|
||||
saved_message_t *saved_msg =
|
||||
&target_client
|
||||
->saved_messages[target_client->saved_message_count - 1];
|
||||
strncpy(saved_msg->sender, client->nickname, COMMAND_LENGTH);
|
||||
strncpy(saved_msg->message, msgbuf.message, MESSAGE_LENGTH);
|
||||
saved_msg->message_id = rand(); // Random id for the message
|
||||
printf("Message saved for offline client %s\n", target_id);
|
||||
|
||||
// Send out an ack for the sender
|
||||
msgbuf_t response = {
|
||||
.mtype = Message,
|
||||
.stype = ACK_ACCEPTED,
|
||||
};
|
||||
send(client->queue_id, &response);
|
||||
continue;
|
||||
}
|
||||
|
||||
saved_message_t *saved_msg =
|
||||
&target_client
|
||||
->saved_messages[target_client->saved_message_count++];
|
||||
strncpy(saved_msg->sender, client->nickname, COMMAND_LENGTH);
|
||||
strncpy(saved_msg->message, msgbuf.message, MESSAGE_LENGTH);
|
||||
saved_msg->message_id = rand(); // Random id for the message
|
||||
|
||||
msgbuf_t response = {
|
||||
.mtype = Message,
|
||||
.stype = ACK_ACCEPTED,
|
||||
};
|
||||
send(client->queue_id, &response);
|
||||
printf("Message saved for offline client %s\n", target_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Forwarding message to user %s: %s\n", target_client->id,
|
||||
msgbuf.message);
|
||||
msgbuf_t forward_msg = {
|
||||
.mtype = Message,
|
||||
.sender = "",
|
||||
};
|
||||
strncpy(forward_msg.sender, client->nickname, COMMAND_LENGTH);
|
||||
strncpy(forward_msg.message, msgbuf.message, MESSAGE_LENGTH);
|
||||
send(target_client->queue_id, &forward_msg);
|
||||
|
||||
forward_msg.stype = ACK_ACCEPTED;
|
||||
forward_msg.mtype = Signal;
|
||||
send(client->queue_id, &forward_msg);
|
||||
|
||||
printf("Forwarding message: %s\n", msgbuf.message);
|
||||
} else if (target_type == 'g') {
|
||||
// Forward to a group
|
||||
|
||||
} else {
|
||||
printf("Invalid target type: %c\n", target_type);
|
||||
msgbuf_t response = {
|
||||
.mtype = Message,
|
||||
.stype = ERR_INVALID_FORMAT,
|
||||
};
|
||||
send(client->queue_id, &response);
|
||||
continue;
|
||||
}
|
||||
|
||||
forward_msg.stype = ACK_ACCEPTED;
|
||||
forward_msg.mtype = Signal;
|
||||
send(client->queue_id, &forward_msg);
|
||||
|
||||
// TODO: Accepted and Delivered ack
|
||||
break;
|
||||
case Logout:
|
||||
|
||||
Reference in New Issue
Block a user