diff --git a/IntegerPool/Client.c b/IntegerPool/Client.c index 3de6883..9280cd4 100644 --- a/IntegerPool/Client.c +++ b/IntegerPool/Client.c @@ -3,6 +3,16 @@ #include "Client.h" +NumberNode * NumberNode_search(LList *llist, int value) +{ + +} + +int NumberNode_has_value(LList *llist, int value) +{ + +} + int print_menu() { int choice; @@ -122,7 +132,7 @@ void process_response(LList *number_list) NumberNode *number_node = (NumberNode *) malloc(sizeof(NumberNode)); number_node->value = response.value; number_node->next = NULL; - if (llist_is_empty(number_list)) + if (LList_is_empty(number_list)) { number_list->head = (void *) number_node; number_list->tail = (void *) number_node; diff --git a/IntegerPool/Client.h b/IntegerPool/Client.h index 12c5699..1399b44 100644 --- a/IntegerPool/Client.h +++ b/IntegerPool/Client.h @@ -8,15 +8,21 @@ #include #include +#include "Common.h" +#include "LList.h" #include "Request.h" #include "Pool.h" -#include "Common.h" typedef struct NumberNode { int value; struct NumberNode *next; } NumberNode; +// NumberNode operations +NumberNode * NumberNode_search(LList *llist, int value); +int NumberNode_has_value(LList *llist, int value); + +// Other operations int print_menu(); void client_request_number(LList *number_list); void client_release_number(LList *number_list); diff --git a/IntegerPool/ClientMain.c b/IntegerPool/ClientMain.c index ee3437f..b4e129d 100644 --- a/IntegerPool/ClientMain.c +++ b/IntegerPool/ClientMain.c @@ -2,7 +2,7 @@ int main() { - LList *number_list = llist_init(); + LList *number_list = LList_init(); int menu_choice = 0; while (TRUE) diff --git a/IntegerPool/LList.c b/IntegerPool/LList.c new file mode 100644 index 0000000..c5e60eb --- /dev/null +++ b/IntegerPool/LList.c @@ -0,0 +1,50 @@ +#include "Common.h" +#include "LList.h" + +LList * LList_init() +{ + LList *llist = (LList *) malloc(sizeof(LList)); + llist->head = NULL; + llist->tail = NULL; + llist->size = 0; + return llist; +} + +int LList_is_empty(LList *llist) +{ + return (llist->size == 0); +} + +// void * LList_search(LList *llist, int value) +// { +// void *node = llist->head; +// if (node != NULL) +// { +// while (node != NULL) +// { +// if (node->value == value) +// { +// return node; +// } +// node = node->next; +// } +// } +// return NULL; +// } + +// int LList_has(LList *llist, int value) +// { +// PoolNode *node = llist->head; +// if (node != NULL) +// { +// while (node != NULL) +// { +// if (node->value == value) +// { +// return TRUE; +// } +// node = node->next; +// } +// } +// return FALSE; +// } \ No newline at end of file diff --git a/IntegerPool/LList.h b/IntegerPool/LList.h new file mode 100644 index 0000000..eb8d11d --- /dev/null +++ b/IntegerPool/LList.h @@ -0,0 +1,21 @@ +#ifndef llist_h +#define llist_h + +#include +#include +#include + +typedef struct LList +{ + void *head; + void *tail; + size_t size; +} LList; + +// LList operators +LList * LList_init(); +int LList_is_empty(LList *llist); +// void * LList_search(LList *llist, int value); +// int LList_has(LList *llist, int value); + +#endif \ No newline at end of file diff --git a/IntegerPool/Makefile b/IntegerPool/Makefile index 28d3a8c..569a4d4 100644 --- a/IntegerPool/Makefile +++ b/IntegerPool/Makefile @@ -1,11 +1,11 @@ CC = gcc # define the source and object files for the server -SERVER_SRC = Pool.c Request.c Response.c Server.c +SERVER_SRC = LList.c Pool.c Request.c Response.c Server.c SERVER_OBJ = $(SERVER_SRC:.c=.o) # source and object files for the client -CLIENT_SRC = ClientMain.c Pool.c Request.c Response.c Client.c +CLIENT_SRC = LList.c ClientMain.c Pool.c Request.c Response.c Client.c CLIENT_OBJ = $(CLIENT_SRC:.c=.o) # Exec files @@ -29,4 +29,7 @@ clean: clean_obj: rm -f $(SERVER_OBJ) - rm -f $(CLIENT_OBJ) \ No newline at end of file + rm -f $(CLIENT_OBJ) + +pro: + make clean && make all && make clean_obj \ No newline at end of file diff --git a/IntegerPool/Pool.c b/IntegerPool/Pool.c index 143f6af..d339ff0 100644 --- a/IntegerPool/Pool.c +++ b/IntegerPool/Pool.c @@ -1,10 +1,11 @@ #include #include #include -#include "Pool.h" + #include "Common.h" +#include "Pool.h" -ClientNode * create_client_node(pid_t client_pid, char *status) +ClientNode * ClientNode_create(pid_t client_pid, char *status) { ClientNode *client_node = (ClientNode *) malloc(sizeof(ClientNode)); if (client_node != NULL) @@ -16,7 +17,7 @@ ClientNode * create_client_node(pid_t client_pid, char *status) return client_node; } -PoolNode * create_pool_node(int value, ClientNode *client) +PoolNode * PoolNode_create(int value, ClientNode *client) { PoolNode *node = (PoolNode *) malloc(sizeof(PoolNode)); if (node != NULL) @@ -41,21 +42,7 @@ PoolNode * create_pool_node(int value, ClientNode *client) return node; } -LList * llist_init() -{ - LList *llist = (LList *) malloc(sizeof(LList)); - llist->head = NULL; - llist->tail = NULL; - llist->size = 0; - return llist; -} - -int llist_is_empty(LList *llist) -{ - return (llist->size == 0); -} - -PoolNode * llist_search_by_value(LList *llist, int value) +PoolNode * PoolNode_search(LList *llist, int value) { PoolNode *node = llist->head; if (node != NULL) @@ -72,7 +59,7 @@ PoolNode * llist_search_by_value(LList *llist, int value) return NULL; } -int llist_is_in_list(LList *llist, int value) +int PoolNode_has_value(LList *llist, int value) { PoolNode *node = llist->head; if (node != NULL) @@ -89,7 +76,7 @@ int llist_is_in_list(LList *llist, int value) return FALSE; } -void print_pool_list(LList *llist) +void PoolList_print(LList *llist) { if (llist->head != NULL) { @@ -100,7 +87,7 @@ void print_pool_list(LList *llist) printf("\nPool Node #%d\n-------------\n", pool_node_count); printf("Value: %d\n", pool_node->value); printf("In use: %d (1 = in use, 0 = free)\n", pool_node->is_in_use); - print_client_list(pool_node->current_client); + ClientList_print(pool_node->current_client); printf("------------------------------------------\n"); pool_node = pool_node->next; pool_node_count++; @@ -112,7 +99,7 @@ void print_pool_list(LList *llist) } } -void print_client_list(ClientNode *client_node) +void ClientList_print(ClientNode *client_node) { if (client_node != NULL) { @@ -125,4 +112,4 @@ void print_client_list(ClientNode *client_node) client_count++; } } -} \ No newline at end of file +} diff --git a/IntegerPool/Pool.h b/IntegerPool/Pool.h index 8ed7f9f..6dbb21c 100644 --- a/IntegerPool/Pool.h +++ b/IntegerPool/Pool.h @@ -1,16 +1,11 @@ #ifndef POOL_H #define POOL_H +#include "LList.h" + #define IN_USE 1 #define FREE 0 -typedef struct LList -{ - void *head; - void *tail; - size_t size; -} LList; - typedef struct ClientNode { pid_t client_pid; @@ -31,18 +26,15 @@ typedef struct PoolNode LList *pool_list; // ClientNode Operators -ClientNode * create_client_node(pid_t client_pid, char *status); +ClientNode * ClientNode_create(pid_t client_pid, char *status); // PoolNode operators -PoolNode * create_pool_node(int value, ClientNode *client); - -// LList operators -LList * llist_init(); -int llist_is_empty(LList *llist); -PoolNode * llist_search_by_value(LList *llist, int value); -int llist_is_in_list(LList *llist, int value); +PoolNode * PoolNode_create(int value, ClientNode *client); +PoolNode * PoolNode_search(LList *llist, int value); +int PoolNode_has_value(LList *llist, int value); // Other operator -void print_pool_list(LList *llist); -void print_client_list(ClientNode *client_node); +void PoolList_print(LList *llist); +void ClientList_print(ClientNode *client_node); + #endif \ No newline at end of file diff --git a/IntegerPool/Request.c b/IntegerPool/Request.c index 2824baa..fd4b552 100644 --- a/IntegerPool/Request.c +++ b/IntegerPool/Request.c @@ -1,7 +1,10 @@ #include #include -#include "Request.h" + #include "Common.h" +#include "LList.h" +#include "Request.h" +#include "Pool.h" void handle_request(Request request, LList *pool_list) { @@ -19,10 +22,10 @@ void handle_request(Request request, LList *pool_list) void request_number(Request request, LList *pool_list) { - if (llist_is_empty(pool_list)) + if (LList_is_empty(pool_list)) { - ClientNode *client_node = create_client_node(request.process_id, "in use"); - PoolNode *pool_node = create_pool_node(request.value, client_node); + ClientNode *client_node = ClientNode_create(request.process_id, "in use"); + PoolNode *pool_node = PoolNode_create(request.value, client_node); pool_list->head = (void *) pool_node; pool_list->tail = (void *) pool_node; pool_list->size++; @@ -31,10 +34,10 @@ void request_number(Request request, LList *pool_list) else { // value exist in pool_list - if (llist_is_in_list(pool_list, request.value)) + if (PoolNode_has_value(pool_list, request.value)) { - PoolNode *pool_node = llist_search_by_value(pool_list, request.value); - ClientNode *client_node = create_client_node(request.process_id, "waiting"); + PoolNode *pool_node = PoolNode_search(pool_list, request.value); + ClientNode *client_node = ClientNode_create(request.process_id, "waiting"); // add new client to the client list ((ClientNode *) pool_node->client_list->tail)->next = client_node; @@ -44,8 +47,8 @@ void request_number(Request request, LList *pool_list) } else { - ClientNode *client_node = create_client_node(request.process_id, "in use"); - PoolNode *pool_node = create_pool_node(request.value, client_node); + ClientNode *client_node = ClientNode_create(request.process_id, "in use"); + PoolNode *pool_node = PoolNode_create(request.value, client_node); ((PoolNode *) pool_list->tail)->next = pool_node; // point the next node of the tail to the new node pool_list->tail = (void *) pool_node; // point the tail to the new node; new node is now became the tail pool_list->size++; diff --git a/IntegerPool/Response.c b/IntegerPool/Response.c index e953da6..f1fc4dc 100644 --- a/IntegerPool/Response.c +++ b/IntegerPool/Response.c @@ -12,7 +12,7 @@ void write_response(pid_t client_id, int response_type, int value) { int fd; mkfifo(FIFO_PATH, FIFO_PERMISSION); - printf("Response:\n Process ID: %d\n Value: %d", client_id, value); + // printf("Response:\n Process ID: %d\n Value: %d", client_id, value); //Handle write end Response response; response.client_id = client_id; diff --git a/IntegerPool/Server.c b/IntegerPool/Server.c index 85fda08..9fe9d57 100644 --- a/IntegerPool/Server.c +++ b/IntegerPool/Server.c @@ -7,6 +7,7 @@ #include #include "Common.h" +#include "LList.h" #include "Request.h" #include "Response.h" #include "Pool.h" @@ -23,7 +24,7 @@ int main() } printf("Done\n"); - pool_list = llist_init(); // initialize pool list + pool_list = LList_init(); // initialize pool list printf("Server started.\n"); while (TRUE) @@ -53,7 +54,7 @@ int main() close(fd); handle_request(request, pool_list); - print_pool_list(pool_list); + PoolList_print(pool_list); // if (request.process_id != -1) // {