Reworked msg command to be easier to use. Added group messaging.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/msg.h>
|
#include <sys/msg.h>
|
||||||
@@ -9,8 +10,7 @@
|
|||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
|
|
||||||
// Make a macro for this cause I'm not typing sizeof twice every time
|
// Make a macro for this cause I'm not typing sizeof twice every time
|
||||||
#define send(queue_id, msgbuf_ptr) \
|
#define send(queue_id, msgbuf_ptr) msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
|
||||||
msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
|
|
||||||
|
|
||||||
static void heartbeat_loop(int server_queue_id, const char *client_id) {
|
static void heartbeat_loop(int server_queue_id, const char *client_id) {
|
||||||
msgbuf_t msg = {.mtype = Hearbeat, .sender = ""};
|
msgbuf_t msg = {.mtype = Hearbeat, .sender = ""};
|
||||||
@@ -33,8 +33,7 @@ static void cleanup_queue(int qid) {
|
|||||||
static void input_loop(int server_queue_id, const char *client_id) {
|
static void input_loop(int server_queue_id, const char *client_id) {
|
||||||
while (1) {
|
while (1) {
|
||||||
// Minimum length plus extra space fore command name and seperators
|
// Minimum length plus extra space fore command name and seperators
|
||||||
// I'd prefer some more space than required to not have to worry about cut
|
// I'd prefer some more space than required to not have to worry about cutoffs
|
||||||
// offs
|
|
||||||
char buffer[COMMAND_LENGTH + MESSAGE_LENGTH + 64];
|
char buffer[COMMAND_LENGTH + MESSAGE_LENGTH + 64];
|
||||||
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
||||||
break;
|
break;
|
||||||
@@ -43,41 +42,32 @@ static void input_loop(int server_queue_id, const char *client_id) {
|
|||||||
buffer[strcspn(buffer, "\n")] = '\0';
|
buffer[strcspn(buffer, "\n")] = '\0';
|
||||||
|
|
||||||
if (strncmp(buffer, "/msg ", 5) == 0) {
|
if (strncmp(buffer, "/msg ", 5) == 0) {
|
||||||
// In a /msg command we expect {u|g}:<nickname up to 14 chars> <message up
|
|
||||||
// to 256 chars>
|
|
||||||
char *space_ptr = strchr(buffer + 5, ' ');
|
char *space_ptr = strchr(buffer + 5, ' ');
|
||||||
if (space_ptr == NULL) {
|
if (space_ptr == NULL) {
|
||||||
printf("Invalid /msg format. Use /msg u:<nickname> <message>\n");
|
printf("Invalid /msg format. Use /msg <nickname> or #<groupname> <message>\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
char target[COMMAND_LENGTH];
|
char target[COMMAND_LENGTH];
|
||||||
size_t target_len = space_ptr - (buffer + 5);
|
size_t target_len = space_ptr - (buffer + 5);
|
||||||
if (target_len >= COMMAND_LENGTH) {
|
|
||||||
printf("Target nickname too long\n");
|
if (target_len == 0 || target_len >= COMMAND_LENGTH) {
|
||||||
|
printf("Invalid target length\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
strncpy(target, buffer + 5, target_len);
|
|
||||||
|
strncpy(target, buffer + 5, target_len); // Copy everything including '#' if present
|
||||||
target[target_len] = '\0';
|
target[target_len] = '\0';
|
||||||
|
|
||||||
char *message = space_ptr + 1;
|
char *message = space_ptr + 1;
|
||||||
if (strlen(message) == 0) {
|
if (strlen(message) == 0) {
|
||||||
printf("Message cannot be empty\n");
|
printf("Message cannot be empty\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the format of the command string
|
|
||||||
if (target[1] != ':') {
|
|
||||||
printf("Invalid target format. Use u:<nickname> or g:<groupname>\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target[0] != 'u' && target[0] != 'g') {
|
|
||||||
printf("Invalid target type. Use 'u' for user or 'g' for group\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
msgbuf_t msg = {.mtype = Message, .sender = ""};
|
msgbuf_t msg = {.mtype = Message, .sender = ""};
|
||||||
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
|
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
|
||||||
snprintf(msg.command, COMMAND_LENGTH, "%s", target);
|
strncpy(msg.command, target, COMMAND_LENGTH - 1); // Send as-is (with '#' if group)
|
||||||
strncpy(msg.message, message, MESSAGE_LENGTH - 1);
|
strncpy(msg.message, message, MESSAGE_LENGTH - 1);
|
||||||
if (send(server_queue_id, &msg) == -1) {
|
if (send(server_queue_id, &msg) == -1) {
|
||||||
perror("msgsnd(/msg) failed");
|
perror("msgsnd(/msg) failed");
|
||||||
@@ -85,12 +75,12 @@ static void input_loop(int server_queue_id, const char *client_id) {
|
|||||||
} else if (strncmp(buffer, "/list ", 6) == 0) {
|
} else if (strncmp(buffer, "/list ", 6) == 0) {
|
||||||
char *offset = buffer + 6;
|
char *offset = buffer + 6;
|
||||||
|
|
||||||
if (strcmp(offset, "active") != 0 && strcmp(offset, "all") != 0) {
|
if (strcmp(offset, "active") != 0 && strcmp(offset, "all") != 0 && strcmp(offset, "groups") != 0) {
|
||||||
printf("Invalid list type. Use /list active or /list all\n");
|
printf("Invalid list type. Use /list active or /list all or /list groups\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
msgbuf_t msg = {.mtype = List_clients, .sender = ""};
|
msgbuf_t msg = {.mtype = List, .sender = ""};
|
||||||
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
|
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
|
||||||
strncpy(msg.command, offset, COMMAND_LENGTH - 1);
|
strncpy(msg.command, offset, COMMAND_LENGTH - 1);
|
||||||
if (send(server_queue_id, &msg) == -1) {
|
if (send(server_queue_id, &msg) == -1) {
|
||||||
@@ -154,16 +144,19 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
/* Wait for login response (server replies with mtype = Login) */
|
/* Wait for login response (server replies with mtype = Login) */
|
||||||
msgbuf_t resp;
|
msgbuf_t resp;
|
||||||
ssize_t r =
|
ssize_t r = msgrcv(client_queue_id, &resp, sizeof(resp) - sizeof(long), Login, 0);
|
||||||
msgrcv(client_queue_id, &resp, sizeof(resp) - sizeof(long), Login, 0);
|
|
||||||
if (r == -1) {
|
if (r == -1) {
|
||||||
perror("msgrcv(login response) failed");
|
perror("msgrcv(login response) failed");
|
||||||
cleanup_queue(client_queue_id);
|
cleanup_queue(client_queue_id);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resp.stype != ACK_ACCEPTED) {
|
if (resp.stype == ERR_ALREADY_LOGGED_IN) {
|
||||||
fprintf(stderr, "Login rejected: code=%d\n", resp.stype);
|
fprintf(stderr, "Login failed: User already logged in\n");
|
||||||
|
cleanup_queue(client_queue_id);
|
||||||
|
return 1;
|
||||||
|
} else if (resp.stype == ERR_USER_NOT_FOUND) {
|
||||||
|
fprintf(stderr, "Login failed: User not found\n");
|
||||||
cleanup_queue(client_queue_id);
|
cleanup_queue(client_queue_id);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -182,8 +175,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
msgbuf_t incoming;
|
msgbuf_t incoming;
|
||||||
ssize_t got = msgrcv(client_queue_id, &incoming,
|
ssize_t got = msgrcv(client_queue_id, &incoming, sizeof(incoming) - sizeof(long), 0, 0);
|
||||||
sizeof(incoming) - sizeof(long), 0, 0);
|
|
||||||
if (got == -1) {
|
if (got == -1) {
|
||||||
perror("msgrcv(incoming) failed");
|
perror("msgrcv(incoming) failed");
|
||||||
|
|
||||||
@@ -198,10 +190,12 @@ int main(int argc, char *argv[]) {
|
|||||||
case Message:
|
case Message:
|
||||||
printf("%s: %s\n", incoming.sender, incoming.message);
|
printf("%s: %s\n", incoming.sender, incoming.message);
|
||||||
break;
|
break;
|
||||||
case List_clients: {
|
case List: {
|
||||||
// Check the command field for the list type
|
// Check the command field for the list type
|
||||||
if (strncmp(incoming.command, "active", COMMAND_LENGTH) == 0) {
|
if (strncmp(incoming.command, "active", COMMAND_LENGTH) == 0) {
|
||||||
printf("Active clients:\n%s\n", incoming.message);
|
printf("Active clients:\n%s\n", incoming.message);
|
||||||
|
} else if (strncmp(incoming.command, "group", COMMAND_LENGTH) == 0) {
|
||||||
|
printf("All group members:\n%s\n", incoming.message);
|
||||||
} else {
|
} else {
|
||||||
printf("All clients:\n%s\n", incoming.message);
|
printf("All clients:\n%s\n", incoming.message);
|
||||||
}
|
}
|
||||||
@@ -209,9 +203,15 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
case Inbox: {
|
case Inbox: {
|
||||||
// Upon recieving an inbox message, check the command field for the index
|
// Upon recieving an inbox message, check the command field for the index
|
||||||
|
// Index of -1 means that there are no messages in the mailbox
|
||||||
|
if (strcmp(incoming.command, "-1") == 0) {
|
||||||
|
printf("Your inbox is empty.\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Index of 0 means that there are messages in the mailbox
|
// Index of 0 means that there are messages in the mailbox
|
||||||
if (strcmp(incoming.command, "0") == 0) {
|
if (strcmp(incoming.command, "0") == 0) {
|
||||||
printf("You have messages in your mailbox!\n");
|
printf("You have messages in your box!\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,8 +219,7 @@ int main(int argc, char *argv[]) {
|
|||||||
if (strcmp(incoming.command, "1") == 0) {
|
if (strcmp(incoming.command, "1") == 0) {
|
||||||
printf("Your inbox messages:\n");
|
printf("Your inbox messages:\n");
|
||||||
}
|
}
|
||||||
printf("[%s] %s: %s\n", incoming.command, incoming.sender,
|
printf("[%s] %s: %s\n", incoming.command, incoming.sender, incoming.message);
|
||||||
incoming.message);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Signal:
|
case Signal:
|
||||||
@@ -228,19 +227,16 @@ int main(int argc, char *argv[]) {
|
|||||||
if (incoming.stype == ACK_ACCEPTED) {
|
if (incoming.stype == ACK_ACCEPTED) {
|
||||||
printf("Message sent successfully.\n");
|
printf("Message sent successfully.\n");
|
||||||
} else if (incoming.stype == ACK_DELIVERED) {
|
} else if (incoming.stype == ACK_DELIVERED) {
|
||||||
printf("Message delivered to the %s.\n", incoming.command);
|
printf("Message delivered to %s.\n", incoming.sender);
|
||||||
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
|
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
|
||||||
printf("Your message could not be delivered: User not found.\n");
|
printf("Your message could not be delivered: User not found.\n");
|
||||||
} else {
|
} else {
|
||||||
// Display whatever if I forgot to handle something
|
// Display whatever if I forgot to handle something
|
||||||
printf("Your message could not be delivered: Error code %d.\n",
|
printf("Unhandled signal - code %d.\n", incoming.stype);
|
||||||
incoming.stype);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Received unknown command of mtype=%ld stype=%d\n",
|
printf("Received unknown command of mtype=%ld stype=%d\n", (long)incoming.mtype, incoming.stype);
|
||||||
(long)incoming.mtype, incoming.stype);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -9,12 +9,13 @@ typedef enum {
|
|||||||
Login,
|
Login,
|
||||||
Logout,
|
Logout,
|
||||||
Hearbeat,
|
Hearbeat,
|
||||||
List_clients,
|
List,
|
||||||
|
Group,
|
||||||
Mute,
|
Mute,
|
||||||
Unmute,
|
Unmute,
|
||||||
Signal,
|
Signal,
|
||||||
Inbox
|
Inbox
|
||||||
} CommandType;
|
} Command_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ACK_ACCEPTED = 0,
|
ACK_ACCEPTED = 0,
|
||||||
@@ -28,8 +29,9 @@ typedef enum {
|
|||||||
} Signal_t;
|
} Signal_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
CommandType mtype;
|
Command_t mtype;
|
||||||
Signal_t stype; // Used with mtype = signal. Responses from the server - to specify errors or acks
|
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 sender[COMMAND_LENGTH]; // Who sent the message
|
||||||
char command[COMMAND_LENGTH]; // Functions as an attribute to mtype -
|
char command[COMMAND_LENGTH]; // Functions as an attribute to mtype -
|
||||||
// nicknames, id to send/mute to
|
// nicknames, id to send/mute to
|
||||||
|
|||||||
@@ -15,36 +15,35 @@
|
|||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
#include "util/util.h"
|
#include "util/util.h"
|
||||||
|
|
||||||
#define send(queue_id, msgbuf_ptr) \
|
#define send(queue_id, msgbuf_ptr) msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
|
||||||
msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char sender[COMMAND_LENGTH];
|
char sender[COMMAND_LENGTH];
|
||||||
char command[COMMAND_LENGTH];
|
char command[COMMAND_LENGTH];
|
||||||
char message[MESSAGE_LENGTH];
|
char message[MESSAGE_LENGTH];
|
||||||
int message_id;
|
char message_id[COMMAND_LENGTH];
|
||||||
} saved_message_t;
|
} saved_message_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *id;
|
char id[COMMAND_LENGTH];
|
||||||
bool logged_in;
|
bool logged_in;
|
||||||
char *muted_clients;
|
char *muted_clients;
|
||||||
char nickname[16];
|
char nickname[COMMAND_LENGTH]; // Nicknames cannot contain special characters
|
||||||
|
// like # (reserved for groups)
|
||||||
int queue_id;
|
int queue_id;
|
||||||
long last_seen;
|
long last_seen;
|
||||||
// Things for keeping track of the saved messages
|
// Things for keeping track of the saved messages
|
||||||
saved_message_t saved_messages[20];
|
saved_message_t saved_messages[20];
|
||||||
int saved_message_count;
|
int saved_message_count;
|
||||||
} Client;
|
} Client_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Client **clients;
|
Client_t **members;
|
||||||
char name[16];
|
char name[COMMAND_LENGTH - 1]; // In the command one char is used for # to indicate a group
|
||||||
int client_count;
|
int client_count;
|
||||||
} Group;
|
} Group_t;
|
||||||
|
|
||||||
static void handle_hearbeat_timeouts(Client *clients, int client_count,
|
static void handle_hearbeat_timeouts(Client_t *clients, int client_count, int semaphore_id) {
|
||||||
int semaphore_id) {
|
|
||||||
struct sembuf sem_op = {
|
struct sembuf sem_op = {
|
||||||
.sem_num = 0,
|
.sem_num = 0,
|
||||||
.sem_op = -1,
|
.sem_op = -1,
|
||||||
@@ -56,10 +55,8 @@ static void handle_hearbeat_timeouts(Client *clients, int client_count,
|
|||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (clients[i].logged_in &&
|
if (clients[i].logged_in && (tv.tv_sec - clients[i].last_seen) > HEARTBEAT_TIMEOUT) {
|
||||||
(tv.tv_sec - clients[i].last_seen) > HEARTBEAT_TIMEOUT) {
|
printf("Client %s has timed out due to missed heartbeats\n", clients[i].id);
|
||||||
printf("Client %s has timed out due to missed heartbeats\n",
|
|
||||||
clients[i].id);
|
|
||||||
clients[i].logged_in = false;
|
clients[i].logged_in = false;
|
||||||
clients[i].queue_id = -1;
|
clients[i].queue_id = -1;
|
||||||
}
|
}
|
||||||
@@ -74,24 +71,29 @@ int main(int argc, char **argv) {
|
|||||||
// TODO: Load config file with clients and groups
|
// TODO: Load config file with clients and groups
|
||||||
int client_count = 2;
|
int client_count = 2;
|
||||||
int group_count = 3;
|
int group_count = 3;
|
||||||
int clients_memory_id =
|
int clients_memory_id = shmget(IPC_PRIVATE, sizeof(Client_t) * client_count, IPC_CREAT | 0666);
|
||||||
shmget(IPC_PRIVATE, sizeof(Client) * client_count, IPC_CREAT | 0666);
|
Client_t *clients = shmat(clients_memory_id, NULL, 0);
|
||||||
Client *clients = shmat(clients_memory_id, NULL, 0);
|
Group_t *groups = malloc(sizeof(Group_t) * 3);
|
||||||
Group *groups = malloc(sizeof(Group) * 3);
|
|
||||||
|
|
||||||
int semaphore_id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
|
int semaphore_id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
|
||||||
semctl(semaphore_id, 0, SETVAL, 1);
|
semctl(semaphore_id, 0, SETVAL, 1);
|
||||||
|
|
||||||
clients[0] = (Client){.id = "test1",
|
clients[0] =
|
||||||
.logged_in = false,
|
(Client_t){.id = "test1", .logged_in = false, .muted_clients = NULL, .nickname = "Kregielnia", .last_seen = 0};
|
||||||
.muted_clients = NULL,
|
clients[1] =
|
||||||
.nickname = "Kregielnia",
|
(Client_t){.id = "test2", .logged_in = false, .muted_clients = NULL, .nickname = "Bajzel", .last_seen = 0};
|
||||||
.last_seen = 0};
|
|
||||||
clients[1] = (Client){.id = "test2",
|
groups[0] = (Group_t){.name = "HelloChat", .client_count = 2, .members = NULL};
|
||||||
.logged_in = false,
|
groups[0].members = malloc(sizeof(Client_t *) * client_count);
|
||||||
.muted_clients = NULL,
|
groups[0].members[0] = &clients[0];
|
||||||
.nickname = "Bajzel",
|
groups[0].members[1] = &clients[1];
|
||||||
.last_seen = 0};
|
|
||||||
|
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];
|
||||||
|
|
||||||
int server_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
|
int server_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
|
||||||
|
|
||||||
@@ -100,8 +102,7 @@ int main(int argc, char **argv) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int id_file_handle =
|
int id_file_handle = open("/home/piotr/server_queue_id", O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||||
open("/home/piotr/server_queue_id", O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
||||||
if (!id_file_handle) {
|
if (!id_file_handle) {
|
||||||
perror("Failed to open file");
|
perror("Failed to open file");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -121,13 +122,12 @@ int main(int argc, char **argv) {
|
|||||||
msgbuf_t msgbuf;
|
msgbuf_t msgbuf;
|
||||||
while (1) {
|
while (1) {
|
||||||
// Read from the ipc, then handle the command
|
// Read from the ipc, then handle the command
|
||||||
int read_status =
|
int read_status = msgrcv(server_queue_id, &msgbuf, sizeof(msgbuf) - sizeof(long), 0, 0);
|
||||||
msgrcv(server_queue_id, &msgbuf, sizeof(msgbuf) - sizeof(long), 0, 0);
|
|
||||||
|
|
||||||
switch (msgbuf.mtype) {
|
switch (msgbuf.mtype) {
|
||||||
case Login: {
|
case Login: {
|
||||||
printf("Received login request for id: %s\n", msgbuf.sender);
|
printf("Received login request for id: %s\n", msgbuf.sender);
|
||||||
Client *client = NULL;
|
Client_t *client = NULL;
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
||||||
client = &clients[i];
|
client = &clients[i];
|
||||||
@@ -185,7 +185,7 @@ int main(int argc, char **argv) {
|
|||||||
case Message: {
|
case Message: {
|
||||||
// Find the client that sent the message then forward it to all the
|
// Find the client that sent the message then forward it to all the
|
||||||
// specified recipients
|
// specified recipients
|
||||||
Client *client = NULL;
|
Client_t *client = NULL;
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
||||||
client = &clients[i];
|
client = &clients[i];
|
||||||
@@ -193,8 +193,7 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Received message from client id %s to %s\n", msgbuf.sender,
|
printf("Received message from client id %s to %s\n", msgbuf.sender, msgbuf.command);
|
||||||
msgbuf.command);
|
|
||||||
|
|
||||||
if (client == NULL || !client->logged_in) {
|
if (client == NULL || !client->logged_in) {
|
||||||
// Client not found or not logged in
|
// Client not found or not logged in
|
||||||
@@ -208,24 +207,30 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now we have to decode if we are to forward this message to a user or a
|
// 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
|
// group. A group is indicated by a # at the start of the command
|
||||||
char target_type = msgbuf.command[0];
|
char target_type = msgbuf.command[0];
|
||||||
char target_id[COMMAND_LENGTH];
|
Client_t **recipients = malloc(sizeof(Client_t *) * client_count - 1); // We won't send to ourselves so one less
|
||||||
// Skip the first char and a separtor (:)
|
|
||||||
strncpy(target_id, msgbuf.command + 2, COMMAND_LENGTH - 2);
|
|
||||||
|
|
||||||
if (target_type == 'u') {
|
if (target_type != '#') {
|
||||||
Client *target_client = NULL;
|
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (strncmp(clients[i].nickname, target_id, COMMAND_LENGTH) == 0) {
|
if (strncmp(clients[i].nickname, msgbuf.command, COMMAND_LENGTH) == 0) {
|
||||||
target_client = &clients[i];
|
recipients[0] = &clients[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Target is a group, find it first
|
||||||
|
Group_t *target_group = NULL;
|
||||||
|
for (int i = 0; i < group_count; i++) {
|
||||||
|
if (strncmp(groups[i].name, msgbuf.command + 1, COMMAND_LENGTH - 1) == 0) {
|
||||||
|
target_group = &groups[i];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target_client == NULL) {
|
// Target group not found
|
||||||
// Target client not found or not logged in
|
if (target_group == NULL) {
|
||||||
printf("Target client %s not found\n", target_id);
|
printf("Target group %s not found\n", msgbuf.command + 1);
|
||||||
msgbuf_t response = {
|
msgbuf_t response = {
|
||||||
.mtype = Message,
|
.mtype = Message,
|
||||||
.stype = ERR_USER_NOT_FOUND,
|
.stype = ERR_USER_NOT_FOUND,
|
||||||
@@ -234,50 +239,55 @@ int main(int argc, char **argv) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Forward the message to all group members except the sender
|
||||||
|
int recipient_idx = 0;
|
||||||
|
for (int i = 0; i < target_group->client_count; i++) {
|
||||||
|
if (strncmp(target_group->members[i]->id, client->id, COMMAND_LENGTH) == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
recipients[recipient_idx++] = target_group->members[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recipient_idx < client_count - 1) {
|
||||||
|
recipients[recipient_idx] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now forward the message to all recipients
|
||||||
|
for (int i = 0; i < client_count - 1; i++) {
|
||||||
|
Client_t *target_client = recipients[i];
|
||||||
|
// Just the empty pointer so no more recipients
|
||||||
|
if (target_client == NULL)
|
||||||
|
break;
|
||||||
|
|
||||||
// Check if the target is online or not, if they are not, store the
|
// Check if the target is online or not, if they are not, store the
|
||||||
// message
|
// message
|
||||||
if (!target_client->logged_in) {
|
if (!target_client->logged_in) {
|
||||||
|
saved_message_t *saved_msg = NULL;
|
||||||
if (target_client->saved_message_count >= 20) {
|
if (target_client->saved_message_count >= 20) {
|
||||||
// Discard the oldest one and shift the rest to make room
|
// Discard the oldest one and shift the rest to make room
|
||||||
for (int i = 1; i < target_client->saved_message_count; i++) {
|
for (int i = 1; i < target_client->saved_message_count; i++) {
|
||||||
target_client->saved_messages[i - 1] =
|
target_client->saved_messages[i - 1] = target_client->saved_messages[i];
|
||||||
target_client->saved_messages[i];
|
|
||||||
}
|
}
|
||||||
saved_message_t *saved_msg =
|
saved_msg = &target_client->saved_messages[target_client->saved_message_count - 1];
|
||||||
&target_client
|
} else {
|
||||||
->saved_messages[target_client->saved_message_count - 1];
|
// There is room for a new message
|
||||||
|
saved_msg = &target_client->saved_messages[target_client->saved_message_count++];
|
||||||
|
}
|
||||||
|
|
||||||
|
get_id(saved_msg->message_id);
|
||||||
strncpy(saved_msg->sender, client->nickname, COMMAND_LENGTH);
|
strncpy(saved_msg->sender, client->nickname, COMMAND_LENGTH);
|
||||||
strncpy(saved_msg->message, msgbuf.message, MESSAGE_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 = {
|
msgbuf_t response = {
|
||||||
.mtype = Message,
|
.mtype = Message,
|
||||||
.stype = ACK_ACCEPTED,
|
.stype = ACK_ACCEPTED,
|
||||||
};
|
};
|
||||||
send(client->queue_id, &response);
|
send(client->queue_id, &response);
|
||||||
|
printf("Message saved for offline client %s\n", target_client->id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
saved_message_t *saved_msg =
|
printf("Forwarding message from user %s to %s\n", client->nickname, target_client->nickname);
|
||||||
&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 from user %s to %s\n", client->nickname,
|
|
||||||
target_client->nickname);
|
|
||||||
msgbuf_t forward_msg = {
|
msgbuf_t forward_msg = {
|
||||||
.mtype = Message,
|
.mtype = Message,
|
||||||
.sender = "",
|
.sender = "",
|
||||||
@@ -291,27 +301,14 @@ int main(int argc, char **argv) {
|
|||||||
send(client->queue_id, &forward_msg);
|
send(client->queue_id, &forward_msg);
|
||||||
|
|
||||||
printf("Forwarding message: %s\n", msgbuf.message);
|
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;
|
|
||||||
}
|
}
|
||||||
// TODO: Accepted and Delivered ack
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Logout: {
|
case Logout: {
|
||||||
}
|
}
|
||||||
case Hearbeat:
|
case Hearbeat:
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0 &&
|
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0 && clients[i].logged_in) {
|
||||||
clients[i].logged_in) {
|
|
||||||
// Take the semaphore to update last_seen
|
// Take the semaphore to update last_seen
|
||||||
struct sembuf sem_op = {
|
struct sembuf sem_op = {
|
||||||
.sem_num = 0,
|
.sem_num = 0,
|
||||||
@@ -329,8 +326,8 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case List_clients: {
|
case List: {
|
||||||
Client *client = NULL;
|
Client_t *client = NULL;
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
||||||
client = &clients[i];
|
client = &clients[i];
|
||||||
@@ -342,6 +339,33 @@ int main(int argc, char **argv) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char list_buffer[MESSAGE_LENGTH] = "";
|
||||||
|
|
||||||
|
if (strncmp(msgbuf.command, "groups", COMMAND_LENGTH) == 0) {
|
||||||
|
for (int i = 0; i < group_count; i++) {
|
||||||
|
bool isMember = false;
|
||||||
|
for (int j = 0; j < groups[i].client_count; j++) {
|
||||||
|
if (strncmp(groups[i].members[j]->id, client->id, COMMAND_LENGTH) == 0) {
|
||||||
|
isMember = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char line[COMMAND_LENGTH + 4 + 9];
|
||||||
|
// Tell the user if they are in the group or not
|
||||||
|
snprintf(line, COMMAND_LENGTH + 4 + 9, "- %s%s\n", groups[i].name, isMember ? " (member)" : "");
|
||||||
|
strncat(list_buffer, line, MESSAGE_LENGTH - strlen(list_buffer) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
msgbuf_t response = {
|
||||||
|
.mtype = List,
|
||||||
|
.command = "groups",
|
||||||
|
};
|
||||||
|
strncpy(response.message, list_buffer, MESSAGE_LENGTH);
|
||||||
|
send(client->queue_id, &response);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Parse the command parameter
|
// Parse the command parameter
|
||||||
// Just check if the user wants the active clients
|
// Just check if the user wants the active clients
|
||||||
// Not worth checking if the other option is valid
|
// Not worth checking if the other option is valid
|
||||||
@@ -349,7 +373,6 @@ int main(int argc, char **argv) {
|
|||||||
if (strncmp(msgbuf.command, "active", COMMAND_LENGTH) == 0) {
|
if (strncmp(msgbuf.command, "active", COMMAND_LENGTH) == 0) {
|
||||||
active_only = true;
|
active_only = true;
|
||||||
}
|
}
|
||||||
char list_buffer[MESSAGE_LENGTH] = "";
|
|
||||||
|
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (active_only && !clients[i].logged_in)
|
if (active_only && !clients[i].logged_in)
|
||||||
@@ -362,7 +385,7 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
msgbuf_t response = {
|
msgbuf_t response = {
|
||||||
.mtype = List_clients,
|
.mtype = List,
|
||||||
.command = "",
|
.command = "",
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -372,7 +395,7 @@ int main(int argc, char **argv) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Inbox: {
|
case Inbox: {
|
||||||
Client *client = NULL;
|
Client_t *client = NULL;
|
||||||
for (int i = 0; i < client_count; i++) {
|
for (int i = 0; i < client_count; i++) {
|
||||||
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
|
||||||
client = &clients[i];
|
client = &clients[i];
|
||||||
@@ -394,20 +417,17 @@ int main(int argc, char **argv) {
|
|||||||
// the counter in command
|
// the counter in command
|
||||||
for (int i = 0; i < client->saved_message_count; i++) {
|
for (int i = 0; i < client->saved_message_count; i++) {
|
||||||
snprintf(response.command, COMMAND_LENGTH, "%d", i + 1);
|
snprintf(response.command, COMMAND_LENGTH, "%d", i + 1);
|
||||||
strncpy(response.sender, client->saved_messages[i].sender,
|
strncpy(response.sender, client->saved_messages[i].sender, COMMAND_LENGTH);
|
||||||
COMMAND_LENGTH);
|
strncpy(response.message, client->saved_messages[i].message, MESSAGE_LENGTH);
|
||||||
strncpy(response.message, client->saved_messages[i].message,
|
|
||||||
MESSAGE_LENGTH);
|
|
||||||
send(client->queue_id, &response);
|
send(client->queue_id, &response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now send ACK_DELIVERED to senders
|
// Now send ACK_DELIVERED to senders
|
||||||
for (int i = 0; i < client->saved_message_count; i++) {
|
for (int i = 0; i < client->saved_message_count; i++) {
|
||||||
saved_message_t *saved_msg = &client->saved_messages[i];
|
saved_message_t *saved_msg = &client->saved_messages[i];
|
||||||
Client *original_sender = NULL;
|
Client_t *original_sender = NULL;
|
||||||
for (int j = 0; j < client_count; j++) {
|
for (int j = 0; j < client_count; j++) {
|
||||||
if (strncmp(clients[j].nickname, saved_msg->sender, COMMAND_LENGTH) ==
|
if (strncmp(clients[j].nickname, saved_msg->sender, COMMAND_LENGTH) == 0) {
|
||||||
0) {
|
|
||||||
original_sender = &clients[j];
|
original_sender = &clients[j];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-8
@@ -1,15 +1,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static char alphs[] = {'-', '_', '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
static char alphs[] = {'-', '_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
|
||||||
'9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||||
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
|
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||||
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0'};
|
||||||
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
|
|
||||||
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0'};
|
|
||||||
|
|
||||||
// Generate an id used for messages. Could be less than 16 chars, but thats what
|
// Generate an id used for messages.
|
||||||
// fits into the `command` field of `msgbuf_t`.
|
// Could be less than 16 chars, but thats what fits into the `command` field of `msgbuf_t`.
|
||||||
void get_id(char *id_buf) {
|
void get_id(char *id_buf) {
|
||||||
int alph_size = strlen(alphs) - 1;
|
int alph_size = strlen(alphs) - 1;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user