1 /* 2 3 Copyright (c) 2015, Arvid Norberg 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 10 * Redistributions of source code must retain the above copyright 11 notice, this list of conditions and the following disclaimer. 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in 14 the documentation and/or other materials provided with the distribution. 15 * Neither the name of the author nor the names of its 16 contributors may be used to endorse or promote products derived 17 from this software without specific prior written permission. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 POSSIBILITY OF SUCH DAMAGE. 30 31 */ 32 33 #include "simulator/simulator.hpp" 34 #include "libtorrent/address.hpp" 35 #include "libtorrent/fwd.hpp" 36 #include "libtorrent/flags.hpp" 37 #include <functional> 38 39 #ifndef TORRENT_SETUP_SWARM_HPP_INCLUDED 40 #define TORRENT_SETUP_SWARM_HPP_INCLUDED 41 42 using lt::operator""_bit; 43 using swarm_test_t = lt::flags::bitfield_flag<std::uint64_t, struct swarm_test_type_tag>; 44 45 struct swarm_test 46 { 47 constexpr static swarm_test_t download = 0_bit; 48 constexpr static swarm_test_t upload = 1_bit; 49 constexpr static swarm_test_t no_auto_stop = 2_bit; 50 constexpr static swarm_test_t large_torrent = 3_bit; 51 constexpr static swarm_test_t no_storage = 4_bit; 52 }; 53 54 void setup_swarm(int num_nodes 55 , swarm_test_t type 56 , std::function<void(lt::settings_pack&)> new_session 57 , std::function<void(lt::add_torrent_params&)> add_torrent 58 , std::function<void(lt::alert const*, lt::session&)> on_alert 59 , std::function<bool(int, lt::session&)> terminate); 60 61 void setup_swarm(int num_nodes 62 , swarm_test_t type 63 , sim::simulation& sim 64 , std::function<void(lt::settings_pack&)> new_session 65 , std::function<void(lt::add_torrent_params&)> add_torrent 66 , std::function<void(lt::alert const*, lt::session&)> on_alert 67 , std::function<bool(int, lt::session&)> terminate); 68 69 void setup_swarm(int num_nodes 70 , swarm_test_t type 71 , sim::simulation& sim 72 , lt::settings_pack const& default_settings 73 , lt::add_torrent_params const& default_add_torrent 74 , std::function<void(lt::settings_pack&)> new_session 75 , std::function<void(lt::add_torrent_params&)> add_torrent 76 , std::function<void(lt::alert const*, lt::session&)> on_alert 77 , std::function<bool(int, lt::session&)> terminate); 78 79 void setup_swarm(int num_nodes 80 , swarm_test_t type 81 , sim::simulation& sim 82 , lt::settings_pack const& default_settings 83 , lt::add_torrent_params const& default_add_torrent 84 , std::function<void(lt::session&)> init_session 85 , std::function<void(lt::settings_pack&)> new_session 86 , std::function<void(lt::add_torrent_params&)> add_torrent 87 , std::function<void(lt::alert const*, lt::session&)> on_alert 88 , std::function<bool(int, lt::session&)> terminate); 89 90 bool has_metadata(lt::session& ses); 91 bool is_seed(lt::session& ses); 92 bool is_finished(lt::session& ses); 93 int completed_pieces(lt::session& ses); 94 void add_extra_peers(lt::session& ses); 95 lt::torrent_status get_status(lt::session& ses); 96 97 std::string save_path(int swarm_id, int idx); 98 99 // disable TCP and enable uTP 100 void utp_only(lt::settings_pack& pack); 101 102 // force encrypted connections 103 void enable_enc(lt::settings_pack& pack); 104 105 struct dsl_config : sim::default_config 106 { 107 virtual sim::route incoming_route(lt::address ip) override; 108 virtual sim::route outgoing_route(lt::address ip) override; 109 }; 110 111 #endif 112 113