Skip to content

Commit 2b1870e

Browse files
committed
Using Boost.Parameter in the HTTP Server constructor interface.
1 parent f829f7d commit 2b1870e

File tree

8 files changed

+171
-43
lines changed

8 files changed

+171
-43
lines changed

boost/network/protocol/http/client/parameters.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
// (See accompanying file LICENSE_1_0.txt or copy at
77
// http://www.boost.org/LICENSE_1_0.txt)
88

9-
#include <boost/parameter.hpp>
9+
#include <boost/network/protocol/http/parameters.hpp>
1010

1111
namespace boost { namespace network { namespace http {
1212

1313
BOOST_PARAMETER_NAME(follow_redirects)
14-
BOOST_PARAMETER_NAME(io_service)
1514
BOOST_PARAMETER_NAME(cache_resolved)
1615

1716
} /* http */
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_PARAMETERS_HPP_20101210
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_PARAMETERS_HPP_20101210
3+
4+
// Copyright 2010 Dean Michael Berris.
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#if !defined(BOOST_PARAMETER_MAX_ARITY)
10+
#define BOOST_PARAMETER_MAX_ARITY 6
11+
#endif
12+
#include <boost/parameter.hpp>
13+
14+
namespace boost { namespace network { namespace http {
15+
16+
BOOST_PARAMETER_NAME(io_service)
17+
18+
} /* http */
19+
20+
} /* network */
21+
22+
} /* boost */
23+
24+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_PARAMETERS_HPP_20101210 */

boost/network/protocol/http/server.hpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <boost/network/protocol/http/request.hpp>
1414
#include <boost/network/protocol/http/server/sync_server.hpp>
1515
#include <boost/network/protocol/http/server/async_server.hpp>
16+
#include <boost/network/protocol/http/server/parameters.hpp>
1617

1718
namespace boost { namespace network { namespace http {
1819

@@ -38,11 +39,16 @@ namespace boost { namespace network { namespace http {
3839
typedef typename server_base<tags::http_server, Handler>::type
3940
server_base;
4041

41-
server(typename server_base::string_type const & address,
42-
typename server_base::string_type const & port,
43-
Handler & handler)
44-
: server_base(address, port, handler) {}
45-
42+
BOOST_PARAMETER_CONSTRUCTOR(
43+
server, (server_base), tag,
44+
(required
45+
(address, (typename server_base::string_type const &))
46+
(port, (typename server_base::string_type const &))
47+
(in_out(handler), (Handler &)))
48+
(optional
49+
(in_out(io_service), (boost::asio::io_service &))
50+
(socket_options, *))
51+
)
4652
};
4753

4854
template <class Handler>
@@ -51,11 +57,17 @@ namespace boost { namespace network { namespace http {
5157
typedef typename server_base<tags::http_async_server, Handler>::type
5258
server_base;
5359

54-
async_server(typename server_base::string_type const & address,
55-
typename server_base::string_type const & port,
56-
Handler & handler,
57-
utils::thread_pool & thread_pool)
58-
: server_base(address, port, handler, thread_pool) {}
60+
BOOST_PARAMETER_CONSTRUCTOR(
61+
async_server, (server_base), tag,
62+
(required
63+
(address, (typename server_base::string_type const &))
64+
(port, (typename server_base::string_type const &))
65+
(in_out(handler), (Handler&))
66+
(in_out(thread_pool), (utils::thread_pool&)))
67+
(optional
68+
(in_out(io_service), (boost::asio::io_service&))
69+
(socket_options, *))
70+
)
5971
};
6072

6173
} // namespace http

boost/network/protocol/http/server/async_server.hpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,45 @@
77
// http://www.boost.org/LICENSE_1_0.txt)
88

99
#include <boost/network/protocol/http/server/async_connection.hpp>
10+
#include <boost/thread/mutex.hpp>
11+
#include <boost/network/protocol/http/server/storage_base.hpp>
1012
#include <boost/network/utils/thread_pool.hpp>
1113

1214
namespace boost { namespace network { namespace http {
1315

1416
template <class Tag, class Handler>
15-
struct async_server_base {
17+
struct async_server_base : server_storage_base {
1618
typedef basic_request<Tag> request;
1719
typedef basic_response<Tag> response;
1820
typedef typename string<Tag>::type string_type;
1921
typedef typename boost::network::http::response_header<Tag>::type response_header;
2022
typedef async_connection<Tag,Handler> connection;
2123
typedef shared_ptr<connection> connection_ptr;
2224

23-
async_server_base(string_type const & address
24-
, string_type const & port
25-
, Handler & handler
26-
, utils::thread_pool & thread_pool)
27-
: handler(handler)
28-
, address_(address)
29-
, port_(port)
30-
, thread_pool(thread_pool)
31-
, self_service(new asio::io_service())
32-
, io_service()
33-
, acceptor(io_service)
25+
template <class ArgPack>
26+
async_server_base(ArgPack const & args)
27+
: server_storage_base(args,
28+
typename mpl::if_<
29+
is_same<
30+
typename parameter::value_type<ArgPack, tag::io_service, void>::type,
31+
void
32+
>,
33+
server_storage_base::no_io_service,
34+
server_storage_base::has_io_service
35+
>::type())
36+
, handler(args[_handler])
37+
, address_(args[_address])
38+
, port_(args[_port])
39+
, thread_pool(args[_thread_pool])
40+
, acceptor(server_storage_base::service_)
3441
, stopping(false)
3542
, new_connection()
3643
, listening_mutex_()
3744
, listening(false)
3845
{}
3946

4047
void run() {
41-
boost::unique_lock<boost::mutex> listening_lock(listening_mutex_);
42-
if (!listening) start_listening();
43-
listening_lock.unlock();
48+
listen();
4449
io_service.run();
4550
};
4651

@@ -51,6 +56,12 @@ namespace boost { namespace network { namespace http {
5156
acceptor.cancel();
5257
}
5358

59+
void listen() {
60+
boost::unique_lock<boost::mutex> listening_lock(listening_mutex_);
61+
if (!listening) start_listening();
62+
listening_lock.unlock();
63+
}
64+
5465
private:
5566
Handler & handler;
5667
string_type address_, port_;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_PARAMETERS_HPP_20101210
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_PARAMETERS_HPP_20101210
3+
4+
// Copyright 2010 Dean Michael Berris.
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#include <boost/network/protocol/http/parameters.hpp>
10+
11+
namespace boost { namespace network { namespace http {
12+
13+
BOOST_PARAMETER_NAME(address)
14+
BOOST_PARAMETER_NAME(port)
15+
BOOST_PARAMETER_NAME(handler)
16+
BOOST_PARAMETER_NAME(thread_pool)
17+
BOOST_PARAMETER_NAME(socket_options)
18+
19+
} /* http */
20+
21+
} /* network */
22+
23+
} /* boost */
24+
25+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_PARAMETERS_HPP_20101210 */
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_STORAGE_BASE_HPP_20101210
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_STORAGE_BASE_HPP_20101210
3+
4+
// Copyright 2010 Dean Michael Berris.
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#include <boost/network/protocol/http/server/parameters.hpp>
10+
11+
namespace boost { namespace network { namespace http {
12+
13+
struct server_storage_base {
14+
struct no_io_service {};
15+
struct has_io_service {};
16+
protected:
17+
template <class ArgPack>
18+
server_storage_base(ArgPack const & args, no_io_service)
19+
: self_service_(new boost::asio::io_service())
20+
, service_(*self_service_)
21+
{}
22+
23+
template <class ArgPack>
24+
server_storage_base(ArgPack const & args, has_io_service)
25+
: self_service_()
26+
, service_(args[_io_service])
27+
{}
28+
29+
std::auto_ptr<asio::io_service> self_service_;
30+
asio::io_service & service_;
31+
};
32+
33+
34+
} /* http */
35+
36+
} /* network */
37+
38+
} /* boost */
39+
40+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_STORAGE_BASE_HPP_20101210 */

boost/network/protocol/http/server/sync_server.hpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,42 @@
1414
#include <boost/network/protocol/http/response.hpp>
1515
#include <boost/network/protocol/http/request.hpp>
1616
#include <boost/network/protocol/http/server/sync_connection.hpp>
17+
#include <boost/network/protocol/http/server/parameters.hpp>
18+
#include <boost/network/protocol/http/server/storage_base.hpp>
1719
#include <boost/network/traits/string.hpp>
1820
#include <boost/thread/mutex.hpp>
1921

2022
namespace boost { namespace network { namespace http {
21-
23+
2224
template <class Tag, class Handler>
23-
struct sync_server_base {
25+
struct sync_server_base : server_storage_base {
2426
typedef typename string<Tag>::type string_type;
2527
typedef basic_request<Tag> request;
2628
typedef basic_response<Tag> response;
2729
typedef typename boost::network::http::response_header<Tag>::type response_header;
2830

29-
sync_server_base(string_type const & address,
30-
string_type const & port,
31-
Handler & handler)
32-
: handler_(handler)
33-
, address_(address)
34-
, port_(port)
35-
, self_service_(new boost::asio::io_service())
36-
, service_(*self_service_)
37-
, acceptor_(service_)
31+
template <class ArgPack>
32+
sync_server_base(ArgPack const & args)
33+
: server_storage_base(args,
34+
typename mpl::if_<
35+
is_same<
36+
typename parameter::value_type<ArgPack, tag::io_service, void>::type,
37+
void
38+
>,
39+
server_storage_base::no_io_service,
40+
server_storage_base::has_io_service
41+
>::type())
42+
, handler_(args[_handler])
43+
, address_(args[_address])
44+
, port_(args[_port])
45+
, acceptor_(server_storage_base::service_)
3846
, new_connection()
3947
, listening_mutex_()
4048
, listening_(false)
4149
{}
4250

4351
void run() {
44-
boost::unique_lock<boost::mutex> listening_lock(listening_mutex_);
45-
if (!listening_) start_listening();
46-
listening_lock.unlock();
52+
listen();
4753
service_.run();
4854
}
4955

@@ -52,12 +58,16 @@ namespace boost { namespace network { namespace http {
5258
acceptor_.cancel();
5359
}
5460

61+
void listen() {
62+
boost::unique_lock<boost::mutex> listening_lock(listening_mutex_);
63+
if (!listening_) start_listening();
64+
listening_lock.unlock();
65+
}
66+
5567
private:
5668

5769
Handler & handler_;
5870
string_type address_, port_;
59-
std::auto_ptr<boost::asio::io_service> self_service_;
60-
boost::asio::io_service & service_;
6171
boost::asio::ip::tcp::acceptor acceptor_;
6272
boost::shared_ptr<sync_connection<Tag,Handler> > new_connection;
6373
boost::mutex listening_mutex_;

libs/network/test/http/server_constructor_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ BOOST_AUTO_TEST_CASE(minimal_constructor) {
4444
}
4545

4646
BOOST_AUTO_TEST_CASE(with_io_service_parameter) {
47+
dummy_sync_handler sync_handler;
48+
dummy_async_handler async_handler;
49+
util::thread_pool pool;
50+
boost::asio::io_service io_service;
51+
52+
BOOST_CHECK_NO_THROW(sync_server sync_instance("127.0.0.1", "80", sync_handler, io_service));
53+
BOOST_CHECK_NO_THROW(async_server async_instance("127.0.0.1", "80", async_handler, pool, io_service));
4754
}
4855

4956
BOOST_AUTO_TEST_CASE(with_socket_options_parameter) {

0 commit comments

Comments
 (0)