diff --git a/src/.clang-format b/.clang-format similarity index 100% rename from src/.clang-format rename to .clang-format diff --git a/src/.clang-tidy b/.clang-tidy similarity index 100% rename from src/.clang-tidy rename to .clang-tidy diff --git a/CMakeLists.txt b/CMakeLists.txt index cd1ae89..d84a16e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,9 +37,8 @@ add_custom_target(run WORKING_DIRECTORY ${CMAKE_PROJECT_DIR} ) -FILE(GLOB SOURCES src/**/*.cpp build_supp/version.cpp) - -add_library(anthracite src/anthracite.cpp ${SOURCES}) +FILE(GLOB LIB_SOURCES lib/*.cpp lib/**/*.cpp build_supp/version.cpp) +add_library(anthracite ${LIB_SOURCES}) add_dependencies(anthracite build-version) add_executable(anthracite-bin src/file_main.cpp) diff --git a/build_supp/version.sh b/build_supp/version.sh index 359e4b2..1a2a16a 100755 --- a/build_supp/version.sh +++ b/build_supp/version.sh @@ -1,4 +1,4 @@ echo "#include " > version.cpp -echo "#include \"../src/version.hpp\"" >> version.cpp +echo "#include \"../lib/version.hpp\"" >> version.cpp echo "const std::string ANTHRACITE_VERSION_STRING = \"$(cat version.txt)\";" >> version.cpp echo "const std::string ANTHRACITE_FULL_VERSION_STRING = \"Anthracite/$(cat version.txt)\";" >> version.cpp diff --git a/src/anthracite.cpp b/lib/anthracite.cpp similarity index 100% rename from src/anthracite.cpp rename to lib/anthracite.cpp diff --git a/src/anthracite.hpp b/lib/anthracite.hpp similarity index 100% rename from src/anthracite.hpp rename to lib/anthracite.hpp diff --git a/src/backends/backend.hpp b/lib/backends/backend.hpp similarity index 100% rename from src/backends/backend.hpp rename to lib/backends/backend.hpp diff --git a/src/backends/file_backend.cpp b/lib/backends/file_backend.cpp similarity index 100% rename from src/backends/file_backend.cpp rename to lib/backends/file_backend.cpp diff --git a/src/backends/file_backend.hpp b/lib/backends/file_backend.hpp similarity index 100% rename from src/backends/file_backend.hpp rename to lib/backends/file_backend.hpp diff --git a/src/http/constants.hpp b/lib/http/constants.hpp similarity index 100% rename from src/http/constants.hpp rename to lib/http/constants.hpp diff --git a/src/http/header_query.hpp b/lib/http/header_query.hpp similarity index 100% rename from src/http/header_query.hpp rename to lib/http/header_query.hpp diff --git a/lib/http/http_request.cpp b/lib/http/http_request.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/http/request.cpp b/lib/http/request.cpp similarity index 100% rename from src/http/request.cpp rename to lib/http/request.cpp diff --git a/src/http/request.hpp b/lib/http/request.hpp similarity index 100% rename from src/http/request.hpp rename to lib/http/request.hpp diff --git a/src/http/response.cpp b/lib/http/response.cpp similarity index 100% rename from src/http/response.cpp rename to lib/http/response.cpp diff --git a/src/http/response.hpp b/lib/http/response.hpp similarity index 100% rename from src/http/response.hpp rename to lib/http/response.hpp diff --git a/src/log/log.cpp b/lib/log/log.cpp similarity index 98% rename from src/log/log.cpp rename to lib/log/log.cpp index f1bc09e..aa0d35a 100644 --- a/src/log/log.cpp +++ b/lib/log/log.cpp @@ -1,5 +1,3 @@ -#pragma once - #include "./log.hpp" namespace anthracite::log { diff --git a/src/log/log.hpp b/lib/log/log.hpp similarity index 100% rename from src/log/log.hpp rename to lib/log/log.hpp diff --git a/src/socket/socket.cpp b/lib/socket/socket.cpp similarity index 75% rename from src/socket/socket.cpp rename to lib/socket/socket.cpp index 790ae9f..43a6280 100644 --- a/src/socket/socket.cpp +++ b/lib/socket/socket.cpp @@ -7,24 +7,12 @@ #include #include #include +#include "./socket.hpp" namespace anthracite::socket { -constexpr int MAX_QUEUE_LENGTH = 100; -class anthracite_socket { -private: - int server_socket; - int client_socket {}; - std::string client_ip; - struct sockaddr_in client_addr {}; - socklen_t client_addr_len {}; - static constexpr struct timeval timeout_tv { - .tv_sec = 5, .tv_usec = 0 - }; - -public: - anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH) +anthracite_socket::anthracite_socket(int port, int max_queue) : server_socket(::socket(AF_INET, SOCK_STREAM, 0)) , client_ip("") { @@ -40,7 +28,7 @@ public: listen(server_socket, max_queue); } - void wait_for_conn() + void anthracite_socket::wait_for_conn() { client_ip = ""; client_socket = accept(server_socket, reinterpret_cast(&client_addr), &client_addr_len); @@ -49,18 +37,18 @@ public: client_ip = std::string(ip_str.data()); } - const std::string& get_client_ip() + const std::string& anthracite_socket::get_client_ip() { return client_ip; } - void close_conn() + void anthracite_socket::close_conn() { close(client_socket); client_socket = -1; } - void send_message(std::string& msg) + void anthracite_socket::send_message(std::string& msg) { if (client_socket == -1) { return; @@ -68,7 +56,7 @@ public: send(client_socket, &msg[0], msg.length(), 0); } - std::string recv_message(int buffer_size) + std::string anthracite_socket::recv_message(int buffer_size) { if (client_socket == -1) { return ""; @@ -85,6 +73,5 @@ public: response[buffer_size] = '\0'; return { response.data() }; } -}; }; diff --git a/lib/socket/socket.hpp b/lib/socket/socket.hpp new file mode 100644 index 0000000..fb1eaf1 --- /dev/null +++ b/lib/socket/socket.hpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace anthracite::socket { + + +class anthracite_socket { + static const int MAX_QUEUE_LENGTH = 100; +private: + int server_socket; + int client_socket {}; + std::string client_ip; + struct sockaddr_in client_addr {}; + socklen_t client_addr_len {}; + static constexpr struct timeval timeout_tv { + .tv_sec = 5, .tv_usec = 0 + }; + +public: + anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH); + + void wait_for_conn(); + const std::string& get_client_ip(); + void close_conn(); + void send_message(std::string& msg); + std::string recv_message(int buffer_size); +}; + +}; diff --git a/src/version.hpp b/lib/version.hpp similarity index 100% rename from src/version.hpp rename to lib/version.hpp diff --git a/src/api_main.cpp b/src/api_main.cpp index 15f7fc7..0da5502 100644 --- a/src/api_main.cpp +++ b/src/api_main.cpp @@ -1,6 +1,6 @@ -#include "./anthracite.hpp" -#include "backends/backend.hpp" -#include "http/constants.hpp" +#include "../lib/anthracite.hpp" +#include "../lib/backends/backend.hpp" +#include "../lib/http/constants.hpp" #include #include #include diff --git a/src/file_main.cpp b/src/file_main.cpp index efbb611..6bc3b54 100644 --- a/src/file_main.cpp +++ b/src/file_main.cpp @@ -1,5 +1,5 @@ -#include "./anthracite.hpp" -#include "backends/file_backend.hpp" +#include "../lib/anthracite.hpp" +#include "../lib/backends/file_backend.hpp" using namespace anthracite; diff --git a/src/socket/socket.hpp b/src/socket/socket.hpp deleted file mode 100644 index 790ae9f..0000000 --- a/src/socket/socket.hpp +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace anthracite::socket { - -constexpr int MAX_QUEUE_LENGTH = 100; - -class anthracite_socket { -private: - int server_socket; - int client_socket {}; - std::string client_ip; - struct sockaddr_in client_addr {}; - socklen_t client_addr_len {}; - static constexpr struct timeval timeout_tv { - .tv_sec = 5, .tv_usec = 0 - }; - -public: - anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH) - : server_socket(::socket(AF_INET, SOCK_STREAM, 0)) - , client_ip("") - { - struct sockaddr_in address {}; - address.sin_family = AF_INET; - address.sin_port = htons(port); - address.sin_addr.s_addr = INADDR_ANY; - - int reuse_opt = 1; - setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(reuse_opt)); - bind(server_socket, reinterpret_cast(&address), sizeof(address)); - - listen(server_socket, max_queue); - } - - void wait_for_conn() - { - client_ip = ""; - client_socket = accept(server_socket, reinterpret_cast(&client_addr), &client_addr_len); - std::array ip_str { 0 }; - inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN); - client_ip = std::string(ip_str.data()); - } - - const std::string& get_client_ip() - { - return client_ip; - } - - void close_conn() - { - close(client_socket); - client_socket = -1; - } - - void send_message(std::string& msg) - { - if (client_socket == -1) { - return; - } - send(client_socket, &msg[0], msg.length(), 0); - } - - std::string recv_message(int buffer_size) - { - if (client_socket == -1) { - return ""; - } - - setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_tv, sizeof timeout_tv); - std::vector response(buffer_size + 1); - ssize_t result = recv(client_socket, response.data(), buffer_size + 1, 0); - - if (result < 1) { - return ""; - } - - response[buffer_size] = '\0'; - return { response.data() }; - } -}; - -};