6d495468c8
Client can now specify the recipient of a message and type it. Messages for offline users are saved, unaccesible for now.
206 lines
5.6 KiB
C
206 lines
5.6 KiB
C
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.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);
|
|
}
|
|
}
|
|
|
|
static void input_loop(int server_queue_id, const char *client_id) {
|
|
while (1) {
|
|
// 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
|
|
// offs
|
|
char buffer[COMMAND_LENGTH + MESSAGE_LENGTH + 64];
|
|
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
|
break;
|
|
}
|
|
|
|
buffer[strcspn(buffer, "\n")] = '\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, ' ');
|
|
if (space_ptr == NULL) {
|
|
printf("Invalid /msg format. Use /msg u:<nickname> <message>\n");
|
|
continue;
|
|
}
|
|
char target[COMMAND_LENGTH];
|
|
size_t target_len = space_ptr - (buffer + 5);
|
|
if (target_len >= COMMAND_LENGTH) {
|
|
printf("Target nickname too long\n");
|
|
continue;
|
|
}
|
|
strncpy(target, buffer + 5, target_len);
|
|
target[target_len] = '\0';
|
|
char *message = space_ptr + 1;
|
|
if (strlen(message) == 0) {
|
|
printf("Message cannot be empty\n");
|
|
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 = ""};
|
|
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
|
|
snprintf(msg.command, COMMAND_LENGTH, "%s", target);
|
|
strncpy(msg.message, message, MESSAGE_LENGTH - 1);
|
|
if (send(server_queue_id, &msg) == -1) {
|
|
perror("msgsnd(/msg) failed");
|
|
}
|
|
} else if (strncmp(buffer, "/list", 5) == 0) {
|
|
// handle list
|
|
} else if (strncmp(buffer, "/mute ", 6) == 0) {
|
|
// handle mute
|
|
} else if (strcmp(buffer, "/quit") == 0) {
|
|
// exit
|
|
} else {
|
|
printf("Unknown command\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc < 2) {
|
|
fprintf(stderr, "Usage: %s <client_id>\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";
|
|
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 (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;
|
|
}
|
|
close(fd);
|
|
|
|
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");
|
|
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);
|
|
|
|
if (fork() == 0) {
|
|
heartbeat_loop(server_queue_id, client_id);
|
|
return 0;
|
|
}
|
|
|
|
if (fork() == 0) {
|
|
input_loop(server_queue_id, client_id);
|
|
return 0;
|
|
}
|
|
|
|
// /* Send a test message to the server */
|
|
// msgbuf_t test = {.mtype = Message, .command = "u:test2", .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) {
|
|
// printf("DEBUG: test.mtype = %u\n", test.mtype);
|
|
// perror("failed while sending a test message");
|
|
// cleanup_queue(client_queue_id);
|
|
// return 1;
|
|
// }
|
|
|
|
while (1) {
|
|
msgbuf_t incoming;
|
|
ssize_t got = msgrcv(client_queue_id, &incoming,
|
|
sizeof(incoming) - sizeof(long), 0, 0);
|
|
if (got == -1) {
|
|
perror("msgrcv(incoming) failed");
|
|
|
|
if (errno == EINTR) {
|
|
continue;
|
|
}
|
|
perror("msgrcv(incoming) failed");
|
|
break;
|
|
}
|
|
|
|
switch (incoming.mtype) {
|
|
case Message:
|
|
printf("%s: %s\n", incoming.sender, incoming.message);
|
|
break;
|
|
case Signal:
|
|
|
|
break;
|
|
default:
|
|
printf("Received unknown mtype=%ld stype=%d\n", (long)incoming.mtype,
|
|
incoming.stype);
|
|
break;
|
|
}
|
|
}
|
|
|
|
cleanup_queue(client_queue_id);
|
|
return 0;
|
|
}
|