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