Compare commits

...

2 Commits

Author SHA1 Message Date
ad70f43da9 DOESN'T COMPILE - Started working on adding message to stack 2023-04-13 08:11:34 -05:00
df9a05a56e Added libstack files 2023-04-13 08:11:15 -05:00
3 changed files with 114 additions and 9 deletions

50
main.c
View File

@@ -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;
}
}

62
stack.c Normal file
View File

@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include "stack.h"
struct Stack {
void** data;
int top_index;
int capacity;
int curr_size;
};
Stack* new_stack(int stack_size) {
assert(stack_size > 0);
Stack* my_stack = malloc(sizeof(Stack));
my_stack->capacity = stack_size;
my_stack->data = malloc(stack_size);
my_stack->top_index = 0;
my_stack->curr_size = 0;
return my_stack;
}
void stack_push(Stack* stack,void* element) {
if (stack->curr_size >= stack->capacity-1) {
stack->data = realloc(stack->data, stack->capacity*2);
stack->capacity *= 2;
}
*(stack->data + stack->top_index) = element;
stack->top_index++;
stack->curr_size++;
return;
}
void* stack_pop(Stack* stack) {
assert( !(stack_isEmpty(stack)) );
free(stack->data + stack->top_index);
stack->top_index--;
void* to_return = *(stack->data + stack->top_index);
*(stack->data + stack->top_index) = NULL;
stack->curr_size--;
return to_return;
}
void* stack_peek(Stack* stack) {
void* to_return = *(stack->data + stack->top_index - 1);
return to_return;
}
int stack_size(Stack* stack) {
return (stack->curr_size);
}
bool stack_isEmpty(Stack* stack) {
if (stack_size(stack) == 0) {
return true;
}
return false;
}

11
stack.h Normal file
View File

@@ -0,0 +1,11 @@
#include <stdbool.h>
typedef struct Stack Stack;
Stack* new_stack(int stack_size);
void stack_push(Stack* stack, void* element);
void* stack_pop(Stack* stack);
void* stack_peek(Stack* stack);
int stack_size(Stack* stack);
bool stack_isEmpty(Stack* stack);