-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
68 lines (58 loc) · 1.55 KB
/
Server.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
#include <fstream>
#include "Server.h"
void Server::start() {
struct sockaddr_in addr{};
auto listener = socket(AF_INET, SOCK_STREAM, 0);
if(listener < 0) {
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
exit(2);
}
listen(listener, 30);
int pid = 0;
for (int i = 0; i < _workers_number; i++) {
pid = fork();
if (pid == -1) {
perror("fork");
exit(3);
}
if (pid == 0) {
auto worker = std::make_unique<Worker>(_documents_root);
worker->run(listener);
}
}
if (pid > 0) {
wait(nullptr);
}
}
void Server::read_config(std::string path) {
std::ifstream f(path);
if (!f.is_open()) {
throw std::exception();
}
std::string line;
std::cout << "Config:" << std::endl;
while(std::getline(f, line)) {
std::cout << line << std::endl;
std::istringstream line_stream{line};
std::string arg_name, arg_val;
line_stream >> arg_name >> arg_val;
std::cout << arg_val << std::endl;
if (arg_name == "document_root") {
_documents_root = arg_val;
} else if (arg_name == "cpu_limit") {
_workers_number = std::atoi(arg_val.c_str());
}
}
f.close();
}