Browse Source

[5189] Use shared pointers to reference the handlers.

Marcin Siodelski 8 years ago
parent
commit
2888baf46b

+ 1 - 6
src/bin/agent/tests/ca_command_mgr_unittests.cc

@@ -174,8 +174,8 @@ public:
     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();
     }
 
@@ -254,11 +254,6 @@ public:
         }
     }
 
-
-    CtrlAgentCommandMgrTest* getTestSelf() {
-        return (this);
-    }
-
     /// @brief a convenience reference to control agent command manager
     CtrlAgentCommandMgr& mgr_;
 

+ 8 - 6
src/lib/asiolink/tests/unix_domain_socket_unittest.cc

@@ -33,9 +33,11 @@ public:
     /// @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();
     }
 
@@ -73,14 +75,14 @@ public:
     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_);
@@ -267,7 +269,7 @@ TEST_F(UnixDomainSocketTest, asyncClientErrors) {
 // the socket is connected.
 TEST_F(UnixDomainSocketTest, getNative) {
     // Start the server.
-    test_socket_.bindServerSocket();
+    test_socket_->bindServerSocket();
 
     // Setup client side.
     UnixDomainSocket socket(io_service_);

+ 12 - 1
src/lib/asiolink/testutils/test_server_unix_socket.h

@@ -10,6 +10,7 @@
 #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>
@@ -39,7 +40,12 @@ class ConnectionPool;
 /// 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.
@@ -61,6 +67,11 @@ public:
     /// @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.

+ 9 - 8
src/lib/http/connection.cc

@@ -34,10 +34,6 @@ HttpConnection:: HttpConnection(asiolink::IOService& io_service,
     : 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),
@@ -72,7 +68,7 @@ HttpConnection::stopThisConnection() {
 void
 HttpConnection::asyncAccept() {
     HttpAcceptorCallback cb = boost::bind(&HttpConnection::acceptorCallback,
-                                          this, _1);
+                                          shared_from_this(), _1);
     try {
         acceptor_.asyncAccept(socket_, cb);
 
@@ -86,8 +82,10 @@ void
 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();
@@ -98,9 +96,11 @@ void
 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();
         }
@@ -133,7 +133,8 @@ HttpConnection::acceptorCallback(const boost::system::error_code& ec) {
                   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();
     }

+ 0 - 6
src/lib/http/connection.h

@@ -187,12 +187,6 @@ private:
     /// @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_;
 

+ 4 - 2
src/lib/http/listener.cc

@@ -10,7 +10,9 @@
 #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;
 
@@ -18,7 +20,7 @@ namespace isc {
 namespace http {
 
 /// @brief Implementation of the @ref HttpListener.
-class HttpListenerImpl {
+class HttpListenerImpl : public boost::enable_shared_from_this<HttpListenerImpl> {
 public:
 
     /// @brief Constructor.
@@ -164,7 +166,7 @@ HttpListenerImpl::accept() {
     // 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,