Skip to content

Commit

Permalink
Merge pull request #4 from san-ghun/dev/config
Browse files Browse the repository at this point in the history
Dev/config
  • Loading branch information
miooo0o authored Jul 12, 2024
2 parents dd20e29 + 6a72e62 commit dfae0b7
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 129 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ CFLAGS = -Wall -Wextra -Werror -std=c++98

RM = rm -f

# SRC_NAME = ./src/main.cpp \
# ./src/server/Server.cpp \
# ./src/network/Poller.cpp \
# ./src/network/Socket.cpp
SRC_NAME = $(shell find ./src -iname "*.cpp")
OBJ_NAME = $(SRC_NAME:.cpp=.o)

Expand Down
22 changes: 20 additions & 2 deletions include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/06/30 16:24:32 by sanghupa ### ########.fr */
/* Updated: 2024/07/08 23:35:53 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef CONFIG_HPP
# define CONFIG_HPP

# include "webserv.hpp"
# include <string>
# include <map>

class Config
{
public:
Config();
~Config();

static void load(const std::string filename);
static std::string get(const std::string key);
static int getInt(const std::string key);
static int getPort();

private:
static std::map<std::string, std::string> _configMap;

static void _parseConfigFile(const std::string filename);
};

#endif
4 changes: 2 additions & 2 deletions include/Poller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/05 20:17:00 by sanghupa ### ########.fr */
/* Updated: 2024/07/09 23:25:27 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,7 +32,7 @@ class Poller
void removeSocket(const Socket socket);
void removeAllSockets();
std::vector<struct pollfd> getPollfds() const;
std::vector<t_event> poll(int timeout);
std::vector<t_event> poll(int timeout);

private:
std::vector<struct pollfd> _pollfds;
Expand Down
12 changes: 10 additions & 2 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:46 by sanghupa #+# #+# */
/* Updated: 2024/07/04 16:35:08 by sanghupa ### ########.fr */
/* Updated: 2024/07/10 21:23:36 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,6 +22,10 @@
# include "RequestHandler.hpp"

// class Config;
class Location;
class Socket;
class Poller;
// class RequestHandler;

class Server
{
Expand Down Expand Up @@ -68,11 +72,15 @@ class Server
bool _running;
Socket _listenSocket;
Poller _poller;
RequestHandler _requestHandler;
std::vector<pollfd> _pollfds;
// RequestHandler _requestHandler;

void _handleNewConnection();
void _handleClientData(Poller::t_event event);

void _acceptNewConnection();
void _handleClientData_2(int clientSocket, size_t idx);

std::vector<std::string> _serverNames;
std::string _serverHost;
std::string _serverPort;
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 21:05:14 by sanghupa #+# #+# */
/* Updated: 2024/07/08 22:49:29 by sanghupa ### ########.fr */
/* Updated: 2024/07/09 23:16:14 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,6 +50,7 @@ int main(int argc, char *argv[])
// Set the signal handler for SIGINT signal
signal(SIGINT, ft_sigint_handler);

(void)argv;
// try-catch block for error handling
// Create a Config object with the provided configuration file
// Config config(argv[1]);
Expand Down
10 changes: 8 additions & 2 deletions src/network/Poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/05 20:25:39 by sanghupa ### ########.fr */
/* Updated: 2024/07/09 23:46:15 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

#include "webserv.hpp"
#include "Poller.hpp"

Poller::Poller()
{}

Poller::~Poller()
{}

void Poller::addSocket(const Socket socket, short events)
{
pollfd pfd;
Expand Down Expand Up @@ -47,7 +53,7 @@ std::vector<Poller::t_event> Poller::poll(int timeout)
}
for (size_t i = 0; i < pollfds.size(); i++)
{
if (pollfds[i].revents & POLLIN)
if (pollfds[i].revents != 0)
{
t_event event;
event.socket = Socket(pollfds[i].fd);
Expand Down
16 changes: 10 additions & 6 deletions src/network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/03 16:23:55 by sanghupa #+# #+# */
/* Updated: 2024/07/05 20:30:38 by sanghupa ### ########.fr */
/* Updated: 2024/07/10 23:07:54 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

#include "webserv.hpp"
#include "Socket.hpp"
#include "cstring"

Socket::Socket()
: _socketfd(-1)
Expand Down Expand Up @@ -56,7 +57,7 @@ void Socket::_bindAddress(int port)
{
memset(&_address, 0, sizeof(_address));
_address.sin_family = AF_INET;
_address.sin_addr.s_addr = htonl(INADDR_ANY);
_address.sin_addr.s_addr = INADDR_ANY;
_address.sin_port = htons(port);
if (::bind(_socketfd, (struct sockaddr*)&_address, sizeof(_address)) < 0)
{
Expand Down Expand Up @@ -93,14 +94,16 @@ void Socket::set_nonblocking()

Socket Socket::accept()
{
std::cout << "\rSocket fd again: " << _socketfd << std::endl;
int clientfd = ::accept(_socketfd, NULL, NULL);
if (clientfd < 0)
{
throw std::runtime_error("Failed to accept connection");
}
// if (clientfd < 0)
// {
// throw std::runtime_error("Failed to accept connection");
// }
return Socket(clientfd);
}

/*
void Socket::connet(const std::string host, int port)
{}
Expand All @@ -109,6 +112,7 @@ ssize_t Socket::recv(char* buf, size_t len)
ssize_t Socket::send(const char* buf, size_t len)
{}
*/

void Socket::close()
{
Expand Down
Loading

0 comments on commit dfae0b7

Please sign in to comment.