-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
48 lines (45 loc) · 1.28 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "main.hpp"
#include "selector/Multiplexer.hpp"
#include <cstdlib>
#include <stdexcept>
#include <unistd.h>
#include <csignal>
int main(int ac, char **av) {
if (ac > 2) {
std::cerr << "Usage: " << av[0] << " [config-file]" << std::endl;
return 1;
}
std::string configFile = "config.conf";
if (ac == 2) {
configFile = av[1];
}
std::vector<Directive> server_directives;
std::vector<Server> servers;
try {
Config config;
config.init(configFile);
server_directives = config.getServers();
} catch (std::runtime_error &e) {
std::cerr << "Parse error:" << std::endl;
std::cerr << e.what() << std::endl;
return 1;
}
try {
servers = validator(server_directives);
server_directives.clear();
Core core(servers);
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
while (true)
try {
Multiplexer multiplexer(servers);
multiplexer.run();
} catch (const std::bad_alloc &e) {
std::cerr << "Out of memory, waiting for resources...\n";
usleep(10000);
}
} catch (std::runtime_error &e) {
std::cerr << e.what() << std::endl;
}
return 0;
}