|
|
|
@ -11,6 +11,7 @@
|
|
|
|
|
#include "easysock.h"
|
|
|
|
|
#include "file_helpers.h"
|
|
|
|
|
#include "message_helpers.h"
|
|
|
|
|
#include "stack.h"
|
|
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 10000
|
|
|
|
|
#define MAX_CONNECTIONS 100
|
|
|
|
@ -34,16 +35,21 @@ int main() {
|
|
|
|
|
socklen_t temp_addrlen;
|
|
|
|
|
|
|
|
|
|
fd_set read_fd_set;
|
|
|
|
|
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
|
|
|
|
User* to_user[MAX_CONNECTIONS] = {NULL};
|
|
|
|
|
User* from_user[MAX_CONNECTIONS] = {NULL};
|
|
|
|
|
FD_ZERO(&read_fd_set);
|
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
|
char data[DATA_SIZE];
|
|
|
|
|
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
|
|
|
|
User* to_user[MAX_CONNECTIONS] = {NULL};
|
|
|
|
|
User* from_user[MAX_CONNECTIONS] = {NULL};
|
|
|
|
|
FD_ZERO(&read_fd_set); char buffer[BUFFER_SIZE];
|
|
|
|
|
char data[DATA_SIZE];
|
|
|
|
|
|
|
|
|
|
num_users = num_of_lines("user_file.txt");
|
|
|
|
|
users = create_user_list("user_file.txt");
|
|
|
|
|
|
|
|
|
|
Stack* message_stack[num_users];
|
|
|
|
|
|
|
|
|
|
for (int i=0;i < num_users; i++) {
|
|
|
|
|
message_stack[i] = new_stack(10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sockaddr addr_struct;
|
|
|
|
|
int server_sock = create_local(4,'T',"127.0.0.1",30000,&addr_struct);
|
|
|
|
|
conn_sockets[0] = server_sock;
|
|
|
|
@ -107,13 +113,18 @@ int main() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* message = fetch_message_string(data);
|
|
|
|
|
if (message == NULL) {
|
|
|
|
|
char* message_string = fetch_message_string(data);
|
|
|
|
|
if (message_string == NULL) {
|
|
|
|
|
printf("Invalid message.\n");
|
|
|
|
|
return -10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Your message is: %s\n",message);
|
|
|
|
|
printf("Your message is: %s\n",message_string);
|
|
|
|
|
Message message = new_message(message_string,from_user[i],to_user[i]);
|
|
|
|
|
|
|
|
|
|
stack_push( message_stack[user_to_index(to_user[i])],message );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close(conn_sockets[i]);
|
|
|
|
|
|
|
|
|
@ -192,3 +203,24 @@ User* fetch_user(char* username) {
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int user_to_index(User* user) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (int i=0;i < num_users; i++) {
|
|
|
|
|
if (user_equals(users[i],user) == true) {
|
|
|
|
|
index = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool user_equals(User* this, User* other) {
|
|
|
|
|
if (( strcmp(this->username,other->username) == 0 ) && ( strcmp(this->password,other->password) == 0 )) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|