organize utility functions for sims a bit better

This commit is contained in:
arvidn
2021-01-24 17:36:46 +01:00
committed by Arvid Norberg
parent 0c24a771db
commit 0655bd4334
7 changed files with 110 additions and 102 deletions

View File

@ -112,73 +112,6 @@ sim::route dsl_config::outgoing_route(asio::ip::address ip)
return sim::route().append(it->second);
}
std::string save_path(int swarm_id, int idx)
{
char path[200];
std::snprintf(path, sizeof(path), "swarm-%04d-peer-%02d"
, swarm_id, idx);
return path;
}
void add_extra_peers(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
auto h = handles[0];
for (int i = 0; i < 30; ++i)
{
char ep[30];
std::snprintf(ep, sizeof(ep), "60.0.0.%d", i + 1);
h.connect_peer(lt::tcp::endpoint(addr(ep), 6881));
}
}
lt::torrent_status get_status(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return lt::torrent_status();
auto h = handles[0];
return h.status();
}
bool has_metadata(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return false;
auto h = handles[0];
return h.status().has_metadata;
}
bool is_seed(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return false;
auto h = handles[0];
return h.status().is_seeding;
}
bool is_finished(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return false;
auto h = handles[0];
return h.status().is_finished;
}
int completed_pieces(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return 0;
auto h = handles[0];
return h.status().num_pieces;
}
namespace {
bool should_print(lt::alert* a)
{
@ -203,24 +136,6 @@ bool should_print(lt::alert* a)
}
}
void utp_only(lt::settings_pack& p)
{
using namespace lt;
p.set_bool(settings_pack::enable_outgoing_tcp, false);
p.set_bool(settings_pack::enable_incoming_tcp, false);
p.set_bool(settings_pack::enable_outgoing_utp, true);
p.set_bool(settings_pack::enable_incoming_utp, true);
}
void enable_enc(lt::settings_pack& p)
{
using namespace lt;
p.set_bool(settings_pack::prefer_rc4, true);
p.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced);
p.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced);
p.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
}
void setup_swarm(int num_nodes
, swarm_test_t type
, std::function<void(lt::settings_pack&)> new_session

View File

@ -87,21 +87,6 @@ void setup_swarm(int num_nodes
, std::function<void(lt::alert const*, lt::session&)> on_alert
, std::function<bool(int, lt::session&)> terminate);
bool has_metadata(lt::session& ses);
bool is_seed(lt::session& ses);
bool is_finished(lt::session& ses);
int completed_pieces(lt::session& ses);
void add_extra_peers(lt::session& ses);
lt::torrent_status get_status(lt::session& ses);
std::string save_path(int swarm_id, int idx);
// disable TCP and enable uTP
void utp_only(lt::settings_pack& pack);
// force encrypted connections
void enable_enc(lt::settings_pack& pack);
struct dsl_config : sim::default_config
{
dsl_config(int kb_per_second = 0, int send_queue_size = 0);

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "test_utils.hpp"
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "utils.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/alert_types.hpp"

View File

@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "test.hpp"
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "utils.hpp"
#if !defined TORRENT_DISABLE_ENCRYPTION

View File

@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session_stats.hpp"
#include "test.hpp"
#include "utils.hpp"
#include "setup_swarm.hpp"
#include "settings.hpp"
#include <fstream>

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/alert.hpp"
#include "libtorrent/io_context.hpp"
#include "setup_swarm.hpp"
#include "setup_transfer.hpp" // for addr()
using namespace lt;
@ -66,6 +67,92 @@ void filter_ips(lt::session& ses)
ses.set_ip_filter(filter);
}
void utp_only(lt::settings_pack& p)
{
using namespace lt;
p.set_bool(settings_pack::enable_outgoing_tcp, false);
p.set_bool(settings_pack::enable_incoming_tcp, false);
p.set_bool(settings_pack::enable_outgoing_utp, true);
p.set_bool(settings_pack::enable_incoming_utp, true);
}
void enable_enc(lt::settings_pack& p)
{
using namespace lt;
p.set_bool(settings_pack::prefer_rc4, true);
p.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced);
p.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced);
p.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
}
std::string save_path(int swarm_id, int idx)
{
char path[200];
std::snprintf(path, sizeof(path), "swarm-%04d-peer-%02d"
, swarm_id, idx);
return path;
}
void add_extra_peers(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
auto h = handles[0];
for (int i = 0; i < 30; ++i)
{
char ep[30];
std::snprintf(ep, sizeof(ep), "60.0.0.%d", i + 1);
h.connect_peer(lt::tcp::endpoint(addr(ep), 6881));
}
}
lt::torrent_status get_status(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return lt::torrent_status();
auto h = handles[0];
return h.status();
}
bool has_metadata(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return false;
auto h = handles[0];
return h.status().has_metadata;
}
bool is_seed(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return false;
auto h = handles[0];
return h.status().is_seeding;
}
bool is_finished(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return false;
auto h = handles[0];
return h.status().is_finished;
}
int completed_pieces(lt::session& ses)
{
auto handles = ses.get_torrents();
TEST_EQUAL(handles.size(), 1);
if (handles.empty()) return 0;
auto h = handles[0];
return h.status().num_pieces;
}
void set_proxy(lt::session& ses, int proxy_type, test_transfer_flags_t const flags)
{
// apply the proxy settings to session 0

View File

@ -37,6 +37,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/address.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/flags.hpp"
#include "libtorrent/torrent_status.hpp"
#include "libtorrent/settings_pack.hpp"
#include "simulator/simulator.hpp"
namespace libtorrent
@ -45,10 +47,26 @@ namespace libtorrent
struct alert;
}
void utp_only(lt::session& ses);
void enable_enc(lt::session& ses);
// adds an IP filter to disallow 50.0.0.1 and 50.0.0.2
void filter_ips(lt::session& ses);
bool has_metadata(lt::session& ses);
bool is_seed(lt::session& ses);
bool is_finished(lt::session& ses);
int completed_pieces(lt::session& ses);
void add_extra_peers(lt::session& ses);
lt::torrent_status get_status(lt::session& ses);
// disable TCP and enable uTP
void utp_only(lt::session& ses);
void utp_only(lt::settings_pack& pack);
// force encrypted connections
void enable_enc(lt::session& ses);
void enable_enc(lt::settings_pack& pack);
std::string save_path(int swarm_id, int idx);
std::unique_ptr<sim::asio::io_context> make_io_context(
sim::simulation& sim, int i);