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