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.

81 lines
2.1 KiB

#include "webserv.hpp"
/*|=======================|
* Environment destructor:
*
* The destructor call all servers and sockets destructors.
*/
Env::~Env() {
for (std::vector< Server * >::iterator it = _servers.begin();
it < _servers.end(); it++) {
delete *it;
}
for (std::vector< Master * >::iterator it = _masters.begin();
it < _masters.end(); it++) {
delete *it;
}
}
2 years ago
/*|=======================|
* Environment constructor:
*
* Input: The JSONParser output
* Output: The env object containing servers and sockets vectors defined inside
* conf file by servers blocks and listens.
*/
Env::Env(JSONNode *conf) {
2 years ago
try {
JSONList servers = conf->obj()["servers"]->lst();
2 years ago
for (std::vector< JSONNode * >::iterator it = servers.begin();
2 years ago
it < servers.end(); it++) {
Server *server = new Server(*it);
_servers.push_back(server);
2 years ago
std::vector< Master * > tmp_s = server->get_sockets(*it);
_masters.insert(_masters.end(), tmp_s.begin(), tmp_s.end());
2 years ago
}
} catch (std::exception &e) {
std::cerr << e.what() << "\n";
2 years ago
}
delete conf;
}
2 years ago
void Env::cycle(void) {
FD_ZERO(&Master::_readfds);
Master::_max_fd = Master::_min_fd;
cout << "==> Check sockets still alive to listen\n";
set_fds();
cout << "|===||===| Waiting some HTTP request... |===||===|\n";
int activity = select(Master::_max_fd + Master::_amount,
&(Master::_readfds), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR))
std::cerr << "Select: " << strerror(errno) << "\n";
2 years ago
cout << "==> Handle requests and answers:\n";
refresh();
}
2 years ago
/*|=======================|
* Append each master_sockets and their clients to list of fds SELECT must look
* at.
*/
void Env::set_fds(void) {
2 years ago
for (std::vector< Master * >::iterator it = _masters.begin();
it < _masters.end(); it++)
2 years ago
(*it)->set_fds();
}
2 years ago
/*|=======================|
* Refresh all master_sockets and their clients datas (disconnect, new
* connection, etc..) and parse requests recieved.
*/
void Env::refresh(void) {
2 years ago
for (std::vector< Master * >::iterator it = _masters.begin();
it < _masters.end(); it++)
try {
(*it)->refresh(this);
} catch (std::exception &e) {
std::cerr << e.what();
}
2 years ago
}