void bindServerSocket(const std::string& response) {
server_socket_.reset(new test::TestServerUnixSocket(*getIOService(),
unixSocketFilePath(),
- TEST_TIMEOUT,
response));
+ server_socket_->startTimer(TEST_TIMEOUT);
server_socket_->bindServerSocket();
}
}
}
-
- CtrlAgentCommandMgrTest* getTestSelf() {
- return (this);
- }
-
/// @brief a convenience reference to control agent command manager
CtrlAgentCommandMgr& mgr_;
/// @brief Constructor.
///
/// Removes unix socket descriptor before the test.
- UnixDomainSocketTest() : io_service_(),
- test_socket_(io_service_, unixSocketFilePath(),
- TEST_TIMEOUT) {
+ UnixDomainSocketTest() :
+ io_service_(),
+ test_socket_(new test::TestServerUnixSocket(io_service_,
+ unixSocketFilePath())) {
+ test_socket_->startTimer(TEST_TIMEOUT);
removeUnixSocketFile();
}
IOService io_service_;
/// @brief Server side unix socket used in these tests.
- test::TestServerUnixSocket test_socket_;
+ test::TestServerUnixSocketPtr test_socket_;
};
// This test verifies that the client can send data over the unix
// domain socket and receive a response.
TEST_F(UnixDomainSocketTest, sendReceive) {
// Start the server.
- test_socket_.bindServerSocket();
+ test_socket_->bindServerSocket();
// Setup client side.
UnixDomainSocket socket(io_service_);
// the socket is connected.
TEST_F(UnixDomainSocketTest, getNative) {
// Start the server.
- test_socket_.bindServerSocket();
+ test_socket_->bindServerSocket();
// Setup client side.
UnixDomainSocket socket(io_service_);
#include <config.h>
#include <asiolink/interval_timer.h>
#include <asiolink/io_service.h>
+#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <gtest/gtest.h>
#include <list>
/// the number of responses sent by the server is greater than
/// expected. The number of responses sent so far can be retrieved
/// using @ref TestServerUnixSocket::getResponseNum.
-class TestServerUnixSocket {
+///
+/// This class uses @c shared_from_this() to pass its instance to the
+/// @c boost::bind function, thus the caller must store shared pointer
+/// to this object.
+class TestServerUnixSocket
+ : public boost::enable_shared_from_this<TestServerUnixSocket> {
public:
/// @brief Constructor.
/// @param test_timeout Test timeout in milliseconds.
void startTimer(const long test_timeout);
+ /// @brief Starts timer for detecting test timeout.
+ ///
+ /// @param test_timeout Test timeout in milliseconds.
+ void startTimer(const long test_timeout);
+
/// @brief Generates response of a given length.
///
/// @param response_size Desired response size.
: request_timer_(io_service),
request_timeout_(request_timeout),
socket_(io_service),
- socket_callback_(boost::bind(&HttpConnection::socketReadCallback, this,
- _1, _2)),
- socket_write_callback_(boost::bind(&HttpConnection::socketWriteCallback,
- this, _1, _2)),
acceptor_(acceptor),
connection_pool_(connection_pool),
response_creator_(response_creator),
void
HttpConnection::asyncAccept() {
HttpAcceptorCallback cb = boost::bind(&HttpConnection::acceptorCallback,
- this, _1);
+ shared_from_this(), _1);
try {
acceptor_.asyncAccept(socket_, cb);
HttpConnection::doRead() {
try {
TCPEndpoint endpoint;
+ SocketCallback cb(boost::bind(&HttpConnection::socketReadCallback,
+ shared_from_this(), _1, _2));
socket_.asyncReceive(static_cast<void*>(buf_.data()), buf_.size(),
- 0, &endpoint, socket_callback_);
+ 0, &endpoint, cb);
} catch (const std::exception& ex) {
stopThisConnection();
HttpConnection::doWrite() {
try {
if (!output_buf_.empty()) {
+ SocketCallback cb(boost::bind(&HttpConnection::socketWriteCallback,
+ shared_from_this(), _1, _2));
socket_.asyncSend(output_buf_.data(),
output_buf_.length(),
- socket_write_callback_);
+ cb);
} else {
stopThisConnection();
}
HTTP_REQUEST_RECEIVE_START)
.arg(getRemoteEndpointAddressAsText())
.arg(static_cast<unsigned>(request_timeout_/1000));
- request_timer_.setup(boost::bind(&HttpConnection::requestTimeoutCallback, this),
+ request_timer_.setup(boost::bind(&HttpConnection::requestTimeoutCallback,
+ shared_from_this()),
request_timeout_, IntervalTimer::ONE_SHOT);
doRead();
}
/// @brief Socket used by this connection.
asiolink::TCPSocket<SocketCallback> socket_;
- /// @brief Callback invoked when data received over the socket.
- SocketCallback socket_callback_;
-
- /// @brief Callback invoked when data sent over the socket.
- SocketCallback socket_write_callback_;
-
/// @brief Reference to the TCP acceptor used to accept new connections.
HttpAcceptor& acceptor_;
#include <http/connection_pool.h>
#include <http/http_acceptor.h>
#include <http/listener.h>
+#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
+#include <iostream>
using namespace isc::asiolink;
namespace http {
/// @brief Implementation of the @ref HttpListener.
-class HttpListenerImpl {
+class HttpListenerImpl : public boost::enable_shared_from_this<HttpListenerImpl> {
public:
/// @brief Constructor.
// depends on the use case.
HttpResponseCreatorPtr response_creator = creator_factory_->create();
HttpAcceptorCallback acceptor_callback =
- boost::bind(&HttpListenerImpl::acceptHandler, this, _1);
+ boost::bind(&HttpListenerImpl::acceptHandler, shared_from_this(), _1);
HttpConnectionPtr conn(new HttpConnection(io_service_, acceptor_,
connections_,
response_creator,