18
18
#include < signal.h>
19
19
20
20
#define Log (line ) \
21
- do { std::cout << line << std::endl; } while (false )
21
+ do { std::cout << line << std::endl; } while (false )
22
22
23
23
struct handler ;
24
24
typedef boost::network::http::async_server<handler> server;
@@ -28,66 +28,66 @@ typedef boost::network::http::async_server<handler> server;
28
28
*/
29
29
struct request_data
30
30
{
31
- const server::request req;
32
- server::connection_ptr conn;
31
+ const server::request req;
32
+ server::connection_ptr conn;
33
33
34
- typedef boost::shared_ptr< request_data > pointer;
34
+ typedef boost::shared_ptr< request_data > pointer;
35
35
36
- request_data (server::request const & req, const server::connection_ptr& conn) :
37
- req (req), conn(conn)
38
- {
39
- }
36
+ request_data (server::request const & req, const server::connection_ptr& conn) :
37
+ req (req), conn(conn)
38
+ {
39
+ }
40
40
};
41
41
42
42
/* *
43
43
* A basic work queue
44
44
*/
45
45
struct work_queue
46
46
{
47
- typedef std::list<request_data::pointer> list;
47
+ typedef std::list<request_data::pointer> list;
48
48
49
- list requests;
50
- boost::mutex mutex;
49
+ list requests;
50
+ boost::mutex mutex;
51
51
52
- inline void put (const request_data::pointer& p_rd)
53
- {
54
- boost::unique_lock< boost::mutex > lock (mutex);
55
- requests.push_back (p_rd);
56
- (void )lock;
57
- }
52
+ inline void put (const request_data::pointer& p_rd)
53
+ {
54
+ boost::unique_lock< boost::mutex > lock (mutex);
55
+ requests.push_back (p_rd);
56
+ (void )lock;
57
+ }
58
58
59
- inline request_data::pointer get ()
60
- {
61
- boost::unique_lock< boost::mutex > lock (mutex);
59
+ inline request_data::pointer get ()
60
+ {
61
+ boost::unique_lock< boost::mutex > lock (mutex);
62
62
63
- request_data::pointer p_ret;
64
- if (!requests.empty ()) {
65
- p_ret = requests.front ();
66
- requests.pop_front ();
67
- }
63
+ request_data::pointer p_ret;
64
+ if (!requests.empty ()) {
65
+ p_ret = requests.front ();
66
+ requests.pop_front ();
67
+ }
68
68
69
- (void )lock;
69
+ (void )lock;
70
70
71
- return p_ret;
72
- }
71
+ return p_ret;
72
+ }
73
73
};
74
74
75
75
struct handler
76
76
{
77
- work_queue& queue;
78
-
79
- handler (work_queue& queue) : queue(queue) { }
80
-
81
- /* *
82
- * Feed the work queue
83
- *
84
- * @param req
85
- * @param conn
86
- */
87
- void operator ()(server::request const & req, const server::connection_ptr& conn)
88
- {
89
- queue.put (boost::make_shared<request_data>(req, conn));
90
- }
77
+ work_queue& queue;
78
+
79
+ handler (work_queue& queue) : queue(queue) { }
80
+
81
+ /* *
82
+ * Feed the work queue
83
+ *
84
+ * @param req
85
+ * @param conn
86
+ */
87
+ void operator ()(server::request const & req, const server::connection_ptr& conn)
88
+ {
89
+ queue.put (boost::make_shared<request_data>(req, conn));
90
+ }
91
91
};
92
92
93
93
/* *
@@ -98,11 +98,11 @@ struct handler
98
98
* @param p_server_instance
99
99
*/
100
100
void shut_me_down (
101
- const boost::system::error_code& error
102
- , int signal, boost::shared_ptr< server > p_server_instance)
101
+ const boost::system::error_code& error
102
+ , int signal, boost::shared_ptr< server > p_server_instance)
103
103
{
104
- if (!error)
105
- p_server_instance->stop ();
104
+ if (!error)
105
+ p_server_instance->stop ();
106
106
}
107
107
108
108
/* *
@@ -112,89 +112,89 @@ void shut_me_down(
112
112
*/
113
113
void process_request (work_queue& queue)
114
114
{
115
- while (!boost::this_thread::interruption_requested ()) {
116
- request_data::pointer p_req (queue.get ());
117
- if (p_req) {
115
+ while (!boost::this_thread::interruption_requested ()) {
116
+ request_data::pointer p_req (queue.get ());
117
+ if (p_req) {
118
118
119
- // some heavy work!
120
- boost::this_thread::sleep (boost::posix_time::seconds (10 ));
119
+ // some heavy work!
120
+ boost::this_thread::sleep (boost::posix_time::seconds (10 ));
121
121
122
- p_req->conn ->set_status (server::connection::ok);
123
- p_req->conn ->write (" Hello, world!" );
124
- }
122
+ p_req->conn ->set_status (server::connection::ok);
123
+ p_req->conn ->write (" Hello, world!" );
124
+ }
125
125
126
- boost::this_thread::sleep (boost::posix_time::microseconds (1000 ));
127
- }
126
+ boost::this_thread::sleep (boost::posix_time::microseconds (1000 ));
127
+ }
128
128
}
129
129
130
130
int main (void ) try
131
131
{
132
- // the thread group
133
- boost::shared_ptr< boost::thread_group > p_threads (
134
- boost::make_shared< boost::thread_group>());
135
-
136
- // setup asio::io_service
137
- boost::shared_ptr< boost::asio::io_service > p_io_service (
138
- boost::make_shared< boost::asio::io_service >());
139
- boost::shared_ptr< boost::asio::io_service::work > p_work (
140
- boost::make_shared< boost::asio::io_service::work >(
141
- boost::ref (*p_io_service)));
142
-
143
- // io_service threads
144
- {
145
- int n_threads = 5 ;
146
- while (0 < n_threads--) {
147
- p_threads->create_thread (
148
- boost::bind (&boost::asio::io_service::run, p_io_service));
149
- }
150
- }
151
-
152
- // the shared work queue
153
- work_queue queue;
154
-
155
- // worker threads that will process the request; off the queue
156
- {
157
- int n_threads = 5 ;
158
- while (0 < n_threads--) {
159
- p_threads->create_thread (
160
- boost::bind (process_request, boost::ref (queue)));
161
- }
162
- }
163
-
164
- // setup the async server
165
- handler request_handler (queue);
166
- boost::shared_ptr< server > p_server_instance (
167
- boost::make_shared<server>(
168
- server::options (request_handler).
169
- address (" 0.0.0.0" )
170
- .port (" 8800" )
171
- .io_service (p_io_service)
172
- .reuse_address (true )
173
- .thread_pool (
174
- boost::make_shared<boost::network::utils::thread_pool>(
175
- 2 , p_io_service, p_threads))));
176
-
177
- // setup clean shutdown
178
- boost::asio::signal_set signals (*p_io_service, SIGINT, SIGTERM);
179
- signals.async_wait (boost::bind (shut_me_down, _1, _2, p_server_instance));
180
-
181
- // run the async server
182
- p_server_instance->run ();
183
-
184
- // we are stopped - shutting down
185
-
186
- p_threads->interrupt_all ();
187
-
188
- p_work.reset ();
189
- p_io_service->stop ();
190
-
191
- p_threads->join_all ();
192
-
193
- Log (" Terminated normally" );
194
- exit (EXIT_SUCCESS);
132
+ // the thread group
133
+ boost::shared_ptr< boost::thread_group > p_threads (
134
+ boost::make_shared< boost::thread_group>());
135
+
136
+ // setup asio::io_service
137
+ boost::shared_ptr< boost::asio::io_service > p_io_service (
138
+ boost::make_shared< boost::asio::io_service >());
139
+ boost::shared_ptr< boost::asio::io_service::work > p_work (
140
+ boost::make_shared< boost::asio::io_service::work >(
141
+ boost::ref (*p_io_service)));
142
+
143
+ // io_service threads
144
+ {
145
+ int n_threads = 5 ;
146
+ while (0 < n_threads--) {
147
+ p_threads->create_thread (
148
+ boost::bind (&boost::asio::io_service::run, p_io_service));
149
+ }
150
+ }
151
+
152
+ // the shared work queue
153
+ work_queue queue;
154
+
155
+ // worker threads that will process the request; off the queue
156
+ {
157
+ int n_threads = 5 ;
158
+ while (0 < n_threads--) {
159
+ p_threads->create_thread (
160
+ boost::bind (process_request, boost::ref (queue)));
161
+ }
162
+ }
163
+
164
+ // setup the async server
165
+ handler request_handler (queue);
166
+ boost::shared_ptr< server > p_server_instance (
167
+ boost::make_shared<server>(
168
+ server::options (request_handler).
169
+ address (" 0.0.0.0" )
170
+ .port (" 8800" )
171
+ .io_service (p_io_service)
172
+ .reuse_address (true )
173
+ .thread_pool (
174
+ boost::make_shared<boost::network::utils::thread_pool>(
175
+ 2 , p_io_service, p_threads))));
176
+
177
+ // setup clean shutdown
178
+ boost::asio::signal_set signals (*p_io_service, SIGINT, SIGTERM);
179
+ signals.async_wait (boost::bind (shut_me_down, _1, _2, p_server_instance));
180
+
181
+ // run the async server
182
+ p_server_instance->run ();
183
+
184
+ // we are stopped - shutting down
185
+
186
+ p_threads->interrupt_all ();
187
+
188
+ p_work.reset ();
189
+ p_io_service->stop ();
190
+
191
+ p_threads->join_all ();
192
+
193
+ Log (" Terminated normally" );
194
+ exit (EXIT_SUCCESS);
195
195
}
196
196
catch (const std::exception & e)
197
197
{
198
- Log (" Abnormal termination - exception:" <<e.what ());
199
- exit (EXIT_FAILURE);
198
+ Log (" Abnormal termination - exception:" <<e.what ());
199
+ exit (EXIT_FAILURE);
200
200
}
0 commit comments