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 { |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 31 | namespace detail { |
| 32 | |
| 33 | class TcpBulkInsertClient : noncopyable |
| 34 | { |
| 35 | public: |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 36 | TcpBulkInsertClient(TcpBulkInsertHandle& writer, std::shared_ptr<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 | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 43 | startReceive(TcpBulkInsertHandle& writer, std::shared_ptr<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)); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [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 |
| 53 | handleReceive(const boost::system::error_code& error, |
| 54 | std::size_t nBytesReceived, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 55 | const std::shared_ptr<TcpBulkInsertClient>& client); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | TcpBulkInsertHandle& m_writer; |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 59 | std::shared_ptr<ip::tcp::socket> m_socket; |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 60 | uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 61 | std::size_t m_inputBufferSize = 0; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | } // namespace detail |
| 65 | |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame^] | 66 | TcpBulkInsertHandle::TcpBulkInsertHandle(boost::asio::io_context& io, |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 67 | RepoStorage& storageHandle) |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame^] | 68 | : m_acceptor(io) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 69 | , m_storageHandle(storageHandle) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | TcpBulkInsertHandle::listen(const std::string& host, const std::string& port) |
| 75 | { |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame^] | 76 | ip::tcp::resolver resolver(m_acceptor.get_executor()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 77 | ip::tcp::resolver::query query(host, port); |
| 78 | |
| 79 | ip::tcp::resolver::iterator endpoint = resolver.resolve(query); |
| 80 | ip::tcp::resolver::iterator end; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 81 | if (endpoint == end) |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 82 | NDN_THROW(Error("Cannot listen on " + host + " port " + port)); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 83 | |
| 84 | m_localEndpoint = *endpoint; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 85 | NDN_LOG_DEBUG("Start listening on " << m_localEndpoint); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 86 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 87 | m_acceptor.open(m_localEndpoint.protocol()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 88 | m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true)); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 89 | if (m_localEndpoint.address().is_v6()) { |
| 90 | m_acceptor.set_option(ip::v6_only(true)); |
| 91 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 92 | m_acceptor.bind(m_localEndpoint); |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 93 | m_acceptor.listen(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 94 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 95 | asyncAccept(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | TcpBulkInsertHandle::stop() |
| 100 | { |
| 101 | m_acceptor.cancel(); |
| 102 | m_acceptor.close(); |
| 103 | } |
| 104 | |
| 105 | void |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 106 | TcpBulkInsertHandle::asyncAccept() |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 107 | { |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame^] | 108 | auto clientSocket = std::make_shared<ip::tcp::socket>(m_acceptor.get_executor()); |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 109 | m_acceptor.async_accept(*clientSocket, |
| 110 | std::bind(&TcpBulkInsertHandle::handleAccept, this, _1, clientSocket)); |
| 111 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 112 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 113 | void |
| 114 | TcpBulkInsertHandle::handleAccept(const boost::system::error_code& error, |
| 115 | const std::shared_ptr<ip::tcp::socket>& socket) |
| 116 | { |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 117 | if (error) { |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 121 | NDN_LOG_DEBUG("New connection from " << socket->remote_endpoint()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 122 | |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 123 | detail::TcpBulkInsertClient::startReceive(*this, socket); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 124 | |
| 125 | // prepare accepting the next connection |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 126 | asyncAccept(); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
| 130 | detail::TcpBulkInsertClient::handleReceive(const boost::system::error_code& error, |
| 131 | std::size_t nBytesReceived, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 132 | const std::shared_ptr<detail::TcpBulkInsertClient>& client) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 133 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 134 | if (error) { |
| 135 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 136 | return; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 137 | |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 138 | boost::system::error_code ec; |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 139 | m_socket->shutdown(ip::tcp::socket::shutdown_both, ec); |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 140 | m_socket->close(ec); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 143 | |
| 144 | m_inputBufferSize += nBytesReceived; |
| 145 | |
| 146 | // do magic |
| 147 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 148 | auto bufferView = ndn::make_span(m_inputBuffer, m_inputBufferSize); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 149 | std::size_t offset = 0; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 150 | bool isOk = true; |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 151 | while (offset < bufferView.size()) { |
| 152 | Block element; |
| 153 | std::tie(isOk, element) = Block::fromBuffer(bufferView.subspan(offset)); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 154 | if (!isOk) |
| 155 | break; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 156 | |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 157 | offset += element.size(); |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 158 | BOOST_ASSERT(offset <= bufferView.size()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 160 | if (element.type() == ndn::tlv::Data) { |
| 161 | try { |
| 162 | Data data(element); |
| 163 | bool isInserted = m_writer.getStorageHandle().insertData(data); |
| 164 | if (isInserted) |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 165 | NDN_LOG_DEBUG("Successfully injected " << data.getName()); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 166 | else |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 167 | NDN_LOG_DEBUG("FAILED to inject " << data.getName()); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 168 | } |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 169 | catch (const std::runtime_error& e) { |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 170 | /// \todo Catch specific error after determining what wireDecode() can throw |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 171 | NDN_LOG_ERROR("Error decoding received Data packet: " << e.what()); |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 172 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 173 | } |
Junxiao Shi | 1e39ddd | 2015-02-28 23:01:27 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 176 | if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) { |
| 177 | boost::system::error_code ec; |
Davide Pesavento | 3c84316 | 2019-07-07 15:06:44 -0400 | [diff] [blame] | 178 | m_socket->shutdown(ip::tcp::socket::shutdown_both, ec); |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 179 | m_socket->close(ec); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 180 | return; |
| 181 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 182 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 183 | if (offset > 0) { |
| 184 | if (offset != m_inputBufferSize) { |
| 185 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, m_inputBuffer); |
| 186 | m_inputBufferSize -= offset; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 187 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 188 | else { |
| 189 | m_inputBufferSize = 0; |
| 190 | } |
| 191 | } |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 192 | |
| 193 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 194 | ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 195 | std::bind(&TcpBulkInsertClient::handleReceive, this, _1, _2, client)); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 198 | } // namespace repo |