Permalink
Browse files

simplify default TCP/HTTPServer construction

  • Loading branch information...
1 parent 5cde6d3 commit 639842fd29da0d6aa40f02de1eac69a74c34d488 @aleks-f aleks-f committed Mar 30, 2013
@@ -73,6 +73,14 @@ class Net_API HTTPServer: public TCPServer
/// information about the HTTP protocol.
{
public:
+ HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, Poco::UInt16 portNumber = 80, HTTPServerParams::Ptr pParams = new HTTPServerParams);
+ /// Creates HTTPServer listening on the given port (default 80).
+ ///
+ /// The server takes ownership of the HTTPRequstHandlerFactory
+ /// and deletes it when it's no longer needed.
+ ///
+ /// New threads are taken from the default thread pool.
+
HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams);
/// Creates the HTTPServer, using the given ServerSocket.
///
@@ -81,7 +89,7 @@ class Net_API HTTPServer: public TCPServer
///
/// The server also takes ownership of the HTTPServerParams object.
///
- /// News threads are taken from the default thread pool.
+ /// New threads are taken from the default thread pool.
HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, Poco::ThreadPool& threadPool, const ServerSocket& socket, HTTPServerParams::Ptr pParams);
/// Creates the HTTPServer, using the given ServerSocket.
@@ -91,7 +99,7 @@ class Net_API HTTPServer: public TCPServer
///
/// The server also takes ownership of the HTTPServerParams object.
///
- /// News threads are taken from the given thread pool.
+ /// New threads are taken from the given thread pool.
~HTTPServer();
/// Destroys the HTTPServer and its HTTPRequestHandlerFactory.
@@ -98,6 +98,20 @@ class Net_API TCPServer: public Poco::Runnable
/// Already served connections, however, will continue being served.
{
public:
+ TCPServer(TCPServerConnectionFactory::Ptr pFactory, Poco::UInt16 portNumber = 0, TCPServerParams::Ptr pParams = 0);
+ /// Creates the TCPServer, with ServerSocket listening on the given port.
+ /// Default port is zero, allowing any availble port. The port number
+ /// can be queried through TCPServer::port() member.
+ ///
+ /// The server takes ownership of the TCPServerConnectionFactory
+ /// and deletes it when it's no longer needed.
+ ///
+ /// The server also takes ownership of the TCPServerParams object.
+ /// If no TCPServerParams object is given, the server's TCPServerDispatcher
+ /// creates its own one.
+ ///
+ /// New threads are taken from the default thread pool.
+
TCPServer(TCPServerConnectionFactory::Ptr pFactory, const ServerSocket& socket, TCPServerParams::Ptr pParams = 0);
/// Creates the TCPServer, using the given ServerSocket.
///
@@ -163,6 +177,9 @@ class Net_API TCPServer: public Poco::Runnable
int refusedConnections() const;
/// Returns the number of refused connections.
+ const ServerSocket& socket() const;
+ /// Returns the underlying server socket.
+
Poco::UInt16 port() const;
/// Returns the port the server socket listens on.
@@ -188,6 +205,12 @@ class Net_API TCPServer: public Poco::Runnable
};
+inline const ServerSocket& TCPServer::socket() const
+{
+ return _socket;
+}
+
+
inline Poco::UInt16 TCPServer::port() const
{
return _socket.address().port();

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -42,6 +42,13 @@ namespace Poco {
namespace Net {
+HTTPServer::HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, Poco::UInt16 portNumber, HTTPServerParams::Ptr pParams):
+ TCPServer(new HTTPServerConnectionFactory(pParams, pFactory), portNumber, pParams),
+ _pFactory(pFactory)
+{
+}
+
+
HTTPServer::HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams):
TCPServer(new HTTPServerConnectionFactory(pParams, pFactory), socket, pParams),
_pFactory(pFactory)
View
@@ -50,6 +50,15 @@ namespace Poco {
namespace Net {
+TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, Poco::UInt16 portNumber, TCPServerParams::Ptr pParams):
+ _socket(ServerSocket(portNumber)),
+ _pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),
+ _thread(threadName(_socket)),
+ _stopped(true)
+{
+}
+
+
TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, const ServerSocket& socket, TCPServerParams::Ptr pParams):
_socket(socket),
_pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),
@@ -253,13 +253,10 @@ void HTTPServerTest::testClosedRequest()
void HTTPServerTest::testIdentityRequestKeepAlive()
{
- ServerSocket svs(0);
- HTTPServerParams* pParams = new HTTPServerParams;
- pParams->setKeepAlive(true);
- HTTPServer srv(new RequestHandlerFactory, svs, pParams);
+ HTTPServer srv(new RequestHandlerFactory);
srv.start();
- HTTPClientSession cs("localhost", svs.address().port());
+ HTTPClientSession cs("localhost", srv.socket().address().port());
cs.setKeepAlive(true);
std::string body(5000, 'x');
HTTPRequest request("POST", "/echoBody", HTTPMessage::HTTP_1_1);
@@ -322,13 +319,10 @@ void HTTPServerTest::testChunkedRequestKeepAlive()
void HTTPServerTest::testClosedRequestKeepAlive()
{
- ServerSocket svs(0);
- HTTPServerParams* pParams = new HTTPServerParams;
- pParams->setKeepAlive(true);
- HTTPServer srv(new RequestHandlerFactory, svs, pParams);
+ HTTPServer srv(new RequestHandlerFactory);
srv.start();
- HTTPClientSession cs("localhost", svs.address().port());
+ HTTPClientSession cs("localhost", srv.socket().address().port());//svs.address().port());
std::string body(5000, 'x');
HTTPRequest request("POST", "/echoBody");
request.setContentType("text/plain");
@@ -97,15 +97,14 @@ TCPServerTest::~TCPServerTest()
void TCPServerTest::testOneConnection()
{
- ServerSocket svs(0);
- TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
+ TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>());
srv.start();
assert (srv.currentConnections() == 0);
assert (srv.currentThreads() == 0);
assert (srv.queuedConnections() == 0);
assert (srv.totalConnections() == 0);
- SocketAddress sa("localhost", svs.address().port());
+ SocketAddress sa("localhost", srv.socket().address().port());
StreamSocket ss1(sa);
std::string data("hello, world");
ss1.sendBytes(data.data(), (int) data.size());
@@ -125,15 +124,14 @@ void TCPServerTest::testOneConnection()
void TCPServerTest::testTwoConnections()
{
- ServerSocket svs(0);
- TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
+ TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>());
srv.start();
assert (srv.currentConnections() == 0);
assert (srv.currentThreads() == 0);
assert (srv.queuedConnections() == 0);
assert (srv.totalConnections() == 0);
- SocketAddress sa("localhost", svs.address().port());
+ SocketAddress sa("localhost", srv.socket().address().port());
StreamSocket ss1(sa);
StreamSocket ss2(sa);
std::string data("hello, world");

0 comments on commit 639842f

Please sign in to comment.