nicolas-arnaud
2 years ago
9 changed files with 114 additions and 88 deletions
@ -1,34 +1,57 @@ |
|||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
|
|
||||
Env::Env(JSONNode *conf) { |
Env::Env(JSONNode *conf) { |
||||
try { |
try { |
||||
JSONList servers = conf->obj()["servers"]->lst(); |
JSONList servers = conf->obj()["servers"]->lst(); |
||||
int i = 0; |
int i = 0; |
||||
string th[8] = {"first", "second", "third", "fourth", |
string th[8] = {"first", "second", "third", "fourth", |
||||
"fifth", "sixth", "seventh", "eigth"}; |
"fifth", "sixth", "seventh", "eigth"}; |
||||
for (std::vector<JSONNode *>::iterator it = servers.begin(); |
for (std::vector<JSONNode *>::iterator it = servers.begin(); |
||||
it < servers.end(); it++) { |
it < servers.end(); it++) { |
||||
Server *server = new Server(*it); |
Server *server = new Server(*it); |
||||
_servers.push_back(server); |
_servers.push_back(server); |
||||
// delete *it;
|
std::vector<Socket *> tmp_s = server->get_sockets(*it); |
||||
cout << th[i] << " server launched.\n"; |
_sockets.insert(_sockets.end(), tmp_s.begin(), tmp_s.end()); |
||||
i++; |
// delete *it;
|
||||
} |
cout << th[i] << " server launched.\n"; |
||||
} catch (std::exception &e) { |
i++; |
||||
cout << e.what(); |
} |
||||
} |
} catch (std::exception &e) { |
||||
delete conf; |
cout << e.what(); |
||||
|
} |
||||
|
delete conf; |
||||
} |
} |
||||
|
|
||||
|
Server *Env::choose_server(Socket *sock, string host) { |
||||
|
std::vector<Server *> exact; |
||||
|
std::vector<Server *> inrange; |
||||
|
|
||||
|
(void)host; |
||||
|
for (std::vector<Server *>::iterator sit = _servers.begin(); |
||||
|
sit < _servers.end(); sit++) { |
||||
|
std::vector<listen_t> serv_listens = (*sit)->_listens; |
||||
|
for (std::vector<listen_t>::iterator it = serv_listens.begin(); |
||||
|
it < serv_listens.end(); it++) { |
||||
|
if (sock->_listen.port != (*it).port) |
||||
|
continue; |
||||
|
if (sock->_listen.ip == (*it).ip) |
||||
|
exact.push_back(*sit); |
||||
|
// else if (is_ip_into(sock->_listen.ip, (*it).ip))
|
||||
|
} |
||||
|
} |
||||
|
return (exact.at(0)); |
||||
|
} |
||||
|
|
||||
void Env::set_fds() { |
void Env::set_fds() { |
||||
for (std::vector<Server *>::iterator it = _servers.begin(); |
for (std::vector<Socket *>::iterator it = _sockets.begin(); |
||||
it < _servers.end(); it++) { |
it < _sockets.end(); it++) { |
||||
(*it)->set_fds(); |
(*it)->set_fds(); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
void Env::refresh() { |
void Env::refresh() { |
||||
for (std::vector<Server *>::iterator it = _servers.begin(); |
for (std::vector<Socket *>::iterator it = _sockets.begin(); |
||||
it < _servers.end(); it++) { |
it < _sockets.end(); it++) { |
||||
(*it)->refresh(); |
(*it)->refresh(this); |
||||
} |
} |
||||
} |
} |
||||
|
Loading…
Reference in new issue