diff --git a/DataStructures/DataStructures.a b/DataStructures/DataStructures.a index 77666ed..c508b36 100644 Binary files a/DataStructures/DataStructures.a and b/DataStructures/DataStructures.a differ diff --git a/Makefile b/Makefile index a08eba5..f9c419c 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,17 @@ # Eric Meehan +############################################################################### +# COMPILER +############################################################################### + +CC=gcc +CFLAGS=-Wall -pedantic +AR=ar +ARFLAGS=rcs +RM=rm -f + + ############################################################################### # MARK: ALL ############################################################################### @@ -14,15 +25,13 @@ all: Main DataStructures Networking Systems - ############################################################################### # MARK: MAIN ############################################################################### # Creates just the top level static library Main: DataStructuresSub NetworkingSub SystemsSub - ar rcs libeom.a Node.o LinkedList.o Queue.o BinarySearchTree.o Entry.o Dictionary.o Client.o Server.o HTTPServer.o HTTPRequest.o ThreadPool.o PeerToPeer.o Files.o - + $(AR) $(ARFLAGS) libeom.a Node.o LinkedList.o Queue.o BinarySearchTree.o Entry.o Dictionary.o Client.o Server.o HTTPServer.o HTTPRequest.o ThreadPool.o PeerToPeer.o Files.o ############################################################################### @@ -31,30 +40,29 @@ Main: DataStructuresSub NetworkingSub SystemsSub # Creates the data structures library DataStructures: DataStructuresSub - ar rcs DataStructures/DataStructures.a Node.o LinkedList.o Queue.o BinarySearchTree.o Entry.o Dictionary.o - + $(AR) $(ARFLAGS) DataStructures/DataStructures.a Node.o LinkedList.o Queue.o BinarySearchTree.o Entry.o Dictionary.o + # Sub components of the data structures library DataStructuresSub: Node LinkedList Queue BinarySearchTree Entry Dictionary Node: - gcc -c DataStructures/Common/Node.c + $(CC) $(CFLAGS) -c DataStructures/Common/Node.c LinkedList: - gcc -c DataStructures/Lists/LinkedList.c + $(CC) $(CFLAGS) -c DataStructures/Lists/LinkedList.c Queue: - gcc -c DataStructures/Lists/Queue.c + $(CC) $(CFLAGS) -c DataStructures/Lists/Queue.c BinarySearchTree: - gcc -c DataStructures/Trees/BinarySearchTree.c + $(CC) $(CFLAGS) -c DataStructures/Trees/BinarySearchTree.c Entry: - gcc -c DataStructures/Dictionary/Entry.c + $(CC) $(CFLAGS) -c DataStructures/Dictionary/Entry.c Dictionary: - gcc -c DataStructures/Dictionary/Dictionary.c - - + $(CC) $(CFLAGS) -c DataStructures/Dictionary/Dictionary.c + ############################################################################### # MARK: NETWORKING @@ -62,27 +70,26 @@ Dictionary: # Creates the networking library Networking: NetworkingSub - ar rcs Networking/Networking.a Server.o HTTPRequest.o HTTPServer.o Node.o LinkedList.o Queue.o BinarySearchTree.o Entry.o Client.o Dictionary.o ThreadPool.o PeerToPeer.o + $(AR) $(ARFLAGS) Networking/Networking.a Server.o HTTPRequest.o HTTPServer.o Node.o LinkedList.o Queue.o BinarySearchTree.o Entry.o Client.o Dictionary.o ThreadPool.o PeerToPeer.o # Sub components of the networking library NetworkingSub: DataStructuresSub SystemsSub Server Client HTTPRequest HTTPServer PeerToPeer Server: - gcc -c Networking/Nodes/Server.c - + $(CC) $(CFLAGS) -c Networking/Nodes/Server.c + Client: - gcc -c Networking/Nodes/Client.c + $(CC) $(CFLAGS) -c Networking/Nodes/Client.c PeerToPeer: - gcc -c Networking/Nodes/PeerToPeer.c + $(CC) $(CFLAGS) -c Networking/Nodes/PeerToPeer.c HTTPServer: - gcc -c Networking/Nodes/HTTPServer.c + $(CC) $(CFLAGS) -c Networking/Nodes/HTTPServer.c HTTPRequest: - gcc -c Networking/Protocols/HTTPRequest.c - - + $(CC) $(CFLAGS) -c Networking/Protocols/HTTPRequest.c + ############################################################################### # MARK: Systems @@ -90,22 +97,42 @@ HTTPRequest: # Creates the systems library Systems: SystemsSub - ar rcs Systems/System.a ThreadPool.o Files.o + $(AR) $(ARFLAGS) Systems/System.a ThreadPool.o Files.o # Sub components of the systems library SystemsSub: ThreadPool Files ThreadPool: - gcc -c Systems/ThreadPool.c + $(CC) $(CFLAGS) -c Systems/ThreadPool.c Files: - gcc -c Systems/Files.c + $(CC) $(CFLAGS) -c Systems/Files.c + + +############################################################################### +# MARK: TEST +############################################################################### + +test: test_p2p test_linked_list test_queue + +test_p2p: test/main.c + $(CC) $(CFLAGS) -o test_p2p test/main.c libeom.a + +test_linked_list: test/test_linked_list.c + $(CC) $(CFLAGS) -o test_linked_list test/test_linked_list.c libeom.a + +test_queue: test/test_queue.c + $(CC) $(CFLAGS) -o test_queue test/test_queue.c libeom.a ############################################################################### # MARK: CLEAN ############################################################################### -# Remove all .o files +# Remove all .o files and test files clean: - rm *.o + $(RM) *.o + $(RM) test_p2p + $(RM) test_linked_list + $(RM) test_queue + diff --git a/Networking/Networking.a b/Networking/Networking.a index 80dad9a..988980f 100644 Binary files a/Networking/Networking.a and b/Networking/Networking.a differ diff --git a/Networking/Nodes/Client.c b/Networking/Nodes/Client.c index d437e27..c39ed81 100644 --- a/Networking/Nodes/Client.c +++ b/Networking/Nodes/Client.c @@ -17,7 +17,6 @@ In general, client objects are used to send requests to server APIs. */ - #include "Client.h" #include @@ -27,7 +26,7 @@ // MARK: FUNCTION PROTOTYPES -char * request(struct Client *client, char *server_ip, void *request, unsigned long size); +char *request(struct Client *client, char *server_ip, void *request, unsigned long size); // MARK: CONSTRUCTORS @@ -36,6 +35,7 @@ struct Client client_constructor(int domain, int service, int protocol, int port // Instantiate a client object. struct Client client; client.domain = domain; + client.service = service; client.port = port; client.interface = interface; // Establish a socket connection. @@ -47,7 +47,7 @@ struct Client client_constructor(int domain, int service, int protocol, int port // MARK: PRIVATE MEMBER METHODS -char * request(struct Client *client, char *server_ip, void *request, unsigned long size) +char *request(struct Client *client, char *server_ip, void *request, unsigned long size) { // Create an address struct for the server. struct sockaddr_in server_address; @@ -57,7 +57,7 @@ char * request(struct Client *client, char *server_ip, void *request, unsigned l server_address.sin_addr.s_addr = (int)client->interface; // Make the connection. inet_pton(client->domain, server_ip, &server_address.sin_addr); - connect(client->socket, (struct sockaddr*)&server_address, sizeof(server_address)); + connect(client->socket, (struct sockaddr *)&server_address, sizeof(server_address)); // Send the request; send(client->socket, request, size, 0); // Read the response. diff --git a/Networking/Protocols/BlockHeaders.h b/Networking/Protocols/BlockHeaders.h new file mode 100644 index 0000000..cfc7f10 --- /dev/null +++ b/Networking/Protocols/BlockHeaders.h @@ -0,0 +1,20 @@ +#ifndef BlockHeaders_h +#define BlockHeaders_h + +struct BlockHeaders // Bytes: +{ + unsigned char previous[64]; // 0 - 63 + unsigned long nonce; // 64 - 71 + + unsigned char by[64]; // 72 - 135 + char timestamp[20]; // 136 - 165 + unsigned long size; // 156 - 164 +}; + +struct Block +{ + struct BlockHeaders headers; + unsigned char data; +}; + +#endif /* BlockHeaders_h */ diff --git a/Networking/Protocols/HTTPRequest.c b/Networking/Protocols/HTTPRequest.c index 606dc21..09b6e13 100644 --- a/Networking/Protocols/HTTPRequest.c +++ b/Networking/Protocols/HTTPRequest.c @@ -29,7 +29,6 @@ #include #include - // MARK: PRIVATE MEMBER METHODS // Functions for extracting the constituent elements from a request string. @@ -37,8 +36,6 @@ void extract_request_line_fields(struct HTTPRequest *request, char *request_line void extract_header_fields(struct HTTPRequest *request, char *header_fields); void extract_body(struct HTTPRequest *request, char *body); - - // MARK: CONSTRUCTORS // Creates an initialized instance of an HTTPRequest using a properly formatted string. @@ -67,7 +64,7 @@ struct HTTPRequest http_request_constructor(char *request_string) extract_body(&request, body); // Return the final product. return request; -}; +} // Destroy a request by freeing the memory in each constituent dictionary. void http_request_destructor(struct HTTPRequest *request) @@ -77,8 +74,6 @@ void http_request_destructor(struct HTTPRequest *request) dictionary_destructor(&request->body); } - - // MARK: PRIVATE MEMBER METHODS // Parses out the request line to retrieve the method, uri, and http version. diff --git a/Systems/System.a b/Systems/System.a index 6081875..f5c3fab 100644 Binary files a/Systems/System.a and b/Systems/System.a differ diff --git a/libeom.a b/libeom.a index f6b4577..e0dc0bf 100644 Binary files a/libeom.a and b/libeom.a differ diff --git a/test/test_linked_list.c b/test/test_linked_list.c new file mode 100644 index 0000000..2d557db --- /dev/null +++ b/test/test_linked_list.c @@ -0,0 +1,40 @@ +// +// test_linked_list.c +// hdelibc +// +// Created by Eric Meehan on 1/31/21. +// + +#include "../libeom.h" + +#include +#include + +int main() +{ + struct LinkedList list = linked_list_constructor(); + + for (int index = 0; index < 10; index++) + { + int *data = (int *)malloc(sizeof(int)); + *data = index; + list.insert(&list, index, data, sizeof(int)); + } + + list.remove(&list, 3); + list.remove(&list, 7); + + for (int index = 0; index < list.length; index++) + { + void *value = list.retrieve(&list, index); + if (value) // NULL denotes no node exists + { + printf("%d\n", *(int *)list.retrieve(&list, index)); + } + } + + // Should return NULL + list.retrieve(&list, 100); + + linked_list_destructor(&list); +} diff --git a/test/test_queue.c b/test/test_queue.c new file mode 100644 index 0000000..faaf52f --- /dev/null +++ b/test/test_queue.c @@ -0,0 +1,34 @@ +// +// test_queue.c +// hdelibc +// +// Created by Eric Meehan on 2/1/21. +// + +#include "../libeom.h" + +#include +#include + +int main() +{ + struct Queue queue = queue_constructor(); + + for (int i = 0; i < 10; i++) + { + int *data = (int *)malloc(sizeof(int)); + *data = i; + queue.push(&queue, data, sizeof(data)); + } + + queue.pop(&queue); + queue.pop(&queue); + + while (queue.list.length) + { + printf("%d\n", *(int *)queue.peek(&queue)); + queue.pop(&queue); + } + + queue_destructor(&queue); +}