Webserv
Loading...
Searching...
No Matches
Env.cpp
Go to the documentation of this file.
1#include "webserv.hpp"
2
3/*|==========|
4 * Environment constructor:
5 *
6 * Input: The JSONParser output
7 * Output: The env object containing servers and sockets vectors defined inside
8 * conf file by servers blocks and listens.
9 */
11 try {
12 JSONNode *node;
13 JSONList lst;
14 if ((node = conf->obj()["servers"])) {
15 lst = conf->obj()["servers"]->lst();
16 for (std::vector<JSONNode *>::iterator it = lst.begin(); it < lst.end(); it++) {
17 Server *server = new Server(*it);
18 this->_servers.push_back(server);
19 std::vector<Master *> tmp_s = server->get_sockets(*it);
20 this->_masters.insert(this->_masters.end(), tmp_s.begin(), tmp_s.end());
21 }
22 }
23 if ((node = conf->obj()["allowed_methods"])) {
24 JSONList lst = node->lst();
25 for (JSONList::iterator it = lst.begin(); it < lst.end(); it++) {
26 this->_allowed_methods.push_back((*it)->str());
27 }
28 }
29 } catch (std::exception &e) {
30 std::cerr << e.what() << "\n";
31 }
32 delete conf;
33}
34
41 for (std::vector<Server *>::iterator it = this->_servers.begin(); it < this->_servers.end(); it++) {
42 delete *it;
43 }
44 for (std::vector<Master *>::iterator it = this->_masters.begin(); it < this->_masters.end(); it++) {
45 delete *it;
46 }
47}
48
55void Env::cycle(void) {
56 FD_ZERO(&Master::_readfds);
58 pre_select();
59 cout << "|===||===| Waiting some HTTP request... |===||===|\n";
60 int activity = select(Master::_min_fd + Master::_amount, &(Master::_readfds), NULL, NULL, NULL);
61 if ((activity < 0) && (errno != EINTR))
62 std::cerr << "Select: " << strerror(errno) << "\n";
64}
65
67void Env::pre_select(void) {
68 cout << "==> Check sockets still alive to listen\n";
69 for (std::vector<Master *>::iterator it = this->_masters.begin(); it < this->_masters.end(); it++)
70 (*it)->pre_select();
71}
77void Env::post_select(void) {
78 cout << "==> Handle requests and answers:\n";
79 for (std::vector<Master *>::iterator it = this->_masters.begin(); it < this->_masters.end(); it++)
80 try {
81 (*it)->post_select(this);
82 } catch (std::exception &e) {
83 std::cerr << e.what();
84 }
85}
void cycle(void)
A Wevserv cycle:
Definition: Env.cpp:55
void post_select(void)
Refresh all master_sockets and their clients datas (disconnect, new connection, etc....
Definition: Env.cpp:77
~Env(void)
Destructor.
Definition: Env.cpp:40
Env(JSONNode *conf)
Definition: Env.cpp:10
std::vector< Master * > _masters
Definition: Env.hpp:7
std::vector< Server * > _servers
Definition: Env.hpp:6
void pre_select(void)
Append each master_sockets and their clients to list of fds SELECT must look at.
Definition: Env.cpp:67
vec_string _allowed_methods
Definition: Env.hpp:8
JSONObject obj()
Definition: Nodes.cpp:3
JSONList lst()
Definition: Nodes.cpp:8
static fd_set _readfds
Definition: Master.hpp:19
static int _amount
< The higher one
Definition: Master.hpp:20
static int _max_fd
< The lower socket fd
Definition: Master.hpp:20
static int _min_fd
< The sockets fd which will be select
Definition: Master.hpp:20
Definition: Server.hpp:4
std::vector< Master * > get_sockets(JSONNode *server)
Create server's defined sockets:
Definition: Server.cpp:79
std::vector< JSONNode * > JSONList
Definition: webserv.hpp:49