-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDalek.cc
80 lines (59 loc) · 1.42 KB
/
Dalek.cc
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
69
70
71
72
73
74
75
76
77
78
79
80
#include "reactor/EventLoop.h"
#include "http/httpd.h"
#include <sys/wait.h>
#include <unistd.h>
static void Exterminate(int signo)
{
kill(-getpid(), SIGINT);
exit(1);
}
static void SavePid()
{
FILE* to_save = fopen("Dalek.pid", "a+");
if (to_save == nullptr)
{
fprintf(stderr, "Dalek.pid not exist!\n");
assert(false);
}
pid_t master_pid = getpid();
fprintf(to_save, "%d\n", master_pid);
fclose(to_save);
}
int main(int argc, char* argv[])
{
pinkx::SyncLogger::init("Dalek.log");
if (argc < 2)
{
fprintf(stderr, "Dalek need more arguments...\n");
exit(1);
}
signal(SIGPIPE, SIG_IGN); // Client closed
signal(SIGINT, Exterminate);
std::string_view arg(argv[1]);
if (arg != "start")
{
fprintf(stderr, "Dalek get error argument!\n");
exit(1);
}
int port = atoi(argv[2]);
int numberOfWorker = atoi(argv[3]);
SavePid();
int newWorkers = 0;
while (true)
{
if (newWorkers == numberOfWorker)
{
int n;
wait(&n); // Keep
}
pid_t w = fork();
newWorkers++;
if (w == 0) break;
}
worker:
pinkx::net::InetAddress address(port, false);
pinkx::EventLoop looper;
pinkx::TimerWheel timer(looper);
pinkx::http::HttpServer Server(looper, timer, address);
looper.loop();
}