Skip to content

Commit

Permalink
add(client): implement client for server test
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 26, 2023
1 parent 58285b7 commit 2e72258
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
!srcs
!srcs/Socket
!srcs/Server
!srcs/Client
!www
!includes
!test
Expand Down
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ include_directories(
includes
srcs/Socket
srcs/Server
srcs/Client
)

# webserv_srcs -----------------------------------------------------------------
set(webserv_srcs
srcs/get_valid_config_file_path.cpp
srcs/Socket/Socket.cpp
srcs/Server/Server.cpp srcs/Server/Server.hpp)
srcs/Server/Server.cpp
srcs/Client/Client.cpp
)

add_executable(webserv
srcs/main.cpp
Expand All @@ -43,12 +46,13 @@ add_executable(webserv
set (unit_test_srcs
test/unit_test/is_valid_file_path/test_get_valid_config_file_path.cpp
test/unit_test/TestSocket.cpp
srcs/Server/Server.cpp srcs/Server/Server.hpp)
test/unit_test/TestServer.cpp
)

add_executable(unit_test
${webserv_srcs}
${unit_test_srcs}
)
)

# test -------------------------------------------------------------------------
target_link_libraries(
Expand Down
97 changes: 97 additions & 0 deletions srcs/Client/Client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <arpa/inet.h>
#include <errno.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"

Client::Client(const char *server_ip, const char *server_port) {
this->_connect_fd = create_connect_socket();
this->_addr = create_connect_addr(server_ip, server_port);
}

Client::~Client() {
if (_connect_fd != ERROR) {
errno = 0;
if (close(_connect_fd) == ERROR) {
std::cerr << strerror(errno) << std::endl;
}
_connect_fd = ERROR;
}
}

int Client::create_connect_socket() {
int connect_fd;
errno = 0;
connect_fd = socket(AF_INET, SOCK_STREAM, 0);
if (connect_fd == ERROR) {
throw std::runtime_error(strerror(errno));
}
return connect_fd;
}

struct sockaddr_in Client::create_connect_addr(const char *server_ip,
const char *server_port) {
struct sockaddr_in addr = {};
const int DECIMAL_BASE = 10;

addr.sin_family = AF_INET;
addr.sin_port = htons(std::strtol(server_port, NULL, DECIMAL_BASE));
addr.sin_addr.s_addr = inet_addr(server_ip);
return addr;
}

void Client::process_server_connect(const std::string &send_msg) {
connect_to_server(this->_connect_fd, this->_addr);
send_to_server(this->_connect_fd, send_msg);
this->_recv_message = recv_message_from_server(this->_connect_fd);
}

void Client::connect_to_server(int connect_fd, struct sockaddr_in addr) {
socklen_t len = sizeof(addr);

errno = 0;
if (connect(connect_fd, (struct sockaddr *)&addr, len) == ERROR) {
throw std::runtime_error(strerror(errno));
}
}

void Client::send_to_server(int connect_fd, const std::string &send_msg) {
ssize_t send_size;
size_t msg_len = send_msg.size() + 1;
const char *msg = send_msg.c_str();

errno = 0;
send_size = send(connect_fd, msg, msg_len, 0);
if (send_size == ERROR) {
throw std::runtime_error(strerror(errno));
}
}

std::string Client::recv_message_from_server(int connect_fd) {
ssize_t recv_size;
char buf[BUFSIZ + 1];
std::string recv_msg;

while (true) {
errno = 0;
recv_size = recv(connect_fd, &buf, BUFSIZ, 0);
if (recv_size == ERROR) {
throw std::runtime_error(strerror(errno));
}
buf[recv_size] = '\0';
recv_msg += buf;
if (recv_size < BUFSIZ) {
break;
}
}
return recv_msg;
}

std::string Client::get_recv_message() const { return this->_recv_message; }
27 changes: 27 additions & 0 deletions srcs/Client/Client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string>
#include "webserv.hpp"

class Client {
public:
Client(const char *server_ip, const char *server_port);
~Client();

std::string get_recv_message() const;
void process_server_connect(const std::string &send_msg);

private:
int _connect_fd;
std::string _recv_message;
struct sockaddr_in _addr;

static int create_connect_socket();
static struct sockaddr_in create_connect_addr(const char *server_ip, const char *server_port);
static void connect_to_server(int connect_fd, struct sockaddr_in addr);
static void send_to_server(int connect_fd, const std::string &send_msg);
static std::string recv_message_from_server(int connect_fd);
};

0 comments on commit 2e72258

Please sign in to comment.