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