Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "tcp-bulk-insert-handle.hpp" |
| 21 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 22 | #include <boost/asio/ip/v6_only.hpp> |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 23 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 24 | #include <ndn-cxx/util/logger.hpp> |
| 25 | |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 26 | NDN_LOG_INIT(repo.TcpHandle); |
| 27 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 28 | namespace ip = boost::asio::ip; |
| 29 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 30 | namespace repo { |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 31 | namespace { |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 32 | |
| 33 | class TcpBulkInsertClient : noncopyable |
| 34 | { |
| 35 | public: |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 36 | TcpBulkInsertClient(TcpBulkInsertHandle& writer, ip::tcp::socket socket) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 37 | : m_writer(writer) |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 38 | , m_socket(std::move(socket)) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | static void |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 43 | startReceive(TcpBulkInsertHandle& writer, ip::tcp::socket socket) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 44 | { |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 45 | auto client = std::make_shared<TcpBulkInsertClient>(writer, std::move(socket)); |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 46 | client->m_socket.async_receive( |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 47 | boost::asio::buffer(client->m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), 0, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 48 | std::bind(&TcpBulkInsertClient::handleReceive, client, _1, _2, client)); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | private: |
| 52 | void |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 53 | handleReceive(const boost::system::error_code& error, std::size_t nBytesReceived, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 54 | const std::shared_ptr<TcpBulkInsertClient>& client); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | TcpBulkInsertHandle& m_writer; |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 58 | ip::tcp::socket m_socket; |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 59 | uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 60 | std::size_t m_inputBufferSize = 0; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 63 | } // namespace |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 64 | |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 65 | TcpBulkInsertHandle::TcpBulkInsertHandle(boost::asio::io_context& io, |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 66 | RepoStorage& storageHandle) |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 67 | : m_acceptor(io) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 68 | , m_storageHandle(storageHandle) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | TcpBulkInsertHandle::listen(const std::string& host, const std::string& port) |
| 74 | { |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 75 | ip::tcp::resolver resolver(m_acceptor.get_executor()); |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 76 | boost::system::error_code ec; |
| 77 | auto results = resolver.resolve(host, port, ec); |
| 78 | if (ec) |
| 79 | NDN_THROW(Error("Cannot resolve " + host + ":" + port + " (" + ec.message() + ")")); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 80 | |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 81 | m_localEndpoint = *results.begin(); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 82 | NDN_LOG_DEBUG("Start listening on " << m_localEndpoint); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 83 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 84 | m_acceptor.open(m_localEndpoint.protocol()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 85 | m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true)); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 86 | if (m_localEndpoint.address().is_v6()) { |
| 87 | m_acceptor.set_option(ip::v6_only(true)); |
| 88 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 89 | m_acceptor.bind(m_localEndpoint); |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 90 | m_acceptor.listen(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 91 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 92 | asyncAccept(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | TcpBulkInsertHandle::stop() |
| 97 | { |
| 98 | m_acceptor.cancel(); |
| 99 | m_acceptor.close(); |
| 100 | } |
| 101 | |
| 102 | void |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 103 | TcpBulkInsertHandle::asyncAccept() |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 104 | { |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 105 | m_acceptor.async_accept([this] (const auto& error, ip::tcp::socket socket) { |
| 106 | if (error) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | NDN_LOG_DEBUG("New connection from " << socket.remote_endpoint()); |
| 111 | TcpBulkInsertClient::startReceive(*this, std::move(socket)); |
| 112 | |
| 113 | // prepare accepting the next connection |
| 114 | asyncAccept(); |
| 115 | }); |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 116 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 117 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 118 | void |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 119 | TcpBulkInsertClient::handleReceive(const boost::system::error_code& error, |
| 120 | std::size_t nBytesReceived, |
| 121 | const std::shared_ptr<TcpBulkInsertClient>& client) |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 122 | { |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 123 | if (error) { |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 124 | if (error == boost::asio::error::operation_aborted) // when socket is closed by someone |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 125 | return; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 126 | |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 127 | boost::system::error_code ec; |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 128 | m_socket.shutdown(ip::tcp::socket::shutdown_both, ec); |
| 129 | m_socket.close(ec); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 130 | return; |
| 131 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 132 | |
| 133 | m_inputBufferSize += nBytesReceived; |
| 134 | |
| 135 | // do magic |
| 136 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 137 | auto bufferView = ndn::make_span(m_inputBuffer, m_inputBufferSize); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 138 | std::size_t offset = 0; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 139 | bool isOk = true; |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 140 | while (offset < bufferView.size()) { |
| 141 | Block element; |
| 142 | std::tie(isOk, element) = Block::fromBuffer(bufferView.subspan(offset)); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 143 | if (!isOk) |
| 144 | break; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 145 | |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 146 | offset += element.size(); |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 147 | BOOST_ASSERT(offset <= bufferView.size()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 148 | |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 149 | if (element.type() == ndn::tlv::Data) { |
| 150 | try { |
| 151 | Data data(element); |
| 152 | bool isInserted = m_writer.getStorageHandle().insertData(data); |
| 153 | if (isInserted) |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 154 | NDN_LOG_DEBUG("Successfully injected " << data.getName()); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 155 | else |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 156 | NDN_LOG_DEBUG("FAILED to inject " << data.getName()); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 157 | } |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 158 | catch (const std::runtime_error& e) { |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 159 | /// \todo Catch specific error after determining what wireDecode() can throw |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 160 | NDN_LOG_ERROR("Error decoding received Data packet: " << e.what()); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 161 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 162 | } |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 165 | if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) { |
| 166 | boost::system::error_code ec; |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 167 | m_socket.shutdown(ip::tcp::socket::shutdown_both, ec); |
| 168 | m_socket.close(ec); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 169 | return; |
| 170 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 171 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 172 | if (offset > 0) { |
| 173 | if (offset != m_inputBufferSize) { |
| 174 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, m_inputBuffer); |
| 175 | m_inputBufferSize -= offset; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 176 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 177 | else { |
| 178 | m_inputBufferSize = 0; |
| 179 | } |
| 180 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 181 | |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 182 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 183 | ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
| 184 | std::bind(&TcpBulkInsertClient::handleReceive, this, _1, _2, client)); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 187 | } // namespace repo |