diff --git a/.gitignore b/.gitignore index f5f7e14c..06c75db6 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ !srcs !srcs/Client +!srcs/Debug !srcs/Result !srcs/Server !srcs/Socket diff --git a/CMakeLists.txt b/CMakeLists.txt index 95d99c53..9b47a440 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,9 @@ project(webserv) set(CMAKE_CXX_STANDARD 98) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic -g -fsanitize=address,undefined -fno-omit-frame-pointer") +set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address,undefined -fno-omit-frame-pointer") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D DEBUG") # google test ------------------------------------------------------------------ include(FetchContent) @@ -25,6 +27,7 @@ enable_testing() include_directories( includes srcs/Client + srcs/Debug srcs/Server srcs/Socket ) @@ -33,6 +36,7 @@ include_directories( set(webserv_srcs srcs/get_valid_config_file_path.cpp srcs/Client/Client.cpp + srcs/Debug/Debug.cpp srcs/Server/Server.cpp srcs/Socket/Socket.cpp ) diff --git a/Makefile b/Makefile index 4ff410ef..8141e14d 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,9 @@ SRCS += $(SERVER_DIR)/Server.cpp SOCKET_DIR = Socket SRCS += $(SOCKET_DIR)/Socket.cpp +#debug +DEBUG_DIR = Debug +SRCS += $(DEBUG_DIR)/Debug.cpp # OBJS ------------------------------------------------------------------------- OBJS_DIR = objs @@ -32,6 +35,7 @@ DEPS = $(OBJS:%.o=%.d) # INCLUDES --------------------------------------------------------------------- INCLUDES_DIR = includes \ + $(SRCS_DIR)/$(DEBUG_DIR) \ $(SRCS_DIR)/$(SERVER_DIR) \ $(SRCS_DIR)/$(SOCKET_DIR) INCLUDES = $(addprefix -I, $(INCLUDES_DIR)) diff --git a/srcs/Client/Client.cpp b/srcs/Client/Client.cpp index b136ecef..83606b15 100644 --- a/srcs/Client/Client.cpp +++ b/srcs/Client/Client.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -7,9 +8,9 @@ #include #include #include -#include "Client.hpp" #include "webserv.hpp" #include "Color.hpp" +#include "Client.hpp" Client::Client(const char *server_ip, const char *server_port) { this->_connect_fd = create_connect_socket(); @@ -49,15 +50,9 @@ struct sockaddr_in Client::create_connect_addr(const char *server_ip, } void Client::process_server_connect(const std::string &send_msg) { - // printf(MAGENTA "client(pid:%d) 1 start\n" RESET, getpid()); - // printf(MAGENTA "client(pid:%d) 2 connect\n" RESET, getpid()); connect_to_server(this->_connect_fd, this->_addr); - // printf(MAGENTA "client(pid:%d) 3 send\n" RESET, getpid()); send_to_server(this->_connect_fd, send_msg); - // printf(MAGENTA "client(pid:%d) 4 recv\n" RESET, getpid()); this->_recv_message = recv_message_from_server(this->_connect_fd); - // printf(MAGENTA " client(pid:%d) recv_msg:[%s]\n" RESET, getpid(), this->_recv_message.c_str()); - // printf(MAGENTA "client(pid:%d) 5 end\n" RESET, getpid()); } void Client::connect_to_server(int connect_fd, struct sockaddr_in addr) { @@ -72,7 +67,6 @@ void Client::connect_to_server(int connect_fd, struct sockaddr_in addr) { void Client::send_to_server(int connect_fd, const std::string &send_msg) { ssize_t send_size; - // std::cout << "msg:" << send_msg << std::endl; size_t msg_len = send_msg.size() + 1; const char *msg = send_msg.c_str(); @@ -89,28 +83,19 @@ std::string Client::recv_message_from_server(int connect_fd) { char buf[BUFSIZ + 1]; std::string recv_msg; - // printf(MAGENTA " client(pid:%d) 4-1 recv_msg start\n" RESET, getpid()); - while (true) { - // printf(MAGENTA " client(pid:%d) 4-2 recv_msg recv\n" RESET, getpid()); errno = 0; recv_size = recv(connect_fd, &buf, BUFSIZ, 0); if (recv_size == ERROR) { - // printf(MAGENTA " client(pid:%d) recv_err errno:%d\n" RESET, getpid(), errno); std::string err_str = "[Client Error] recv: " + std::string(strerror(errno)); throw std::runtime_error(RED + err_str + RESET); } buf[recv_size] = '\0'; - // printf(MAGENTA " client(pid:%d) 4-3 buf:[%s]\n" RESET, getpid(), buf); recv_msg += buf; if (recv_size < BUFSIZ) { - // std::cout << MAGENTA " 3-6" RESET << std::endl; break; } - // printf(MAGENTA " client(pid:%d) 4-4 next\n" RESET, getpid()); } - // printf(MAGENTA " client(pid:%d) 4-5 recv_msg fin\n" RESET, getpid()); - return recv_msg; } diff --git a/srcs/Debug/Debug.cpp b/srcs/Debug/Debug.cpp new file mode 100644 index 00000000..a36ce769 --- /dev/null +++ b/srcs/Debug/Debug.cpp @@ -0,0 +1,37 @@ +# include +# include +# include +# include "Color.hpp" +# include "Debug.hpp" + +pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER; + +void DEBUG_SERVER_PRINT(const char *fmt, ...) { +#ifdef DEBUG + pthread_mutex_lock(&print_mutex); + dprintf(STDERR_FILENO, "%s%s", BLUE, "#DEBUG server: "); + va_list args; + va_start(args, fmt); + vdprintf(STDERR_FILENO, fmt, args); + va_end(args); + dprintf(STDERR_FILENO, "%s\n", RESET); + pthread_mutex_unlock(&print_mutex); +#else + (void)fmt; +#endif +} + +void DEBUG_CLIENT_PRINT(const char *fmt, ...) { +#ifdef DEBUG + pthread_mutex_lock(&print_mutex); + dprintf(STDERR_FILENO, "%s%s", MAGENTA, "#DEBUG client: "); + va_list args; + va_start(args, fmt); + vdprintf(STDERR_FILENO, fmt, args); + va_end(args); + dprintf(STDERR_FILENO, "%s\n", RESET); + pthread_mutex_unlock(&print_mutex); +#else + (void)fmt; +#endif +} diff --git a/srcs/Debug/Debug.hpp b/srcs/Debug/Debug.hpp new file mode 100644 index 00000000..92e05631 --- /dev/null +++ b/srcs/Debug/Debug.hpp @@ -0,0 +1,8 @@ +#pragma once + +# include + +extern pthread_mutex_t print_mutex; + +void DEBUG_SERVER_PRINT(const char *fmt, ...); +void DEBUG_CLIENT_PRINT(const char *fmt, ...); diff --git a/srcs/Server/Server.cpp b/srcs/Server/Server.cpp index 791b891e..a34bb8aa 100644 --- a/srcs/Server/Server.cpp +++ b/srcs/Server/Server.cpp @@ -10,6 +10,7 @@ #include #include "webserv.hpp" #include "Color.hpp" +#include "Debug.hpp" #include "Result.hpp" #include "Server.hpp" @@ -133,6 +134,7 @@ Result Server::communicate_with_ready_client() { } this->_recv_message = recv_result.get_ok_value(); // printf(BLUE " server recv_msg:[%s]\n" RESET, this->_recv_message.c_str()); + DEBUG_SERVER_PRINT("connected. recv:[%s]", this->_recv_message.c_str()); // request, response HttpRequest request = HttpRequest(this->_recv_message); diff --git a/srcs/Server/Server.hpp b/srcs/Server/Server.hpp index c97803b1..71078bb8 100644 --- a/srcs/Server/Server.hpp +++ b/srcs/Server/Server.hpp @@ -40,7 +40,7 @@ class Server { private: Socket _socket; - std::string _recv_message; + std::string _recv_message; // for test. this variable valid only connect with 1 client std::vector _client_fds; fd_set _fds; diff --git a/test/unit_test/TestServer.cpp b/test/unit_test/TestServer.cpp index a6dfb7e6..00a12d41 100644 --- a/test/unit_test/TestServer.cpp +++ b/test/unit_test/TestServer.cpp @@ -1,7 +1,5 @@ #include -#include #include -#include #include #include #include @@ -9,19 +7,21 @@ #include "webserv.hpp" #include "Client.hpp" #include "Color.hpp" +#include "Debug.hpp" #include "Server.hpp" struct s_server { - const char *server_ip; - const char *server_port; - std::string recv_msg; + const char *server_ip; + const char *server_port; + std::string recv_msg; }; struct s_client { - const char *server_ip; - const char *server_port; - std::string send_msg; - std::string recv_msg; + int no; + const char *server_ip; + const char *server_port; + std::string send_msg; + std::string recv_msg; }; static void run_server_and_client(const char *server_ip, @@ -259,9 +259,13 @@ static void *run_server(void *server_info) { bool is_server_success = true; try { + DEBUG_SERVER_PRINT("start"); Server server = Server(s->server_ip, s->server_port); + DEBUG_SERVER_PRINT("connecting..."); server.process_client_connection(); s->recv_msg = server.get_recv_message(); + // vvv this func can print only 1 message vv + // printf("server connected. recv:[%s]\n", s->recv_msg.c_str()); } catch (std::exception const &e) { is_server_success = false; @@ -275,10 +279,13 @@ static void *run_client(void *client_info) { bool is_client_success = true; try { + DEBUG_CLIENT_PRINT("no:%d start", c->no); Client client = Client(c->server_ip, c->server_port); sleep(1); + DEBUG_CLIENT_PRINT("no:%d connecting...", c->no); client.process_server_connect(c->send_msg); c->recv_msg = client.get_recv_message(); + DEBUG_CLIENT_PRINT("no:%d connected. recv:[%s]", c->no, c->recv_msg.c_str()); } catch (std::exception const &e) { is_client_success = false; @@ -293,7 +300,7 @@ static void run_server_and_client(const char *server_ip, std::string &server_recv_msg, std::string &client_recv_msg) { s_server server_info = {server_ip, server_port, ""}; - s_client client_info = {server_ip, server_port, client_send_msg, ""}; + s_client client_info = {0, server_ip, server_port, client_send_msg, ""}; pthread_t server_tid, client_tid; int ret_server, ret_client; bool is_server_success, is_client_success; @@ -315,12 +322,21 @@ static void run_server_and_client(const char *server_ip, if (!is_client_success) { throw std::runtime_error("client error"); } - // std::cout << "server_recv_msg:" << server_info.recv_msg << std::endl; - // std::cout << "client_recv_msg:" << client_info.recv_msg << std::endl; server_recv_msg = server_info.recv_msg; client_recv_msg = client_info.recv_msg; } +static std::vector init_client_infos(int client_count, + const char *server_ip, + const char *server_port, + const std::string &client_send_msg) { + std::vector client_infos(client_count, {0, server_ip, server_port, client_send_msg, ""}); + for (int i = 0; i < client_count; ++i) { + client_infos[i].no = i; + } + return client_infos; +} + static void run_server_and_multi_client(const char *server_ip, const char *server_port, const std::string &client_send_msg, @@ -328,7 +344,7 @@ static void run_server_and_multi_client(const char *server_ip, std::vector &client_recv_msgs, int client_count) { s_server server_info = {server_ip, server_port, ""}; - std::vector client_infos(client_count, {server_ip, server_port, client_send_msg, ""}); + std::vector client_infos = init_client_infos(client_count, server_ip, server_port, client_send_msg); pthread_t server_tid; std::vector client_tids(client_count); int ret_server, ret_client; @@ -355,8 +371,6 @@ static void run_server_and_multi_client(const char *server_ip, throw std::runtime_error("client error"); } } - // std::cout << "server_recv_msg:" << server_info.recv_msg << std::endl; - // std::cout << "client_recv_msg:" << client_info.recv_msg << std::endl; server_recv_msg = server_info.recv_msg; for (int i = 0; i < client_count; ++i) { client_recv_msgs[i] = client_infos[i].recv_msg;