1818#include < signal.h>
1919
2020#define Log (line ) \
21- do { std::cout << line << std::endl; }while (false )
21+ do { std::cout << line << std::endl; }while (false )
2222
2323struct handler ;
2424typedef boost::network::http::async_server<handler> server;
@@ -28,66 +28,66 @@ typedef boost::network::http::async_server<handler> server;
2828*/
2929struct request_data
3030{
31- const server::request req;
32- server::connection_ptr conn;
31+ const server::request req;
32+ server::connection_ptr conn;
3333
34- typedef boost::shared_ptr< request_data > pointer;
34+ typedef boost::shared_ptr< request_data > pointer;
3535
36- request_data (server::requestconst & req,const server::connection_ptr& conn) :
37- req (req), conn(conn)
38- {
39- }
36+ request_data (server::requestconst & req,const server::connection_ptr& conn) :
37+ req (req), conn(conn)
38+ {
39+ }
4040};
4141
4242/* *
4343 * A basic work queue
4444*/
4545struct work_queue
4646{
47- typedef std::list<request_data::pointer> list;
47+ typedef std::list<request_data::pointer> list;
4848
49- list requests;
50- boost::mutex mutex;
49+ list requests;
50+ boost::mutex mutex;
5151
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+ }
5858
59- inline request_data::pointerget ()
60- {
61- boost::unique_lock< boost::mutex >lock (mutex);
59+ inline request_data::pointerget ()
60+ {
61+ boost::unique_lock< boost::mutex >lock (mutex);
6262
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+ }
6868
69- (void )lock;
69+ (void )lock;
7070
71- return p_ret;
72- }
71+ return p_ret;
72+ }
7373};
7474
7575struct handler
7676{
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::requestconst & 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::requestconst & req,const server::connection_ptr& conn)
88+ {
89+ queue.put (boost::make_shared<request_data>(req, conn));
90+ }
9191};
9292
9393/* *
@@ -98,11 +98,11 @@ struct handler
9898 * @param p_server_instance
9999*/
100100void 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)
103103{
104- if (!error)
105- p_server_instance->stop ();
104+ if (!error)
105+ p_server_instance->stop ();
106106}
107107
108108/* *
@@ -112,89 +112,89 @@ void shut_me_down(
112112*/
113113void process_request (work_queue& queue)
114114{
115- while (!boost::this_thread::interruption_requested ()) {
116- request_data::pointerp_req (queue.get ());
117- if (p_req) {
115+ while (!boost::this_thread::interruption_requested ()) {
116+ request_data::pointerp_req (queue.get ());
117+ if (p_req) {
118118
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 ));
121121
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+ }
125125
126- boost::this_thread::sleep (boost::posix_time::microseconds (1000 ));
127- }
126+ boost::this_thread::sleep (boost::posix_time::microseconds (1000 ));
127+ }
128128}
129129
130130int main (void ) try
131131{
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- handlerrequest_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_setsignals (*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+ handlerrequest_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_setsignals (*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);
195195}
196196catch (const std::exception& e)
197197{
198- Log (" Abnormal termination - exception:" <<e.what ());
199- exit (EXIT_FAILURE);
198+ Log (" Abnormal termination - exception:" <<e.what ());
199+ exit (EXIT_FAILURE);
200200}