Skip to content

Commit

Permalink
add(debug): add debug_print func
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 31, 2023
1 parent 66655f1 commit e96d2bb
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

!srcs
!srcs/Client
!srcs/Debug
!srcs/Result
!srcs/Server
!srcs/Socket
Expand Down
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -25,6 +27,7 @@ enable_testing()
include_directories(
includes
srcs/Client
srcs/Debug
srcs/Server
srcs/Socket
)
Expand All @@ -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
)
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
19 changes: 2 additions & 17 deletions srcs/Client/Client.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>
#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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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();

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

Expand Down
37 changes: 37 additions & 0 deletions srcs/Debug/Debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# include <unistd.h>
# include <stdarg.h>
# include <cstdio>
# 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
}
8 changes: 8 additions & 0 deletions srcs/Debug/Debug.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

# include <pthread.h>

extern pthread_mutex_t print_mutex;

void DEBUG_SERVER_PRINT(const char *fmt, ...);
void DEBUG_CLIENT_PRINT(const char *fmt, ...);
2 changes: 2 additions & 0 deletions srcs/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>
#include "webserv.hpp"
#include "Color.hpp"
#include "Debug.hpp"
#include "Result.hpp"
#include "Server.hpp"

Expand Down Expand Up @@ -133,6 +134,7 @@ Result<int, std::string> 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);
Expand Down
2 changes: 1 addition & 1 deletion srcs/Server/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> _client_fds;
fd_set _fds;

Expand Down
44 changes: 29 additions & 15 deletions test/unit_test/TestServer.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#include <netdb.h>
#include <sys/socket.h>
#include <pthread.h>
#include <cstdio>
#include <exception>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -315,20 +322,29 @@ 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<s_client> init_client_infos(int client_count,
const char *server_ip,
const char *server_port,
const std::string &client_send_msg) {
std::vector<s_client> 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,
std::string &server_recv_msg,
std::vector<std::string> &client_recv_msgs,
int client_count) {
s_server server_info = {server_ip, server_port, ""};
std::vector<s_client> client_infos(client_count, {server_ip, server_port, client_send_msg, ""});
std::vector<s_client> client_infos = init_client_infos(client_count, server_ip, server_port, client_send_msg);
pthread_t server_tid;
std::vector<pthread_t> client_tids(client_count);
int ret_server, ret_client;
Expand All @@ -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;
Expand Down

0 comments on commit e96d2bb

Please sign in to comment.