diff options
author | Razvan Becheriu <razvan@isc.org> | 2023-12-14 19:16:52 +0100 |
---|---|---|
committer | Razvan Becheriu <razvan@isc.org> | 2024-03-05 08:50:05 +0100 |
commit | 1b070fe4b53be34d34bdbfe59ec22d65b27fa982 (patch) | |
tree | ff4fa433d4739afe2e9944b46cbab587afd302c5 /src | |
parent | [#3271] bump version in configure.ac to 2.5.7 (diff) | |
download | kea-1b070fe4b53be34d34bdbfe59ec22d65b27fa982.tar.xz kea-1b070fe4b53be34d34bdbfe59ec22d65b27fa982.zip |
[#3190] use smart pointer to capture IOService instance
Diffstat (limited to 'src')
108 files changed, 785 insertions, 757 deletions
diff --git a/src/bin/agent/ca_command_mgr.cc b/src/bin/agent/ca_command_mgr.cc index 848753d333..2fa35b0b50 100644 --- a/src/bin/agent/ca_command_mgr.cc +++ b/src/bin/agent/ca_command_mgr.cc @@ -226,7 +226,7 @@ CtrlAgentCommandMgr::forwardCommand(const std::string& service, // Forward command and receive reply. IOServicePtr io_service(new IOService());; - ClientConnection conn(*io_service); + ClientConnection conn(io_service); boost::system::error_code received_ec; ConstJSONFeedPtr received_feed; conn.start(ClientConnection::SocketPath(socket_name), diff --git a/src/bin/agent/ca_process.cc b/src/bin/agent/ca_process.cc index 0c6128f66a..fd36a46e42 100644 --- a/src/bin/agent/ca_process.cc +++ b/src/bin/agent/ca_process.cc @@ -164,7 +164,7 @@ CtrlAgentProcess::configure(isc::data::ConstElementPtr config_set, // Create http listener. It will open up a TCP socket and be // prepared to accept incoming connection. HttpListenerPtr http_listener - (new HttpListener(*getIOService(), server_address, + (new HttpListener(getIOService(), server_address, server_port, tls_context, rcf, HttpListener::RequestTimeout(TIMEOUT_AGENT_RECEIVE_COMMAND), HttpListener::IdleTimeout(TIMEOUT_AGENT_IDLE_CONNECTION_TIMEOUT))); diff --git a/src/bin/agent/tests/ca_command_mgr_unittests.cc b/src/bin/agent/tests/ca_command_mgr_unittests.cc index 9eceedfc77..05ec8c7bc7 100644 --- a/src/bin/agent/tests/ca_command_mgr_unittests.cc +++ b/src/bin/agent/tests/ca_command_mgr_unittests.cc @@ -170,7 +170,7 @@ public: /// @param use_thread Indicates if the IO service will be ran in thread. void bindServerSocket(const std::string& response, const bool use_thread = false) { - server_socket_.reset(new test::TestServerUnixSocket(*getIOService(), + server_socket_.reset(new test::TestServerUnixSocket(getIOService(), unixSocketFilePath(), response)); server_socket_->startTimer(TEST_TIMEOUT); diff --git a/src/bin/agent/tests/ca_controller_unittests.cc b/src/bin/agent/tests/ca_controller_unittests.cc index 3ffab4452e..1e0c58c30e 100644 --- a/src/bin/agent/tests/ca_controller_unittests.cc +++ b/src/bin/agent/tests/ca_controller_unittests.cc @@ -242,7 +242,7 @@ TEST_F(CtrlAgentControllerTest, launchNormalShutdown) { // Tests that the SIGINT triggers a normal shutdown. TEST_F(CtrlAgentControllerTest, sigintShutdown) { // Setup to raise SIGHUP in 1 ms. - TimedSignal sighup(*getIOService(), SIGINT, 1); + TimedSignal sighup(getIOService(), SIGINT, 1); // Write valid_agent_config and then run launch() for a maximum // of 1000 ms. @@ -258,7 +258,7 @@ TEST_F(CtrlAgentControllerTest, sigintShutdown) { // Tests that the SIGTERM triggers a normal shutdown. TEST_F(CtrlAgentControllerTest, sigtermShutdown) { // Setup to raise SIGTERM in 1 ms. - TimedSignal sighup(*getIOService(), SIGTERM, 1); + TimedSignal sighup(getIOService(), SIGTERM, 1); // Write valid_agent_config and then run launch() for a maximum of 1 s. time_duration elapsed_time; @@ -307,7 +307,7 @@ TEST_F(CtrlAgentControllerTest, successfulConfigUpdate) { // Schedule reconfiguration. scheduleTimedWrite(second_config, 100); // Schedule SIGHUP signal to trigger reconfiguration. - TimedSignal sighup(*getIOService(), SIGHUP, 200); + TimedSignal sighup(getIOService(), SIGHUP, 200); // Start the server. time_duration elapsed_time; @@ -372,7 +372,7 @@ TEST_F(CtrlAgentControllerTest, unsuccessfulConfigUpdate) { // Schedule reconfiguration. scheduleTimedWrite(second_config, 100); // Schedule SIGHUP signal to trigger reconfiguration. - TimedSignal sighup(*getIOService(), SIGHUP, 200); + TimedSignal sighup(getIOService(), SIGHUP, 200); // Start the server. time_duration elapsed_time; @@ -438,7 +438,7 @@ TEST_F(CtrlAgentControllerTest, noListenerChange) { // Schedule reconfiguration. scheduleTimedWrite(second_config, 100); // Schedule SIGHUP signal to trigger reconfiguration. - TimedSignal sighup(*getIOService(), SIGHUP, 200); + TimedSignal sighup(getIOService(), SIGHUP, 200); // Start the server. time_duration elapsed_time; diff --git a/src/bin/agent/tests/ca_process_unittests.cc b/src/bin/agent/tests/ca_process_unittests.cc index 6f9bd12d97..ecb2563188 100644 --- a/src/bin/agent/tests/ca_process_unittests.cc +++ b/src/bin/agent/tests/ca_process_unittests.cc @@ -67,7 +67,7 @@ TEST(CtrlAgentProcess, construction) { TEST_F(CtrlAgentProcessTest, shutdown) { // Use an asiolink IntervalTimer and callback to generate the // shutdown invocation. (Note IntervalTimer setup is in milliseconds). - IntervalTimer timer(*getIOService()); + IntervalTimer timer(getIOService()); timer.setup(std::bind(&CtrlAgentProcessTest::genShutdownCallback, this), 200); diff --git a/src/bin/d2/d2_queue_mgr.cc b/src/bin/d2/d2_queue_mgr.cc index 71949b3bcf..12efe742e8 100644 --- a/src/bin/d2/d2_queue_mgr.cc +++ b/src/bin/d2/d2_queue_mgr.cc @@ -131,7 +131,7 @@ D2QueueMgr::startListening() { // Instruct the listener to start listening and set state accordingly. try { - listener_->startListening(*io_service_); + listener_->startListening(io_service_); mgr_state_ = RUNNING; } catch (const isc::Exception& ex) { isc_throw(D2QueueMgrError, "D2QueueMgr listener start failed: " diff --git a/src/bin/d2/tests/d2_controller_unittests.cc b/src/bin/d2/tests/d2_controller_unittests.cc index 6725d87480..76ed9e826b 100644 --- a/src/bin/d2/tests/d2_controller_unittests.cc +++ b/src/bin/d2/tests/d2_controller_unittests.cc @@ -207,7 +207,7 @@ TEST_F(D2ControllerTest, invalidConfigReload) { scheduleTimedWrite("{ \"string_test\": BOGUS JSON }", 100); // Setup to raise SIGHUP in 200 ms. - TimedSignal sighup(*getIOService(), SIGHUP, 200); + TimedSignal sighup(getIOService(), SIGHUP, 200); // Write valid_d2_config and then run launch() for a maximum of 500 ms. time_duration elapsed_time; @@ -246,7 +246,7 @@ TEST_F(D2ControllerTest, validConfigReload) { scheduleTimedWrite(second_cfg, 100); // Setup to raise SIGHUP in 200 ms. - TimedSignal sighup(*getIOService(), SIGHUP, 200); + TimedSignal sighup(getIOService(), SIGHUP, 200); // Write valid_d2_config and then run launch() for a maximum of 500ms. time_duration elapsed_time; @@ -269,7 +269,7 @@ TEST_F(D2ControllerTest, validConfigReload) { // Tests that the SIGINT triggers a normal shutdown. TEST_F(D2ControllerTest, sigintShutdown) { // Setup to raise SIGINT in 1 ms. - TimedSignal sighup(*getIOService(), SIGINT, 1); + TimedSignal sighup(getIOService(), SIGINT, 1); // Write valid_d2_config and then run launch() for a maximum of 1000 ms. time_duration elapsed_time; @@ -286,7 +286,7 @@ TEST_F(D2ControllerTest, sigintShutdown) { // Tests that the SIGTERM triggers a normal shutdown. TEST_F(D2ControllerTest, sigtermShutdown) { // Setup to raise SIGTERM in 1 ms. - TimedSignal sighup(*getIOService(), SIGTERM, 1); + TimedSignal sighup(getIOService(), SIGTERM, 1); // Write valid_d2_config and then run launch() for a maximum of 1 s. time_duration elapsed_time; diff --git a/src/bin/d2/tests/d2_process_unittests.cc b/src/bin/d2/tests/d2_process_unittests.cc index e2e11b2a74..bc6be18a17 100644 --- a/src/bin/d2/tests/d2_process_unittests.cc +++ b/src/bin/d2/tests/d2_process_unittests.cc @@ -561,7 +561,7 @@ TEST_F(D2ProcessTest, canShutdown) { TEST_F(D2ProcessTest, normalShutdown) { // Use an asiolink IntervalTimer and callback to generate the // shutdown invocation. (Note IntervalTimer setup is in milliseconds). - isc::asiolink::IntervalTimer timer(*getIOService()); + isc::asiolink::IntervalTimer timer(getIOService()); timer.setup(std::bind(&D2ProcessTest::genShutdownCallback, this), 2 * 1000); @@ -585,7 +585,7 @@ TEST_F(D2ProcessTest, normalShutdown) { TEST_F(D2ProcessTest, fatalErrorShutdown) { // Use an asiolink IntervalTimer and callback to generate the // the exception. (Note IntervalTimer setup is in milliseconds). - isc::asiolink::IntervalTimer timer(*getIOService()); + isc::asiolink::IntervalTimer timer(getIOService()); timer.setup(std::bind(&D2ProcessTest::genFatalErrorCallback, this), 2 * 1000); diff --git a/src/bin/d2/tests/d2_queue_mgr_unittests.cc b/src/bin/d2/tests/d2_queue_mgr_unittests.cc index e5e3f117ae..dbbeec807b 100644 --- a/src/bin/d2/tests/d2_queue_mgr_unittests.cc +++ b/src/bin/d2/tests/d2_queue_mgr_unittests.cc @@ -209,7 +209,7 @@ class QueueMgrUDPTest : public virtual ::testing::Test, public D2StatTest, NameChangeSender::RequestSendHandler { public: asiolink::IOServicePtr io_service_; - NameChangeSenderPtr sender_; + NameChangeSenderPtr sender_; isc::asiolink::IntervalTimer test_timer_; D2QueueMgrPtr queue_mgr_; @@ -218,7 +218,7 @@ public: std::vector<NameChangeRequestPtr> received_ncrs_; QueueMgrUDPTest() : io_service_(new isc::asiolink::IOService()), - test_timer_(*io_service_), + test_timer_(io_service_), send_result_(NameChangeSender::SUCCESS) { isc::asiolink::IOAddress addr(TEST_ADDRESS); // Create our sender instance. Note that reuse_address is true. @@ -359,7 +359,7 @@ TEST_F (QueueMgrUDPTest, liveFeed) { ASSERT_EQ(D2QueueMgr::RUNNING, queue_mgr_->getMgrState()); // Place the sender into sending state. - ASSERT_NO_THROW(sender_->startSending(*io_service_)); + ASSERT_NO_THROW(sender_->startSending(io_service_)); ASSERT_TRUE(sender_->amSending()); // Iterate over the list of requests sending and receiving diff --git a/src/bin/d2/tests/d2_update_mgr_unittests.cc b/src/bin/d2/tests/d2_update_mgr_unittests.cc index 028b5986d8..920ccc884e 100644 --- a/src/bin/d2/tests/d2_update_mgr_unittests.cc +++ b/src/bin/d2/tests/d2_update_mgr_unittests.cc @@ -692,7 +692,7 @@ TEST_F(D2UpdateMgrTest, addTransaction) { // Create a server based on the transaction's current server, and // start it listening. - FauxServer server(*io_service_, *(trans->getCurrentServer())); + FauxServer server(io_service_, *(trans->getCurrentServer())); server.receive(FauxServer::USE_RCODE, dns::Rcode::NOERROR()); // Run sweep and IO until everything is done. @@ -747,9 +747,9 @@ TEST_F(D2UpdateMgrTest, removeTransaction) { ASSERT_EQ(1, trans->getUpdateAttempts()); ASSERT_EQ(StateModel::NOP_EVT, trans->getNextEvent()); - // Create a server based on the transaction's current server, - // and start it listening. - FauxServer server(*io_service_, *(trans->getCurrentServer())); + // Create a server based on the transaction's current server, and + // start it listening. + FauxServer server(io_service_, *(trans->getCurrentServer())); server.receive(FauxServer::USE_RCODE, dns::Rcode::NOERROR()); // Run sweep and IO until everything is done. @@ -797,7 +797,7 @@ TEST_F(D2UpdateMgrTest, errorTransaction) { ASSERT_TRUE(trans->getCurrentServer()); // Create a server and start it listening. - FauxServer server(*io_service_, *(trans->getCurrentServer())); + FauxServer server(io_service_, *(trans->getCurrentServer())); server.receive(FauxServer::CORRUPT_RESP); // Run sweep and IO until everything is done. @@ -836,7 +836,7 @@ TEST_F(D2UpdateMgrTest, multiTransaction) { // that all of configured servers have the same address. // and start it listening. asiolink::IOAddress server_ip("127.0.0.1"); - FauxServer server(*io_service_, server_ip, 5301); + FauxServer server(io_service_, server_ip, 5301); server.receive(FauxServer::USE_RCODE, dns::Rcode::NOERROR()); // Run sweep and IO until everything is done. @@ -908,7 +908,7 @@ TEST_F(D2UpdateMgrTest, simpleAddTransaction) { // Create a server based on the transaction's current server, and // start it listening. - FauxServer server(*io_service_, *(trans->getCurrentServer())); + FauxServer server(io_service_, *(trans->getCurrentServer())); server.receive(FauxServer::USE_RCODE, dns::Rcode::NOERROR()); // Run sweep and IO until everything is done. @@ -964,9 +964,9 @@ TEST_F(D2UpdateMgrTest, simpleRemoveTransaction) { ASSERT_EQ(1, trans->getUpdateAttempts()); ASSERT_EQ(StateModel::NOP_EVT, trans->getNextEvent()); - // Create a server based on the transaction's current server, - // and start it listening. - FauxServer server(*io_service_, *(trans->getCurrentServer())); + // Create a server based on the transaction's current server, and + // start it listening. + FauxServer server(io_service_, *(trans->getCurrentServer())); server.receive(FauxServer::USE_RCODE, dns::Rcode::NOERROR()); // Run sweep and IO until everything is done. diff --git a/src/bin/dhcp4/tests/kea_controller_unittest.cc b/src/bin/dhcp4/tests/kea_controller_unittest.cc index 9e06434fec..f5877d8f36 100644 --- a/src/bin/dhcp4/tests/kea_controller_unittest.cc +++ b/src/bin/dhcp4/tests/kea_controller_unittest.cc @@ -229,7 +229,7 @@ public: /// stops the IO service and causes the function to return. void runTimersWithTimeout(const IOServicePtr& io_service, const long timeout_ms, std::function<bool()> cond = std::function<bool()>()) { - IntervalTimer timer(*io_service); + IntervalTimer timer(io_service); std::atomic<bool> stopped(false); timer.setup([&io_service, &stopped]() { stopped = true; diff --git a/src/bin/dhcp6/tests/kea_controller_unittest.cc b/src/bin/dhcp6/tests/kea_controller_unittest.cc index 2458cb9d6b..068fc96845 100644 --- a/src/bin/dhcp6/tests/kea_controller_unittest.cc +++ b/src/bin/dhcp6/tests/kea_controller_unittest.cc @@ -216,7 +216,7 @@ public: /// stops the IO service and causes the function to return. void runTimersWithTimeout(const IOServicePtr& io_service, const long timeout_ms, std::function<bool()> cond = std::function<bool()>()) { - IntervalTimer timer(*io_service); + IntervalTimer timer(io_service); std::atomic<bool> stopped(false); timer.setup([&io_service, &stopped]() { stopped = true; diff --git a/src/bin/netconf/http_control_socket.cc b/src/bin/netconf/http_control_socket.cc index 02f5b50738..197c51916c 100644 --- a/src/bin/netconf/http_control_socket.cc +++ b/src/bin/netconf/http_control_socket.cc @@ -79,7 +79,7 @@ HttpControlSocket::sendCommand(ConstElementPtr command) { } IOServicePtr io_service(new IOService()); - HttpClient client(*io_service, false); + HttpClient client(io_service, false); boost::system::error_code received_ec; string receive_errmsg; HttpResponseJsonPtr response(new HttpResponseJson()); diff --git a/src/bin/netconf/tests/control_socket_unittests.cc b/src/bin/netconf/tests/control_socket_unittests.cc index c284a226d1..996bcbae40 100644 --- a/src/bin/netconf/tests/control_socket_unittests.cc +++ b/src/bin/netconf/tests/control_socket_unittests.cc @@ -144,7 +144,7 @@ public: /// @brief Constructor. UnixControlSocketTest() - : ThreadedTest(), io_service_() { + : ThreadedTest(), io_service_(new IOService()) { } void SetUp() override { @@ -160,7 +160,7 @@ public: // io_service must be stopped after the thread returns, // otherwise the thread may never return if it is // waiting for the completion of some asynchronous tasks. - io_service_.stop(); + io_service_->stop(); removeUnixSocketFile(); } @@ -200,7 +200,7 @@ public: void reflectServer(); /// @brief IOService object. - IOService io_service_; + IOServicePtr io_service_; }; // UnixControlSocketTest /// @brief Server method running in a thread reflecting the command. @@ -211,14 +211,14 @@ void UnixControlSocketTest::reflectServer() { // Acceptor. boost::asio::local::stream_protocol::acceptor - acceptor(io_service_.getInternalIOService()); + acceptor(io_service_->getInternalIOService()); EXPECT_NO_THROW_LOG(acceptor.open()); boost::asio::local::stream_protocol::endpoint endpoint(unixSocketFilePath()); EXPECT_NO_THROW_LOG(acceptor.bind(endpoint)); EXPECT_NO_THROW_LOG(acceptor.listen()); boost::asio::local::stream_protocol::socket - socket(io_service_.getInternalIOService()); + socket(io_service_->getInternalIOService()); // Ready. signalReady(); @@ -241,7 +241,7 @@ UnixControlSocketTest::reflectServer() { accepted = true; }); while (!accepted && !timeout) { - io_service_.runOne(); + io_service_->runOne(); } ASSERT_FALSE(ec); @@ -255,7 +255,7 @@ UnixControlSocketTest::reflectServer() { received = cnt; }); while (!received && !timeout) { - io_service_.runOne(); + io_service_->runOne(); } ASSERT_FALSE(ec); rbuf.resize(received); @@ -274,7 +274,7 @@ UnixControlSocketTest::reflectServer() { sent = cnt; }); while (!sent && !timeout) { - io_service_.runOne(); + io_service_->runOne(); } ASSERT_FALSE(ec); @@ -511,6 +511,10 @@ public: /// @brief Test fixture class for http control sockets. class HttpControlSocketTest : public ThreadedTest { public: + /// @brief Constructor. + HttpControlSocketTest() : io_service_(new IOService()) { + } + void SetUp() override { SysrepoSetup::cleanSharedMemory(); } @@ -524,7 +528,7 @@ public: // io_service must be stopped after the thread returns, // otherwise the thread may never return if it is // waiting for the completion of some asynchronous tasks. - io_service_.stop(); + io_service_->stop(); } /// @brief Returns socket URL. @@ -562,12 +566,12 @@ public: signalReady(); // Until stop() is called run IO service. while (!isStopping()) { - io_service_.runOne(); + io_service_->runOne(); } // Main thread signalled that the thread should // terminate. Launch any outstanding IO service // handlers. - io_service_.poll(); + io_service_->poll(); // Nothing more to do. Signal that the thread is // done so as the main thread can close HTTP // listener and clean up after the test. @@ -587,7 +591,7 @@ public: // If the thread is blocked on running the IO // service, post the empty handler to cause // runOne to return. - io_service_.post([]() { return; }); + io_service_->post([]() { return; }); // We asked that the thread stops. Let's wait // for it to signal that it has stopped. waitStopped(); @@ -600,7 +604,7 @@ public: } /// @brief IOService object. - IOService io_service_; + IOServicePtr io_service_; /// @brief Pointer to listener. HttpListenerPtr listener_; @@ -611,12 +615,11 @@ void HttpControlSocketTest::createReflectListener() { HttpResponseCreatorFactoryPtr factory(new TestHttpResponseCreatorFactory()); - listener_.reset(new - HttpListener(io_service_, - IOAddress(SERVER_ADDRESS), SERVER_PORT, - TlsContextPtr(), factory, - HttpListener::RequestTimeout(2000), - HttpListener::IdleTimeout(2000))); + listener_.reset(new HttpListener(io_service_, + IOAddress(SERVER_ADDRESS), SERVER_PORT, + TlsContextPtr(), factory, + HttpListener::RequestTimeout(2000), + HttpListener::IdleTimeout(2000))); } // Verifies that the createControlSocket template can create a http diff --git a/src/bin/netconf/tests/netconf_controller_unittests.cc b/src/bin/netconf/tests/netconf_controller_unittests.cc index 84fcfc024c..774ee30506 100644 --- a/src/bin/netconf/tests/netconf_controller_unittests.cc +++ b/src/bin/netconf/tests/netconf_controller_unittests.cc @@ -157,7 +157,7 @@ TEST_F(NetconfControllerTest, launchNormalShutdown) { // Tests that the SIGINT triggers a normal shutdown. TEST_F(NetconfControllerTest, sigintShutdown) { // Setup to raise SIGINT in 1 ms. - TimedSignal sighup(*getIOService(), SIGINT, 1); + TimedSignal sighup(getIOService(), SIGINT, 1); // Write valid_netconf_config and then run launch() for a maximum // of 500 ms. @@ -173,7 +173,7 @@ TEST_F(NetconfControllerTest, sigintShutdown) { // Tests that the SIGTERM triggers a normal shutdown. TEST_F(NetconfControllerTest, sigtermShutdown) { // Setup to raise SIGTERM in 1 ms. - TimedSignal sighup(*getIOService(), SIGTERM, 1); + TimedSignal sighup(getIOService(), SIGTERM, 1); // Write valid_netconf_config and then run launch() for a maximum // of 500 ms. diff --git a/src/bin/netconf/tests/netconf_process_unittests.cc b/src/bin/netconf/tests/netconf_process_unittests.cc index 0b82b6bbee..3cfdbd040a 100644 --- a/src/bin/netconf/tests/netconf_process_unittests.cc +++ b/src/bin/netconf/tests/netconf_process_unittests.cc @@ -63,7 +63,7 @@ TEST(NetconfProcess, construction) { TEST_F(NetconfProcessTest, shutdown) { // Use an asiolink IntervalTimer and callback to generate the // shutdown invocation. (Note IntervalTimer setup is in milliseconds). - IntervalTimer timer(*getIOService()); + IntervalTimer timer(getIOService()); timer.setup(std::bind(&NetconfProcessTest::genShutdownCallback, this), 200); diff --git a/src/bin/netconf/tests/netconf_unittests.cc b/src/bin/netconf/tests/netconf_unittests.cc index 9a58b8a587..fd3340d33c 100644 --- a/src/bin/netconf/tests/netconf_unittests.cc +++ b/src/bin/netconf/tests/netconf_unittests.cc @@ -275,7 +275,7 @@ NetconfAgentTest::fakeServer() { // Timeout. bool timeout = false; - IntervalTimer timer(*io_service_); + IntervalTimer timer(io_service_); timer.setup([&timeout]() { timeout = true; FAIL() << "timeout"; diff --git a/src/bin/netconf/unix_control_socket.cc b/src/bin/netconf/unix_control_socket.cc index e89e522612..5eed615515 100644 --- a/src/bin/netconf/unix_control_socket.cc +++ b/src/bin/netconf/unix_control_socket.cc @@ -55,7 +55,7 @@ ConstElementPtr UnixControlSocket::sendCommand(ConstElementPtr command) { // We are using our own IO service because this method is synchronous. IOServicePtr io_service(new IOService()); - ClientConnection conn(*io_service); + ClientConnection conn(io_service); boost::system::error_code received_ec; ConstJSONFeedPtr received_feed; diff --git a/src/hooks/dhcp/high_availability/communication_state.cc b/src/hooks/dhcp/high_availability/communication_state.cc index d7f9cde03e..38b7566df3 100644 --- a/src/hooks/dhcp/high_availability/communication_state.cc +++ b/src/hooks/dhcp/high_availability/communication_state.cc @@ -207,7 +207,7 @@ CommunicationState::startHeartbeatInternal(const long interval, } if (!timer_) { - timer_.reset(new IntervalTimer(*io_service_)); + timer_.reset(new IntervalTimer(io_service_)); } if (settings_modified) { diff --git a/src/hooks/dhcp/high_availability/ha_service.cc b/src/hooks/dhcp/high_availability/ha_service.cc index 471fad1806..d148afa81e 100644 --- a/src/hooks/dhcp/high_availability/ha_service.cc +++ b/src/hooks/dhcp/high_availability/ha_service.cc @@ -94,10 +94,10 @@ HAService::HAService(const unsigned int id, const IOServicePtr& io_service, // Create the client and(or) listener as appropriate. if (!config_->getEnableMultiThreading()) { // Not configured for multi-threading, start a client in ST mode. - client_.reset(new HttpClient(*io_service_, false)); + client_.reset(new HttpClient(io_service_, false)); } else { // Create an MT-mode client. - client_.reset(new HttpClient(*io_service_, true, + client_.reset(new HttpClient(io_service_, true, config_->getHttpClientThreads(), true)); // If we're configured to use our own listener create and start it. @@ -2347,10 +2347,9 @@ HAService::processSynchronize(const std::string& server_name, int HAService::synchronize(std::string& status_message, const std::string& server_name, const unsigned int max_period) { - lease_sync_filter_.apply(); - IOService io_service; + IOServicePtr io_service(new IOService()); HttpClient client(io_service, false); asyncSyncLeases(client, server_name, max_period, Lease4Ptr(), @@ -2393,7 +2392,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na // The synchronization process is completed, so let's break // the IO service so as we can return the response to the // controlling client. - io_service.stop(); + io_service->stop(); }); } else { @@ -2403,7 +2402,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na status_message = error_message; } - io_service.stop(); + io_service->stop(); } }); @@ -2423,7 +2422,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na // The synchronization process is completed, so let's break // the IO service so as we can return the response to the // controlling client. - io_service.stop(); + io_service->stop(); }); } @@ -2431,7 +2430,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na } else { // Also stop IO service if there is no need to enable DHCP // service. - io_service.stop(); + io_service->stop(); } }); @@ -2444,7 +2443,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na // Run the IO service until it is stopped by any of the callbacks. This // makes it synchronous. - io_service.run(); + io_service->run(); // End measuring duration. stopwatch.stop(); @@ -2562,7 +2561,7 @@ HAService::sendLeaseUpdatesFromBacklog() { return (true); } - IOService io_service; + IOServicePtr io_service(new IOService()); HttpClient client(io_service, false); auto remote_config = config_->getFailoverPeerConfig(); bool updates_successful = true; @@ -2574,7 +2573,7 @@ HAService::sendLeaseUpdatesFromBacklog() { asyncSendLeaseUpdatesFromBacklog(client, remote_config, [&](const bool success, const std::string&, const int) { - io_service.stop(); + io_service->stop(); updates_successful = success; }); @@ -2582,7 +2581,7 @@ HAService::sendLeaseUpdatesFromBacklog() { Stopwatch stopwatch; // Run the IO service until it is stopped by the callback. This makes it synchronous. - io_service.run(); + io_service->run(); // End measuring duration. stopwatch.stop(); @@ -2652,19 +2651,19 @@ HAService::asyncSendHAReset(HttpClient& http_client, bool HAService::sendHAReset() { - IOService io_service; + IOServicePtr io_service(new IOService()); HttpClient client(io_service, false); auto remote_config = config_->getFailoverPeerConfig(); bool reset_successful = true; asyncSendHAReset(client, remote_config, [&](const bool success, const std::string&, const int) { - io_service.stop(); + io_service->stop(); reset_successful = success; }); // Run the IO service until it is stopped by the callback. This makes it synchronous. - io_service.run(); + io_service->run(); return (reset_successful); } @@ -2756,7 +2755,7 @@ HAService::processMaintenanceStart() { // to know the type of the expected response. HttpResponseJsonPtr response = boost::make_shared<HttpResponseJson>(); - IOService io_service; + IOServicePtr io_service(new IOService()); HttpClient client(io_service, false); boost::system::error_code captured_ec; @@ -2773,7 +2772,7 @@ HAService::processMaintenanceStart() { const HttpResponsePtr& response, const std::string& error_str) { - io_service.stop(); + io_service->stop(); // There are three possible groups of errors. One is the IO error // causing issues in communication with the peer. Another one is @@ -2823,7 +2822,7 @@ HAService::processMaintenanceStart() { // Run the IO service until it is stopped by any of the callbacks. This // makes it synchronous. - io_service.run(); + io_service->run(); // If there was a communication problem with the partner we assume that // the partner is already down while we receive this command. @@ -2883,7 +2882,7 @@ HAService::processMaintenanceCancel() { // to know the type of the expected response. HttpResponseJsonPtr response = boost::make_shared<HttpResponseJson>(); - IOService io_service; + IOServicePtr io_service(new IOService()); HttpClient client(io_service, false); std::string error_message; @@ -2897,7 +2896,7 @@ HAService::processMaintenanceCancel() { const HttpResponsePtr& response, const std::string& error_str) { - io_service.stop(); + io_service->stop(); // Handle first two groups of errors. if (ec || !error_str.empty()) { @@ -2937,7 +2936,7 @@ HAService::processMaintenanceCancel() { // Run the IO service until it is stopped by any of the callbacks. This // makes it synchronous. - io_service.run(); + io_service->run(); // There was an error in communication with the partner or the // partner was unable to revert its state. diff --git a/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc b/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc index dd6507cc58..2840f58065 100644 --- a/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc +++ b/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc @@ -594,15 +594,15 @@ public: factory_(new TestHttpResponseCreatorFactory()), factory2_(new TestHttpResponseCreatorFactory()), factory3_(new TestHttpResponseCreatorFactory()), - listener_(new HttpListener(*io_service_, IOAddress(SERVER_ADDRESS), + listener_(new HttpListener(io_service_, IOAddress(SERVER_ADDRESS), SERVER_PORT, TlsContextPtr(), factory_, HttpListener::RequestTimeout(REQUEST_TIMEOUT), HttpListener::IdleTimeout(IDLE_TIMEOUT))), - listener2_(new HttpListener(*io_service_, IOAddress(SERVER_ADDRESS), + listener2_(new HttpListener(io_service_, IOAddress(SERVER_ADDRESS), SERVER_PORT + 1, TlsContextPtr(), factory2_, HttpListener::RequestTimeout(REQUEST_TIMEOUT), HttpListener::IdleTimeout(IDLE_TIMEOUT))), - listener3_(new HttpListener(*io_service_, IOAddress(SERVER_ADDRESS), + listener3_(new HttpListener(io_service_, IOAddress(SERVER_ADDRESS), SERVER_PORT + 2, TlsContextPtr(), factory3_, HttpListener::RequestTimeout(REQUEST_TIMEOUT), HttpListener::IdleTimeout(IDLE_TIMEOUT))), diff --git a/src/hooks/dhcp/high_availability/tests/ha_test.cc b/src/hooks/dhcp/high_availability/tests/ha_test.cc index f61dbcc61f..b7c43fea49 100644 --- a/src/hooks/dhcp/high_availability/tests/ha_test.cc +++ b/src/hooks/dhcp/high_availability/tests/ha_test.cc @@ -72,7 +72,7 @@ HATest::startHAService() { void HATest::runIOService(long ms) { io_service_->restart(); - IntervalTimer timer(*io_service_); + IntervalTimer timer(io_service_); timer.setup(std::bind(&IOService::stop, io_service_), ms, IntervalTimer::ONE_SHOT); io_service_->run(); @@ -82,7 +82,7 @@ HATest::runIOService(long ms) { void HATest::runIOService(long ms, std::function<bool()> stop_condition) { io_service_->restart(); - IntervalTimer timer(*io_service_); + IntervalTimer timer(io_service_); bool timeout = false; timer.setup(std::bind(&HATest::stopIOServiceHandler, this, std::ref(timeout)), ms, IntervalTimer::ONE_SHOT); diff --git a/src/lib/asiodns/io_fetch.cc b/src/lib/asiodns/io_fetch.cc index dd6632ffdd..df8cf18565 100644 --- a/src/lib/asiodns/io_fetch.cc +++ b/src/lib/asiodns/io_fetch.cc @@ -55,17 +55,17 @@ const int DBG_ALL = DBGLVL_TRACE_DETAIL + 20; /// want keep the same data). Organising the data in this way keeps copying to /// a minimum. struct IOFetchData { - + IOServicePtr io_service_; ///< The IO service // The first two members are shared pointers to a base class because what is // actually instantiated depends on whether the fetch is over UDP or TCP, // which is not known until construction of the IOFetch. Use of a shared // pointer here is merely to ensure deletion when the data object is deleted. - boost::scoped_ptr<IOAsioSocket<IOFetch> > socket; + boost::scoped_ptr<IOAsioSocket<IOFetch>> socket; ///< Socket to use for I/O boost::scoped_ptr<IOEndpoint> remote_snd;///< Where the fetch is sent boost::scoped_ptr<IOEndpoint> remote_rcv;///< Where the response came from - OutputBufferPtr msgbuf; ///< Wire buffer for question - OutputBufferPtr received; ///< Received data put here + OutputBufferPtr msgbuf; ///< Wire buffer for question + OutputBufferPtr received; ///< Received data put here IOFetch::Callback* callback; ///< Called on I/O Completion boost::asio::deadline_timer timer; ///< Timer to measure timeouts IOFetch::Protocol protocol; ///< Protocol being used @@ -103,14 +103,14 @@ struct IOFetchData { /// \param wait Timeout for the fetch (in ms). /// /// TODO: May need to alter constructor (see comment 4 in Trac ticket #554) - IOFetchData(IOFetch::Protocol proto, IOService& service, + IOFetchData(IOFetch::Protocol proto, const IOServicePtr& service, const IOAddress& address, uint16_t port, OutputBufferPtr& buff, - IOFetch::Callback* cb, int wait) : + IOFetch::Callback* cb, int wait) : io_service_(service), socket((proto == IOFetch::UDP) ? static_cast<IOAsioSocket<IOFetch>*>( - new UDPSocket<IOFetch>(service)) : + new UDPSocket<IOFetch>(io_service_)) : static_cast<IOAsioSocket<IOFetch>*>( - new TCPSocket<IOFetch>(service)) + new TCPSocket<IOFetch>(io_service_)) ), remote_snd((proto == IOFetch::UDP) ? static_cast<IOEndpoint*>(new UDPEndpoint(address, port)) : @@ -123,7 +123,7 @@ struct IOFetchData { msgbuf(new OutputBuffer(512)), received(buff), callback(cb), - timer(service.getInternalIOService()), + timer(io_service_->getInternalIOService()), protocol(proto), cumulative(0), expected(0), @@ -151,7 +151,7 @@ struct IOFetchData { /// IOFetch Constructor - just initialize the private data -IOFetch::IOFetch(Protocol protocol, IOService& service, +IOFetch::IOFetch(Protocol protocol, const IOServicePtr& service, const isc::dns::Question& question, const IOAddress& address, uint16_t port, OutputBufferPtr& buff, Callback* cb, int wait, bool edns) { MessagePtr query_msg(new Message(Message::RENDER)); @@ -159,7 +159,7 @@ IOFetch::IOFetch(Protocol protocol, IOService& service, cb, wait, edns); } -IOFetch::IOFetch(Protocol protocol, IOService& service, +IOFetch::IOFetch(Protocol protocol, const IOServicePtr& service, OutputBufferPtr& outpkt, const IOAddress& address, uint16_t port, OutputBufferPtr& buff, Callback* cb, int wait) : data_(new IOFetchData(protocol, service, @@ -168,7 +168,7 @@ IOFetch::IOFetch(Protocol protocol, IOService& service, data_->packet = true; } -IOFetch::IOFetch(Protocol protocol, IOService& service, +IOFetch::IOFetch(Protocol protocol, const IOServicePtr& service, ConstMessagePtr query_message, const IOAddress& address, uint16_t port, OutputBufferPtr& buff, Callback* cb, int wait) { MessagePtr msg(new Message(Message::RENDER)); @@ -185,7 +185,7 @@ IOFetch::IOFetch(Protocol protocol, IOService& service, void IOFetch::initIOFetch(MessagePtr& query_msg, Protocol protocol, - IOService& service, + const IOServicePtr& service, const isc::dns::Question& question, const IOAddress& address, uint16_t port, OutputBufferPtr& buff, Callback* cb, int wait, bool edns) { diff --git a/src/lib/asiodns/io_fetch.h b/src/lib/asiodns/io_fetch.h index 4b247abfa4..fea13e0455 100644 --- a/src/lib/asiodns/io_fetch.h +++ b/src/lib/asiodns/io_fetch.h @@ -132,7 +132,7 @@ public: /// -1 indicates no timeout. /// \param edns true if the request should be EDNS. The default value is /// true. - IOFetch(Protocol protocol, isc::asiolink::IOService& service, + IOFetch(Protocol protocol, const isc::asiolink::IOServicePtr& service, const isc::dns::Question& question, const isc::asiolink::IOAddress& address, uint16_t port, isc::util::OutputBufferPtr& buff, Callback* cb, @@ -159,7 +159,7 @@ public: /// and deleting it if necessary. /// \param wait Timeout for the fetch (in ms). The default value of /// -1 indicates no timeout. - IOFetch(Protocol protocol, isc::asiolink::IOService& service, + IOFetch(Protocol protocol, const isc::asiolink::IOServicePtr& service, isc::dns::ConstMessagePtr query_message, const isc::asiolink::IOAddress& address, uint16_t port, isc::util::OutputBufferPtr& buff, Callback* cb, @@ -184,7 +184,7 @@ public: /// (default = 53) /// \param wait Timeout for the fetch (in ms). The default value of /// -1 indicates no timeout. - IOFetch(Protocol protocol, isc::asiolink::IOService& service, + IOFetch(Protocol protocol, const isc::asiolink::IOServicePtr& service, isc::util::OutputBufferPtr& outpkt, const isc::asiolink::IOAddress& address, uint16_t port, isc::util::OutputBufferPtr& buff, Callback* cb, @@ -218,7 +218,7 @@ private: /// parameter "query_message" /// \param query_message the message to be sent out. void initIOFetch(isc::dns::MessagePtr& query_message, Protocol protocol, - isc::asiolink::IOService& service, + const isc::asiolink::IOServicePtr& service, const isc::dns::Question& question, const isc::asiolink::IOAddress& address, uint16_t port, isc::util::OutputBufferPtr& buff, Callback* cb, int wait, diff --git a/src/lib/asiodns/tests/io_fetch_unittest.cc b/src/lib/asiodns/tests/io_fetch_unittest.cc index 6529c356ba..5bab99f6a3 100644 --- a/src/lib/asiodns/tests/io_fetch_unittest.cc +++ b/src/lib/asiodns/tests/io_fetch_unittest.cc @@ -55,7 +55,7 @@ const bool DEBUG = false; class IOFetchTest : public virtual ::testing::Test, public virtual IOFetch::Callback { public: - IOService service_; ///< Service to run the query + IOServicePtr service_; ///< Service to run the query IOFetch::Result expected_; ///< Expected result of the callback bool run_; ///< Did the callback run already? Question question_; ///< What to ask @@ -88,7 +88,7 @@ public: /// \brief Constructor IOFetchTest() : - service_(), + service_(new IOService()), expected_(IOFetch::NOTSET), run_(false), question_(Name("example.net"), RRClass::IN(), RRType::A()), @@ -101,7 +101,7 @@ public: // Timeout interval chosen to ensure no timeout protocol_(IOFetch::TCP), // for initialization - will be changed cumulative_(0), - timer_(service_.getInternalIOService()), + timer_(service_->getInternalIOService()), receive_buffer_(), expected_buffer_(new OutputBuffer(512)), send_buffer_(), @@ -455,7 +455,7 @@ public: } // ... and cause the run loop to exit. - service_.stop(); + service_->stop(); } // The next set of methods are the tests themselves. A number of the TCP @@ -474,15 +474,15 @@ public: expected_ = IOFetch::STOPPED; // Post the query - service_.post(fetch); + service_->post(fetch); // Post query_.stop() (yes, the std::bind thing is just // query_.stop()). - service_.post(std::bind(&IOFetch::stop, fetch, IOFetch::STOPPED)); + service_->post(std::bind(&IOFetch::stop, fetch, IOFetch::STOPPED)); // Run both of them. run() returns when everything in the I/O service // queue has completed. - service_.run(); + service_->run(); EXPECT_TRUE(run_); } @@ -500,9 +500,9 @@ public: // Stop before it is started fetch.stop(); - service_.post(fetch); + service_->post(fetch); - service_.run(); + service_->run(); EXPECT_TRUE(run_); } @@ -516,8 +516,8 @@ public: protocol_ = protocol; expected_ = IOFetch::TIME_OUT; - service_.post(fetch); - service_.run(); + service_->post(fetch); + service_->run(); EXPECT_TRUE(run_); } @@ -542,21 +542,21 @@ public: } // Socket into which the connection will be accepted. - tcp::socket socket(service_.getInternalIOService()); + tcp::socket socket(service_->getInternalIOService()); // Acceptor object - called when the connection is made, the handler // will initiate a read on the socket. - tcp::acceptor acceptor(service_.getInternalIOService(), + tcp::acceptor acceptor(service_->getInternalIOService(), tcp::endpoint(tcp::v4(), TEST_PORT)); acceptor.async_accept(socket, std::bind(&IOFetchTest::tcpAcceptHandler, this, &socket, ph::_1)); // Post the TCP fetch object to send the query and receive the response. - service_.post(tcp_fetch_); + service_->post(tcp_fetch_); // ... and execute all the callbacks. This exits when the fetch // completes. - service_.run(); + service_->run(); EXPECT_TRUE(run_); // Make sure the callback did execute // Tidy up @@ -574,7 +574,7 @@ public: protocol_ = IOFetch::UDP; // Set up the server. - udp::socket socket(service_.getInternalIOService(), udp::v4()); + udp::socket socket(service_->getInternalIOService(), udp::v4()); socket.set_option(socket_base::reuse_address(true)); socket.bind(udp::endpoint(TEST_HOST, TEST_PORT)); return_data_ = "Message returned to the client"; @@ -586,12 +586,12 @@ public: std::bind(&IOFetchTest::udpReceiveHandler, this, &remote, &socket, ph::_1, ph::_2, bad_qid, second_send)); - service_.post(udp_fetch_); + service_->post(udp_fetch_); if (debug_) { cout << "udpSendReceive: async_receive_from posted," "waiting for callback" << endl; } - service_.run(); + service_->run(); socket.close(); diff --git a/src/lib/asiolink/botan_boost_tls.h b/src/lib/asiolink/botan_boost_tls.h index 45dcfa8630..5c74d2702a 100644 --- a/src/lib/asiolink/botan_boost_tls.h +++ b/src/lib/asiolink/botan_boost_tls.h @@ -107,9 +107,9 @@ typedef Botan::TLS::Stream<boost::asio::ip::tcp::socket> TlsStreamImpl; /// @note The caller must not provide a null pointer to the TLS context. template <typename Callback, typename TlsStreamImpl> TlsStreamBase<Callback, TlsStreamImpl>:: -TlsStreamBase(IOService& service, TlsContextPtr context) - : TlsStreamImpl(service.getInternalIOService(), context->getContext()), - role_(context->getRole()) { +TlsStreamBase(const IOServicePtr& io_service, TlsContextPtr context) + : StreamService(io_service), TlsStreamImpl(io_service->getInternalIOService(), + context->getContext()), role_(context->getRole()) { } /// @brief Botan boost ASIO TLS stream. @@ -128,7 +128,7 @@ public: /// @param service I/O Service object used to manage the stream. /// @param context Pointer to the TLS context. /// @note The caller must not provide a null pointer to the TLS context. - TlsStream(IOService& service, TlsContextPtr context) + TlsStream(const IOServicePtr& service, TlsContextPtr context) : Base(service, context) { } diff --git a/src/lib/asiolink/botan_tls.h b/src/lib/asiolink/botan_tls.h index 0a9b3b16c6..df0ad33767 100644 --- a/src/lib/asiolink/botan_tls.h +++ b/src/lib/asiolink/botan_tls.h @@ -95,8 +95,9 @@ typedef boost::asio::ip::tcp::socket TlsStreamImpl; /// @note The caller must not provide a null pointer to the TLS context. template <typename Callback, typename TlsStreamImpl> TlsStreamBase<Callback, TlsStreamImpl>:: -TlsStreamBase(IOService& service, TlsContextPtr context) - : TlsStreamImpl(service.getInternalIOService()), role_(context->getRole()) { +TlsStreamBase(const IOServicePtr& io_service, TlsContextPtr context) + : StreamService(io_service), TlsStreamImpl(io_service->getInternalIOService()), + role_(context->getRole()) { } /// @brief Botan fake TLS stream. @@ -114,7 +115,7 @@ public: /// @param service I/O Service object used to manage the stream. /// @param context Pointer to the TLS context. /// @note The caller must not provide a null pointer to the TLS context. - TlsStream(IOService& service, TlsContextPtr context) + TlsStream(const IOServicePtr& service, TlsContextPtr context) : Base(service, context) { } diff --git a/src/lib/asiolink/common_tls.h b/src/lib/asiolink/common_tls.h index b119760ebf..87306d9171 100644 --- a/src/lib/asiolink/common_tls.h +++ b/src/lib/asiolink/common_tls.h @@ -118,12 +118,22 @@ public: TlsRole role_; }; +class StreamService { +public: + /// @brief Constructor. + StreamService(const IOServicePtr& io_service) : io_service_(io_service) { + } +private: + /// @brief The IO service used to handle events. + IOServicePtr io_service_; +}; + /// @brief TLS stream base class. /// /// @tparam Callback The type of callbacks. /// @tparam TlsStreamImpl The type of underlying TLS streams. template <typename Callback, typename TlsStreamImpl> -class TlsStreamBase : public TlsStreamImpl { +class TlsStreamBase : public StreamService, public TlsStreamImpl { public: /// @brief Constructor. @@ -131,7 +141,7 @@ public: /// @param service I/O Service object used to manage the stream. /// @param context Pointer to the TLS context. /// @note The caller must not provide a null pointer to the TLS context. - TlsStreamBase(IOService& service, TlsContextPtr context); + TlsStreamBase(const IOServicePtr& service, TlsContextPtr context); /// @brief Destructor. virtual ~TlsStreamBase() { } diff --git a/src/lib/asiolink/interval_timer.cc b/src/lib/asiolink/interval_timer.cc index 72aafe3f79..6226bf1e28 100644 --- a/src/lib/asiolink/interval_timer.cc +++ b/src/lib/asiolink/interval_timer.cc @@ -40,7 +40,7 @@ public: /// @brief Constructor. /// /// @param io_service The IO service used to handle events. - IntervalTimerImpl(IOService& io_service); + IntervalTimerImpl(const IOServicePtr& io_service); /// @brief Destructor. ~IntervalTimerImpl(); @@ -85,6 +85,9 @@ private: /// @brief The interval in milliseconds. std::atomic<long> interval_; + /// @brief The IO service used to handle events. + IOServicePtr io_service_; + /// @brief The asio timer. boost::asio::deadline_timer timer_; @@ -101,8 +104,8 @@ private: static const long INVALIDATED_INTERVAL = -1; }; -IntervalTimerImpl::IntervalTimerImpl(IOService& io_service) : - interval_(0), timer_(io_service.getInternalIOService()), +IntervalTimerImpl::IntervalTimerImpl(const IOServicePtr& io_service) : + interval_(0), io_service_(io_service), timer_(io_service_->getInternalIOService()), mode_(IntervalTimer::REPEATING) { } @@ -173,7 +176,7 @@ IntervalTimerImpl::callback(const boost::system::error_code& ec) { } } -IntervalTimer::IntervalTimer(IOService& io_service) : +IntervalTimer::IntervalTimer(const IOServicePtr& io_service) : impl_(new IntervalTimerImpl(io_service)) { } diff --git a/src/lib/asiolink/interval_timer.h b/src/lib/asiolink/interval_timer.h index 5dc8b71ab3..0475abcadc 100644 --- a/src/lib/asiolink/interval_timer.h +++ b/src/lib/asiolink/interval_timer.h @@ -79,8 +79,8 @@ public: /// memory allocation fails inside the method. /// This constructor may also throw \c boost::system::system_error. /// - /// \param io_service A reference to an instance of IOService - IntervalTimer(IOService& io_service); + /// \param io_service A smart pointer to an instance of IOService + IntervalTimer(const IOServicePtr& io_service); /// \brief The destructor. /// diff --git a/src/lib/asiolink/io_acceptor.h b/src/lib/asiolink/io_acceptor.h index d08bf39d9b..9454ea010d 100644 --- a/src/lib/asiolink/io_acceptor.h +++ b/src/lib/asiolink/io_acceptor.h @@ -37,9 +37,9 @@ public: /// @brief Constructor. /// /// @param io_service Reference to the IO service. - explicit IOAcceptor(IOService& io_service) - : IOSocket(), - acceptor_(new typename ProtocolType::acceptor(io_service.getInternalIOService())) { + explicit IOAcceptor(const IOServicePtr& io_service) + : IOSocket(), io_service_(io_service), + acceptor_(new typename ProtocolType::acceptor(io_service_->getInternalIOService())) { } /// @brief Destructor. @@ -123,13 +123,14 @@ protected: acceptor_->async_accept(socket.getASIOSocket(), callback); } + /// @brief The IO service used to handle events. + IOServicePtr io_service_; /// @brief Underlying ASIO acceptor implementation. boost::shared_ptr<typename ProtocolType::acceptor> acceptor_; }; - } // end of namespace asiolink } // end of isc diff --git a/src/lib/asiolink/io_service_signal.cc b/src/lib/asiolink/io_service_signal.cc index c498702324..d755af4518 100644 --- a/src/lib/asiolink/io_service_signal.cc +++ b/src/lib/asiolink/io_service_signal.cc @@ -27,7 +27,7 @@ public: /// /// @param io_service the process IO service. /// @param handler the signal handler. - IOSignalSetImpl(IOServicePtr io_service, IOSignalHandler handler); + IOSignalSetImpl(const IOServicePtr& io_service, IOSignalHandler handler); /// @brief Destructor. ~IOSignalSetImpl(); @@ -65,7 +65,7 @@ private: void callback(const boost::system::error_code& ec, int signum); }; -IOSignalSetImpl::IOSignalSetImpl(IOServicePtr io_service, +IOSignalSetImpl::IOSignalSetImpl(const IOServicePtr& io_service, IOSignalHandler handler) : io_service_(io_service), signal_set_(io_service_->getInternalIOService()), @@ -122,7 +122,7 @@ IOSignalSetImpl::remove(int signum) { } } -IOSignalSet::IOSignalSet(IOServicePtr io_service, IOSignalHandler handler) : +IOSignalSet::IOSignalSet(const IOServicePtr& io_service, IOSignalHandler handler) : impl_(new IOSignalSetImpl(io_service, handler)) { // It can throw but the error is fatal... impl_->install(); diff --git a/src/lib/asiolink/io_service_signal.h b/src/lib/asiolink/io_service_signal.h index 8fbcbe1660..57db9906d4 100644 --- a/src/lib/asiolink/io_service_signal.h +++ b/src/lib/asiolink/io_service_signal.h @@ -29,7 +29,7 @@ public: /// /// @param io_service IOService to which to send the signal. /// @param handler Handler to call when a signal is received. - IOSignalSet(asiolink::IOServicePtr io_service, IOSignalHandler handler); + IOSignalSet(const asiolink::IOServicePtr& io_service, IOSignalHandler handler); /// @brief Destructor. ~IOSignalSet(); diff --git a/src/lib/asiolink/openssl_tls.h b/src/lib/asiolink/openssl_tls.h index a558fd1044..957d9e5ad2 100644 --- a/src/lib/asiolink/openssl_tls.h +++ b/src/lib/asiolink/openssl_tls.h @@ -117,9 +117,9 @@ typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> TlsStreamImpl; /// @note The caller must not provide a null pointer to the TLS context. template <typename Callback, typename TlsStreamImpl> TlsStreamBase<Callback, TlsStreamImpl>:: -TlsStreamBase(IOService& service, TlsContextPtr context) - : TlsStreamImpl(service.getInternalIOService(), context->getContext()), - role_(context->getRole()) { +TlsStreamBase(const IOServicePtr& io_service, TlsContextPtr context) + : StreamService(io_service), TlsStreamImpl(io_service->getInternalIOService(), + context->getContext()), role_(context->getRole()) { } /// @brief OpenSSL TLS stream. @@ -137,7 +137,7 @@ public: /// @param service I/O Service object used to manage the stream. /// @param context Pointer to the TLS context. /// @note The caller must not provide a null pointer to the TLS context. - TlsStream(IOService& service, TlsContextPtr context) + TlsStream(const IOServicePtr& service, TlsContextPtr context) : Base(service, context) { } diff --git a/src/lib/asiolink/tcp_acceptor.h b/src/lib/asiolink/tcp_acceptor.h index 4fab1ddbbe..58296d86c1 100644 --- a/src/lib/asiolink/tcp_acceptor.h +++ b/src/lib/asiolink/tcp_acceptor.h @@ -35,7 +35,7 @@ public: /// @brief Constructor. /// /// @param io_service IO service. - explicit TCPAcceptor(IOService& io_service) + explicit TCPAcceptor(const IOServicePtr& io_service) : IOAcceptor<boost::asio::ip::tcp, C>(io_service) { } diff --git a/src/lib/asiolink/tcp_socket.h b/src/lib/asiolink/tcp_socket.h index 40a53bec96..c2e70fcfe9 100644 --- a/src/lib/asiolink/tcp_socket.h +++ b/src/lib/asiolink/tcp_socket.h @@ -68,7 +68,7 @@ public: /// socket. In this case, the open() and close() methods are used. /// /// \param service I/O Service object used to manage the socket. - TCPSocket(IOService& service); + TCPSocket(const IOServicePtr& service); /// \brief Destructor virtual ~TCPSocket(); @@ -222,6 +222,10 @@ public: } private: + + /// @brief The IO service used to handle events. + IOServicePtr io_service_; + /// Two variables to hold the socket - a socket and a pointer to it. This /// handles the case where a socket is passed to the TCPSocket on /// construction, or where it is asked to manage its own socket. @@ -261,8 +265,8 @@ TCPSocket<C>::TCPSocket(boost::asio::ip::tcp::socket& socket) : // Constructor - create socket on the fly template <typename C> -TCPSocket<C>::TCPSocket(IOService& service) : - socket_ptr_(new boost::asio::ip::tcp::socket(service.getInternalIOService())), +TCPSocket<C>::TCPSocket(const IOServicePtr& io_service) : io_service_(io_service), + socket_ptr_(new boost::asio::ip::tcp::socket(io_service_->getInternalIOService())), socket_(*socket_ptr_) { } diff --git a/src/lib/asiolink/tests/interval_timer_unittest.cc b/src/lib/asiolink/tests/interval_timer_unittest.cc index c875b06a66..b2cd459ac1 100644 --- a/src/lib/asiolink/tests/interval_timer_unittest.cc +++ b/src/lib/asiolink/tests/interval_timer_unittest.cc @@ -25,7 +25,7 @@ using namespace isc::asiolink; class IntervalTimerTest : public ::testing::Test { protected: IntervalTimerTest() : - io_service_(), timer_called_(false), timer_cancel_success_(false) + io_service_(new IOService()), timer_called_(false), timer_cancel_success_(false) {} ~IntervalTimerTest() {} class TimerCallBack { @@ -33,7 +33,7 @@ protected: TimerCallBack(IntervalTimerTest* test_obj) : test_obj_(test_obj) {} void operator()() const { test_obj_->timer_called_ = true; - test_obj_->io_service_.stop(); + test_obj_->io_service_->stop(); return; } private: @@ -72,7 +72,7 @@ protected: } else if (count_ == 2) { // Second time of call back. // Stop io_service to stop all timers. - test_obj_->io_service_.stop(); + test_obj_->io_service_->stop(); // Compare the value of counter_.counter_ with stored one. // If TimerCallBackCounter was not called (expected behavior), // they are same. @@ -119,7 +119,7 @@ protected: // Second time of call back. // If it reaches here, re-setup() is failed (unexpected). // We should stop here. - test_obj_->io_service_.stop(); + test_obj_->io_service_->stop(); } return; } @@ -143,7 +143,7 @@ protected: int& counter_; }; protected: - IOService io_service_; + IOServicePtr io_service_; bool timer_called_; bool timer_cancel_success_; }; @@ -169,7 +169,7 @@ TEST_F(IntervalTimerTest, startIntervalTimer) { // setup timer itimer.setup(TimerCallBack(this), 100); EXPECT_EQ(100, itimer.getInterval()); - io_service_.run(); + io_service_->run(); // Control reaches here after io_service_ was stopped by TimerCallBack. // delta: difference between elapsed time and 100 milliseconds. @@ -226,7 +226,7 @@ TEST_F(IntervalTimerTest, destructIntervalTimer) { itimer_canceller.setup( TimerCallBackCancelDeleter(this, itimer_counter, callback_canceller), 300); - io_service_.run(); + io_service_->run(); EXPECT_TRUE(timer_cancel_success_); } @@ -238,7 +238,7 @@ TEST_F(IntervalTimerTest, cancel) { unsigned int counter = 0; itimer_counter.setup(TimerCallBackCanceller(counter, itimer_counter), 100); itimer_watcher.setup(TimerCallBack(this), 200); - io_service_.run(); + io_service_->run(); EXPECT_EQ(1, counter); EXPECT_EQ(0, itimer_counter.getInterval()); @@ -255,7 +255,7 @@ TEST_F(IntervalTimerTest, overwriteIntervalTimer) { // - increments internal counter in callback function // (TimerCallBackCounter) // interval: 300 milliseconds - // - io_service_.stop() (TimerCallBack) + // - io_service_->stop() (TimerCallBack) // interval: 100 milliseconds // itimer_overwriter (B) // (Calls TimerCallBackOverwriter) @@ -281,7 +281,7 @@ TEST_F(IntervalTimerTest, overwriteIntervalTimer) { start = boost::posix_time::microsec_clock::universal_time(); itimer.setup(TimerCallBackCounter(this), 300); itimer_overwriter.setup(TimerCallBackOverwriter(this, itimer), 400); - io_service_.run(); + io_service_->run(); // Control reaches here after io_service_ was stopped by // TimerCallBackCounter or TimerCallBackOverwriter. @@ -314,7 +314,7 @@ TEST_F(IntervalTimerTest, intervalModeTest) { // we've hit our goals. It won't return zero unless is out of // work or the service has been stopped by the test timer. int cnt = 0; - while (((cnt = io_service_.runOne()) > 0) && (repeater_count < 5)) { + while (((cnt = io_service_->runOne()) > 0) && (repeater_count < 5)) { // deliberately empty }; @@ -341,7 +341,7 @@ TEST_F(IntervalTimerTest, timerReuseTest) { // Run until a single event handler executes. This should be our // one-shot expiring. - io_service_.runOne(); + io_service_->runOne(); // Verify the timer expired once. ASSERT_EQ(one_shot_count, 1); @@ -351,7 +351,7 @@ TEST_F(IntervalTimerTest, timerReuseTest) { // Run until a single event handler executes. This should be our // one-shot expiring. - io_service_.runOne(); + io_service_->runOne(); // Verify the timer expired once. ASSERT_EQ(one_shot_count, 2); @@ -363,7 +363,7 @@ TEST_F(IntervalTimerTest, timerReuseTest) { // we've hit our goals. It won't return zero unless is out of // work or the service has been stopped by the test timer. int cnt = 0; - while ((cnt = io_service_.runOne()) && (one_shot_count < 4)) { + while ((cnt = io_service_->runOne()) && (one_shot_count < 4)) { // deliberately empty }; diff --git a/src/lib/asiolink/tests/io_service_signal_unittests.cc b/src/lib/asiolink/tests/io_service_signal_unittests.cc index 26e3dc8cdd..721172b6ab 100644 --- a/src/lib/asiolink/tests/io_service_signal_unittests.cc +++ b/src/lib/asiolink/tests/io_service_signal_unittests.cc @@ -50,9 +50,9 @@ public: /// @brief Constructor. IOSignalTest() : - io_service_(new asiolink::IOService()), test_timer_(*io_service_), - test_time_ms_(0), io_signal_set_(), - processed_signals_(), stop_at_count_(0), handler_throw_error_(false) { + io_service_(new asiolink::IOService()), test_timer_(io_service_), + test_time_ms_(0), io_signal_set_(), processed_signals_(), stop_at_count_(0), + handler_throw_error_(false) { io_signal_set_.reset(new IOSignalSet(io_service_, std::bind(&IOSignalTest::processSignal, @@ -118,7 +118,7 @@ TEST_F(IOSignalTest, singleSignalTest) { ASSERT_NO_THROW(io_signal_set_->add(SIGINT)); // Use TimedSignal to generate SIGINT 100 ms after we start IOService::run. - TimedSignal sig_int(*io_service_, SIGINT, 100); + TimedSignal sig_int(io_service_, SIGINT, 100); // The first handler executed is the IOSignal's internal timer expire // callback. @@ -143,7 +143,7 @@ TEST_F(IOSignalTest, singleSignalTest) { ASSERT_NO_THROW(io_signal_set_->remove(SIGINT)); // Use TimedSignal to generate SIGINT 100 ms after we start IOService::run. - TimedSignal sig_int_too_late(*io_service_, SIGINT, 100); + TimedSignal sig_int_too_late(io_service_, SIGINT, 100); // The first handler executed is the IOSignal's internal timer expire // callback. @@ -174,7 +174,7 @@ TEST_F(IOSignalTest, hammer) { // User a repeating TimedSignal so we should generate a signal every 1 ms // until we hit our stop count. - TimedSignal sig_int(*io_service_, SIGINT, 1, + TimedSignal sig_int(io_service_, SIGINT, 1, asiolink::IntervalTimer::REPEATING); // Start processing IO. This should continue until we stop either by @@ -203,7 +203,7 @@ TEST_F(IOSignalTest, handlerThrow) { stop_at_count_ = 1; // Use TimedSignal to generate SIGINT after we start IOService::run. - TimedSignal sig_int(*io_service_, SIGINT, 100, + TimedSignal sig_int(io_service_, SIGINT, 100, asiolink::IntervalTimer::REPEATING); // Set the test flag to cause the handler to throw an exception. @@ -235,14 +235,14 @@ TEST_F(IOSignalTest, mixedSignals) { // Since signal order arrival cannot be guaranteed, we'll use // explicit one shot signals so we can guarantee how many // of each signal we should get. - TimedSignal sig1(*io_service_, SIGINT, 2); - TimedSignal sig2(*io_service_, SIGUSR1, 2); - TimedSignal sig3(*io_service_, SIGINT, 2); - TimedSignal sig4(*io_service_, SIGUSR2, 2); - TimedSignal sig5(*io_service_, SIGINT, 2); - TimedSignal sig6(*io_service_, SIGUSR1, 2); - TimedSignal sig7(*io_service_, SIGINT, 2); - TimedSignal sig8(*io_service_, SIGUSR2, 2); + TimedSignal sig1(io_service_, SIGINT, 2); + TimedSignal sig2(io_service_, SIGUSR1, 2); + TimedSignal sig3(io_service_, SIGINT, 2); + TimedSignal sig4(io_service_, SIGUSR2, 2); + TimedSignal sig5(io_service_, SIGINT, 2); + TimedSignal sig6(io_service_, SIGUSR1, 2); + TimedSignal sig7(io_service_, SIGINT, 2); + TimedSignal sig8(io_service_, SIGUSR2, 2); // Start processing IO. This should continue until we stop either by // hitting the stop count or if things go wrong, max test time. diff --git a/src/lib/asiolink/tests/process_spawn_unittest.cc b/src/lib/asiolink/tests/process_spawn_unittest.cc index 76706543f0..ea7d386bbb 100644 --- a/src/lib/asiolink/tests/process_spawn_unittest.cc +++ b/src/lib/asiolink/tests/process_spawn_unittest.cc @@ -54,7 +54,7 @@ public: /// @brief Constructor. ProcessSpawnTest() : - io_service_(getIOService()), test_timer_(*io_service_), + io_service_(getIOService()), test_timer_(io_service_), test_time_ms_(0), io_signal_set_(), processed_signals_() { io_signal_set_.reset(new IOSignalSet(io_service_, diff --git a/src/lib/asiolink/tests/tcp_acceptor_unittest.cc b/src/lib/asiolink/tests/tcp_acceptor_unittest.cc index 8a4ed06656..5ca4eebcd3 100644 --- a/src/lib/asiolink/tests/tcp_acceptor_unittest.cc +++ b/src/lib/asiolink/tests/tcp_acceptor_unittest.cc @@ -64,8 +64,8 @@ public: /// connect() to connect to the server. /// /// @param io_service IO service to be stopped on error. - explicit TCPClient(IOService& io_service) - : io_service_(io_service.getInternalIOService()), socket_(io_service_) { + explicit TCPClient(const IOServicePtr& io_service) + : io_service_(io_service), socket_(io_service_->getInternalIOService()) { } /// @brief Destructor. @@ -105,7 +105,7 @@ public: if (ec.value() != boost::asio::error::in_progress) { ADD_FAILURE() << "error occurred while connecting: " << ec.message(); - io_service_.stop(); + io_service_->stop(); } } } @@ -117,8 +117,8 @@ public: private: - /// @brief Holds reference to the IO service. - boost::asio::io_service& io_service_; + /// @brief Holds the IO service. + IOServicePtr io_service_; /// @brief A socket used for the connection. boost::asio::ip::tcp::socket socket_; @@ -148,7 +148,7 @@ public: /// @param acceptor Reference to the TCP acceptor on which asyncAccept /// will be called. /// @param callback Callback function for the asyncAccept. - explicit Acceptor(IOService& io_service, TestTCPAcceptor& acceptor, + explicit Acceptor(const IOServicePtr& io_service, TestTCPAcceptor& acceptor, const TCPAcceptorCallback& callback) : socket_(io_service), acceptor_(acceptor), callback_(callback) { } @@ -201,7 +201,7 @@ public: /// against endlessly running IO service when TCP connections are /// unsuccessful. TCPAcceptorTest() - : io_service_(), acceptor_(io_service_), + : io_service_(new IOService()), acceptor_(io_service_), asio_endpoint_(boost::asio::ip::address::from_string(SERVER_ADDRESS), SERVER_PORT), endpoint_(asio_endpoint_), test_timer_(io_service_), connections_(), @@ -284,12 +284,12 @@ public: } else { ++aborted_connections_num_; } - io_service_.stop(); + io_service_->stop(); } // We have reached the maximum number of connections - end the test. if (++connections_num_ >= max_connections_) { - io_service_.stop(); + io_service_->stop(); return; } @@ -301,11 +301,11 @@ public: /// It stops the IO service and reports test timeout. void timeoutHandler() { ADD_FAILURE() << "Timeout occurred while running the test!"; - io_service_.stop(); + io_service_->stop(); } /// @brief IO service. - IOService io_service_; + IOServicePtr io_service_; /// @brief TCPAcceptor under test. TestTCPAcceptor acceptor_; @@ -355,7 +355,7 @@ TEST_F(TCPAcceptorTest, asyncAccept) { // Run the IO service until we have accepted 10 connections, an error // or test timeout occurred. - io_service_.run(); + io_service_->run(); // Make sure that all accepted connections have been recorded. EXPECT_EQ(10, connections_num_); @@ -431,7 +431,7 @@ TEST_F(TCPAcceptorTest, close) { acceptor_.close(); // Run the IO service. - io_service_.run(); + io_service_->run(); // The connections should have been aborted. EXPECT_EQ(1, connections_num_); diff --git a/src/lib/asiolink/tests/tcp_socket_unittest.cc b/src/lib/asiolink/tests/tcp_socket_unittest.cc index 370d71d455..b5b2c6e9d6 100644 --- a/src/lib/asiolink/tests/tcp_socket_unittest.cc +++ b/src/lib/asiolink/tests/tcp_socket_unittest.cc @@ -234,7 +234,7 @@ serverRead(tcp::socket& socket, TCPCallback& server_cb) { TEST(TCPSocket, processReceivedData) { const uint16_t PACKET_SIZE = 16382; // Amount of "real" data in the buffer - IOService service; // Used to instantiate socket + IOServicePtr service(new IOService); // Used to instantiate socket TCPSocket<TCPCallback> test(service); // Socket under test uint8_t inbuff[PACKET_SIZE + 2]; // Buffer to check OutputBufferPtr outbuff(new OutputBuffer(16)); @@ -308,10 +308,10 @@ TEST(TCPSocket, processReceivedData) { TEST(TCPSocket, sequenceTest) { // Common objects. - IOService service; // Service object for async control + IOServicePtr service(new IOService()); // Service object for async control // The client - the TCPSocket being tested - TCPSocket<TCPCallback> client(service);// Socket under test + TCPSocket<TCPCallback> client(service); // Socket under test TCPCallback client_cb("Client"); // Async I/O callback function TCPEndpoint client_remote_endpoint; // Where client receives message from OutputBufferPtr client_buffer(new OutputBuffer(128)); @@ -324,7 +324,7 @@ TEST(TCPSocket, sequenceTest) { TCPEndpoint server_endpoint(server_address, SERVER_PORT); // Endpoint describing server TCPEndpoint server_remote_endpoint; // Address where server received message from - tcp::socket server_socket(service.getInternalIOService()); + tcp::socket server_socket(service->getInternalIOService()); // Socket used for server // Step 1. Create the connection between the client and the server. Set @@ -335,8 +335,8 @@ TEST(TCPSocket, sequenceTest) { server_cb.queued() = TCPCallback::ACCEPT; server_cb.called() = TCPCallback::NONE; server_cb.setCode(42); // Some error - tcp::acceptor acceptor(service.getInternalIOService(), - tcp::endpoint(tcp::v4(), SERVER_PORT)); + tcp::acceptor acceptor(service->getInternalIOService(), + tcp::endpoint(tcp::v4(), SERVER_PORT)); acceptor.set_option(tcp::acceptor::reuse_address(true)); acceptor.async_accept(server_socket, server_cb); @@ -348,8 +348,8 @@ TEST(TCPSocket, sequenceTest) { client.open(&server_endpoint, client_cb); // Run the open and the accept callback and check that they ran. - service.runOne(); - service.runOne(); + service->runOne(); + service->runOne(); EXPECT_EQ(TCPCallback::ACCEPT, server_cb.called()); EXPECT_EQ(0, server_cb.getCode()); @@ -377,7 +377,7 @@ TEST(TCPSocket, sequenceTest) { // Wait for the client callback to complete. (Must do this first on // Solaris: if we do the synchronous read first, the test hangs.) - service.runOne(); + service->runOne(); // Synchronously read the data from the server.; serverRead(server_socket, server_cb); @@ -442,7 +442,7 @@ TEST(TCPSocket, sequenceTest) { bool server_complete = false; bool client_complete = false; while (!server_complete || !client_complete) { - service.runOne(); + service->runOne(); // Has the server run? if (!server_complete) { diff --git a/src/lib/asiolink/tests/tls_acceptor_unittest.cc b/src/lib/asiolink/tests/tls_acceptor_unittest.cc index f7e65e4879..d042566839 100644 --- a/src/lib/asiolink/tests/tls_acceptor_unittest.cc +++ b/src/lib/asiolink/tests/tls_acceptor_unittest.cc @@ -65,8 +65,8 @@ public: /// connect() to connect to the server. /// /// @param io_service IO service to be stopped on error. - explicit TLSClient(IOService& io_service) - : io_service_(io_service.getInternalIOService()), socket_(io_service_) { + explicit TLSClient(const IOServicePtr& io_service) + : io_service_(io_service), socket_(io_service_->getInternalIOService()) { } /// @brief Destructor. @@ -106,7 +106,7 @@ public: if (ec.value() != error::in_progress) { ADD_FAILURE() << "error occurred while connecting: " << ec.message(); - io_service_.stop(); + io_service_->stop(); } } } @@ -119,7 +119,7 @@ public: private: /// @brief Holds reference to the IO service. - io_service& io_service_; + IOServicePtr io_service_; /// @brief A socket used for the connection. ip::tcp::socket socket_; @@ -150,11 +150,11 @@ public: /// @param acceptor Reference to the TLS acceptor on which asyncAccept /// will be called. /// @param callback Callback function for the asyncAccept. - explicit Acceptor(IOService& io_service, + explicit Acceptor(const IOServicePtr& io_service, TlsContextPtr context, TestTLSAcceptor& acceptor, const TLSAcceptorCallback& callback) - : socket_(io_service, context), acceptor_(acceptor), + : io_service_(io_service), socket_(io_service_, context), acceptor_(acceptor), callback_(callback) { } @@ -177,6 +177,9 @@ public: private: + /// @brief IO service used by the tests. + IOServicePtr io_service_; + /// @brief Socket into which connection is accepted. TLSSocket<SocketCallback> socket_; @@ -206,7 +209,7 @@ public: /// against endlessly running IO service when TLS connections are /// unsuccessful. TLSAcceptorTest() - : io_service_(), acceptor_(io_service_), + : io_service_(new IOService()), acceptor_(io_service_), asio_endpoint_(ip::address::from_string(SERVER_ADDRESS), SERVER_PORT), endpoint_(asio_endpoint_), test_timer_(io_service_), connections_(), @@ -290,12 +293,12 @@ public: } else { ++aborted_connections_num_; } - io_service_.stop(); + io_service_->stop(); } // We have reached the maximum number of connections - end the test. if (++connections_num_ >= max_connections_) { - io_service_.stop(); + io_service_->stop(); return; } @@ -307,11 +310,11 @@ public: /// It stops the IO service and reports test timeout. void timeoutHandler() { ADD_FAILURE() << "Timeout occurred while running the test!"; - io_service_.stop(); + io_service_->stop(); } /// @brief IO service. - IOService io_service_; + IOServicePtr io_service_; /// @brief TLSAcceptor under test. TestTLSAcceptor acceptor_; @@ -361,7 +364,7 @@ TEST_F(TLSAcceptorTest, asyncAccept) { // Run the IO service until we have accepted 10 connections, an error // or test timeout occurred. - io_service_.run(); + io_service_->run(); // Make sure that all accepted connections have been recorded. EXPECT_EQ(10, connections_num_); @@ -437,7 +440,7 @@ TEST_F(TLSAcceptorTest, close) { acceptor_.close(); // Run the IO service. - io_service_.run(); + io_service_->run(); // The connections should have been aborted. EXPECT_EQ(1, connections_num_); diff --git a/src/lib/asiolink/tests/tls_socket_unittest.cc b/src/lib/asiolink/tests/tls_socket_unittest.cc index 0c81ec7626..57773f69cb 100644 --- a/src/lib/asiolink/tests/tls_socket_unittest.cc +++ b/src/lib/asiolink/tests/tls_socket_unittest.cc @@ -238,7 +238,7 @@ TEST(TLSSocket, processReceivedData) { const uint16_t PACKET_SIZE = 16382; // Used to instantiate socket - IOService service; + IOServicePtr service(new IOService()); TlsContextPtr context(new TlsContext(CLIENT)); // Socket under test TLSSocket<TLSCallback> test(service, context); @@ -319,7 +319,7 @@ TEST(TLSSocket, sequenceTest) { // Common objects. // Service object for async control - IOService service; + IOServicePtr service(new IOService()); // The client - the TLSSocket being tested TlsContextPtr client_ctx; @@ -344,7 +344,7 @@ TEST(TLSSocket, sequenceTest) { TlsContextPtr server_ctx; test::configServer(server_ctx); // Stream used for server. - TlsStreamImpl server(service.getInternalIOService(), server_ctx->getContext()); + TlsStreamImpl server(service->getInternalIOService(), server_ctx->getContext()); // Step 1. Create the connection between the client and the server. Set // up the server to accept incoming connections and have the client open @@ -354,7 +354,7 @@ TEST(TLSSocket, sequenceTest) { server_cb.queued() = TLSCallback::ACCEPT; server_cb.called() = TLSCallback::NONE; server_cb.setCode(42); // Some error - tcp::acceptor acceptor(service.getInternalIOService(), + tcp::acceptor acceptor(service->getInternalIOService(), tcp::endpoint(tcp::v4(), SERVER_PORT)); acceptor.set_option(tcp::acceptor::reuse_address(true)); acceptor.async_accept(server.lowest_layer(), server_cb); @@ -369,7 +369,7 @@ TEST(TLSSocket, sequenceTest) { // Run the open and the accept callback and check that they ran. while ((server_cb.called() == TLSCallback::NONE) || (client_cb.called() == TLSCallback::NONE)) { - service.runOne(); + service->runOne(); } EXPECT_EQ(TLSCallback::ACCEPT, server_cb.called()); EXPECT_EQ(0, server_cb.getCode()); @@ -398,7 +398,7 @@ TEST(TLSSocket, sequenceTest) { while ((server_cb.called() == TLSCallback::NONE) || (client_cb.called() == TLSCallback::NONE)) { - service.runOne(); + service->runOne(); } EXPECT_EQ(TLSCallback::HANDSHAKE, client_cb.called()); EXPECT_EQ(0, client_cb.getCode()); @@ -419,7 +419,7 @@ TEST(TLSSocket, sequenceTest) { // Wait for the client callback to complete. (Must do this first on // Solaris: if we do the synchronous read first, the test hangs.) while (client_cb.called() == TLSCallback::NONE) { - service.runOne(); + service->runOne(); } // Synchronously read the data from the server.; @@ -486,7 +486,7 @@ TEST(TLSSocket, sequenceTest) { bool server_complete = false; bool client_complete = false; while (!server_complete || !client_complete) { - service.runOne(); + service->runOne(); // Has the server run? if (!server_complete) { diff --git a/src/lib/asiolink/tests/tls_unittest.cc b/src/lib/asiolink/tests/tls_unittest.cc index 68709fbff8..f57b9a47e3 100644 --- a/src/lib/asiolink/tests/tls_unittest.cc +++ b/src/lib/asiolink/tests/tls_unittest.cc @@ -730,7 +730,7 @@ TEST(TLSTest, configureError) { // Test if we can get a stream. TEST(TLSTest, stream) { - IOService service; + IOServicePtr service(new IOService()); TlsContextPtr ctx(new TlsContext(TlsRole::CLIENT)); boost::scoped_ptr<TlsStream<TestCallback> > st; EXPECT_NO_THROW(st.reset(new TlsStream<TestCallback>(service, ctx))); @@ -738,7 +738,7 @@ TEST(TLSTest, stream) { // Test what happens when handshake is forgotten. TEST(TLSTest, noHandshake) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -748,7 +748,7 @@ TEST(TLSTest, noHandshake) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -765,7 +765,7 @@ TEST(TLSTest, noHandshake) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -790,7 +790,7 @@ TEST(TLSTest, noHandshake) { TestCallback send_cb; async_write(client, boost::asio::buffer(send_buf), send_cb); while (!timeout && !send_cb.getCalled()) { - service.runOne(); + service->runOne(); } timer1.cancel(); @@ -817,7 +817,7 @@ TEST(TLSTest, noHandshake) { TestCallback receive_cb; server.async_read_some(boost::asio::buffer(receive_buf), receive_cb); while (!timeout && !receive_cb.getCalled()) { - service.runOne(); + service->runOne(); } timer2.cancel(); @@ -845,7 +845,7 @@ TEST(TLSTest, noHandshake) { // Test what happens when the server was not configured. TEST(TLSTest, serverNotConfigured) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx(new TlsContext(TlsRole::SERVER)); @@ -855,7 +855,7 @@ TEST(TLSTest, serverNotConfigured) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -872,7 +872,7 @@ TEST(TLSTest, serverNotConfigured) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -898,7 +898,7 @@ TEST(TLSTest, serverNotConfigured) { TestCallback client_cb; client.handshake(client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -945,7 +945,7 @@ TEST(TLSTest, serverNotConfigured) { // Test what happens when the client was not configured. TEST(TLSTest, clientNotConfigured) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -955,7 +955,7 @@ TEST(TLSTest, clientNotConfigured) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -972,7 +972,7 @@ TEST(TLSTest, clientNotConfigured) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -998,7 +998,7 @@ TEST(TLSTest, clientNotConfigured) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1044,7 +1044,7 @@ TEST(TLSTest, clientNotConfigured) { // Test what happens when the client is HTTP (vs HTTPS). TEST(TLSTest, clientHTTPnoS) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1054,13 +1054,13 @@ TEST(TLSTest, clientHTTPnoS) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); // Client part. - tcp::socket client(service.getInternalIOService()); + tcp::socket client(service->getInternalIOService()); // Connect to. client.open(tcp::v4()); @@ -1069,7 +1069,7 @@ TEST(TLSTest, clientHTTPnoS) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1099,7 +1099,7 @@ TEST(TLSTest, clientHTTPnoS) { client.async_send(boost::asio::buffer(send_buf), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1139,7 +1139,7 @@ TEST(TLSTest, clientHTTPnoS) { // Test what happens when the client does not use HTTP nor HTTP. TEST(TLSTest, unknownClient) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1149,13 +1149,13 @@ TEST(TLSTest, unknownClient) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); // Client part. - tcp::socket client(service.getInternalIOService()); + tcp::socket client(service->getInternalIOService()); // Connect to. client.open(tcp::v4()); @@ -1164,7 +1164,7 @@ TEST(TLSTest, unknownClient) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1194,7 +1194,7 @@ TEST(TLSTest, unknownClient) { client.async_send(boost::asio::buffer(send_buf), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1230,7 +1230,7 @@ TEST(TLSTest, unknownClient) { // Test what happens when the client uses a certificate from another CA. TEST(TLSTest, anotherClient) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1240,7 +1240,7 @@ TEST(TLSTest, anotherClient) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -1257,7 +1257,7 @@ TEST(TLSTest, anotherClient) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1283,7 +1283,7 @@ TEST(TLSTest, anotherClient) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1331,7 +1331,7 @@ TEST(TLSTest, anotherClient) { // Test what happens when the client uses a self-signed certificate. TEST(TLSTest, selfSigned) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1341,7 +1341,7 @@ TEST(TLSTest, selfSigned) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -1358,7 +1358,7 @@ TEST(TLSTest, selfSigned) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1384,7 +1384,7 @@ TEST(TLSTest, selfSigned) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1440,7 +1440,7 @@ TEST(TLSTest, selfSigned) { // Test what happens when handshake is forgotten. TEST(TLSTest, noHandshakeCloseonError) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1450,7 +1450,7 @@ TEST(TLSTest, noHandshakeCloseonError) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -1467,7 +1467,7 @@ TEST(TLSTest, noHandshakeCloseonError) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1492,7 +1492,7 @@ TEST(TLSTest, noHandshakeCloseonError) { TestCallback send_cb(&client.lowest_layer()); async_write(client, boost::asio::buffer(send_buf), send_cb); while (!timeout && !send_cb.getCalled()) { - service.runOne(); + service->runOne(); } timer1.cancel(); @@ -1519,7 +1519,7 @@ TEST(TLSTest, noHandshakeCloseonError) { TestCallback receive_cb; server.async_read_some(boost::asio::buffer(receive_buf), receive_cb); while (!timeout && !receive_cb.getCalled()) { - service.runOne(); + service->runOne(); } timer2.cancel(); @@ -1543,7 +1543,7 @@ TEST(TLSTest, noHandshakeCloseonError) { // Test what happens when the server was not configured. TEST(TLSTest, serverNotConfiguredCloseonError) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx(new TlsContext(TlsRole::SERVER)); @@ -1553,7 +1553,7 @@ TEST(TLSTest, serverNotConfiguredCloseonError) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -1570,7 +1570,7 @@ TEST(TLSTest, serverNotConfiguredCloseonError) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1596,7 +1596,7 @@ TEST(TLSTest, serverNotConfiguredCloseonError) { TestCallback client_cb; client.handshake(client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1642,7 +1642,7 @@ TEST(TLSTest, serverNotConfiguredCloseonError) { // Test what happens when the client was not configured. TEST(TLSTest, clientNotConfiguredCloseonError) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1652,7 +1652,7 @@ TEST(TLSTest, clientNotConfiguredCloseonError) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -1669,7 +1669,7 @@ TEST(TLSTest, clientNotConfiguredCloseonError) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1695,7 +1695,7 @@ TEST(TLSTest, clientNotConfiguredCloseonError) { TestCallback client_cb(&client.lowest_layer()); client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1740,7 +1740,7 @@ TEST(TLSTest, clientNotConfiguredCloseonError) { // Test what happens when the client is HTTP (vs HTTPS). TEST(TLSTest, clientHTTPnoSCloseonError) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1750,13 +1750,13 @@ TEST(TLSTest, clientHTTPnoSCloseonError) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); // Client part. - tcp::socket client(service.getInternalIOService()); + tcp::socket client(service->getInternalIOService()); // Connect to. client.open(tcp::v4()); @@ -1765,7 +1765,7 @@ TEST(TLSTest, clientHTTPnoSCloseonError) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1795,7 +1795,7 @@ TEST(TLSTest, clientHTTPnoSCloseonError) { client.async_send(boost::asio::buffer(send_buf), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1835,7 +1835,7 @@ TEST(TLSTest, clientHTTPnoSCloseonError) { // Test what happens when the client uses a certificate from another CA. TEST(TLSTest, anotherClientCloseonError) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1845,7 +1845,7 @@ TEST(TLSTest, anotherClientCloseonError) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -1862,7 +1862,7 @@ TEST(TLSTest, anotherClientCloseonError) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1888,7 +1888,7 @@ TEST(TLSTest, anotherClientCloseonError) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -1934,7 +1934,7 @@ TEST(TLSTest, anotherClientCloseonError) { // Test what happens when the client uses a self-signed certificate. TEST(TLSTest, selfSignedCloseonError) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -1944,7 +1944,7 @@ TEST(TLSTest, selfSignedCloseonError) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -1961,7 +1961,7 @@ TEST(TLSTest, selfSignedCloseonError) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -1987,7 +1987,7 @@ TEST(TLSTest, selfSignedCloseonError) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2040,7 +2040,7 @@ TEST(TLSTest, selfSignedCloseonError) { // Test what happens when the client uses a certificate from another CA // but the client certificate request and validation are disabled. TEST(TLSTest, anotherClientNoReq) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -2050,7 +2050,7 @@ TEST(TLSTest, anotherClientNoReq) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -2067,7 +2067,7 @@ TEST(TLSTest, anotherClientNoReq) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -2093,7 +2093,7 @@ TEST(TLSTest, anotherClientNoReq) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2112,7 +2112,7 @@ TEST(TLSTest, anotherClientNoReq) { // Test what happens when the server uses a certificate without subject // alternative name (but still a version 3 certificate). TEST(TLSTest, serverRaw) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -2122,7 +2122,7 @@ TEST(TLSTest, serverRaw) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -2139,7 +2139,7 @@ TEST(TLSTest, serverRaw) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -2165,7 +2165,7 @@ TEST(TLSTest, serverRaw) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2185,7 +2185,7 @@ TEST(TLSTest, serverRaw) { // Test what happens when the client uses a trusted self-signed certificate. // Not really a failure case as it works... TEST(TLSTest, trustedSelfSigned) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx; @@ -2195,7 +2195,7 @@ TEST(TLSTest, trustedSelfSigned) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -2212,7 +2212,7 @@ TEST(TLSTest, trustedSelfSigned) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -2238,7 +2238,7 @@ TEST(TLSTest, trustedSelfSigned) { TestCallback client_cb; client.async_handshake(roleToImpl(TlsRole::CLIENT), client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2263,7 +2263,7 @@ TEST(TLSTest, trustedSelfSigned) { // Test what happens when the shutdown receiver is inactive. TEST(TLSTest, shutdownInactive) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx(new TlsContext(TlsRole::SERVER)); @@ -2273,7 +2273,7 @@ TEST(TLSTest, shutdownInactive) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -2290,7 +2290,7 @@ TEST(TLSTest, shutdownInactive) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -2316,7 +2316,7 @@ TEST(TLSTest, shutdownInactive) { TestCallback client_cb; client.handshake(client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2336,7 +2336,7 @@ TEST(TLSTest, shutdownInactive) { TestCallback shutdown_cb; client.shutdown(shutdown_cb); while (!timeout && !shutdown_cb.getCalled()) { - service.runOne(); + service->runOne(); } timer2.cancel(); @@ -2361,7 +2361,7 @@ TEST(TLSTest, shutdownInactive) { // Test what happens when the shutdown receiver is active. TEST(TLSTest, shutdownActive) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx(new TlsContext(TlsRole::SERVER)); @@ -2371,7 +2371,7 @@ TEST(TLSTest, shutdownActive) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -2388,7 +2388,7 @@ TEST(TLSTest, shutdownActive) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -2414,7 +2414,7 @@ TEST(TLSTest, shutdownActive) { TestCallback client_cb; client.handshake(client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2439,7 +2439,7 @@ TEST(TLSTest, shutdownActive) { TestCallback shutdown_cb; client.shutdown(shutdown_cb); while (!timeout && (!shutdown_cb.getCalled() || !receive_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer2.cancel(); @@ -2473,7 +2473,7 @@ TEST(TLSTest, shutdownActive) { // Test what happens when the shutdown receiver is inactive on shutdown // and immediate close. TEST(TLSTest, shutdownCloseInactive) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx(new TlsContext(TlsRole::SERVER)); @@ -2483,7 +2483,7 @@ TEST(TLSTest, shutdownCloseInactive) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -2500,7 +2500,7 @@ TEST(TLSTest, shutdownCloseInactive) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -2526,7 +2526,7 @@ TEST(TLSTest, shutdownCloseInactive) { TestCallback client_cb; client.handshake(client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2547,9 +2547,9 @@ TEST(TLSTest, shutdownCloseInactive) { client.shutdown(shutdown_cb); // Post a close which should be called after the shutdown. - service.post([&client] { client.lowest_layer().close(); }); + service->post([&client] { client.lowest_layer().close(); }); while (!timeout && !shutdown_cb.getCalled()) { - service.runOne(); + service->runOne(); } timer2.cancel(); @@ -2577,7 +2577,7 @@ TEST(TLSTest, shutdownCloseInactive) { // Test what happens when the shutdown receiver is active with an // immediate close. TEST(TLSTest, shutdownCloseActive) { - IOService service; + IOServicePtr service(new IOService()); // Server part. TlsContextPtr server_ctx(new TlsContext(TlsRole::SERVER)); @@ -2587,7 +2587,7 @@ TEST(TLSTest, shutdownCloseActive) { // Accept a client. tcp::endpoint server_ep(tcp::endpoint(address::from_string(SERVER_ADDRESS), SERVER_PORT)); - tcp::acceptor acceptor(service.getInternalIOService(), server_ep); + tcp::acceptor acceptor(service->getInternalIOService(), server_ep); acceptor.set_option(tcp::acceptor::reuse_address(true)); TestCallback accept_cb; acceptor.async_accept(server.lowest_layer(), accept_cb); @@ -2604,7 +2604,7 @@ TEST(TLSTest, shutdownCloseActive) { // Run accept and connect. while (!accept_cb.getCalled() || !connect_cb.getCalled()) { - service.runOne(); + service->runOne(); } // Verify the error codes. @@ -2630,7 +2630,7 @@ TEST(TLSTest, shutdownCloseActive) { TestCallback client_cb; client.handshake(client_cb); while (!timeout && (!server_cb.getCalled() || !client_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer.cancel(); @@ -2656,9 +2656,9 @@ TEST(TLSTest, shutdownCloseActive) { client.shutdown(shutdown_cb); // Post a close which should be called after the shutdown. - service.post([&client] { client.lowest_layer().close(); }); + service->post([&client] { client.lowest_layer().close(); }); while (!timeout && (!shutdown_cb.getCalled() || !receive_cb.getCalled())) { - service.runOne(); + service->runOne(); } timer2.cancel(); diff --git a/src/lib/asiolink/tests/udp_socket_unittest.cc b/src/lib/asiolink/tests/udp_socket_unittest.cc index cc16d0ae1a..5ef43c167d 100644 --- a/src/lib/asiolink/tests/udp_socket_unittest.cc +++ b/src/lib/asiolink/tests/udp_socket_unittest.cc @@ -158,17 +158,16 @@ private: // two bytes of a buffer. TEST(UDPSocket, processReceivedData) { - IOService service; // Used to instantiate socket - UDPSocket<UDPCallback> test(service); // Socket under test - uint8_t inbuff[32]; // Buffer to check - OutputBufferPtr outbuff(new OutputBuffer(16)); - // Where data is put - // cppcheck-suppress variableScope - size_t expected; // Expected amount of data - // cppcheck-suppress variableScope - size_t offset; // Where to put next data - // cppcheck-suppress variableScope - size_t cumulative; // Cumulative data received + IOServicePtr service(new IOService()); // Used to instantiate socket + UDPSocket<UDPCallback> test(service); // Socket under test + uint8_t inbuff[32]; // Buffer to check + OutputBufferPtr outbuff(new OutputBuffer(16)); // Where data is put + // cppcheck-suppress variableScope + size_t expected; // Expected amount of data + // cppcheck-suppress variableScope + size_t offset; // Where to put next data + // cppcheck-suppress variableScope + size_t cumulative; // Cumulative data received // Set some dummy values in the buffer to check for (uint8_t i = 0; i < sizeof(inbuff); ++i) { @@ -206,31 +205,27 @@ TEST(UDPSocket, processReceivedData) { // message to a server, receiving an asynchronous message from the server and // closing. TEST(UDPSocket, SequenceTest) { - // Common objects. - IOService service; // Service object for async control - + IOServicePtr service(new IOService()); // Service object for async control // Server - IOAddress server_address(SERVER_ADDRESS); // Address of target server - UDPCallback server_cb("Server"); // Server callback - UDPEndpoint server_endpoint( // Endpoint describing server - server_address, SERVER_PORT); - UDPEndpoint server_remote_endpoint; // Address where server received message from + IOAddress server_address(SERVER_ADDRESS); // Address of target server + UDPCallback server_cb("Server"); // Server callback + UDPEndpoint server_endpoint(server_address, SERVER_PORT); // Endpoint describing server + UDPEndpoint server_remote_endpoint; // Address where server received message from // The client - the UDPSocket being tested - UDPSocket<UDPCallback> client(service);// Socket under test - UDPCallback client_cb("Client"); // Async I/O callback function - UDPEndpoint client_remote_endpoint; // Where client receives message from - size_t client_cumulative = 0; // Cumulative data received - size_t client_offset = 0; // Offset into buffer where data is put - size_t client_expected = 0; // Expected amount of data - OutputBufferPtr client_buffer(new OutputBuffer(16)); - // Where data is put + UDPSocket<UDPCallback> client(service); // Socket under test + UDPCallback client_cb("Client"); // Async I/O callback function + UDPEndpoint client_remote_endpoint; // Where client receives message from + size_t client_cumulative = 0; // Cumulative data received + size_t client_offset = 0; // Offset into buffer where data is put + size_t client_expected = 0; // Expected amount of data + OutputBufferPtr client_buffer(new OutputBuffer(16)); // Where data is put // The server - with which the client communicates. For convenience, we // use the same io_service, and use the endpoint object created for // the client to send to as the endpoint object in the constructor. - boost::asio::ip::udp::socket server(service.getInternalIOService(), + boost::asio::ip::udp::socket server(service->getInternalIOService(), server_endpoint.getASIOEndpoint()); server.set_option(socket_base::reuse_address(true)); @@ -250,15 +245,15 @@ TEST(UDPSocket, SequenceTest) { EXPECT_FALSE(server_cb.getCalled()); // Write something to the server using the client - the callback should not - // be called until we call the io_service.run() method. + // be called until we call the io_service->run() method. client_cb.setCalled(false); client_cb.setCode(7); // Arbitrary number client.asyncSend(OUTBOUND_DATA, sizeof(OUTBOUND_DATA), &server_endpoint, client_cb); EXPECT_FALSE(client_cb.getCalled()); // Execute the two callbacks. - service.runOne(); - service.runOne(); + service->runOne(); + service->runOne(); EXPECT_TRUE(client_cb.getCalled()); EXPECT_EQ(0, client_cb.getCode()); @@ -286,8 +281,8 @@ TEST(UDPSocket, SequenceTest) { server_remote_endpoint.getASIOEndpoint(), server_cb); // Expect two callbacks to run. - service.runOne(); - service.runOne(); + service->runOne(); + service->runOne(); EXPECT_TRUE(client_cb.getCalled()); EXPECT_EQ(0, client_cb.getCode()); diff --git a/src/lib/asiolink/tests/unix_domain_socket_unittest.cc b/src/lib/asiolink/tests/unix_domain_socket_unittest.cc index 033c80375a..b237d0cf13 100644 --- a/src/lib/asiolink/tests/unix_domain_socket_unittest.cc +++ b/src/lib/asiolink/tests/unix_domain_socket_unittest.cc @@ -33,7 +33,7 @@ public: /// /// Removes unix socket descriptor before the test. UnixDomainSocketTest() : - io_service_(), + io_service_(new IOService()), test_socket_(new test::TestServerUnixSocket(io_service_, unixSocketFilePath())), response_(), @@ -105,7 +105,7 @@ public: } /// @brief IO service used by the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Server side unix socket used in these tests. test::TestServerUnixSocketPtr test_socket_; @@ -138,7 +138,7 @@ TEST_F(UnixDomainSocketTest, sendReceive) { // Run IO service to generate server's response. while ((test_socket_->getResponseNum() < 1) && (!test_socket_->isStopped())) { - io_service_.runOne(); + io_service_->runOne(); } // Receive response from the socket. @@ -179,7 +179,7 @@ TEST_F(UnixDomainSocketTest, asyncSendReceive) { )); // Run IO service until connect handler is invoked. while (!connect_handler_invoked && (!test_socket_->isStopped())) { - io_service_.runOne(); + io_service_->runOne(); } // We are going to asynchronously send the 'foo' over the unix socket. @@ -202,7 +202,7 @@ TEST_F(UnixDomainSocketTest, asyncSendReceive) { // Run IO service to generate server's response. while ((test_socket_->getResponseNum() < 1) && (!test_socket_->isStopped())) { - io_service_.runOne(); + io_service_->runOne(); } // There is no guarantee that all data have been sent so we only check that @@ -215,7 +215,7 @@ TEST_F(UnixDomainSocketTest, asyncSendReceive) { // Run IO service until we get the full response from the server. while ((response_.size() < expected_response.size()) && !test_socket_->isStopped()) { - io_service_.runOne(); + io_service_->runOne(); } // Check that the entire response has been received and is correct. @@ -259,7 +259,7 @@ TEST_F(UnixDomainSocketTest, asyncClientErrors) { EXPECT_TRUE(ec); }); while (!connect_handler_invoked && !test_socket_->isStopped()) { - io_service_.runOne(); + io_service_->runOne(); } // Send @@ -272,7 +272,7 @@ TEST_F(UnixDomainSocketTest, asyncClientErrors) { EXPECT_TRUE(ec); }); while (!send_handler_invoked && !test_socket_->isStopped()) { - io_service_.runOne(); + io_service_->runOne(); } // Receive @@ -285,7 +285,7 @@ TEST_F(UnixDomainSocketTest, asyncClientErrors) { EXPECT_TRUE(ec); }); while (!receive_handler_invoked && !test_socket_->isStopped()) { - io_service_.runOne(); + io_service_->runOne(); } } diff --git a/src/lib/asiolink/testutils/test_server_unix_socket.cc b/src/lib/asiolink/testutils/test_server_unix_socket.cc index 93d53d96d5..9df24be1b2 100644 --- a/src/lib/asiolink/testutils/test_server_unix_socket.cc +++ b/src/lib/asiolink/testutils/test_server_unix_socket.cc @@ -142,7 +142,7 @@ public: /// @brief Constructor. /// /// @param io_service Reference to the IO service. - ConnectionPool(IOService& io_service) + ConnectionPool(IOServicePtr& io_service) : io_service_(io_service), connections_(), next_socket_(), response_num_(0) { } @@ -160,7 +160,7 @@ public: /// @return Pointer to the socket. UnixSocketPtr getSocket() { if (!next_socket_) { - next_socket_.reset(new UnixSocket(io_service_.getInternalIOService())); + next_socket_.reset(new UnixSocket(io_service_->getInternalIOService())); } return (next_socket_); } @@ -206,8 +206,8 @@ public: private: - /// @brief Reference to the IO service. - IOService& io_service_; + /// @brief Pointer to the IO service. + IOServicePtr io_service_; /// @brief Container holding established connections. std::set<ConnectionPtr> connections_; @@ -222,15 +222,15 @@ private: }; -TestServerUnixSocket::TestServerUnixSocket(IOService& io_service, +TestServerUnixSocket::TestServerUnixSocket(const IOServicePtr& io_service, const std::string& socket_file_path, const std::string& custom_response) : io_service_(io_service), server_endpoint_(socket_file_path), - server_acceptor_(io_service_.getInternalIOService()), + server_acceptor_(io_service_->getInternalIOService()), test_timer_(io_service_), custom_response_(custom_response), - connection_pool_(new ConnectionPool(io_service)), + connection_pool_(new ConnectionPool(io_service_)), stopped_(false), running_(false) { } @@ -274,8 +274,7 @@ TestServerUnixSocket::bindServerSocket(const bool use_thread) { // when the thread has already started and the IO service is running. The // main thread can move forward when it receives this signal from the handler. if (use_thread) { - io_service_.post(std::bind(&TestServerUnixSocket::signalRunning, - this)); + io_service_->post(std::bind(&TestServerUnixSocket::signalRunning, this)); } } @@ -292,8 +291,7 @@ TestServerUnixSocket::acceptHandler(const boost::system::error_code& ec) { void TestServerUnixSocket::accept() { server_acceptor_.async_accept(*(connection_pool_->getSocket()), - std::bind(&TestServerUnixSocket::acceptHandler, this, - ph::_1)); // error + std::bind(&TestServerUnixSocket::acceptHandler, this, ph::_1)); // error } void @@ -316,7 +314,7 @@ TestServerUnixSocket::waitForRunning() { void TestServerUnixSocket::timeoutHandler() { ADD_FAILURE() << "Timeout occurred while running the test!"; - io_service_.stop(); + io_service_->stop(); stopped_ = true; } diff --git a/src/lib/asiolink/testutils/test_server_unix_socket.h b/src/lib/asiolink/testutils/test_server_unix_socket.h index cf156738c2..2b00aa40f7 100644 --- a/src/lib/asiolink/testutils/test_server_unix_socket.h +++ b/src/lib/asiolink/testutils/test_server_unix_socket.h @@ -53,7 +53,7 @@ public: /// @param io_service IO service. /// @param socket_file_path Socket file path. /// @param custom_response Custom response to be sent to the client. - TestServerUnixSocket(IOService& io_service, + TestServerUnixSocket(const IOServicePtr& io_service, const std::string& socket_file_path, const std::string& custom_response = ""); @@ -123,7 +123,7 @@ private: void signalRunning(); /// @brief IO service used by the tests. - IOService& io_service_; + IOServicePtr io_service_; /// @brief Server endpoint. boost::asio::local::stream_protocol::endpoint server_endpoint_; diff --git a/src/lib/asiolink/testutils/timed_signal.h b/src/lib/asiolink/testutils/timed_signal.h index f1cd0c9775..0b949223b7 100644 --- a/src/lib/asiolink/testutils/timed_signal.h +++ b/src/lib/asiolink/testutils/timed_signal.h @@ -31,7 +31,7 @@ public: /// raised. /// @param mode selects between a one-shot signal or a signal which repeats /// at "milliseconds" interval. - TimedSignal(asiolink::IOService& io_service, int signum, int milliseconds, + TimedSignal(asiolink::IOServicePtr& io_service, int signum, int milliseconds, const asiolink::IntervalTimer::Mode& mode = asiolink::IntervalTimer::ONE_SHOT) : timer_(new asiolink::IntervalTimer(io_service)) { diff --git a/src/lib/asiolink/tls_acceptor.h b/src/lib/asiolink/tls_acceptor.h index c5755592c0..1acac5ce3d 100644 --- a/src/lib/asiolink/tls_acceptor.h +++ b/src/lib/asiolink/tls_acceptor.h @@ -34,7 +34,7 @@ public: /// @brief Constructor. /// /// @param io_service IO service. - explicit TLSAcceptor(IOService& io_service) : TCPAcceptor<C>(io_service) { + explicit TLSAcceptor(const IOServicePtr& io_service) : TCPAcceptor<C>(io_service) { } /// @brief Destructor. diff --git a/src/lib/asiolink/tls_socket.h b/src/lib/asiolink/tls_socket.h index cdd2f78d0c..179952d98f 100644 --- a/src/lib/asiolink/tls_socket.h +++ b/src/lib/asiolink/tls_socket.h @@ -42,7 +42,7 @@ public: /// /// @param service I/O Service object used to manage the socket. /// @param context Pointer to TLS context. - TLSSocket(IOService& service, TlsContextPtr context); + TLSSocket(const IOServicePtr& service, TlsContextPtr context); /// @brief Destructor. virtual ~TLSSocket() { } @@ -224,6 +224,9 @@ public: } private: + /// @brief The IO service used to handle events. + IOServicePtr io_service_; + /// Two variables to hold the stream - a stream and a pointer to it. This /// handles the case where a stream is passed to the TLSSocket on /// construction, or where it is asked to manage its own stream. @@ -266,10 +269,10 @@ TLSSocket<C>::TLSSocket(TlsStream<C>& stream) : // Constructor - create socket on the fly. template <typename C> -TLSSocket<C>::TLSSocket(IOService& service, TlsContextPtr context) : - stream_ptr_(new TlsStream<C>(service, context)), - stream_(*stream_ptr_), socket_(stream_.lowest_layer()), send_buffer_() -{ +TLSSocket<C>::TLSSocket(const IOServicePtr& io_service, TlsContextPtr context) + : io_service_(io_service), + stream_ptr_(new TlsStream<C>(io_service, context)), + stream_(*stream_ptr_), socket_(stream_.lowest_layer()), send_buffer_() { } // Open the socket. diff --git a/src/lib/asiolink/udp_socket.h b/src/lib/asiolink/udp_socket.h index 095ede4576..68adb351d8 100644 --- a/src/lib/asiolink/udp_socket.h +++ b/src/lib/asiolink/udp_socket.h @@ -56,7 +56,7 @@ public: /// socket. In this case, the open() and close() methods are used. /// /// \param service I/O Service object used to manage the socket. - UDPSocket(IOService& service); + UDPSocket(const IOServicePtr& service); /// \brief Destructor virtual ~UDPSocket(); @@ -147,6 +147,9 @@ public: private: + /// @brief The IO service used to handle events. + IOServicePtr io_service_; + // Two variables to hold the socket - a socket and a pointer to it. This // handles the case where a socket is passed to the UDPSocket on // construction, or where it is asked to manage its own socket. @@ -172,8 +175,8 @@ UDPSocket<C>::UDPSocket(boost::asio::ip::udp::socket& socket) : // Constructor - create socket on the fly template <typename C> -UDPSocket<C>::UDPSocket(IOService& service) : - socket_ptr_(new boost::asio::ip::udp::socket(service.getInternalIOService())), +UDPSocket<C>::UDPSocket(const IOServicePtr& io_service) : io_service_(io_service), + socket_ptr_(new boost::asio::ip::udp::socket(io_service_->getInternalIOService())), socket_(*socket_ptr_), isopen_(false) { } diff --git a/src/lib/asiolink/unix_domain_socket.cc b/src/lib/asiolink/unix_domain_socket.cc index e5c3dd27ad..12efab3917 100644 --- a/src/lib/asiolink/unix_domain_socket.cc +++ b/src/lib/asiolink/unix_domain_socket.cc @@ -25,8 +25,8 @@ public: /// @brief Constructor. /// /// @param io_service IO service to be used by the socket class. - UnixDomainSocketImpl(IOService& io_service) - : socket_(io_service.getInternalIOService()) { + UnixDomainSocketImpl(const IOServicePtr& io_service) + : io_service_(io_service), socket_(io_service_->getInternalIOService()) { } /// @brief Destructor. @@ -159,6 +159,9 @@ public: /// @brief Closes the socket. void close(); + /// @brief The IO service used to handle events. + IOServicePtr io_service_; + /// @brief Instance of the boost asio unix domain socket. stream_protocol::socket socket_; }; @@ -280,7 +283,7 @@ UnixDomainSocketImpl::close() { } } -UnixDomainSocket::UnixDomainSocket(IOService& io_service) +UnixDomainSocket::UnixDomainSocket(const IOServicePtr& io_service) : impl_(new UnixDomainSocketImpl(io_service)) { } diff --git a/src/lib/asiolink/unix_domain_socket.h b/src/lib/asiolink/unix_domain_socket.h index cd02f41e56..b0f4358bff 100644 --- a/src/lib/asiolink/unix_domain_socket.h +++ b/src/lib/asiolink/unix_domain_socket.h @@ -41,7 +41,7 @@ public: /// /// @param io_service Reference to IOService to be used by this /// class. - explicit UnixDomainSocket(IOService& io_service); + explicit UnixDomainSocket(const IOServicePtr& io_service); /// @brief Returns native socket representation. virtual int getNative() const; diff --git a/src/lib/asiolink/unix_domain_socket_acceptor.h b/src/lib/asiolink/unix_domain_socket_acceptor.h index 8aa11cadca..aa3d04ea71 100644 --- a/src/lib/asiolink/unix_domain_socket_acceptor.h +++ b/src/lib/asiolink/unix_domain_socket_acceptor.h @@ -32,7 +32,7 @@ public: /// @brief Constructor. /// /// @param io_service Reference to the IO service. - explicit UnixDomainSocketAcceptor(IOService& io_service) + explicit UnixDomainSocketAcceptor(const IOServicePtr& io_service) : IOAcceptor<boost::asio::local::stream_protocol, std::function<void(const boost::system::error_code&)> >(io_service) { } diff --git a/src/lib/config/client_connection.cc b/src/lib/config/client_connection.cc index 6217c1aae1..6f53eca261 100644 --- a/src/lib/config/client_connection.cc +++ b/src/lib/config/client_connection.cc @@ -27,7 +27,7 @@ public: /// @brief Constructor. /// /// @param io_service Reference to the IO service. - explicit ClientConnectionImpl(IOService& io_service); + explicit ClientConnectionImpl(const IOServicePtr& io_service); /// @brief This method schedules timer or reschedules existing timer. /// @@ -115,7 +115,7 @@ private: long timeout_; }; -ClientConnectionImpl::ClientConnectionImpl(IOService& io_service) +ClientConnectionImpl::ClientConnectionImpl(const IOServicePtr& io_service) : socket_(io_service), feed_(), current_command_(), timer_(io_service), timeout_(0) { } @@ -268,7 +268,7 @@ ClientConnectionImpl::timeoutCallback(ClientConnection::Handler handler) { terminate(boost::asio::error::timed_out, handler); } -ClientConnection::ClientConnection(asiolink::IOService& io_service) +ClientConnection::ClientConnection(const IOServicePtr& io_service) : impl_(new ClientConnectionImpl(io_service)) { } @@ -280,6 +280,5 @@ ClientConnection::start(const ClientConnection::SocketPath& socket_path, impl_->start(socket_path, command, handler, timeout); } - } // end of namespace config } // end of namespace isc diff --git a/src/lib/config/client_connection.h b/src/lib/config/client_connection.h index b88854bcd8..0cd25c66b5 100644 --- a/src/lib/config/client_connection.h +++ b/src/lib/config/client_connection.h @@ -108,7 +108,7 @@ public: /// @brief Constructor. /// /// @param io_service Reference to the IO service. - explicit ClientConnection(asiolink::IOService& io_service); + explicit ClientConnection(const asiolink::IOServicePtr& io_service); /// @brief Starts asynchronous transaction with a remote endpoint. /// diff --git a/src/lib/config/cmd_http_listener.cc b/src/lib/config/cmd_http_listener.cc index aa917c7824..e02f65eec0 100644 --- a/src/lib/config/cmd_http_listener.cc +++ b/src/lib/config/cmd_http_listener.cc @@ -62,7 +62,7 @@ CmdHttpListener::start() { // Create the HTTP listener. It will open up a TCP socket and be // prepared to accept incoming connections. - http_listener_.reset(new HttpListener(*thread_io_service_, address_, + http_listener_.reset(new HttpListener(thread_io_service_, address_, port_, tls_context_, rcf, HttpListener::RequestTimeout(TIMEOUT_AGENT_RECEIVE_COMMAND), HttpListener::IdleTimeout(TIMEOUT_AGENT_IDLE_CONNECTION_TIMEOUT))); diff --git a/src/lib/config/command_mgr.cc b/src/lib/config/command_mgr.cc index 8dcb84072a..cf21a697ab 100644 --- a/src/lib/config/command_mgr.cc +++ b/src/lib/config/command_mgr.cc @@ -67,7 +67,7 @@ public: const boost::shared_ptr<UnixDomainSocket>& socket, ConnectionPool& connection_pool, const long timeout) - : socket_(socket), timeout_timer_(*io_service), timeout_(timeout), + : socket_(socket), timeout_timer_(io_service), timeout_(timeout), buf_(), response_(), connection_pool_(connection_pool), feed_(), response_in_progress_(false), watch_socket_(new util::WatchSocket()) { @@ -181,7 +181,6 @@ public: void receiveHandler(const boost::system::error_code& ec, size_t bytes_transferred); - /// @brief Handler invoked when the data is sent over the control socket. /// /// If there are still data to be sent, another asynchronous send is @@ -463,7 +462,6 @@ Connection::timeoutHandler() { doSend(); } - } namespace isc { @@ -575,7 +573,7 @@ CommandMgrImpl::openCommandSocket(const isc::data::ConstElementPtr& socket_info) try { // Start asynchronous acceptor service. - acceptor_.reset(new UnixDomainSocketAcceptor(*io_service_)); + acceptor_.reset(new UnixDomainSocketAcceptor(io_service_)); UnixDomainSocketEndpoint endpoint(socket_name_); acceptor_->open(endpoint); acceptor_->bind(endpoint); @@ -593,7 +591,7 @@ CommandMgrImpl::openCommandSocket(const isc::data::ConstElementPtr& socket_info) void CommandMgrImpl::doAccept() { // Create a socket into which the acceptor will accept new connection. - socket_.reset(new UnixDomainSocket(*io_service_)); + socket_.reset(new UnixDomainSocket(io_service_)); acceptor_->asyncAccept(*socket_, [this](const boost::system::error_code& ec) { if (!ec) { // New connection is arriving. Start asynchronous transmission. @@ -644,7 +642,6 @@ CommandMgr::getControlSocketFD() { return (impl_->acceptor_ ? impl_->acceptor_->getNative() : -1); } - CommandMgr& CommandMgr::instance() { static CommandMgr cmd_mgr; @@ -661,6 +658,5 @@ CommandMgr::setConnectionTimeout(const long timeout) { impl_->timeout_ = timeout; } - -}; // end of isc::config -}; // end of isc +} // end of isc::config +} // end of isc diff --git a/src/lib/config/tests/client_connection_unittests.cc b/src/lib/config/tests/client_connection_unittests.cc index 9e57575fb6..38597ce810 100644 --- a/src/lib/config/tests/client_connection_unittests.cc +++ b/src/lib/config/tests/client_connection_unittests.cc @@ -33,7 +33,7 @@ public: /// /// Removes unix socket descriptor before the test. ClientConnectionTest() : - io_service_(), + io_service_(new IOService()), test_socket_(new test::TestServerUnixSocket(io_service_, unixSocketFilePath())) { removeUnixSocketFile(); @@ -76,7 +76,7 @@ public: } /// @brief IO service used by the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Server side unix socket used in these tests. test::TestServerUnixSocketPtr test_socket_; @@ -116,7 +116,7 @@ TEST_F(ClientConnectionTest, success) { }); // Run the connection. while (!handler_invoked && !test_socket_->isStopped()) { - io_service_.runOne(); + io_service_->runOne(); } } @@ -153,7 +153,7 @@ TEST_F(ClientConnectionTest, timeout) { }, ClientConnection::Timeout(1000)); while (!handler_invoked && !test_socket_->isStopped()) { - io_service_.runOne(); + io_service_->runOne(); } } @@ -176,7 +176,7 @@ TEST_F(ClientConnectionTest, connectionError) { }); while (!handler_invoked && !test_socket_->isStopped()) { - io_service_.runOne(); + io_service_->runOne(); } } diff --git a/src/lib/config/tests/cmd_http_listener_unittests.cc b/src/lib/config/tests/cmd_http_listener_unittests.cc index 0093980411..fbb5c8ac79 100644 --- a/src/lib/config/tests/cmd_http_listener_unittests.cc +++ b/src/lib/config/tests/cmd_http_listener_unittests.cc @@ -54,7 +54,7 @@ public: /// Starts test timer which detects timeouts, deregisters all commands /// from CommandMgr, and enables multi-threading mode. CmdHttpListenerTest() - : listener_(), io_service_(), test_timer_(io_service_), + : listener_(), io_service_(new IOService()), test_timer_(io_service_), run_io_service_timer_(io_service_), clients_(), num_threads_(), num_clients_(), num_in_progress_(0), num_finished_(0), chunk_size_(0), pause_cnt_(0) { @@ -171,7 +171,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Runs IO service with optional timeout. @@ -192,10 +192,10 @@ public: size_t num_done = 0; while (num_done != request_limit) { // Always call restart() before we call run(); - io_service_.restart(); + io_service_->restart(); // Run until a client stops the service. - io_service_.run(); + io_service_->run(); // If all the clients are done receiving, the test is done. num_done = 0; @@ -687,7 +687,7 @@ public: CmdHttpListenerPtr listener_; /// @brief IO service used in drive the test and test clients. - IOService io_service_; + IOServicePtr io_service_; /// @brief Asynchronous timer service to detect timeouts. IntervalTimer test_timer_; diff --git a/src/lib/d2srv/dns_client.cc b/src/lib/d2srv/dns_client.cc index 5798207670..4467577dc2 100644 --- a/src/lib/d2srv/dns_client.cc +++ b/src/lib/d2srv/dns_client.cc @@ -100,7 +100,7 @@ public: /// @param tsig_key A pointer to an @c D2TsigKeyPtr object that will /// (if not null) be used to sign the DNS Update message and verify the /// response. - void doUpdate(asiolink::IOService& io_service, + void doUpdate(const asiolink::IOServicePtr& io_service, const asiolink::IOAddress& ns_addr, const uint16_t ns_port, D2UpdateMessage& update, @@ -222,7 +222,7 @@ DNSClientImpl::getStatus(const asiodns::IOFetch::Result result) { } void -DNSClientImpl::doUpdate(asiolink::IOService& io_service, +DNSClientImpl::doUpdate(const asiolink::IOServicePtr& io_service, const IOAddress& ns_addr, const uint16_t ns_port, D2UpdateMessage& update, @@ -275,7 +275,7 @@ DNSClientImpl::doUpdate(asiolink::IOService& io_service, // Post the task to the task queue in the IO service. Caller will actually // run these tasks by executing IOService::run. - io_service.post(io_fetch); + io_service->post(io_fetch); // Update sent statistics. incrStats("update-sent"); @@ -311,7 +311,7 @@ DNSClient::getMaxTimeout() { } void -DNSClient::doUpdate(asiolink::IOService& io_service, +DNSClient::doUpdate(const asiolink::IOServicePtr& io_service, const IOAddress& ns_addr, const uint16_t ns_port, D2UpdateMessage& update, diff --git a/src/lib/d2srv/dns_client.h b/src/lib/d2srv/dns_client.h index c63a7a0c0a..63a2c5c16a 100644 --- a/src/lib/d2srv/dns_client.h +++ b/src/lib/d2srv/dns_client.h @@ -139,7 +139,7 @@ public: /// @param tsig_key A pointer to an @c D2TsigKeyPtr object that will /// (if not null) be used to sign the DNS Update message and verify the /// response. - void doUpdate(asiolink::IOService& io_service, + void doUpdate(const asiolink::IOServicePtr& io_service, const asiolink::IOAddress& ns_addr, const uint16_t ns_port, D2UpdateMessage& update, diff --git a/src/lib/d2srv/nc_trans.cc b/src/lib/d2srv/nc_trans.cc index e9534c256c..e9adce5432 100644 --- a/src/lib/d2srv/nc_trans.cc +++ b/src/lib/d2srv/nc_trans.cc @@ -197,7 +197,7 @@ NameChangeTransaction::sendUpdate(const std::string& comment) { // for the current server. If not we would need to add that. D2ParamsPtr d2_params = cfg_mgr_->getD2Params(); - dns_client_->doUpdate(*io_service_, current_server_->getIpAddress(), + dns_client_->doUpdate(io_service_, current_server_->getIpAddress(), current_server_->getPort(), *dns_update_request_, d2_params->getDnsServerTimeout(), tsig_key_); // Message is on its way, so the next event should be NOP_EVT. diff --git a/src/lib/d2srv/tests/dns_client_unittests.cc b/src/lib/d2srv/tests/dns_client_unittests.cc index 76a02a0379..9ffe0963e1 100644 --- a/src/lib/d2srv/tests/dns_client_unittests.cc +++ b/src/lib/d2srv/tests/dns_client_unittests.cc @@ -60,7 +60,7 @@ class DNSClientTest : public ::testing::Test, DNSClient::Callback, public D2StatTest { public: /// @brief The IOService which handles IO operations. - IOService service_; + IOServicePtr service_; /// @brief The UDP socket. std::unique_ptr<udp::socket> socket_; @@ -107,7 +107,7 @@ public: /// waiting for a response. Some of the tests are checking DNSClient behavior /// in case when response from the server is not received. Tests output would /// become messy if such errors were logged. - DNSClientTest() : service_(), socket_(), endpoint_(), + DNSClientTest() : service_(new IOService()), socket_(), endpoint_(), status_(DNSClient::SUCCESS), corrupt_response_(false), expect_response_(true), test_timer_(service_), received_(0), expected_(0), go_on_(false) { @@ -136,7 +136,7 @@ public: virtual void operator()(DNSClient::Status status) { status_ = status; if (!expected_ || (expected_ == ++received_)) { - service_.stop(); + service_->stop(); } if (expect_response_) { @@ -168,7 +168,7 @@ public: /// /// This callback stops all running (hanging) tasks on IO service. void testTimeoutHandler() { - service_.stop(); + service_->stop(); FAIL() << "Test timeout hit."; } @@ -367,7 +367,7 @@ public: // This starts the execution of tasks posted to IOService. run() blocks // until stop() is called in the completion callback function. - service_.run(); + service_->run(); } @@ -392,7 +392,7 @@ public: // responses. The reuse address option is set so as both sockets can // use the same address. This new socket is bound to the test address // and port, where requests will be sent. - socket_.reset(new udp::socket(service_.getInternalIOService(), + socket_.reset(new udp::socket(service_->getInternalIOService(), boost::asio::ip::udp::v4())); socket_->set_option(socket_base::reuse_address(true)); socket_->bind(udp::endpoint(address::from_string(TEST_ADDRESS), @@ -437,14 +437,14 @@ public: // Kick of the message exchange by actually running the scheduled // "send" and "receive" operations. - service_.run(); + service_->run(); socket_->close(); // Since the callback, operator(), calls stop() on the io_service, // we must reset it in order for subsequent calls to run() or // runOne() to work. - service_.restart(); + service_->restart(); } /// @brief Performs a single request-response exchange with or without TSIG. @@ -465,7 +465,7 @@ public: ASSERT_NO_THROW(message.setZone(Name("example.com"), RRClass::IN())); // Setup our "loopback" server. - udp::socket udp_socket(service_.getInternalIOService(), boost::asio::ip::udp::v4()); + udp::socket udp_socket(service_->getInternalIOService(), boost::asio::ip::udp::v4()); udp_socket.set_option(socket_base::reuse_address(true)); udp_socket.bind(udp::endpoint(address::from_string(TEST_ADDRESS), TEST_PORT)); @@ -489,14 +489,14 @@ public: // Kick of the message exchange by actually running the scheduled // "send" and "receive" operations. - service_.run(); + service_->run(); udp_socket.close(); // Since the callback, operator(), calls stop() on the io_service, // we must reset it in order for subsequent calls to run() or // runOne() to work. - service_.restart(); + service_->restart(); } }; diff --git a/src/lib/d2srv/tests/nc_trans_unittests.cc b/src/lib/d2srv/tests/nc_trans_unittests.cc index 89a23b7806..e1df8e27e6 100644 --- a/src/lib/d2srv/tests/nc_trans_unittests.cc +++ b/src/lib/d2srv/tests/nc_trans_unittests.cc @@ -1000,7 +1000,7 @@ TEST_F(NameChangeTransactionTest, sendUpdateCorruptResponse) { ASSERT_TRUE(name_change->selectFwdServer()); // Create a server and start it listening. - FauxServer server(*io_service_, *(name_change->getCurrentServer())); + FauxServer server(io_service_, *(name_change->getCurrentServer())); server.receive(FauxServer::CORRUPT_RESP); // Build a valid request, call sendUpdate and process the response. @@ -1020,7 +1020,7 @@ TEST_F(NameChangeTransactionTest, sendUpdate) { ASSERT_TRUE(name_change->selectFwdServer()); // Create a server and start it listening. - FauxServer server(*io_service_, *(name_change->getCurrentServer())); + FauxServer server(io_service_, *(name_change->getCurrentServer())); server.receive (FauxServer::USE_RCODE, dns::Rcode::NOERROR()); // Build a valid request, call sendUpdate and process the response. @@ -1049,7 +1049,7 @@ TEST_F(NameChangeTransactionTest, tsigUnsignedResponse) { ASSERT_TRUE(name_change->selectFwdServer()); // Create a server and start it listening. - FauxServer server(*io_service_, *(name_change->getCurrentServer())); + FauxServer server(io_service_, *(name_change->getCurrentServer())); server.receive (FauxServer::USE_RCODE, dns::Rcode::NOERROR()); // Do the update. @@ -1080,7 +1080,7 @@ TEST_F(NameChangeTransactionTest, tsigInvalidResponse) { // Create a server, tell it to sign responses with a "random" key, // then start it listening. - FauxServer server(*io_service_, *(name_change->getCurrentServer())); + FauxServer server(io_service_, *(name_change->getCurrentServer())); server.receive (FauxServer::INVALID_TSIG, dns::Rcode::NOERROR()); // Do the update. @@ -1113,7 +1113,7 @@ TEST_F(NameChangeTransactionTest, tsigUnexpectedSignedResponse) { // Create a server, tell it to sign responses with a "random" key, // then start it listening. - FauxServer server(*io_service_, *(name_change->getCurrentServer())); + FauxServer server(io_service_, *(name_change->getCurrentServer())); server.receive (FauxServer::INVALID_TSIG, dns::Rcode::NOERROR()); // Perform an update without TSIG. @@ -1156,7 +1156,7 @@ TEST_F(NameChangeTransactionTest, tsigAllValid) { ASSERT_TRUE(name_change->selectFwdServer()); // Create a server, set its TSIG key, and then start it listening. - FauxServer server(*io_service_, *(name_change->getCurrentServer())); + FauxServer server(io_service_, *(name_change->getCurrentServer())); // Since we create a new server instance each time we need to tell // it not reschedule receives automatically. server.perpetual_receive_ = false; diff --git a/src/lib/d2srv/testutils/nc_test_utils.cc b/src/lib/d2srv/testutils/nc_test_utils.cc index 6791eb9639..56c3988e3b 100644 --- a/src/lib/d2srv/testutils/nc_test_utils.cc +++ b/src/lib/d2srv/testutils/nc_test_utils.cc @@ -47,7 +47,6 @@ const char* valid_d2_config = "{ " " \"port\": 100 } ] } " "] } }"; - const char* TEST_DNS_SERVER_IP = "127.0.0.1"; size_t TEST_DNS_SERVER_PORT = 5301; @@ -56,33 +55,32 @@ const bool NO_RDATA = false; //*************************** FauxServer class *********************** -FauxServer::FauxServer(asiolink::IOService& io_service, +FauxServer::FauxServer(asiolink::IOServicePtr& io_service, asiolink::IOAddress& address, size_t port) - :io_service_(io_service), address_(address), port_(port), - server_socket_(), receive_pending_(false), perpetual_receive_(true), - tsig_key_() { + : io_service_(io_service), address_(address), port_(port), + server_socket_(), receive_pending_(false), perpetual_receive_(true), + tsig_key_() { - server_socket_.reset(new boost::asio::ip::udp::socket(io_service_.getInternalIOService(), - boost::asio::ip::udp::v4())); + server_socket_.reset(new boost::asio::ip::udp::socket(io_service_->getInternalIOService(), + boost::asio::ip::udp::v4())); server_socket_->set_option(boost::asio::socket_base::reuse_address(true)); isc::asiolink::UDPEndpoint endpoint(address_, port_); server_socket_->bind(endpoint.getASIOEndpoint()); } -FauxServer::FauxServer(asiolink::IOService& io_service, +FauxServer::FauxServer(asiolink::IOServicePtr& io_service, DnsServerInfo& server) - :io_service_(io_service), address_(server.getIpAddress()), - port_(server.getPort()), server_socket_(), receive_pending_(false), - perpetual_receive_(true), tsig_key_() { - server_socket_.reset(new boost::asio::ip::udp::socket(io_service_.getInternalIOService(), - boost::asio::ip::udp::v4())); + : io_service_(io_service), address_(server.getIpAddress()), + port_(server.getPort()), server_socket_(), receive_pending_(false), + perpetual_receive_(true), tsig_key_() { + server_socket_.reset(new boost::asio::ip::udp::socket(io_service_->getInternalIOService(), + boost::asio::ip::udp::v4())); server_socket_->set_option(boost::asio::socket_base::reuse_address(true)); isc::asiolink::UDPEndpoint endpoint(address_, port_); server_socket_->bind(endpoint.getASIOEndpoint()); } - FauxServer::~FauxServer() { } @@ -210,13 +208,11 @@ FauxServer::requestHandler(const boost::system::error_code& error, } } - - //********************** TimedIO class *********************** TimedIO::TimedIO() - : io_service_(new isc::asiolink::IOService()), - timer_(*io_service_), run_time_(0) { + : io_service_(new isc::asiolink::IOService()), timer_(io_service_), + run_time_(0) { } TimedIO::~TimedIO() { @@ -371,7 +367,6 @@ TransactionTest::setupForIPv6Transaction(dhcp_ddns::NameChangeType chg_type, setupForIPv6Transaction(chg_type, change_mask, makeTSIGKeyInfo(key_name)); } - //********************** Functions **************************** void diff --git a/src/lib/d2srv/testutils/nc_test_utils.h b/src/lib/d2srv/testutils/nc_test_utils.h index dac4abe844..cd6152cd82 100644 --- a/src/lib/d2srv/testutils/nc_test_utils.h +++ b/src/lib/d2srv/testutils/nc_test_utils.h @@ -42,8 +42,8 @@ public: INVALID_TSIG // Generate a response with the wrong TSIG key }; - /// @brief Reference to IOService to use for IO processing. - asiolink::IOService& io_service_; + /// @brief The IO service used to handle events. + asiolink::IOServicePtr io_service_; /// @brief IP address at which to listen for requests. const asiolink::IOAddress& address_; @@ -77,7 +77,7 @@ public: /// @param io_service IOService to be used for socket IO. /// @param address IP address at which the server should listen. /// @param port Port number at which the server should listen. - FauxServer(asiolink::IOService& io_service, asiolink::IOAddress& address, + FauxServer(asiolink::IOServicePtr& io_service, asiolink::IOAddress& address, size_t port); /// @brief Constructor @@ -85,7 +85,7 @@ public: /// @param io_service IOService to be used for socket IO. /// @param server DnsServerInfo of server the DNS server. This supplies the /// server's ip address and port. - FauxServer(asiolink::IOService& io_service, DnsServerInfo& server); + FauxServer(asiolink::IOServicePtr& io_service, DnsServerInfo& server); /// @brief Destructor virtual ~FauxServer(); diff --git a/src/lib/dhcp_ddns/ncr_io.cc b/src/lib/dhcp_ddns/ncr_io.cc index 8e9e9cf115..1179c74adb 100644 --- a/src/lib/dhcp_ddns/ncr_io.cc +++ b/src/lib/dhcp_ddns/ncr_io.cc @@ -58,7 +58,7 @@ NameChangeListener::NameChangeListener(RequestReceiveHandler& void -NameChangeListener::startListening(isc::asiolink::IOService& io_service) { +NameChangeListener::startListening(const isc::asiolink::IOServicePtr& io_service) { if (amListening()) { // This amounts to a programmatic error. isc_throw(NcrListenerError, "NameChangeListener is already listening"); @@ -160,14 +160,14 @@ NameChangeListener::invokeRecvHandler(const Result result, NameChangeSender::NameChangeSender(RequestSendHandler& send_handler, size_t send_queue_max) : sending_(false), send_handler_(send_handler), - send_queue_max_(send_queue_max), io_service_(NULL), mutex_(new mutex) { + send_queue_max_(send_queue_max), mutex_(new mutex) { // Queue size must be big enough to hold at least 1 entry. setQueueMaxSize(send_queue_max); } void -NameChangeSender::startSending(isc::asiolink::IOService& io_service) { +NameChangeSender::startSending(const isc::asiolink::IOServicePtr& io_service) { if (amSending()) { // This amounts to a programmatic error. isc_throw(NcrSenderError, "NameChangeSender is already sending"); @@ -188,12 +188,12 @@ NameChangeSender::startSending(isc::asiolink::IOService& io_service) { } void -NameChangeSender::startSendingInternal(isc::asiolink::IOService& io_service) { +NameChangeSender::startSendingInternal(const isc::asiolink::IOServicePtr& io_service) { // Clear send marker. ncr_to_send_.reset(); // Remember io service we're given. - io_service_ = &io_service; + io_service_ = io_service; open(io_service); // Set our status to sending. @@ -211,7 +211,7 @@ NameChangeSender::stopSending() { setSending(false); // If there is an outstanding IO to complete, attempt to process it. - if (ioReady() && io_service_ != NULL) { + if (ioReady() && io_service_) { try { runReadyIO(); } catch (const std::exception& ex) { @@ -232,7 +232,7 @@ NameChangeSender::stopSending() { DHCP_DDNS_NCR_SEND_CLOSE_ERROR).arg(ex.what()); } - io_service_ = NULL; + io_service_.reset(); } void diff --git a/src/lib/dhcp_ddns/ncr_io.h b/src/lib/dhcp_ddns/ncr_io.h index 664057d961..54633df625 100644 --- a/src/lib/dhcp_ddns/ncr_io.h +++ b/src/lib/dhcp_ddns/ncr_io.h @@ -110,7 +110,6 @@ public: isc::Exception(file, line, what) { }; }; - /// @brief Abstract interface for receiving NameChangeRequests. /// /// NameChangeListener provides the means to: @@ -222,7 +221,7 @@ public: /// /// @throw NcrListenError if the listener is already "listening" or /// in the event the open or doReceive methods fail. - void startListening(isc::asiolink::IOService& io_service); + void startListening(const isc::asiolink::IOServicePtr& io_service); /// @brief Closes the IO source and stops listen logic. /// @@ -231,6 +230,7 @@ public: void stopListening(); protected: + /// @brief Initiates an asynchronous receive /// /// Sets context information to indicate that IO is in progress and invokes @@ -278,7 +278,7 @@ protected: /// /// @throw If the implementation encounters an error it MUST /// throw it as an isc::Exception or derivative. - virtual void open(isc::asiolink::IOService& io_service) = 0; + virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0; /// @brief Abstract method which closes the IO source. /// @@ -352,7 +352,6 @@ private: /// @brief Defines a smart pointer to an instance of a listener. typedef boost::shared_ptr<NameChangeListener> NameChangeListenerPtr; - /// @brief Thrown when a NameChangeSender encounters an error. class NcrSenderError : public isc::Exception { public: @@ -381,7 +380,6 @@ public: isc::Exception(file, line, what) { }; }; - /// @brief Abstract interface for sending NameChangeRequests. /// /// NameChangeSender provides the means to: @@ -529,7 +527,7 @@ public: /// /// @throw NcrSenderError if the sender is already "sending" or /// NcrSenderOpenError if the open fails. - void startSending(isc::asiolink::IOService & io_service); + void startSending(const isc::asiolink::IOServicePtr& io_service); /// @brief Closes the IO sink and stops send logic. /// @@ -588,7 +586,7 @@ private: /// @brief Prepares the IO for transmission in a thread safe context. /// /// @param io_service is the IOService that will handle IO event processing. - void startSendingInternal(isc::asiolink::IOService & io_service); + void startSendingInternal(const isc::asiolink::IOServicePtr & io_service); /// @brief Queues the given request to be sent in a thread safe context. /// @@ -677,7 +675,7 @@ protected: /// /// @throw If the implementation encounters an error it MUST /// throw it as an isc::Exception or derivative. - virtual void open(isc::asiolink::IOService& io_service) = 0; + virtual void open(const isc::asiolink::IOServicePtr& io_service) = 0; /// @brief Abstract method which closes the IO sink. /// @@ -819,6 +817,16 @@ private: sending_ = value; } +protected: + + /// @brief Pointer to the IOService currently being used by the sender. + /// @note We need to remember the io_service but we receive it by + /// reference. Use a raw pointer to store it. This value should never be + /// exposed and is only valid while in send mode. + asiolink::IOServicePtr io_service_; + +private: + /// @brief Boolean indicator which tracks sending status. bool sending_; @@ -834,12 +842,6 @@ private: /// @brief Pointer to the request which is in the process of being sent. NameChangeRequestPtr ncr_to_send_; - /// @brief Pointer to the IOService currently being used by the sender. - /// @note We need to remember the io_service but we receive it by - /// reference. Use a raw pointer to store it. This value should never be - /// exposed and is only valid while in send mode. - asiolink::IOService* io_service_; - /// @brief The mutex used to protect internal state. const boost::scoped_ptr<std::mutex> mutex_; }; diff --git a/src/lib/dhcp_ddns/ncr_udp.cc b/src/lib/dhcp_ddns/ncr_udp.cc index c46307e657..f32404cb54 100644 --- a/src/lib/dhcp_ddns/ncr_udp.cc +++ b/src/lib/dhcp_ddns/ncr_udp.cc @@ -86,14 +86,16 @@ NameChangeUDPListener::~NameChangeUDPListener() { } void -NameChangeUDPListener::open(isc::asiolink::IOService& io_service) { +NameChangeUDPListener::open(const isc::asiolink::IOServicePtr& io_service) { // create our endpoint and bind the low level socket to it. isc::asiolink::UDPEndpoint endpoint(ip_address_, port_); + io_service_ = io_service; + // Create the low level socket. try { asio_socket_.reset(new boost::asio::ip::udp:: - socket(io_service.getInternalIOService(), + socket(io_service_->getInternalIOService(), (ip_address_.isV4() ? boost::asio::ip::udp::v4() : boost::asio::ip::udp::v6()))); @@ -106,6 +108,7 @@ NameChangeUDPListener::open(isc::asiolink::IOService& io_service) { asio_socket_->bind(endpoint.getASIOEndpoint()); } catch (const boost::system::system_error& ex) { asio_socket_.reset(); + io_service_.reset(); isc_throw (NcrUDPError, ex.code().message()); } @@ -147,6 +150,7 @@ NameChangeUDPListener::close() { } socket_.reset(); + io_service_.reset(); } void @@ -226,14 +230,16 @@ NameChangeUDPSender::~NameChangeUDPSender() { } void -NameChangeUDPSender::open(isc::asiolink::IOService& io_service) { +NameChangeUDPSender::open(const isc::asiolink::IOServicePtr& io_service) { // create our endpoint and bind the low level socket to it. isc::asiolink::UDPEndpoint endpoint(ip_address_, port_); + io_service_ = io_service; + // Create the low level socket. try { asio_socket_.reset(new boost::asio::ip::udp:: - socket(io_service.getInternalIOService(), + socket(io_service_->getInternalIOService(), (ip_address_.isV4() ? boost::asio::ip::udp::v4() : boost::asio::ip::udp::v6()))); @@ -245,6 +251,8 @@ NameChangeUDPSender::open(isc::asiolink::IOService& io_service) { // Bind the low level socket to our endpoint. asio_socket_->bind(endpoint.getASIOEndpoint()); } catch (const boost::system::system_error& ex) { + asio_socket_.reset(); + io_service_.reset(); isc_throw (NcrUDPError, ex.code().message()); } @@ -288,6 +296,7 @@ NameChangeUDPSender::close() { closeWatchSocket(); watch_socket_.reset(); + io_service_.reset(); } void diff --git a/src/lib/dhcp_ddns/ncr_udp.h b/src/lib/dhcp_ddns/ncr_udp.h index 01284aff7e..2b134238ba 100644 --- a/src/lib/dhcp_ddns/ncr_udp.h +++ b/src/lib/dhcp_ddns/ncr_udp.h @@ -110,7 +110,6 @@ #include <boost/shared_array.hpp> - /// responsibility of the completion handler to perform the steps necessary /// to interpret the raw data provided by the service outcome. The /// UDPCallback operator implementation is mostly a pass through. @@ -351,7 +350,7 @@ public: /// @param io_service the IOService which will monitor the socket. /// /// @throw NcrUDPError if the open fails. - virtual void open(isc::asiolink::IOService& io_service); + virtual void open(const isc::asiolink::IOServicePtr& io_service); /// @brief Closes the UDPSocket. /// @@ -397,7 +396,12 @@ public: /// the socket receive completion. void receiveCompletionHandler(const bool successful, const UDPCallback* recv_callback); + private: + + /// @brief The IO service used to handle events. + isc::asiolink::IOServicePtr io_service_; + /// @brief IP address on which to listen for requests. isc::asiolink::IOAddress ip_address_; @@ -431,7 +435,6 @@ private: //@} }; - /// @brief Provides the ability to send NameChangeRequests via UDP socket /// /// This class is a derivation of the NameChangeSender which is capable of @@ -469,7 +472,6 @@ public: /// @brief Destructor virtual ~NameChangeUDPSender(); - /// @brief Opens a UDP socket using the given IOService. /// /// Creates a NameChangeUDPSocket bound to the sender's IP address @@ -478,8 +480,7 @@ public: /// @param io_service the IOService which will monitor the socket. /// /// @throw NcrUDPError if the open fails. - virtual void open(isc::asiolink::IOService& io_service); - + virtual void open(const isc::asiolink::IOServicePtr& io_service); /// @brief Closes the UDPSocket. /// diff --git a/src/lib/dhcp_ddns/tests/ncr_udp_unittests.cc b/src/lib/dhcp_ddns/tests/ncr_udp_unittests.cc index 758689b835..bb48609814 100644 --- a/src/lib/dhcp_ddns/tests/ncr_udp_unittests.cc +++ b/src/lib/dhcp_ddns/tests/ncr_udp_unittests.cc @@ -24,6 +24,7 @@ using namespace std; using namespace isc; +using namespace isc::asiolink; using namespace isc::util; using namespace isc::dhcp_ddns; @@ -88,9 +89,8 @@ public: /// 1. Given valid parameters, the listener constructor works TEST(NameChangeUDPListenerBasicTest, constructionTests) { // Verify the default constructor works. - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); + IOAddress ip_address(TEST_ADDRESS); uint32_t port = LISTENER_PORT; - isc::asiolink::IOService io_service; SimpleListenHandler ncr_handler; // Verify that valid constructor works. EXPECT_NO_THROW(NameChangeUDPListener(ip_address, port, FMT_JSON, @@ -105,9 +105,9 @@ TEST(NameChangeUDPListenerBasicTest, constructionTests) { /// 4. Return to the listening state after stopping TEST(NameChangeUDPListenerBasicTest, basicListenTests) { // Verify the default constructor works. - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); + IOAddress ip_address(TEST_ADDRESS); uint32_t port = LISTENER_PORT; - isc::asiolink::IOService io_service; + IOServicePtr io_service(new IOService()); SimpleListenHandler ncr_handler; NameChangeListenerPtr listener; @@ -134,7 +134,7 @@ TEST(NameChangeUDPListenerBasicTest, basicListenTests) { EXPECT_TRUE(listener->isIoPending()); // Verify that IO pending is false, after cancel event occurs. - EXPECT_NO_THROW(io_service.runOne()); + EXPECT_NO_THROW(io_service->runOne()); EXPECT_FALSE(listener->isIoPending()); // Verify that attempting to stop listening when we are not is ok. @@ -148,29 +148,28 @@ TEST(NameChangeUDPListenerBasicTest, basicListenTests) { /// @brief Compares two NameChangeRequests for equality. bool checkSendVsReceived(NameChangeRequestPtr sent_ncr, NameChangeRequestPtr received_ncr) { - return ((sent_ncr && received_ncr) && - (*sent_ncr == *received_ncr)); + return ((sent_ncr && received_ncr) && (*sent_ncr == *received_ncr)); } /// @brief Text fixture for testing NameChangeUDPListener class NameChangeUDPListenerTest : public virtual ::testing::Test, NameChangeListener::RequestReceiveHandler { public: - isc::asiolink::IOService io_service_; + IOServicePtr io_service_; NameChangeListener::Result result_; NameChangeRequestPtr sent_ncr_; NameChangeRequestPtr received_ncr_; NameChangeListenerPtr listener_; - isc::asiolink::IntervalTimer test_timer_; + IntervalTimer test_timer_; /// @brief Constructor // // Instantiates the listener member and the test timer. The timer is used // to ensure a test doesn't go awry and hang forever. NameChangeUDPListenerTest() - : io_service_(), result_(NameChangeListener::SUCCESS), + : io_service_(new IOService()), result_(NameChangeListener::SUCCESS), test_timer_(io_service_) { - isc::asiolink::IOAddress addr(TEST_ADDRESS); + IOAddress addr(TEST_ADDRESS); listener_.reset(new NameChangeUDPListener(addr, LISTENER_PORT, FMT_JSON, *this, true)); @@ -197,7 +196,7 @@ public: // Create a UDP socket through which our "sender" will send the NCR. boost::asio::ip::udp::socket - udp_socket(io_service_.getInternalIOService(), boost::asio::ip::udp::v4()); + udp_socket(io_service_->getInternalIOService(), boost::asio::ip::udp::v4()); // Create an endpoint pointed at the listener. boost::asio::ip::udp::endpoint @@ -228,7 +227,7 @@ public: /// /// This callback stops all running (hanging) tasks on IO service. void testTimeoutHandler() { - io_service_.stop(); + io_service_->stop(); FAIL() << "Test timeout hit."; } }; @@ -252,7 +251,7 @@ TEST_F(NameChangeUDPListenerTest, basicReceiveTests) { ASSERT_NO_THROW(sendNcr(valid_msgs[i])); // Execute no more then one event, which should be receive complete. - EXPECT_NO_THROW(io_service_.runOne()); + EXPECT_NO_THROW(io_service_->runOne()); // Verify the "application" status value for a successful complete. EXPECT_EQ(NameChangeListener::SUCCESS, result_); @@ -268,7 +267,7 @@ TEST_F(NameChangeUDPListenerTest, basicReceiveTests) { EXPECT_FALSE(listener_->amListening()); // Verify that IO pending is false, after cancel event occurs. - EXPECT_NO_THROW(io_service_.runOne()); + EXPECT_NO_THROW(io_service_->runOne()); EXPECT_FALSE(listener_->isIoPending()); } @@ -312,9 +311,8 @@ public: /// 3. Default construction provides default max queue size /// 4. Construction with a custom max queue size works TEST_F(NameChangeUDPSenderBasicTest, constructionTests) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); + IOAddress ip_address(TEST_ADDRESS); uint32_t port = SENDER_PORT; - isc::asiolink::IOService io_service; SimpleSendHandler ncr_handler; // Verify that constructing with an queue size of zero is not allowed. @@ -349,9 +347,8 @@ TEST_F(NameChangeUDPSenderBasicTest, constructionTestsMultiThreading) { // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); + IOAddress ip_address(TEST_ADDRESS); uint32_t port = SENDER_PORT; - isc::asiolink::IOService io_service; SimpleSendHandler ncr_handler; // Verify that constructing with an queue size of zero is not allowed. @@ -378,8 +375,8 @@ TEST_F(NameChangeUDPSenderBasicTest, constructionTestsMultiThreading) { /// @brief Tests NameChangeUDPSender basic send functionality TEST_F(NameChangeUDPSenderBasicTest, basicSendTests) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Tests are based on a list of messages, get the count now. @@ -506,8 +503,8 @@ TEST_F(NameChangeUDPSenderBasicTest, basicSendTestsMultiThreading) { // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Tests are based on a list of messages, get the count now. @@ -632,8 +629,8 @@ TEST_F(NameChangeUDPSenderBasicTest, basicSendTestsMultiThreading) { /// @brief Tests that sending gets kick-started if the queue isn't empty /// when startSending is called. TEST_F(NameChangeUDPSenderBasicTest, autoStart) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Tests are based on a list of messages, get the count now. @@ -687,8 +684,8 @@ TEST_F(NameChangeUDPSenderBasicTest, autoStartMultiThreading) { // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Tests are based on a list of messages, get the count now. @@ -738,9 +735,9 @@ TEST_F(NameChangeUDPSenderBasicTest, autoStartMultiThreading) { /// @brief Tests NameChangeUDPSender basic send with INADDR_ANY and port 0. TEST_F(NameChangeUDPSenderBasicTest, anyAddressSend) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOAddress any_address("0.0.0.0"); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOAddress any_address("0.0.0.0"); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Tests are based on a list of messages, get the count now. @@ -776,9 +773,9 @@ TEST_F(NameChangeUDPSenderBasicTest, anyAddressSendMultiThreading) { // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOAddress any_address("0.0.0.0"); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOAddress any_address("0.0.0.0"); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Tests are based on a list of messages, get the count now. @@ -811,9 +808,9 @@ TEST_F(NameChangeUDPSenderBasicTest, anyAddressSendMultiThreading) { /// @brief Test the NameChangeSender::assumeQueue method. TEST_F(NameChangeUDPSenderBasicTest, assumeQueue) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); + IOAddress ip_address(TEST_ADDRESS); uint32_t port = SENDER_PORT; - isc::asiolink::IOService io_service; + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; NameChangeRequestPtr ncr; @@ -883,9 +880,9 @@ TEST_F(NameChangeUDPSenderBasicTest, assumeQueueMultiThreading) { // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); + IOAddress ip_address(TEST_ADDRESS); uint32_t port = SENDER_PORT; - isc::asiolink::IOService io_service; + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; NameChangeRequestPtr ncr; @@ -957,20 +954,20 @@ class NameChangeUDPTest : public virtual ::testing::Test, NameChangeListener::RequestReceiveHandler, NameChangeSender::RequestSendHandler { public: - isc::asiolink::IOService io_service_; + IOServicePtr io_service_; NameChangeListener::Result recv_result_; NameChangeSender::Result send_result_; NameChangeListenerPtr listener_; NameChangeSenderPtr sender_; - isc::asiolink::IntervalTimer test_timer_; + IntervalTimer test_timer_; std::vector<NameChangeRequestPtr> sent_ncrs_; std::vector<NameChangeRequestPtr> received_ncrs_; NameChangeUDPTest() - : io_service_(), recv_result_(NameChangeListener::SUCCESS), + : io_service_(new IOService()), recv_result_(NameChangeListener::SUCCESS), send_result_(NameChangeSender::SUCCESS), test_timer_(io_service_) { - isc::asiolink::IOAddress addr(TEST_ADDRESS); + IOAddress addr(TEST_ADDRESS); // Create our listener instance. Note that reuse_address is true. listener_.reset( new NameChangeUDPListener(addr, LISTENER_PORT, FMT_JSON, @@ -1019,7 +1016,7 @@ public: /// /// This callback stops all running (hanging) tasks on IO service. void testTimeoutHandler() { - io_service_.stop(); + io_service_->stop(); FAIL() << "Test timeout hit."; } @@ -1098,7 +1095,7 @@ TEST_F(NameChangeUDPTest, roundTripTest) { // Execute callbacks until we have sent and received all of messages. while (sender_->getQueueSize() > 0 || (received_ncrs_.size() < num_msgs)) { - EXPECT_NO_THROW(io_service_.runOne()); + EXPECT_NO_THROW(io_service_->runOne()); } // Send queue should be empty. @@ -1116,7 +1113,7 @@ TEST_F(NameChangeUDPTest, roundTripTest) { EXPECT_FALSE(listener_->amListening()); // Verify that IO pending is false, after cancel event occurs. - EXPECT_NO_THROW(io_service_.runOne()); + EXPECT_NO_THROW(io_service_->runOne()); EXPECT_FALSE(listener_->isIoPending()); // Verify that we can gracefully stop sending. @@ -1152,7 +1149,7 @@ TEST_F(NameChangeUDPTest, roundTripTestMultiThreading) { // Execute callbacks until we have sent and received all of messages. while (sender_->getQueueSize() > 0 || (received_ncrs_.size() < num_msgs)) { - EXPECT_NO_THROW(io_service_.runOne()); + EXPECT_NO_THROW(io_service_->runOne()); } // Send queue should be empty. @@ -1171,7 +1168,7 @@ TEST_F(NameChangeUDPTest, roundTripTestMultiThreading) { EXPECT_FALSE(listener_->amListening()); // Verify that IO pending is false, after cancel event occurs. - EXPECT_NO_THROW(io_service_.runOne()); + EXPECT_NO_THROW(io_service_->runOne()); EXPECT_FALSE(listener_->isIoPending()); // Verify that we can gracefully stop sending. @@ -1182,8 +1179,8 @@ TEST_F(NameChangeUDPTest, roundTripTestMultiThreading) { // Tests error handling of a failure to mark the watch socket ready, when // sendRequest() is called. TEST_F(NameChangeUDPSenderBasicTest, watchClosedBeforeSendRequest) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Create the sender and put into send mode. @@ -1217,8 +1214,8 @@ TEST_F(NameChangeUDPSenderBasicTest, watchClosedBeforeSendRequestMultiThreading) // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Create the sender and put into send mode. @@ -1249,8 +1246,8 @@ TEST_F(NameChangeUDPSenderBasicTest, watchClosedBeforeSendRequestMultiThreading) // Tests error handling of a failure to mark the watch socket ready, when // sendNext() is called during completion handling. TEST_F(NameChangeUDPSenderBasicTest, watchClosedAfterSendRequest) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Create the sender and put into send mode. @@ -1291,8 +1288,8 @@ TEST_F(NameChangeUDPSenderBasicTest, watchClosedAfterSendRequestMultiThreading) // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Create the sender and put into send mode. @@ -1331,8 +1328,8 @@ TEST_F(NameChangeUDPSenderBasicTest, watchClosedAfterSendRequestMultiThreading) // Tests error handling of a failure to clear the watch socket during // completion handling. TEST_F(NameChangeUDPSenderBasicTest, watchSocketBadRead) { - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Create the sender and put into send mode. @@ -1382,8 +1379,8 @@ TEST_F(NameChangeUDPSenderBasicTest, watchSocketBadReadMultiThreading) { // Enable multi-threading MultiThreadingMgr::instance().setMode(true); - isc::asiolink::IOAddress ip_address(TEST_ADDRESS); - isc::asiolink::IOService io_service; + IOAddress ip_address(TEST_ADDRESS); + IOServicePtr io_service(new IOService()); SimpleSendHandler ncr_handler; // Create the sender and put into send mode. diff --git a/src/lib/dhcpsrv/d2_client_mgr.cc b/src/lib/dhcpsrv/d2_client_mgr.cc index 807f728d40..cb66a44446 100644 --- a/src/lib/dhcpsrv/d2_client_mgr.cc +++ b/src/lib/dhcpsrv/d2_client_mgr.cc @@ -259,14 +259,14 @@ D2ClientMgr::startSender(D2ClientErrorHandler error_handler) { // Create a our own service instance when we are not being multiplexed // into an external service.. private_io_service_.reset(new asiolink::IOService()); - startSender(error_handler, *private_io_service_); + startSender(error_handler, private_io_service_); LOG_INFO(dhcpsrv_logger, DHCPSRV_DHCP_DDNS_SENDER_STARTED) .arg(d2_client_config_->toText()); } void D2ClientMgr::startSender(D2ClientErrorHandler error_handler, - isc::asiolink::IOService& io_service) { + const isc::asiolink::IOServicePtr& io_service) { if (amSending()) { return; } diff --git a/src/lib/dhcpsrv/d2_client_mgr.h b/src/lib/dhcpsrv/d2_client_mgr.h index 65c034e235..356f47768b 100644 --- a/src/lib/dhcpsrv/d2_client_mgr.h +++ b/src/lib/dhcpsrv/d2_client_mgr.h @@ -287,7 +287,7 @@ public: /// @throw D2ClientError if sender instance is null. Underlying layer /// may throw NCRSenderExceptions exceptions. void startSender(D2ClientErrorHandler error_handler, - isc::asiolink::IOService& io_service); + const isc::asiolink::IOServicePtr& io_service); /// @brief Enables sending NameChangeRequests to kea-dhcp-ddns /// diff --git a/src/lib/dhcpsrv/tests/cfg_expiration_unittest.cc b/src/lib/dhcpsrv/tests/cfg_expiration_unittest.cc index ee5b3a76d8..067a697809 100644 --- a/src/lib/dhcpsrv/tests/cfg_expiration_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_expiration_unittest.cc @@ -322,7 +322,7 @@ public: /// /// @param timeout_ms Amount of time after which the method returns. void runTimersWithTimeout(const long timeout_ms) { - IntervalTimer timer(*io_service_); + IntervalTimer timer(io_service_); timer.setup([this]() { io_service_->stop(); }, timeout_ms, IntervalTimer::ONE_SHOT); diff --git a/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc b/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc index 5b10f964e3..b02d7841f4 100644 --- a/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc @@ -117,7 +117,7 @@ CfgIfaceTest::unicastOpen(const std::string& iface_name) const { void CfgIfaceTest::doWait(const long timeout) { - asiolink::IntervalTimer timer(*io_service_); + asiolink::IntervalTimer timer(io_service_); timer.setup([this]() { io_service_->stop(); }, timeout, asiolink::IntervalTimer::ONE_SHOT); diff --git a/src/lib/dhcpsrv/tests/d2_udp_unittest.cc b/src/lib/dhcpsrv/tests/d2_udp_unittest.cc index 106e7bc4d3..777f070ad8 100644 --- a/src/lib/dhcpsrv/tests/d2_udp_unittest.cc +++ b/src/lib/dhcpsrv/tests/d2_udp_unittest.cc @@ -21,6 +21,7 @@ #include <sys/select.h> using namespace std; +using namespace isc::asiolink; using namespace isc::dhcp; using namespace isc; namespace ph = std::placeholders; @@ -65,10 +66,9 @@ public: // Update the configuration with one that is enabled. D2ClientConfigPtr new_cfg; - isc::asiolink::IOAddress server_ip(server_address); - isc::asiolink::IOAddress sender_ip(server_ip.isV4() ? - D2ClientConfig::DFT_V4_SENDER_IP : - D2ClientConfig::DFT_V6_SENDER_IP); + IOAddress server_ip(server_address); + IOAddress sender_ip(server_ip.isV4() ? D2ClientConfig::DFT_V4_SENDER_IP : + D2ClientConfig::DFT_V6_SENDER_IP); ASSERT_NO_THROW(new_cfg.reset(new D2ClientConfig(true, server_ip, server_port, @@ -296,7 +296,7 @@ TEST_F(D2ClientMgrTest, udpSendExternalIOService) { enableDdns("127.0.0.1", 53001, dhcp_ddns::NCR_UDP); // Place sender in send mode using an external IO service. - asiolink::IOService io_service; + asiolink::IOServicePtr io_service(new IOService()); ASSERT_NO_THROW(startSender(getErrorHandler(), io_service)); // select_fd should evaluate to NOT ready to read. @@ -328,7 +328,7 @@ TEST_F(D2ClientMgrTest, udpSendExternalIOService6) { enableDdns("::1", 53001, dhcp_ddns::NCR_UDP); // Place sender in send mode using an external IO service. - asiolink::IOService io_service; + asiolink::IOServicePtr io_service(new IOService()); ASSERT_NO_THROW(startSender(getErrorHandler(), io_service)); // select_fd should evaluate to NOT ready to read. diff --git a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc index e9e7a2b72d..bca014b60e 100644 --- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc @@ -242,7 +242,7 @@ public: /// /// @param ms Duration in milliseconds. void setTestTime(const uint32_t ms) { - IntervalTimer timer(*io_service_); + IntervalTimer timer(io_service_); timer.setup([this]() { io_service_->stop(); }, ms, IntervalTimer::ONE_SHOT); @@ -260,8 +260,8 @@ public: bool waitForProcess(const Memfile_LeaseMgr& lease_mgr, const uint8_t timeout) { const uint32_t iterations_max = timeout * 1000; - IntervalTimer fast_path_timer(*io_service_); - IntervalTimer timer(*io_service_); + IntervalTimer fast_path_timer(io_service_); + IntervalTimer timer(io_service_); bool elapsed = false; timer.setup([&]() { io_service_->stop(); diff --git a/src/lib/dhcpsrv/tests/timer_mgr_unittest.cc b/src/lib/dhcpsrv/tests/timer_mgr_unittest.cc index 9dd1845d33..e76474a54d 100644 --- a/src/lib/dhcpsrv/tests/timer_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/timer_mgr_unittest.cc @@ -165,7 +165,7 @@ TimerMgrTest::registerTimer(const std::string& timer_name, const long timer_inte void TimerMgrTest::doWait(const long timeout, const bool /*call_receive*/) { - IntervalTimer timer(*io_service_); + IntervalTimer timer(io_service_); timer.setup([this]() { io_service_->stop(); }, timeout, IntervalTimer::ONE_SHOT); diff --git a/src/lib/dhcpsrv/timer_mgr.cc b/src/lib/dhcpsrv/timer_mgr.cc index a8d970a2ad..3fa1fd6560 100644 --- a/src/lib/dhcpsrv/timer_mgr.cc +++ b/src/lib/dhcpsrv/timer_mgr.cc @@ -58,7 +58,7 @@ struct TimerInfo { /// during the timer registration. /// @param interval Timer interval in milliseconds. /// @param mode Interval timer scheduling mode. - TimerInfo(asiolink::IOService& io_service, + TimerInfo(const asiolink::IOServicePtr& io_service, const asiolink::IntervalTimer::Callback& user_callback, const long interval, const asiolink::IntervalTimer::Mode& mode) @@ -274,7 +274,7 @@ TimerMgrImpl::registerTimerInternal(const std::string& timer_name, // Create a structure holding the configuration for the timer. It will // create the instance if the IntervalTimer. It will also hold the // callback, interval and scheduling mode parameters. - TimerInfoPtr timer_info(new TimerInfo(*io_service_, callback, + TimerInfoPtr timer_info(new TimerInfo(io_service_, callback, interval, scheduling_mode)); // Actually register the timer. diff --git a/src/lib/http/client.cc b/src/lib/http/client.cc index 1f139a3cd9..66ab6490e0 100644 --- a/src/lib/http/client.cc +++ b/src/lib/http/client.cc @@ -118,7 +118,7 @@ public: /// @param conn_pool Back pointer to the connection pool to which this /// connection belongs. /// @param url URL associated with this connection. - explicit Connection(IOService& io_service, + explicit Connection(const IOServicePtr& io_service, const TlsContextPtr& tls_context, const ConnectionPoolPtr& conn_pool, const Url& url); @@ -482,7 +482,7 @@ public: /// connections. /// @param max_url_connections maximum number of concurrent /// connections allowed per URL. - explicit ConnectionPool(IOService& io_service, size_t max_url_connections) + explicit ConnectionPool(const IOServicePtr& io_service, size_t max_url_connections) : io_service_(io_service), destinations_(), pool_mutex_(), max_url_connections_(max_url_connections) { } @@ -515,8 +515,8 @@ public: /// should be processed. void postProcessNextRequest(const Url& url, const TlsContextPtr& tls_context) { - io_service_.post(std::bind(&ConnectionPool::processNextRequest, - shared_from_this(), url, tls_context)); + io_service_->post(std::bind(&ConnectionPool::processNextRequest, + shared_from_this(), url, tls_context)); } /// @brief Queue next request for sending to the server. @@ -1106,7 +1106,7 @@ private: } /// @brief A reference to the IOService that drives socket IO. - IOService& io_service_; + IOServicePtr io_service_; /// @brief Map of Destinations by URL and TLS context. std::map<DestinationDescriptor, DestinationPtr> destinations_; @@ -1118,7 +1118,7 @@ private: size_t max_url_connections_; }; -Connection::Connection(IOService& io_service, +Connection::Connection(const IOServicePtr& io_service, const TlsContextPtr& tls_context, const ConnectionPoolPtr& conn_pool, const Url& url) @@ -1783,7 +1783,7 @@ public: /// the thread pool threads will be created and started, with the /// operational state being RUNNING. Applicable only when thread-pool size /// is greater than zero. - HttpClientImpl(IOService& io_service, size_t thread_pool_size = 0, + HttpClientImpl(const IOServicePtr& io_service, size_t thread_pool_size = 0, bool defer_thread_start = false) : thread_pool_size_(thread_pool_size), thread_pool_() { if (thread_pool_size_ > 0) { @@ -1792,7 +1792,7 @@ public: // Create the connection pool. Note that we use the thread_pool_size // as the maximum connections per URL value. - conn_pool_.reset(new ConnectionPool(*thread_io_service_, thread_pool_size_)); + conn_pool_.reset(new ConnectionPool(thread_io_service_, thread_pool_size_)); // Create the thread pool. thread_pool_.reset(new IoServiceThreadPool(thread_io_service_, thread_pool_size_, @@ -1947,7 +1947,7 @@ private: IoServiceThreadPoolPtr thread_pool_; }; -HttpClient::HttpClient(IOService& io_service, bool multi_threading_enabled, +HttpClient::HttpClient(const IOServicePtr& io_service, bool multi_threading_enabled, size_t thread_pool_size, bool defer_thread_start) { if (!multi_threading_enabled && thread_pool_size) { isc_throw(InvalidOperation, diff --git a/src/lib/http/client.h b/src/lib/http/client.h index bea90576b7..7215d44520 100644 --- a/src/lib/http/client.h +++ b/src/lib/http/client.h @@ -145,7 +145,7 @@ public: /// the thread pool threads will be created and started, with the /// operational state being RUNNING. Applicable only when thread-pool size /// is greater than zero. - explicit HttpClient(asiolink::IOService& io_service, + explicit HttpClient(const asiolink::IOServicePtr& io_service, bool multi_threading_enabled, size_t thread_pool_size = 0, bool defer_thread_start = false); diff --git a/src/lib/http/connection.cc b/src/lib/http/connection.cc index b1e57bddbe..880d87df0c 100644 --- a/src/lib/http/connection.cc +++ b/src/lib/http/connection.cc @@ -62,7 +62,7 @@ SocketCallback::operator()(boost::system::error_code ec, size_t length) { callback_(ec, length); } -HttpConnection::HttpConnection(asiolink::IOService& io_service, +HttpConnection::HttpConnection(const asiolink::IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, HttpConnectionPool& connection_pool, diff --git a/src/lib/http/connection.h b/src/lib/http/connection.h index 70109de660..4597ca4b48 100644 --- a/src/lib/http/connection.h +++ b/src/lib/http/connection.h @@ -241,7 +241,7 @@ public: /// @param request_timeout Configured timeout for a HTTP request. /// @param idle_timeout Timeout after which persistent HTTP connection is /// closed by the server. - HttpConnection(asiolink::IOService& io_service, + HttpConnection(const asiolink::IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const asiolink::TlsContextPtr& tls_context, HttpConnectionPool& connection_pool, diff --git a/src/lib/http/listener.cc b/src/lib/http/listener.cc index 2e6d2e1c62..14103de588 100644 --- a/src/lib/http/listener.cc +++ b/src/lib/http/listener.cc @@ -15,7 +15,7 @@ using namespace isc::asiolink; namespace isc { namespace http { -HttpListener::HttpListener(IOService& io_service, +HttpListener::HttpListener(const IOServicePtr& io_service, const asiolink::IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, diff --git a/src/lib/http/listener.h b/src/lib/http/listener.h index 8965f29f1c..b6f49f4c97 100644 --- a/src/lib/http/listener.h +++ b/src/lib/http/listener.h @@ -96,7 +96,7 @@ public: /// /// @throw HttpListenerError when any of the specified parameters is /// invalid. - HttpListener(asiolink::IOService& io_service, + HttpListener(const asiolink::IOServicePtr& io_service, const asiolink::IOAddress& server_address, const unsigned short server_port, const asiolink::TlsContextPtr& tls_context, diff --git a/src/lib/http/listener_impl.cc b/src/lib/http/listener_impl.cc index fdcbdd0c8b..80db799405 100644 --- a/src/lib/http/listener_impl.cc +++ b/src/lib/http/listener_impl.cc @@ -16,7 +16,7 @@ namespace ph = std::placeholders; namespace isc { namespace http { -HttpListenerImpl::HttpListenerImpl(IOService& io_service, +HttpListenerImpl::HttpListenerImpl(const IOServicePtr& io_service, const asiolink::IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, diff --git a/src/lib/http/listener_impl.h b/src/lib/http/listener_impl.h index 4ad1960844..0ac3de7c14 100644 --- a/src/lib/http/listener_impl.h +++ b/src/lib/http/listener_impl.h @@ -44,7 +44,7 @@ public: /// /// @throw HttpListenerError when any of the specified parameters is /// invalid. - HttpListenerImpl(asiolink::IOService& io_service, + HttpListenerImpl(const asiolink::IOServicePtr& io_service, const asiolink::IOAddress& server_address, const unsigned short server_port, const asiolink::TlsContextPtr& tls_context, @@ -102,8 +102,8 @@ protected: virtual HttpConnectionPtr createConnection(const HttpResponseCreatorPtr& response_creator, const HttpAcceptorCallback& callback); - /// @brief Reference to the IO service. - asiolink::IOService& io_service_; + /// @brief Pointer to the IO service. + asiolink::IOServicePtr io_service_; /// @brief TLS context. asiolink::TlsContextPtr tls_context_; diff --git a/src/lib/http/tests/client_mt_unittests.cc b/src/lib/http/tests/client_mt_unittests.cc index b68c88572b..bb39f68e14 100644 --- a/src/lib/http/tests/client_mt_unittests.cc +++ b/src/lib/http/tests/client_mt_unittests.cc @@ -196,7 +196,7 @@ public: /// @brief Constructor. MultiThreadingHttpClientTest() - : io_service_(), client_(), listener_(), factory_(), listeners_(), factories_(), + : io_service_(new IOService()), client_(), listener_(), factory_(), listeners_(), factories_(), test_timer_(io_service_), num_threads_(0), num_batches_(0), num_listeners_(0), expected_requests_(0), num_in_progress_(0), num_finished_(0), paused_(false), pause_cnt_(0) { @@ -229,7 +229,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Runs the test's IOService until the desired number of requests @@ -237,10 +237,10 @@ public: void runIOService(size_t request_limit) { while (getRRCount() < request_limit) { // Always call reset() before we call run(); - io_service_.restart(); + io_service_->restart(); // Run until a client stops the service. - io_service_.run(); + io_service_->run(); } } @@ -354,7 +354,7 @@ public: num_in_progress_ = 0; test_cv_.notify_all(); // Stop the test's IOService. - io_service_.stop(); + io_service_->stop(); } else { // I'm done but others aren't wait here. bool ret = test_cv_.wait_for(lck, std::chrono::seconds(10), @@ -410,8 +410,8 @@ public: std::unique_lock<std::mutex> lck(test_mutex_); clientRRs_.push_back(clientRR); ++num_finished_; - if ((num_finished_ >= expected_requests_) && !io_service_.stopped()) { - io_service_.stop(); + if ((num_finished_ >= expected_requests_) && !io_service_->stopped()) { + io_service_->stop(); } } @@ -773,7 +773,7 @@ public: } /// @brief IO service used in the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Instance of the client used in the tests. HttpClientPtr client_; diff --git a/src/lib/http/tests/connection_pool_unittests.cc b/src/lib/http/tests/connection_pool_unittests.cc index b8337c3d36..d6267a0e14 100644 --- a/src/lib/http/tests/connection_pool_unittests.cc +++ b/src/lib/http/tests/connection_pool_unittests.cc @@ -105,7 +105,7 @@ public: /// @brief Constructor. HttpConnectionPoolTest() - : io_service_(), + : io_service_(new IOService()), acceptor_(new HttpAcceptor(io_service_)), connection_pool_(), response_creator_(new TestHttpResponseCreator()) { @@ -216,7 +216,7 @@ public: ASSERT_EQ(1, pool.hasConnection(conn1)); } - IOService io_service_; ///< IO service. + IOServicePtr io_service_; ///< IO service. HttpAcceptorPtr acceptor_; ///< Test acceptor. HttpConnectionPool connection_pool_; ///< Test connection pool. HttpResponseCreatorPtr response_creator_; ///< Test response creator. diff --git a/src/lib/http/tests/server_client_unittests.cc b/src/lib/http/tests/server_client_unittests.cc index 47a23625db..6572e8381a 100644 --- a/src/lib/http/tests/server_client_unittests.cc +++ b/src/lib/http/tests/server_client_unittests.cc @@ -208,7 +208,7 @@ template<typename HttpConnectionType> class HttpListenerImplCustom : public HttpListenerImpl { public: - HttpListenerImplCustom(IOService& io_service, + HttpListenerImplCustom(const IOServicePtr& io_service, const IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, @@ -270,7 +270,7 @@ public: /// /// @throw HttpListenerError when any of the specified parameters is /// invalid. - HttpListenerCustom(IOService& io_service, + HttpListenerCustom(const IOServicePtr& io_service, const IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, @@ -308,7 +308,7 @@ public: /// @param request_timeout Configured timeout for a HTTP request. /// @param idle_timeout Timeout after which persistent HTTP connection is /// closed by the server. - HttpConnectionLongWriteBuffer(IOService& io_service, + HttpConnectionLongWriteBuffer(const IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, HttpConnectionPool& connection_pool, @@ -355,7 +355,7 @@ public: /// @param request_timeout Configured timeout for a HTTP request. /// @param idle_timeout Timeout after which persistent HTTP connection is /// closed by the server. - HttpConnectionTransactionChange(IOService& io_service, + HttpConnectionTransactionChange(const IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, HttpConnectionPool& connection_pool, @@ -399,7 +399,7 @@ public: /// /// Starts test timer which detects timeouts. HttpListenerTest() - : io_service_(), factory_(new TestHttpResponseCreatorFactory()), + : io_service_(new IOService()), factory_(new TestHttpResponseCreatorFactory()), test_timer_(io_service_), run_io_service_timer_(io_service_), clients_() { test_timer_.setup(std::bind(&HttpListenerTest::timeoutHandler, this, true), TEST_TIMEOUT, IntervalTimer::ONE_SHOT); @@ -435,7 +435,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Runs IO service with optional timeout. @@ -443,16 +443,16 @@ public: /// @param timeout Optional value specifying for how long the io service /// should be ran. void runIOService(long timeout = 0) { - io_service_.restart(); + io_service_->restart(); if (timeout > 0) { run_io_service_timer_.setup(std::bind(&HttpListenerTest::timeoutHandler, this, false), timeout, IntervalTimer::ONE_SHOT); } - io_service_.run(); - io_service_.restart(); - io_service_.poll(); + io_service_->run(); + io_service_->restart(); + io_service_->poll(); } /// @brief Returns HTTP OK response expected by unit tests. @@ -554,7 +554,7 @@ public: } /// @brief IO service used in the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Pointer to the response creator factory. HttpResponseCreatorFactoryPtr factory_; @@ -593,7 +593,7 @@ TEST_F(HttpListenerTest, listen) { EXPECT_EQ(httpOk(HttpVersion::HTTP_11()), client->getResponse()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } @@ -645,7 +645,7 @@ TEST_F(HttpListenerTest, keepAlive) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that persistent HTTP connection is established by default @@ -694,7 +694,7 @@ TEST_F(HttpListenerTest, persistentConnection) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that "keep-alive" connection is closed by the server after @@ -753,7 +753,7 @@ TEST_F(HttpListenerTest, keepAliveTimeout) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that persistent connection is closed by the server after @@ -810,7 +810,7 @@ TEST_F(HttpListenerTest, persistentConnectionTimeout) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that HTTP/1.1 connection remains open even if there is an @@ -863,7 +863,7 @@ TEST_F(HttpListenerTest, persistentConnectionBadBody) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that the HTTP listener can't be started twice. @@ -938,7 +938,7 @@ TEST_F(HttpListenerTest, invalidIdleTimeout) { // This test verifies that listener can't be bound to the port to which // other server is bound. TEST_F(HttpListenerTest, addressInUse) { - tcp::acceptor acceptor(io_service_.getInternalIOService()); + tcp::acceptor acceptor(io_service_->getInternalIOService()); // Use other port than SERVER_PORT to make sure that this TCP connection // doesn't affect subsequent tests. tcp::endpoint endpoint(address::from_string(SERVER_ADDRESS), @@ -1021,7 +1021,7 @@ public: listener_.stop(); listener2_.stop(); listener3_.stop(); - io_service_.poll(); + io_service_->poll(); MultiThreadingMgr::instance().setMode(false); } @@ -1076,7 +1076,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_FALSE(ec); })); @@ -1090,7 +1090,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_FALSE(ec); })); @@ -1137,7 +1137,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_FALSE(ec); })); @@ -1151,7 +1151,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_FALSE(ec); })); @@ -1193,7 +1193,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -1209,7 +1209,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -1250,7 +1250,7 @@ public: request1, response1, [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); EXPECT_FALSE(ec); })); @@ -1273,7 +1273,7 @@ public: request2, response2, [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); EXPECT_FALSE(ec); })); @@ -1306,7 +1306,7 @@ public: [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); // The server should have returned an IO error. EXPECT_TRUE(ec); })); @@ -1339,7 +1339,7 @@ public: [this](const boost::system::error_code& ec, const HttpResponsePtr& response, const std::string& parsing_error) { - io_service_.stop(); + io_service_->stop(); // There should be no IO error (answer from the server is received). EXPECT_FALSE(ec); // The response object is NULL because it couldn't be finalized. @@ -1380,7 +1380,7 @@ public: const HttpResponsePtr& response, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } // In this particular case we know exactly the type of the // IO error returned, because the client explicitly sets this @@ -1408,7 +1408,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } })); @@ -1439,7 +1439,7 @@ public: const HttpResponsePtr& response, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } // In this particular case we know exactly the type of the // IO error returned, because the client explicitly sets this @@ -1466,7 +1466,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } })); @@ -1560,7 +1560,7 @@ public: [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); // Everything should be ok. EXPECT_TRUE(ec.value() == 0); @@ -1596,7 +1596,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_FALSE(ec); @@ -1616,7 +1616,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_FALSE(ec); }, @@ -1691,7 +1691,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num == 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_EQ(1, monitor.connect_cnt_); // We should have 1 connect. @@ -1751,7 +1751,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num == 1) { - io_service_.stop(); + io_service_->stop(); } EXPECT_EQ(2, monitor.connect_cnt_); // We should have 1 connect. diff --git a/src/lib/http/tests/test_http_client.h b/src/lib/http/tests/test_http_client.h index 3b164756f1..55568a9ad9 100644 --- a/src/lib/http/tests/test_http_client.h +++ b/src/lib/http/tests/test_http_client.h @@ -31,10 +31,10 @@ public: /// @param io_service IO service to be stopped on error or completion. /// @param server_address string containing the IP address of the server. /// @param port port number of the server. - explicit TestHttpClient(IOService& io_service, + explicit TestHttpClient(const IOServicePtr& io_service, const std::string& server_address = "127.0.0.1", uint16_t port = 18123) - : io_service_(io_service.getInternalIOService()), socket_(io_service_), + : io_service_(io_service), socket_(io_service_->getInternalIOService()), buf_(), response_(), server_address_(server_address), server_port_(port), receive_done_(false) { } @@ -65,7 +65,7 @@ public: if (ec.value() != boost::asio::error::in_progress) { ADD_FAILURE() << "error occurred while connecting: " << ec.message(); - io_service_.stop(); + io_service_->stop(); return; } } @@ -100,7 +100,7 @@ public: } else { ADD_FAILURE() << "error occurred while connecting: " << ec.message(); - io_service_.stop(); + io_service_->stop(); return; } } @@ -143,7 +143,7 @@ public: // Error occurred, bail... ADD_FAILURE() << "error occurred while receiving HTTP" " response from the server: " << ec.message(); - io_service_.stop(); + io_service_->stop(); } } @@ -156,7 +156,7 @@ public: // expecting. if (response_.find("\r\n\r\n", 0) != std::string::npos) { receive_done_ = true; - io_service_.stop(); + io_service_->stop(); } else { receivePartialResponse(); } @@ -245,8 +245,8 @@ public: private: - /// @brief Holds reference to the IO service. - boost::asio::io_service& io_service_; + /// @brief Holds pointer to the IO service. + isc::asiolink::IOServicePtr io_service_; /// @brief A socket used for the connection. boost::asio::ip::tcp::socket socket_; diff --git a/src/lib/http/tests/tls_client_unittests.cc b/src/lib/http/tests/tls_client_unittests.cc index 8874214421..4309a87456 100644 --- a/src/lib/http/tests/tls_client_unittests.cc +++ b/src/lib/http/tests/tls_client_unittests.cc @@ -227,7 +227,7 @@ public: /// /// Starts test timer which detects timeouts. HttpListenerTest() - : io_service_(), factory_(new TestHttpResponseCreatorFactory()), + : io_service_(new IOService()), factory_(new TestHttpResponseCreatorFactory()), test_timer_(io_service_), run_io_service_timer_(io_service_) { test_timer_.setup(std::bind(&HttpListenerTest::timeoutHandler, this, true), TEST_TIMEOUT, IntervalTimer::ONE_SHOT); @@ -242,7 +242,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Runs IO service with optional timeout. @@ -250,20 +250,20 @@ public: /// @param timeout Optional value specifying for how long the io service /// should be ran (ms). void runIOService(long timeout = 0) { - io_service_.restart(); + io_service_->restart(); if (timeout > 0) { run_io_service_timer_.setup(std::bind(&HttpListenerTest::timeoutHandler, this, false), timeout, IntervalTimer::ONE_SHOT); } - io_service_.run(); - io_service_.restart(); - io_service_.poll(); + io_service_->run(); + io_service_->restart(); + io_service_->poll(); } /// @brief IO service used in the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Pointer to the response creator factory. HttpResponseCreatorFactoryPtr factory_; @@ -315,7 +315,7 @@ public: listener_->stop(); listener2_->stop(); listener3_->stop(); - io_service_.poll(); + io_service_->poll(); MultiThreadingMgr::instance().setMode(false); HttpRequest::recordSubject_ = false; HttpRequest::recordIssuer_ = false; @@ -372,7 +372,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -388,7 +388,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -437,7 +437,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -453,7 +453,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -501,7 +501,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -517,7 +517,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -562,7 +562,7 @@ public: request1, response1, [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); } @@ -587,7 +587,7 @@ public: request2, response2, [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); } @@ -622,7 +622,7 @@ public: [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); // The server should have returned an IO error. if (!ec) { ADD_FAILURE() << "asyncSendRequest didn't fail"; @@ -657,7 +657,7 @@ public: [this](const boost::system::error_code& ec, const HttpResponsePtr& response, const std::string& parsing_error) { - io_service_.stop(); + io_service_->stop(); // There should be no IO error (answer from the server is received). if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -700,7 +700,7 @@ public: const HttpResponsePtr& response, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } // In this particular case we know exactly the type of the // IO error returned, because the client explicitly sets this @@ -728,7 +728,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } })); @@ -759,7 +759,7 @@ public: const HttpResponsePtr& response, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } // In this particular case we know exactly the type of the // IO error returned, because the client explicitly sets this @@ -786,7 +786,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++cb_num > 1) { - io_service_.stop(); + io_service_->stop(); } })); @@ -880,7 +880,7 @@ public: [this](const boost::system::error_code& ec, const HttpResponsePtr&, const std::string&) { - io_service_.stop(); + io_service_->stop(); // Everything should be ok. if (ec) { @@ -918,7 +918,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { @@ -940,7 +940,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num > 1) { - io_service_.stop(); + io_service_->stop(); } if (ec) { ADD_FAILURE() << "asyncSendRequest failed: " << ec.message(); @@ -1017,7 +1017,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num == 1) { - io_service_.stop(); + io_service_->stop(); } // We should have 1 connect. @@ -1084,7 +1084,7 @@ public: const HttpResponsePtr&, const std::string&) { if (++resp_num == 1) { - io_service_.stop(); + io_service_->stop(); } // We should have 1 connect. diff --git a/src/lib/http/tests/tls_server_unittests.cc b/src/lib/http/tests/tls_server_unittests.cc index f5c592ce6b..db788ce56f 100644 --- a/src/lib/http/tests/tls_server_unittests.cc +++ b/src/lib/http/tests/tls_server_unittests.cc @@ -212,7 +212,7 @@ template<typename HttpConnectionType> class HttpListenerImplCustom : public HttpListenerImpl { public: - HttpListenerImplCustom(IOService& io_service, + HttpListenerImplCustom(const IOServicePtr& io_service, const IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, @@ -276,7 +276,7 @@ public: /// /// @throw HttpListenerError when any of the specified parameters is /// invalid. - HttpListenerCustom(IOService& io_service, + HttpListenerCustom(const IOServicePtr& io_service, const IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, @@ -314,7 +314,7 @@ public: /// @param request_timeout Configured timeout for a HTTP request. /// @param idle_timeout Timeout after which persistent HTTP connection is /// closed by the server. - HttpConnectionLongWriteBuffer(IOService& io_service, + HttpConnectionLongWriteBuffer(const IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, HttpConnectionPool& connection_pool, @@ -361,7 +361,7 @@ public: /// @param request_timeout Configured timeout for a HTTP request. /// @param idle_timeout Timeout after which persistent HTTP connection is /// closed by the server. - HttpConnectionTransactionChange(IOService& io_service, + HttpConnectionTransactionChange(const IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, HttpConnectionPool& connection_pool, @@ -406,10 +406,9 @@ public: /// /// @param io_service IO service to be stopped on error. /// @param tls_context TLS context. - TestHttpClient(IOService& io_service, TlsContextPtr tls_context) - : io_service_(io_service.getInternalIOService()), - stream_(io_service_, tls_context->getContext()), - buf_(), response_() { + TestHttpClient(const IOServicePtr& io_service, TlsContextPtr tls_context) + : io_service_(io_service), stream_(io_service_->getInternalIOService(), + tls_context->getContext()), buf_(), response_() { } /// @brief Destructor. @@ -438,7 +437,7 @@ public: if (ec.value() != boost::asio::error::in_progress) { ADD_FAILURE() << "error occurred while connecting: " << ec.message(); - io_service_.stop(); + io_service_->stop(); return; } } @@ -447,7 +446,7 @@ public: if (ec) { ADD_FAILURE() << "error occurred during handshake: " << ec.message(); - io_service_.stop(); + io_service_->stop(); return; } sendRequest(request); @@ -483,7 +482,7 @@ public: } else { ADD_FAILURE() << "error occurred while connecting: " << ec.message(); - io_service_.stop(); + io_service_->stop(); return; } } @@ -526,7 +525,7 @@ public: // Error occurred, bail... ADD_FAILURE() << "error occurred while receiving HTTP" " response from the server: " << ec.message(); - io_service_.stop(); + io_service_->stop(); } } @@ -538,7 +537,7 @@ public: // Two consecutive new lines end the part of the response we're // expecting. if (response_.find("\r\n\r\n", 0) != std::string::npos) { - io_service_.stop(); + io_service_->stop(); } else { receivePartialResponse(); @@ -620,8 +619,8 @@ public: private: - /// @brief Holds reference to the IO service. - boost::asio::io_service& io_service_; + /// @brief Holds pointer to the IO service. + isc::asiolink::IOServicePtr io_service_; /// @brief A socket used for the connection. TlsStreamImpl stream_; @@ -644,7 +643,7 @@ public: /// /// Starts test timer which detects timeouts. HttpsListenerTest() - : io_service_(), factory_(new TestHttpResponseCreatorFactory()), + : io_service_(new IOService()), factory_(new TestHttpResponseCreatorFactory()), test_timer_(io_service_), run_io_service_timer_(io_service_), clients_(), server_context_(), client_context_() { configServer(server_context_); @@ -684,7 +683,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Runs IO service with optional timeout. @@ -692,16 +691,16 @@ public: /// @param timeout Optional value specifying for how long the io service /// should be ran. void runIOService(long timeout = 0) { - io_service_.restart(); + io_service_->restart(); if (timeout > 0) { run_io_service_timer_.setup(std::bind(&HttpsListenerTest::timeoutHandler, this, false), timeout, IntervalTimer::ONE_SHOT); } - io_service_.run(); - io_service_.restart(); - io_service_.poll(); + io_service_->run(); + io_service_->restart(); + io_service_->poll(); } /// @brief Returns HTTP OK response expected by unit tests. @@ -803,7 +802,7 @@ public: } /// @brief IO service used in the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Pointer to the response creator factory. HttpResponseCreatorFactoryPtr factory_; @@ -848,7 +847,7 @@ TEST_F(HttpsListenerTest, listen) { EXPECT_EQ(httpOk(HttpVersion::HTTP_11()), client->getResponse()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } @@ -900,7 +899,7 @@ TEST_F(HttpsListenerTest, keepAlive) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that persistent HTTP connection is established by default @@ -949,7 +948,7 @@ TEST_F(HttpsListenerTest, persistentConnection) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that "keep-alive" connection is closed by the server after @@ -1008,7 +1007,7 @@ TEST_F(HttpsListenerTest, keepAliveTimeout) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that persistent connection is closed by the server after @@ -1065,7 +1064,7 @@ TEST_F(HttpsListenerTest, persistentConnectionTimeout) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that HTTP/1.1 connection remains open even if there is an @@ -1118,7 +1117,7 @@ TEST_F(HttpsListenerTest, persistentConnectionBadBody) { EXPECT_TRUE(client->isConnectionClosed()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that the HTTP listener can't be started twice. @@ -1193,7 +1192,7 @@ TEST_F(HttpsListenerTest, invalidIdleTimeout) { // This test verifies that listener can't be bound to the port to which // other server is bound. TEST_F(HttpsListenerTest, addressInUse) { - tcp::acceptor acceptor(io_service_.getInternalIOService()); + tcp::acceptor acceptor(io_service_->getInternalIOService()); // Use other port than SERVER_PORT to make sure that this TCP connection // doesn't affect subsequent tests. tcp::endpoint endpoint(address::from_string(SERVER_ADDRESS), diff --git a/src/lib/process/tests/d_controller_unittests.cc b/src/lib/process/tests/d_controller_unittests.cc index c927b7521b..e52cbab9db 100644 --- a/src/lib/process/tests/d_controller_unittests.cc +++ b/src/lib/process/tests/d_controller_unittests.cc @@ -249,7 +249,7 @@ TEST_F(DStubControllerTest, missingConfigFileArgument) { TEST_F(DStubControllerTest, launchRuntimeError) { // Use an asiolink IntervalTimer and callback to generate the // shutdown invocation. (Note IntervalTimer setup is in milliseconds). - isc::asiolink::IntervalTimer timer(*getIOService()); + isc::asiolink::IntervalTimer timer(getIOService()); timer.setup(genFatalErrorCallback, 2000); // Write the valid, empty, config and then run launch() for 5000 ms @@ -337,9 +337,9 @@ TEST_F(DStubControllerTest, ioSignals) { controller_->recordSignalOnly(true); // Setup to raise SIGHUP in 10 ms. - TimedSignal sighup(*getIOService(), SIGHUP, 10); - TimedSignal sigint(*getIOService(), SIGINT, 100); - TimedSignal sigterm(*getIOService(), SIGTERM, 200); + TimedSignal sighup(getIOService(), SIGHUP, 10); + TimedSignal sigint(getIOService(), SIGINT, 100); + TimedSignal sigterm(getIOService(), SIGTERM, 200); // Write the valid, empty, config and then run launch() for 500 ms time_duration elapsed_time; @@ -362,7 +362,7 @@ TEST_F(DStubControllerTest, invalidConfigReload) { scheduleTimedWrite("{ \"string_test\": BOGUS JSON }", 100); // Setup to raise SIGHUP in 200 ms. - TimedSignal sighup(*getIOService(), SIGHUP, 200); + TimedSignal sighup(getIOService(), SIGHUP, 200); // Write the config and then run launch() for 500 ms // After startup, which will load the initial configuration this enters @@ -383,7 +383,7 @@ TEST_F(DStubControllerTest, alternateParsing) { controller_->useAlternateParser(true); // Setup to raise SIGHUP in 200 ms. - TimedSignal sighup(*getIOService(), SIGHUP, 200); + TimedSignal sighup(getIOService(), SIGHUP, 200); // Write the config and then run launch() for 500 ms // After startup, which will load the initial configuration this enters @@ -406,8 +406,8 @@ TEST_F(DStubControllerTest, validConfigReload) { scheduleTimedWrite("{ \"string_test\": \"second value\" }", 100); // Setup to raise SIGHUP in 200 ms and another at 400 ms. - TimedSignal sighup(*getIOService(), SIGHUP, 200); - TimedSignal sighup2(*getIOService(), SIGHUP, 400); + TimedSignal sighup(getIOService(), SIGHUP, 200); + TimedSignal sighup2(getIOService(), SIGHUP, 400); // Write the config and then run launch() for 800 ms time_duration elapsed_time; @@ -423,7 +423,7 @@ TEST_F(DStubControllerTest, validConfigReload) { // Tests that the SIGINT triggers a normal shutdown. TEST_F(DStubControllerTest, sigintShutdown) { // Setup to raise SIGHUP in 1 ms. - TimedSignal sighup(*getIOService(), SIGINT, 1); + TimedSignal sighup(getIOService(), SIGINT, 1); // Write the config and then run launch() for 1000 ms time_duration elapsed_time; @@ -452,7 +452,7 @@ TEST_F(DStubControllerTest, getVersion) { // Tests that the SIGTERM triggers a normal shutdown. TEST_F(DStubControllerTest, sigtermShutdown) { // Setup to raise SIGHUP in 1 ms. - TimedSignal sighup(*getIOService(), SIGTERM, 1); + TimedSignal sighup(getIOService(), SIGTERM, 1); // Write the config and then run launch() for 1000 ms time_duration elapsed_time; diff --git a/src/lib/process/testutils/d_test_stubs.cc b/src/lib/process/testutils/d_test_stubs.cc index ec9eb3b1b8..c9f23598b4 100644 --- a/src/lib/process/testutils/d_test_stubs.cc +++ b/src/lib/process/testutils/d_test_stubs.cc @@ -194,7 +194,7 @@ void DControllerTest::scheduleTimedWrite(const std::string& config, int write_time_ms) { new_cfg_content_ = config; - write_timer_.reset(new asiolink::IntervalTimer(*getIOService())); + write_timer_.reset(new asiolink::IntervalTimer(getIOService())); write_timer_->setup(std::bind(&DControllerTest::timedWriteCallback, this), write_time_ms, asiolink::IntervalTimer::ONE_SHOT); } @@ -206,7 +206,7 @@ DControllerTest::runWithConfig(const std::string& config, int run_time_ms, writeFile(config); // Shutdown (without error) after runtime. - isc::asiolink::IntervalTimer timer(*getIOService()); + isc::asiolink::IntervalTimer timer(getIOService()); timer.setup(genShutdownCallback, run_time_ms); // Record start time, and invoke launch(). @@ -236,7 +236,7 @@ DControllerTest::runWithConfig(const std::string& config, int run_time_ms, writeFile(config); // Shutdown (without error) after runtime. - isc::asiolink::IntervalTimer timer(*getIOService()); + isc::asiolink::IntervalTimer timer(getIOService()); timer.setup([&] { callback(); genShutdownCallback(); }, run_time_ms); // Record start time, and invoke launch(). diff --git a/src/lib/tcp/mt_tcp_listener_mgr.cc b/src/lib/tcp/mt_tcp_listener_mgr.cc index a8afa6d965..aca4f1d650 100644 --- a/src/lib/tcp/mt_tcp_listener_mgr.cc +++ b/src/lib/tcp/mt_tcp_listener_mgr.cc @@ -57,7 +57,7 @@ MtTcpListenerMgr::start() { thread_io_service_.reset(new IOService()); // Create a new TCPListener derivation using the factory. - tcp_listener_ = listener_factory_(*thread_io_service_, + tcp_listener_ = listener_factory_(thread_io_service_, address_, port_, tls_context_, diff --git a/src/lib/tcp/mt_tcp_listener_mgr.h b/src/lib/tcp/mt_tcp_listener_mgr.h index 6fc2600bb7..cba368a5fe 100644 --- a/src/lib/tcp/mt_tcp_listener_mgr.h +++ b/src/lib/tcp/mt_tcp_listener_mgr.h @@ -22,7 +22,7 @@ const long TCP_IDLE_CONNECTION_TIMEOUT = 300 * 1000; /// @brief Defines a factory function for creating TcpListeners. typedef std::function< - TcpListenerPtr(asiolink::IOService& io_service, + TcpListenerPtr(const asiolink::IOServicePtr& io_service, const asiolink::IOAddress& server_address, const unsigned short server_port, const asiolink::TlsContextPtr& tls_context, diff --git a/src/lib/tcp/tcp_connection.cc b/src/lib/tcp/tcp_connection.cc index d0e5393048..0445b35a90 100644 --- a/src/lib/tcp/tcp_connection.cc +++ b/src/lib/tcp/tcp_connection.cc @@ -48,7 +48,7 @@ SocketCallback::operator()(boost::system::error_code ec, size_t length) { callback_(ec, length); } -TcpConnection::TcpConnection(asiolink::IOService& io_service, +TcpConnection::TcpConnection(const asiolink::IOServicePtr& io_service, const TcpConnectionAcceptorPtr& acceptor, const TlsContextPtr& tls_context, TcpConnectionPool& connection_pool, @@ -56,7 +56,8 @@ TcpConnection::TcpConnection(asiolink::IOService& io_service, const TcpConnectionFilterCallback& connection_filter, const long idle_timeout, const size_t read_max /* = 32768 */) - : tls_context_(tls_context), + : io_service_(io_service), + tls_context_(tls_context), idle_timeout_(idle_timeout), idle_timer_(io_service), tcp_socket_(), diff --git a/src/lib/tcp/tcp_connection.h b/src/lib/tcp/tcp_connection.h index 8b71d077f8..7212d45c6a 100644 --- a/src/lib/tcp/tcp_connection.h +++ b/src/lib/tcp/tcp_connection.h @@ -208,7 +208,7 @@ public: /// @param idle_timeout Timeout after which a TCP connection is /// closed by the server. /// @param read_max maximum size of a single socket read. Defaults to 32K. - TcpConnection(asiolink::IOService& io_service, + TcpConnection(const asiolink::IOServicePtr& io_service, const TcpConnectionAcceptorPtr& acceptor, const asiolink::TlsContextPtr& tls_context, TcpConnectionPool& connection_pool, @@ -422,6 +422,9 @@ protected: return (input_buf_.size()); } + /// @brief The IO service used to handle events. + asiolink::IOServicePtr io_service_; + /// @brief TLS context. asiolink::TlsContextPtr tls_context_; diff --git a/src/lib/tcp/tcp_listener.cc b/src/lib/tcp/tcp_listener.cc index dd327ae994..b668ee39a6 100644 --- a/src/lib/tcp/tcp_listener.cc +++ b/src/lib/tcp/tcp_listener.cc @@ -14,7 +14,7 @@ namespace ph = std::placeholders; namespace isc { namespace tcp { -TcpListener::TcpListener(IOService& io_service, +TcpListener::TcpListener(const IOServicePtr& io_service, const IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, diff --git a/src/lib/tcp/tcp_listener.h b/src/lib/tcp/tcp_listener.h index 26f74c5ccd..0045c69a55 100644 --- a/src/lib/tcp/tcp_listener.h +++ b/src/lib/tcp/tcp_listener.h @@ -56,7 +56,7 @@ public: /// /// @throw TcpListenerError when any of the specified parameters is /// invalid. - TcpListener(asiolink::IOService& io_service, + TcpListener(const asiolink::IOServicePtr& io_service, const asiolink::IOAddress& server_address, const unsigned short server_port, const asiolink::TlsContextPtr& tls_context, @@ -135,8 +135,8 @@ protected: const TcpConnectionAcceptorCallback& acceptor_callback, const TcpConnectionFilterCallback& connection_filter); - /// @brief Reference to the IO service. - asiolink::IOService& io_service_; + /// @brief Pointer to the IO service. + asiolink::IOServicePtr io_service_; /// @brief TLS context. asiolink::TlsContextPtr tls_context_; diff --git a/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc b/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc index ab561c6010..782d7efb3e 100644 --- a/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc +++ b/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc @@ -51,7 +51,7 @@ public: /// /// Starts test timer which detects timeouts, and enables multi-threading mode. MtTcpListenerMgrTest() - : mt_listener_mgr_(), io_service_(), test_timer_(io_service_), + : mt_listener_mgr_(), io_service_(new IOService()), test_timer_(io_service_), run_io_service_timer_(io_service_), clients_(), num_threads_(), num_clients_(), num_in_progress_(0), num_finished_(0), chunk_size_(0), pause_cnt_(0), response_handler_(0) { @@ -112,7 +112,7 @@ public: } /// @brief TcpListener factory for MtTcpListener to instantiate new listeners. - TcpListenerPtr listenerFactory(asiolink::IOService& io_service, + TcpListenerPtr listenerFactory(const asiolink::IOServicePtr& io_service, const asiolink::IOAddress& server_address, const unsigned short server_port, const asiolink::TlsContextPtr& tls_context, @@ -135,7 +135,7 @@ public: /// /// @param fail_on_timeout Specifies if test failure should be reported. void clientDone() { - io_service_.stop(); + io_service_->stop(); } /// @brief Initiates a command via a new TCP client. @@ -206,7 +206,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Runs IO service with optional timeout. @@ -227,10 +227,10 @@ public: size_t num_done = 0; while (num_done != request_limit) { // Always call restart() before we call run(); - io_service_.restart(); + io_service_->restart(); // Run until a client stops the service. - io_service_.run(); + io_service_->run(); // If all the clients are done receiving, the test is done. num_done = 0; @@ -428,7 +428,7 @@ public: // Create an MtTcpListenerMgr with prescribed number of threads. createMtTcpListenerMgr(num_threads, std::bind(&MtTcpListenerMgrTest::synchronizedCommandHandler, - this, ph::_1)); + this, ph::_1)); // Start it and verify it is running. ASSERT_NO_THROW_LOG(mt_listener_mgr_->start()); @@ -703,7 +703,7 @@ public: MtTcpListenerMgrPtr mt_listener_mgr_; /// @brief IO service used in drive the test and test clients. - IOService io_service_; + IOServicePtr io_service_; /// @brief Asynchronous timer service to detect timeouts. IntervalTimer test_timer_; diff --git a/src/lib/tcp/tests/tcp_listener_unittests.cc b/src/lib/tcp/tests/tcp_listener_unittests.cc index cd91f20bfa..e2b906bae6 100644 --- a/src/lib/tcp/tests/tcp_listener_unittests.cc +++ b/src/lib/tcp/tests/tcp_listener_unittests.cc @@ -64,7 +64,7 @@ public: /// /// Starts test timer which detects timeouts. TcpListenerTest() - : io_service_(), test_timer_(io_service_), + : io_service_(new IOService()), test_timer_(io_service_), run_io_service_timer_(io_service_), clients_(), clients_done_(0) { test_timer_.setup(std::bind(&TcpListenerTest::timeoutHandler, this, true), @@ -129,7 +129,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Callback function each client invokes when done. @@ -141,7 +141,7 @@ public: ++clients_done_; if (clients_done_ >= clients_.size()) { // They're all done or dead. Stop the service. - io_service_.stop(); + io_service_->stop(); } } @@ -150,7 +150,7 @@ public: /// @param timeout Optional value specifying for how long the io service /// should be ran. void runIOService(long timeout = 0) { - io_service_.restart(); + io_service_->restart(); if (timeout > 0) { run_io_service_timer_.setup(std::bind(&TcpListenerTest::timeoutHandler, @@ -158,9 +158,9 @@ public: timeout, IntervalTimer::ONE_SHOT); } - io_service_.run(); - io_service_.restart(); - io_service_.poll(); + io_service_->run(); + io_service_->restart(); + io_service_->poll(); } /// @brief Filter that denies every other connection. @@ -182,7 +182,7 @@ public: } /// @brief IO service used in the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Asynchronous timer service to detect timeouts. IntervalTimer test_timer_; @@ -234,7 +234,7 @@ TEST_F(TcpListenerTest, listen) { ASSERT_EQ(expected_entries, listener.audit_trail_->getConnectionTrail(1)); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that a TCP connection can receive a complete @@ -266,7 +266,7 @@ TEST_F(TcpListenerTest, splitReads) { EXPECT_FALSE(client->expectedEof()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that a TCP connection can be established and used to @@ -295,7 +295,7 @@ TEST_F(TcpListenerTest, idleTimeoutTest) { EXPECT_TRUE(client->expectedEof()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } TEST_F(TcpListenerTest, multipleClientsListen) { @@ -335,7 +335,7 @@ TEST_F(TcpListenerTest, multipleClientsListen) { } listener.stop(); - io_service_.poll(); + io_service_->poll(); } // Verify that the listener handles multiple requests for multiple @@ -388,7 +388,7 @@ TEST_F(TcpListenerTest, multipleRequetsPerClients) { } listener.stop(); - io_service_.poll(); + io_service_->poll(); } // Verify that connection filtering can eliminate specific connections. @@ -450,7 +450,7 @@ TEST_F(TcpListenerTest, filterClientsTest) { } listener.stop(); - io_service_.poll(); + io_service_->poll(); } // Exercises TcpStreamRequest::postBuffer() through various diff --git a/src/lib/tcp/tests/tcp_test_client.h b/src/lib/tcp/tests/tcp_test_client.h index f58e3873c3..94be72f34d 100644 --- a/src/lib/tcp/tests/tcp_test_client.h +++ b/src/lib/tcp/tests/tcp_test_client.h @@ -77,13 +77,13 @@ public: /// @param tls_context /// @param server_address string containing the IP address of the server. /// @param port port number of the server. - explicit TcpTestClient(isc::asiolink::IOService& io_service, + explicit TcpTestClient(const isc::asiolink::IOServicePtr& io_service, std::function<void()> done_callback, isc::asiolink::TlsContextPtr tls_context = isc::asiolink::TlsContextPtr(), const std::string& server_address = "127.0.0.1", uint16_t port = 18123) - : io_service_(io_service.getInternalIOService()), + : io_service_(io_service), tls_context_(tls_context), tcp_socket_(), tls_socket_(), done_callback_(done_callback), @@ -382,8 +382,8 @@ public: private: - /// @brief Holds reference to the IO service. - boost::asio::io_service& io_service_; + /// @brief Holds pointer to the IO service. + isc::asiolink::IOServicePtr io_service_; /// @brief TLS context. isc::asiolink::TlsContextPtr tls_context_; diff --git a/src/lib/tcp/tests/tcp_test_listener.h b/src/lib/tcp/tests/tcp_test_listener.h index 2b28fb7855..44d9d372b2 100644 --- a/src/lib/tcp/tests/tcp_test_listener.h +++ b/src/lib/tcp/tests/tcp_test_listener.h @@ -121,7 +121,7 @@ public: typedef std::function<std::string(const std::string&)> ResponseHandler; /// @brief Constructor - TcpTestConnection(IOService& io_service, + TcpTestConnection(const IOServicePtr& io_service, const TcpConnectionAcceptorPtr& acceptor, const TlsContextPtr& tls_context, TcpConnectionPool& connection_pool, @@ -232,7 +232,7 @@ typedef boost::shared_ptr<TcpTestConnection> TcpTestConnectionPtr; class TcpTestListener : public TcpListener { public: /// @brief Constructor - TcpTestListener(IOService& io_service, + TcpTestListener(const IOServicePtr& io_service, const IOAddress& server_address, const unsigned short server_port, const TlsContextPtr& tls_context, diff --git a/src/lib/tcp/tests/tls_listener_unittests.cc b/src/lib/tcp/tests/tls_listener_unittests.cc index a3b03d6f4b..6b7db45596 100644 --- a/src/lib/tcp/tests/tls_listener_unittests.cc +++ b/src/lib/tcp/tests/tls_listener_unittests.cc @@ -59,7 +59,7 @@ public: /// /// Starts test timer which detects timeouts. TlsListenerTest() - : io_service_(), test_timer_(io_service_), + : io_service_(new IOService()), test_timer_(io_service_), run_io_service_timer_(io_service_), clients_(), clients_done_(0) { test_timer_.setup(std::bind(&TlsListenerTest::timeoutHandler, this, true), @@ -139,7 +139,7 @@ public: if (fail_on_timeout) { ADD_FAILURE() << "Timeout occurred while running the test!"; } - io_service_.stop(); + io_service_->stop(); } /// @brief Callback function each client invokes when done. @@ -151,7 +151,7 @@ public: ++clients_done_; if (clients_done_ >= clients_.size()) { // They're all done or dead. Stop the service. - io_service_.stop(); + io_service_->stop(); } } @@ -160,7 +160,7 @@ public: /// @param timeout Optional value specifying for how long the io service /// should be ran. void runIOService(long timeout = 0) { - io_service_.restart(); + io_service_->restart(); if (timeout > 0) { run_io_service_timer_.setup(std::bind(&TlsListenerTest::timeoutHandler, @@ -168,9 +168,9 @@ public: timeout, IntervalTimer::ONE_SHOT); } - io_service_.run(); - io_service_.restart(); - io_service_.poll(); + io_service_->run(); + io_service_->restart(); + io_service_->poll(); } /// @brief Filter that denies every other connection. @@ -192,7 +192,7 @@ public: } /// @brief IO service used in the tests. - IOService io_service_; + IOServicePtr io_service_; /// @brief Asynchronous timer service to detect timeouts. IntervalTimer test_timer_; @@ -244,7 +244,7 @@ TEST_F(TlsListenerTest, listen) { ASSERT_EQ(expected_entries, listener.audit_trail_->getConnectionTrail(1)); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that a TLS connection is denied to a client @@ -305,7 +305,7 @@ TEST_F(TlsListenerTest, splitReads) { EXPECT_FALSE(client->expectedEof()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that a TLS connection can be established and used to @@ -334,7 +334,7 @@ TEST_F(TlsListenerTest, idleTimeoutTest) { EXPECT_TRUE(client->expectedEof()); listener.stop(); - io_service_.poll(); + io_service_->poll(); } // This test verifies that TLS connections with multiple clients. @@ -375,7 +375,7 @@ TEST_F(TlsListenerTest, multipleClientsListen) { } listener.stop(); - io_service_.poll(); + io_service_->poll(); } // Verify that the listener handles multiple requests for multiple @@ -428,7 +428,7 @@ TEST_F(TlsListenerTest, multipleRequetsPerClients) { } listener.stop(); - io_service_.poll(); + io_service_->poll(); } // Verify that connection filtering can eliminate specific connections. @@ -492,7 +492,7 @@ TEST_F(TlsListenerTest, filterClientsTest) { } listener.stop(); - io_service_.poll(); + io_service_->poll(); } } |