Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_TESTS_DAEMON_FACE_DUMMY_STREAM_SENDER_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_DUMMY_STREAM_SENDER_HPP |
| 28 | |
| 29 | #include "core/scheduler.hpp" |
| 30 | #include "core/global-io.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
| 35 | |
| 36 | template<class Protocol, class Dataset> |
| 37 | class DummyStreamSender : public Dataset |
| 38 | { |
| 39 | public: |
| 40 | typedef typename Protocol::endpoint Endpoint; |
| 41 | typedef typename Protocol::socket Socket; |
| 42 | |
| 43 | class Error : public std::runtime_error |
| 44 | { |
| 45 | public: |
| 46 | explicit |
| 47 | Error(const std::string& what) |
| 48 | : std::runtime_error(what) |
| 49 | { |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | DummyStreamSender() |
| 54 | : socket(getGlobalIoService()) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | start(const Endpoint& endpoint) |
| 60 | { |
| 61 | socket.async_connect(endpoint, |
| 62 | bind(&DummyStreamSender::onSuccessfullConnect, this, _1)); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | onSuccessfullConnect(const boost::system::error_code& error) |
| 67 | { |
| 68 | if (error) |
| 69 | { |
| 70 | throw Error("Connection aborted"); |
| 71 | } |
| 72 | |
| 73 | // This value may need to be adjusted if some dataset exceeds 100k |
| 74 | socket.set_option(boost::asio::socket_base::send_buffer_size(100000)); |
| 75 | |
| 76 | for (typename Dataset::Container::iterator i = this->data.begin(); |
| 77 | i != this->data.end(); ++i) |
| 78 | { |
| 79 | socket.async_send(boost::asio::buffer(*i), |
| 80 | bind(&DummyStreamSender::onSendFinished, this, _1, false)); |
| 81 | } |
| 82 | |
| 83 | socket.async_send(boost::asio::buffer(static_cast<const uint8_t*>(0), 0), |
| 84 | bind(&DummyStreamSender::onSendFinished, this, _1, true)); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | onSendFinished(const boost::system::error_code& error, bool isFinal) |
| 89 | { |
| 90 | if (error) { |
| 91 | throw Error("Connection aborted"); |
| 92 | } |
| 93 | |
| 94 | if (isFinal) { |
| 95 | scheduler::schedule(ndn::time::seconds(1), |
| 96 | bind(&DummyStreamSender::stop, this)); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | stop() |
| 102 | { |
| 103 | // Terminate test |
| 104 | boost::system::error_code error; |
| 105 | socket.shutdown(Socket::shutdown_both, error); |
| 106 | socket.close(error); |
| 107 | } |
| 108 | |
| 109 | public: |
| 110 | Socket socket; |
| 111 | }; |
| 112 | |
| 113 | } // namespace tests |
| 114 | } // namespace nfd |
| 115 | |
| 116 | #endif // NFD_TESTS_DAEMON_FACE_DUMMY_STREAM_SENDER_HPP |