diff --git a/.waf-tools/default-compiler-flags.py b/.waf-tools/default-compiler-flags.py index 7746db9..4e09a82 100644 --- a/.waf-tools/default-compiler-flags.py +++ b/.waf-tools/default-compiler-flags.py @@ -128,7 +128,11 @@ def getCompilerVersion(self, conf): def getGeneralFlags(self, conf): """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" - return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} + return { + 'CXXFLAGS': [], + 'LINKFLAGS': [], + 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'], + } def getDebugFlags(self, conf): """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode""" diff --git a/examples/data-producer.cpp b/examples/data-producer.cpp index 2a23e9c..c564671 100644 --- a/examples/data-producer.cpp +++ b/examples/data-producer.cpp @@ -87,7 +87,7 @@ class Publisher private: ndn::Face m_face; - ndn::Scheduler m_scheduler{m_face.getIoService()}; + ndn::Scheduler m_scheduler{m_face.getIoContext()}; std::uniform_int_distribution<> m_randomDist{200, 1000}; }; @@ -118,7 +118,7 @@ void Publisher::generateFromFile() { if (insertStream.eof()) { - m_face.getIoService().stop(); + m_face.getIoContext().stop(); return; } diff --git a/src/handles/tcp-bulk-insert-handle.cpp b/src/handles/tcp-bulk-insert-handle.cpp index 2713d03..cd26334 100644 --- a/src/handles/tcp-bulk-insert-handle.cpp +++ b/src/handles/tcp-bulk-insert-handle.cpp @@ -28,40 +28,39 @@ NDN_LOG_INIT(repo.TcpHandle); namespace ip = boost::asio::ip; namespace repo { -namespace detail { +namespace { class TcpBulkInsertClient : noncopyable { public: - TcpBulkInsertClient(TcpBulkInsertHandle& writer, std::shared_ptr socket) + TcpBulkInsertClient(TcpBulkInsertHandle& writer, ip::tcp::socket socket) : m_writer(writer) , m_socket(std::move(socket)) { } static void - startReceive(TcpBulkInsertHandle& writer, std::shared_ptr socket) + startReceive(TcpBulkInsertHandle& writer, ip::tcp::socket socket) { auto client = std::make_shared(writer, std::move(socket)); - client->m_socket->async_receive( + client->m_socket.async_receive( boost::asio::buffer(client->m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), 0, std::bind(&TcpBulkInsertClient::handleReceive, client, _1, _2, client)); } private: void - handleReceive(const boost::system::error_code& error, - std::size_t nBytesReceived, + handleReceive(const boost::system::error_code& error, std::size_t nBytesReceived, const std::shared_ptr& client); private: TcpBulkInsertHandle& m_writer; - std::shared_ptr m_socket; + ip::tcp::socket m_socket; uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; std::size_t m_inputBufferSize = 0; }; -} // namespace detail +} // namespace TcpBulkInsertHandle::TcpBulkInsertHandle(boost::asio::io_context& io, RepoStorage& storageHandle) @@ -74,14 +73,12 @@ void TcpBulkInsertHandle::listen(const std::string& host, const std::string& port) { ip::tcp::resolver resolver(m_acceptor.get_executor()); - ip::tcp::resolver::query query(host, port); + boost::system::error_code ec; + auto results = resolver.resolve(host, port, ec); + if (ec) + NDN_THROW(Error("Cannot resolve " + host + ":" + port + " (" + ec.message() + ")")); - ip::tcp::resolver::iterator endpoint = resolver.resolve(query); - ip::tcp::resolver::iterator end; - if (endpoint == end) - NDN_THROW(Error("Cannot listen on " + host + " port " + port)); - - m_localEndpoint = *endpoint; + m_localEndpoint = *results.begin(); NDN_LOG_DEBUG("Start listening on " << m_localEndpoint); m_acceptor.open(m_localEndpoint.protocol()); @@ -105,39 +102,31 @@ TcpBulkInsertHandle::stop() void TcpBulkInsertHandle::asyncAccept() { - auto clientSocket = std::make_shared(m_acceptor.get_executor()); - m_acceptor.async_accept(*clientSocket, - std::bind(&TcpBulkInsertHandle::handleAccept, this, _1, clientSocket)); -} - -void -TcpBulkInsertHandle::handleAccept(const boost::system::error_code& error, - const std::shared_ptr& socket) -{ - if (error) { - return; - } - - NDN_LOG_DEBUG("New connection from " << socket->remote_endpoint()); + m_acceptor.async_accept([this] (const auto& error, ip::tcp::socket socket) { + if (error) { + return; + } - detail::TcpBulkInsertClient::startReceive(*this, socket); + NDN_LOG_DEBUG("New connection from " << socket.remote_endpoint()); + TcpBulkInsertClient::startReceive(*this, std::move(socket)); - // prepare accepting the next connection - asyncAccept(); + // prepare accepting the next connection + asyncAccept(); + }); } void -detail::TcpBulkInsertClient::handleReceive(const boost::system::error_code& error, - std::size_t nBytesReceived, - const std::shared_ptr& client) +TcpBulkInsertClient::handleReceive(const boost::system::error_code& error, + std::size_t nBytesReceived, + const std::shared_ptr& client) { if (error) { - if (error == boost::system::errc::operation_canceled) // when socket is closed by someone + if (error == boost::asio::error::operation_aborted) // when socket is closed by someone return; boost::system::error_code ec; - m_socket->shutdown(ip::tcp::socket::shutdown_both, ec); - m_socket->close(ec); + m_socket.shutdown(ip::tcp::socket::shutdown_both, ec); + m_socket.close(ec); return; } @@ -175,8 +164,8 @@ detail::TcpBulkInsertClient::handleReceive(const boost::system::error_code& erro if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) { boost::system::error_code ec; - m_socket->shutdown(ip::tcp::socket::shutdown_both, ec); - m_socket->close(ec); + m_socket.shutdown(ip::tcp::socket::shutdown_both, ec); + m_socket.close(ec); return; } @@ -190,9 +179,9 @@ detail::TcpBulkInsertClient::handleReceive(const boost::system::error_code& erro } } - m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, - ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, - std::bind(&TcpBulkInsertClient::handleReceive, this, _1, _2, client)); + m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, + ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, + std::bind(&TcpBulkInsertClient::handleReceive, this, _1, _2, client)); } } // namespace repo diff --git a/src/handles/tcp-bulk-insert-handle.hpp b/src/handles/tcp-bulk-insert-handle.hpp index a0a8e7a..ab1ade7 100644 --- a/src/handles/tcp-bulk-insert-handle.hpp +++ b/src/handles/tcp-bulk-insert-handle.hpp @@ -55,10 +55,6 @@ class TcpBulkInsertHandle : noncopyable void asyncAccept(); - void - handleAccept(const boost::system::error_code& error, - const std::shared_ptr& socket); - private: boost::asio::ip::tcp::acceptor m_acceptor; boost::asio::ip::tcp::endpoint m_localEndpoint; diff --git a/tests/integrated/command-fixture.cpp b/tests/integrated/command-fixture.cpp deleted file mode 100644 index 28990e3..0000000 --- a/tests/integrated/command-fixture.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2014-2022, Regents of the University of California. - * - * This file is part of NDN repo-ng (Next generation of NDN repository). - * See AUTHORS.md for complete list of repo-ng authors and contributors. - * - * repo-ng is free software: you can redistribute it and/or modify it under the terms - * of the GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. - * - * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * repo-ng, e.g., in COPYING.md file. If not, see . - */ - -#include "command-fixture.hpp" - -namespace repo::tests { - -CommandFixture::CommandFixture() - : scheduler(repoFace.getIoService()) - , keyChain(m_keyChain) - , dispatcher(repoFace, keyChain) - , validator(repoFace) -{ - this->addIdentity("/ndn/test/repo"); - this->saveIdentityCertificate("/ndn/test/repo", "tests/integrated/insert-delete-test.cert"); - validator.load("tests/integrated/insert-delete-validator-config.conf"); -} - -} // namespace repo::tests diff --git a/tests/integrated/command-fixture.hpp b/tests/integrated/command-fixture.hpp index 6d4aa44..507a9f1 100644 --- a/tests/integrated/command-fixture.hpp +++ b/tests/integrated/command-fixture.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2022, Regents of the University of California. + * Copyright (c) 2014-2023, Regents of the University of California. * * This file is part of NDN repo-ng (Next generation of NDN repository). * See AUTHORS.md for complete list of repo-ng authors and contributors. @@ -30,15 +30,19 @@ namespace repo::tests { class CommandFixture : public virtual IdentityManagementFixture { protected: - CommandFixture(); + CommandFixture() + { + addIdentity("/ndn/test/repo"); + saveIdentityCertificate("/ndn/test/repo", "tests/integrated/insert-delete-test.cert"); + validator.load("tests/integrated/insert-delete-validator-config.conf"); + } protected: Face repoFace; - Scheduler scheduler; - ndn::KeyChain& keyChain; - ndn::mgmt::Dispatcher dispatcher; + Scheduler scheduler{repoFace.getIoContext()}; + ndn::mgmt::Dispatcher dispatcher{repoFace, m_keyChain}; /// \todo #4091 switch to ValidatorPolicyConf and load insert-delete-validator-config.conf - ndn::security::ValidatorConfig validator; + ndn::security::ValidatorConfig validator{repoFace}; }; } // namespace repo::tests diff --git a/tests/integrated/test-basic-command-insert-delete.cpp b/tests/integrated/test-basic-command-insert-delete.cpp index 0f868eb..f1a9110 100644 --- a/tests/integrated/test-basic-command-insert-delete.cpp +++ b/tests/integrated/test-basic-command-insert-delete.cpp @@ -50,9 +50,9 @@ class Fixture : public CommandFixture, public RepoStorageFixture, public Dataset Fixture() : writeHandle(repoFace, *handle, dispatcher, scheduler, validator) , deleteHandle(repoFace, *handle, dispatcher, scheduler, validator) - , insertFace(repoFace.getIoService()) - , deleteFace(repoFace.getIoService()) - , signer(keyChain) + , insertFace(repoFace.getIoContext()) + , deleteFace(repoFace.getIoContext()) + , signer(m_keyChain) { Name cmdPrefix("/repo/command"); repoFace.registerPrefix(cmdPrefix, nullptr, @@ -114,11 +114,11 @@ template void Fixture::onInsertInterest(const Interest& interest) { - Data data(Name(interest.getName())); + Data data(interest.getName()); data.setContent(CONTENT); - data.setFreshnessPeriod(0_ms); - keyChain.sign(data); + m_keyChain.sign(data); insertFace.put(data); + auto eventIt = insertEvents.find(interest.getName()); if (eventIt != insertEvents.end()) { eventIt->second.cancel(); diff --git a/tests/integrated/test-basic-interest-read.cpp b/tests/integrated/test-basic-interest-read.cpp index 81ceb98..c2b6a17 100644 --- a/tests/integrated/test-basic-interest-read.cpp +++ b/tests/integrated/test-basic-interest-read.cpp @@ -38,15 +38,15 @@ class BasicInterestReadFixture : public RepoStorageFixture, public Dataset { public: BasicInterestReadFixture() - : scheduler(repoFace.getIoService()) + : scheduler(repoFace.getIoContext()) , readHandle(repoFace, *handle, 0) - , readFace(repoFace.getIoService()) + , readFace(repoFace.getIoContext()) { } ~BasicInterestReadFixture() { - repoFace.getIoService().stop(); + repoFace.getIoContext().stop(); } void diff --git a/tests/unit/read-handle.t.cpp b/tests/unit/read-handle.t.cpp index 9d36b18..553f651 100644 --- a/tests/unit/read-handle.t.cpp +++ b/tests/unit/read-handle.t.cpp @@ -44,7 +44,7 @@ class Fixture : public RepoStorageFixture public: Fixture() : face({true, true}) - , scheduler(face.getIoService()) + , scheduler(face.getIoContext()) , subsetLength(1) , dataPrefix("/ndn/test/prefix") , identity("/ndn/test/identity") diff --git a/tests/unit/tcp-bulk-insert-handle.cpp b/tests/unit/tcp-bulk-insert-handle.cpp index 9c0398b..e227167 100644 --- a/tests/unit/tcp-bulk-insert-handle.cpp +++ b/tests/unit/tcp-bulk-insert-handle.cpp @@ -18,10 +18,11 @@ */ #include "handles/tcp-bulk-insert-handle.hpp" -#include "storage/sqlite-storage.hpp" + #include "../repo-storage-fixture.hpp" #include "../dataset-fixtures.hpp" +#include #include namespace repo::tests { @@ -34,28 +35,21 @@ class TcpClient void start(const std::string& host, const std::string& port) { - using namespace boost::asio; - - ip::tcp::resolver resolver(ioCtx); - ip::tcp::resolver::query query(host, port); - - ip::tcp::resolver::iterator endpoint = resolver.resolve(query); - ip::tcp::resolver::iterator end; - - if (endpoint == end) + boost::asio::ip::tcp::resolver resolver(ioCtx); + boost::system::error_code ec; + auto results = resolver.resolve(host, port, ec); + if (ec) { BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]"); + } - ip::tcp::endpoint serverEndpoint = *endpoint; - - socket.async_connect(serverEndpoint, - std::bind(&TcpClient::onSuccessfullConnect, this, _1)); + socket.async_connect(*results.begin(), std::bind(&TcpClient::handleConnect, this, _1)); } virtual void - onSuccessfullConnect(const boost::system::error_code& error) + handleConnect(const boost::system::error_code& error) { if (error) { - BOOST_FAIL("TCP connection aborted"); + BOOST_FAIL("TCP connection failed"); } } @@ -78,9 +72,9 @@ class TcpBulkInsertFixture : public TcpClient, } void - onSuccessfullConnect(const boost::system::error_code& error) override + handleConnect(const boost::system::error_code& error) override { - TcpClient::onSuccessfullConnect(error); + TcpClient::handleConnect(error); // This value may need to be adjusted if some dataset exceeds 100k socket.set_option(boost::asio::socket_base::send_buffer_size(100000)); diff --git a/tools/ndnputfile.cpp b/tools/ndnputfile.cpp index 04e85ee..b011c71 100644 --- a/tools/ndnputfile.cpp +++ b/tools/ndnputfile.cpp @@ -72,7 +72,7 @@ class NdnPutFile : boost::noncopyable , timeout(0) , insertStream(nullptr) , isVerbose(false) - , m_scheduler(m_face.getIoService()) + , m_scheduler(m_face.getIoContext()) , m_timestampVersion(toUnixTimestamp(system_clock::now()).count()) , m_processId(0) , m_checkPeriod(DEFAULT_CHECK_PERIOD) @@ -110,9 +110,6 @@ class NdnPutFile : boost::noncopyable void onRegisterFailed(const ndn::Name& prefix, const std::string& reason); - void - stopProcess(); - void signData(ndn::Data& data); @@ -222,7 +219,7 @@ NdnPutFile::run() bind(&NdnPutFile::onRegisterFailed, this, _1, _2)); if (hasTimeout) - m_scheduler.schedule(timeout, [this] { stopProcess(); }); + m_scheduler.schedule(timeout, [this] { m_face.getIoContext().stop(); }); m_face.processEvents(); } @@ -348,12 +345,6 @@ NdnPutFile::onRegisterFailed(const ndn::Name& prefix, const std::string& reason) NDN_THROW(Error("onRegisterFailed: " + reason)); } -void -NdnPutFile::stopProcess() -{ - m_face.getIoService().stop(); -} - void NdnPutFile::signData(ndn::Data& data) { @@ -393,7 +384,7 @@ NdnPutFile::onCheckCommandResponse(const ndn::Interest&, const ndn::Data& data) if (isSingle) { if (insertCount == 1) { - m_face.getIoService().stop(); + m_face.getIoContext().stop(); return; } } @@ -401,7 +392,7 @@ NdnPutFile::onCheckCommandResponse(const ndn::Interest&, const ndn::Data& data) // write operation has been finished if (insertCount == m_currentSegmentNo) { - m_face.getIoService().stop(); + m_face.getIoContext().stop(); return; } }