Skip to content

Commit

Permalink
Avoid deprecated Boost.Asio interfaces
Browse files Browse the repository at this point in the history
Change-Id: I7041c89ea9147e08c8b6226b84a6d17dddeed0e1
  • Loading branch information
Pesa committed Nov 12, 2023
1 parent 7df36bd commit 164ae3b
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 132 deletions.
6 changes: 5 additions & 1 deletion .waf-tools/default-compiler-flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions examples/data-producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
};

Expand Down Expand Up @@ -118,7 +118,7 @@ void
Publisher::generateFromFile()
{
if (insertStream.eof()) {
m_face.getIoService().stop();
m_face.getIoContext().stop();
return;
}

Expand Down
75 changes: 32 additions & 43 deletions src/handles/tcp-bulk-insert-handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ip::tcp::socket> socket)
TcpBulkInsertClient(TcpBulkInsertHandle& writer, ip::tcp::socket socket)
: m_writer(writer)
, m_socket(std::move(socket))
{
}

static void
startReceive(TcpBulkInsertHandle& writer, std::shared_ptr<ip::tcp::socket> socket)
startReceive(TcpBulkInsertHandle& writer, ip::tcp::socket socket)
{
auto client = std::make_shared<TcpBulkInsertClient>(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<TcpBulkInsertClient>& client);

private:
TcpBulkInsertHandle& m_writer;
std::shared_ptr<ip::tcp::socket> 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)
Expand All @@ -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());
Expand All @@ -105,39 +102,31 @@ TcpBulkInsertHandle::stop()
void
TcpBulkInsertHandle::asyncAccept()
{
auto clientSocket = std::make_shared<ip::tcp::socket>(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<ip::tcp::socket>& 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<detail::TcpBulkInsertClient>& client)
TcpBulkInsertClient::handleReceive(const boost::system::error_code& error,
std::size_t nBytesReceived,
const std::shared_ptr<TcpBulkInsertClient>& 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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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
4 changes: 0 additions & 4 deletions src/handles/tcp-bulk-insert-handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ class TcpBulkInsertHandle : noncopyable
void
asyncAccept();

void
handleAccept(const boost::system::error_code& error,
const std::shared_ptr<boost::asio::ip::tcp::socket>& socket);

private:
boost::asio::ip::tcp::acceptor m_acceptor;
boost::asio::ip::tcp::endpoint m_localEndpoint;
Expand Down
35 changes: 0 additions & 35 deletions tests/integrated/command-fixture.cpp

This file was deleted.

16 changes: 10 additions & 6 deletions tests/integrated/command-fixture.hpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/integrated/test-basic-command-insert-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -114,11 +114,11 @@ template<class T>
void
Fixture<T>::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();
Expand Down
6 changes: 3 additions & 3 deletions tests/integrated/test-basic-interest-read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/read-handle.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
30 changes: 12 additions & 18 deletions tests/unit/tcp-bulk-insert-handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <boost/asio/ip/tcp.hpp>
#include <boost/test/unit_test.hpp>

namespace repo::tests {
Expand All @@ -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");
}
}

Expand All @@ -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));
Expand Down
Loading

0 comments on commit 164ae3b

Please sign in to comment.