Compare commits

..

3 Commits

4 changed files with 31 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
EXEC_FILE=main EXEC_FILE=main
TARGETS=$(EXEC_FILE).o message.o user.o message_helpers.o file_helpers.o TARGETS=$(EXEC_FILE).o message.o user.o message_helpers.o file_helpers.o stack.o
CFLAGS= CFLAGS=
@@ -14,6 +14,7 @@ message.o: message.c
user.o: user.c user.o: user.c
message_helpers.o: message_helpers.c message_helpers.o: message_helpers.c
file_helpers.o: file_helpers.c file_helpers.o: file_helpers.c
stack.o: stack.c
$(TARGETS): $(TARGETS):
gcc $(CFLAGS) -c -o $@ $^ gcc $(CFLAGS) -c -o $@ $^

13
main.c
View File

@@ -20,6 +20,8 @@
User** create_user_list(char* filename); User** create_user_list(char* filename);
void sigint_handler(int dummy); void sigint_handler(int dummy);
User* fetch_user(char* username); User* fetch_user(char* username);
int user_to_index(User* user);
bool user_equals(User* this, User* other);
User** users; User** users;
int num_users; int num_users;
@@ -120,10 +122,17 @@ int main() {
} }
printf("Your message is: %s\n",message_string); printf("Your message is: %s\n",message_string);
Message message = new_message(message_string,from_user[i],to_user[i]);
if (user_to_index(to_user[i]) < 0) {
printf("Recipient user does not exist!\n");
return -20;
}
Message* message = new_message(message_string,from_user[i],to_user[i]);
stack_push( message_stack[user_to_index(to_user[i])],message ); stack_push( message_stack[user_to_index(to_user[i])],message );
printf("Message has been pushed onto stack for %s\n",to_user[i]->username);
close(conn_sockets[i]); close(conn_sockets[i]);
@@ -205,7 +214,7 @@ User* fetch_user(char* username) {
} }
int user_to_index(User* user) { int user_to_index(User* user) {
int index = 0; int index = -1;
for (int i=0;i < num_users; i++) { for (int i=0;i < num_users; i++) {
if (user_equals(users[i],user) == true) { if (user_equals(users[i],user) == true) {
index = i; index = i;

View File

@@ -4,14 +4,22 @@
#include <time.h> #include <time.h>
#include "message.h" #include "message.h"
Message new_message(char* string, User from, User to) { Message* new_message(char* string, User* from, User* to) {
Message new_message; Message* new_message;
new_message.text = malloc((strlen(string)+1)*sizeof(char)); new_message = malloc(sizeof(Message));
strcpy(new_message.text,string); new_message->text = malloc((strlen(string)+1)*sizeof(char));
strcpy(new_message->text,string);
new_message.sender = from; new_message->sender = malloc(sizeof(User));
new_message.recipient = to; new_message->recipient = malloc(sizeof(User));
memcpy(new_message->sender, from, sizeof(User));
memcpy(new_message->recipient, to, sizeof(User));
// *(new_message->sender) = *from;
// *(new_message->recipient) = *to;
time_t rawtime; time_t rawtime;
struct tm timeinfo; struct tm timeinfo;
@@ -19,7 +27,7 @@ Message new_message(char* string, User from, User to) {
time(&rawtime); time(&rawtime);
timeinfo = *localtime(&rawtime); timeinfo = *localtime(&rawtime);
new_message.timeinfo = timeinfo; new_message->timeinfo = timeinfo;
return new_message; return new_message;

View File

@@ -11,10 +11,10 @@ typedef struct Message_s Message;
struct Message_s { struct Message_s {
char* text; char* text;
struct tm timeinfo; struct tm timeinfo;
User sender; User* sender;
User recipient; User* recipient;
}; };
Message new_message(char* text,User from,User to); Message* new_message(char* text,User* from,User* to);
#endif #endif