-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
61 lines (50 loc) · 1.46 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
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include "Server.h"
#if !defined(_WIN32)
#include <pthread.h>
#include <signal.h>
int main(int argc, char* argv[])
{
try {
if (argc != 4) {
std::cerr << "Usage: deviceatlas <address> <port> <threads>\n";
std::cerr << " For IPv4, try:\n";
std::cerr << " receiver 0.0.0.0 80 1 .\n";
std::cerr << " For IPv6, try:\n";
std::cerr << " receiver 0::0 80 1 .\n";
return 1;
}
// Block all signals for background thread.
sigset_t new_mask;
sigfillset(&new_mask);
sigset_t old_mask;
pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
// Run server in background thread.
std::size_t num_threads = boost::lexical_cast<std::size_t>(argv[3]);
http::server::Server s(argv[1], argv[2], num_threads);
boost::thread t(boost::bind(&http::server::Server::run, &s));
// Restore previous signals.
pthread_sigmask(SIG_SETMASK, &old_mask, 0);
// Wait for signal indicating time to shut down.
sigset_t wait_mask;
sigemptyset(&wait_mask);
sigaddset(&wait_mask, SIGINT);
sigaddset(&wait_mask, SIGQUIT);
sigaddset(&wait_mask, SIGTERM);
pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
int sig = 0;
sigwait(&wait_mask, &sig);
// Stop the server.
s.stop();
t.join();
} catch (std::exception& e) {
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}
#endif // !defined(_WIN32)