You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

/**
* @file Env.cpp
* @brief The main server object. Contain all servers and sockets.
* @author Narnaud
* @version 0.1
* @date 2023-01-17
*/
#include "webserv.hpp"
/**
* @brief Constructor
2 years ago
*
* The instance contain servers defined in configuration file by servers list and sockets by listens ones.
*
* @param conf The JsonParser output
2 years ago
*/
2 years ago
Env::Env(JSONNode *conf) {
2 years ago
try {
2 years ago
JSONNode *node;
2 years ago
JSONList lst;
if ((node = conf->obj()["servers"])) {
2 years ago
lst = conf->obj()["servers"]->lst();
2 years ago
for (std::vector<JSONNode *>::iterator it = lst.begin(); it < lst.end(); it++) {
2 years ago
Server *server = new Server(*it);
this->_servers.push_back(server);
std::vector<Master *> tmp_s = server->create_masters(*it);
this->_masters.insert(this->_masters.end(), tmp_s.begin(), tmp_s.end());
2 years ago
}
}
2 years ago
if ((node = conf->obj()["allowed_methods"])) {
2 years ago
JSONList lst = node->lst();
2 years ago
for (JSONList::iterator it = lst.begin(); it < lst.end(); it++) {
this->_allowed_methods.push_back((*it)->str());
2 years ago
}
2 years ago
}
2 years ago
} catch (std::exception &e) { std::cerr << e.what() << "\n"; }
2 years ago
delete conf;
}
2 years ago
/**
* @brief Destructor.
*
* The destructor call all servers and sockets destructors.
*/
2 years ago
Env::~Env() {
for (std::vector<Server *>::iterator it = this->_servers.begin(); it < this->_servers.end(); it++) { delete *it; }
for (std::vector<Master *>::iterator it = this->_masters.begin(); it < this->_masters.end(); it++) { delete *it; }
}
2 years ago
/**
* @brief A Wevserv cycle:
* - append sockets to listen to select list
2 years ago
* - select ehem
* - refresh and handle requests
*/
2 years ago
void Env::cycle(void) {
2 years ago
FD_ZERO(&Master::_readfds);
Master::_max_fd = Master::_min_fd;
2 years ago
pre_select();
2 years ago
cout << "|===||===| Waiting some HTTP request... |===||===|\n";
2 years ago
int activity = select(Master::_max_fd + 1, &(Master::_readfds), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) std::cerr << "Select: " << strerror(errno) << "\n";
post_select();
2 years ago
}
2 years ago
/// @brief Append each master_sockets and their clients to list of fds SELECT must look at.
void Env::pre_select(void) {
cout << "==> Check sockets still alive to listen\n";
for (std::vector<Master *>::iterator it = this->_masters.begin(); it < this->_masters.end(); it++)
(*it)->pre_select();
}
2 years ago
/**
* @brief Refresh all master_sockets and their clients datas (disconnect, new
2 years ago
* connection, etc..) and parse requests recieved.
*/
2 years ago
void Env::post_select(void) {
cout << "==> Handle requests and answers:\n";
for (std::vector<Master *>::iterator it = this->_masters.begin(); it < this->_masters.end(); it++) try {
(*it)->post_select(this);
} catch (std::exception &e) { std::cerr << e.what(); }
2 years ago
}