Update visiibilty

This commit is contained in:
Igor Katson 2021-07-04 11:05:20 +01:00
parent ad867e8e3c
commit 9472d66bf9
3 changed files with 77 additions and 39 deletions

View file

@ -22,20 +22,20 @@ pub async fn make_and_run_http_api(
let state = state.clone();
let start_time = Instant::now();
let initial_downloaded_and_checked =
state.stats.downloaded_and_checked.load(Ordering::Relaxed);
state.stats().downloaded_and_checked.load(Ordering::Relaxed);
move || {
let mut buf = Vec::new();
writeln!(buf, "{:#?}", state.stats_snapshot()).unwrap();
writeln!(
buf,
"Average download time: {:?}",
state.stats.average_piece_download_time()
state.stats().average_piece_download_time()
)
.unwrap();
// Poor mans download speed computation
let elapsed = start_time.elapsed();
let downloaded_bytes = state.stats.downloaded_and_checked.load(Ordering::Relaxed)
let downloaded_bytes = state.stats().downloaded_and_checked.load(Ordering::Relaxed)
- initial_downloaded_and_checked;
let downloaded_mb = downloaded_bytes as f64 / 1024f64 / 1024f64;
writeln!(

View file

@ -4,10 +4,7 @@ use std::{
net::SocketAddr,
ops::Deref,
path::{Path, PathBuf},
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
sync::{atomic::Ordering, Arc},
time::{Duration, Instant},
};
@ -19,7 +16,7 @@ use librqbit_core::{
torrent_metainfo::TorrentMetaV1Info,
};
use log::{debug, info};
use parking_lot::{Mutex, RwLock};
use parking_lot::Mutex;
use reqwest::Url;
use sha1w::Sha1;
use size_format::SizeFormatterBinary as SF;
@ -29,7 +26,7 @@ use crate::{
file_ops::FileOps,
http_api::make_and_run_http_api,
spawn_utils::{spawn, BlockingSpawner},
torrent_state::{AtomicStats, TorrentState, TorrentStateLocked},
torrent_state::TorrentState,
tracker_comms::{TrackerError, TrackerRequest, TrackerRequestEvent, TrackerResponse},
};
pub struct TorrentManagerBuilder {
@ -212,23 +209,18 @@ impl TorrentManager {
lengths,
);
let state = Arc::new(TorrentState {
let state = Arc::new(TorrentState::new(
info,
info_hash,
torrent: info,
peer_id,
locked: Arc::new(RwLock::new(TorrentStateLocked {
peers: Default::default(),
chunks: chunk_tracker,
})),
files,
stats: AtomicStats {
have: AtomicU64::new(initial_check_results.have_bytes),
..Default::default()
},
needed: initial_check_results.needed_bytes,
chunk_tracker,
lengths,
initial_check_results.have_bytes,
initial_check_results.needed_bytes,
spawner,
});
));
let estimator = Arc::new(SpeedEstimator::new(5));
let mgr = Arc::new(Self {
@ -250,8 +242,8 @@ impl TorrentManager {
let state = mgr.state.clone();
async move {
loop {
let downloaded = state.stats.downloaded_and_checked.load(Ordering::Relaxed);
let needed = state.needed;
let downloaded = state.stats().downloaded_and_checked.load(Ordering::Relaxed);
let needed = state.initially_needed();
let remaining = needed - downloaded;
estimator.add_snapshot(downloaded, remaining, Instant::now());
tokio::time::sleep(Duration::from_secs(1)).await;
@ -266,16 +258,16 @@ impl TorrentManager {
loop {
let live_peer_stats = self.state.locked.read().peers.stats();
let seen_peers_count = self.state.locked.read().peers.seen().len();
let have = self.state.stats.have.load(Ordering::Relaxed);
let fetched = self.state.stats.fetched_bytes.load(Ordering::Relaxed);
let needed = self.state.needed;
let have = self.state.stats().have.load(Ordering::Relaxed);
let fetched = self.state.stats().fetched_bytes.load(Ordering::Relaxed);
let needed = self.state.initially_needed();
let downloaded = self
.state
.stats
.stats()
.downloaded_and_checked
.load(Ordering::Relaxed);
let remaining = needed - downloaded;
let uploaded = self.state.stats.uploaded.load(Ordering::Relaxed);
let uploaded = self.state.stats().uploaded.load(Ordering::Relaxed);
let downloaded_pct = if downloaded == needed {
100f64
} else {
@ -326,8 +318,8 @@ impl TorrentManager {
let mut event = Some(TrackerRequestEvent::Started);
loop {
let request = TrackerRequest {
info_hash: self.state.info_hash,
peer_id: self.state.peer_id,
info_hash: self.state.info_hash(),
peer_id: self.state.peer_id(),
port: 6778,
uploaded: self.state.get_uploaded(),
downloaded: self.state.get_downloaded(),

View file

@ -14,6 +14,7 @@ use buffers::{ByteBuf, ByteString};
use clone_to_owned::CloneToOwned;
use futures::{stream::FuturesUnordered, StreamExt};
use librqbit_core::{
info_hash::InfoHash,
lengths::{ChunkInfo, Lengths, ValidPieceIndex},
torrent_metainfo::TorrentMetaV1Info,
};
@ -192,21 +193,66 @@ pub struct StatsSnapshot {
}
pub struct TorrentState {
pub torrent: TorrentMetaV1Info<ByteString>,
info: TorrentMetaV1Info<ByteString>,
pub locked: Arc<RwLock<TorrentStateLocked>>,
pub files: Vec<Arc<Mutex<File>>>,
pub info_hash: [u8; 20],
pub peer_id: [u8; 20],
pub lengths: Lengths,
pub needed: u64,
pub stats: AtomicStats,
files: Vec<Arc<Mutex<File>>>,
info_hash: [u8; 20],
peer_id: [u8; 20],
lengths: Lengths,
needed: u64,
stats: AtomicStats,
pub spawner: BlockingSpawner,
spawner: BlockingSpawner,
}
impl TorrentState {
#[allow(clippy::too_many_arguments)]
pub fn new(
info: TorrentMetaV1Info<ByteString>,
info_hash: [u8; 20],
peer_id: [u8; 20],
files: Vec<Arc<Mutex<File>>>,
chunk_tracker: ChunkTracker,
lengths: Lengths,
have_bytes: u64,
needed_bytes: u64,
spawner: BlockingSpawner,
) -> Self {
TorrentState {
info_hash,
info,
peer_id,
locked: Arc::new(RwLock::new(TorrentStateLocked {
peers: Default::default(),
chunks: chunk_tracker,
})),
files,
stats: AtomicStats {
have: AtomicU64::new(have_bytes),
..Default::default()
},
needed: needed_bytes,
lengths,
spawner,
}
}
pub fn info(&self) -> &TorrentMetaV1Info<ByteString> {
&self.info
}
pub fn info_hash(&self) -> InfoHash {
self.info_hash
}
pub fn peer_id(&self) -> [u8; 20] {
self.peer_id
}
pub fn file_ops(&self) -> FileOps<'_, Sha1> {
FileOps::new(&self.torrent, &self.files, &self.lengths)
FileOps::new(&self.info, &self.files, &self.lengths)
}
pub fn initially_needed(&self) -> u64 {
self.needed
}
pub fn stats(&self) -> &AtomicStats {
&self.stats
}
pub fn get_next_needed_piece(&self, peer_handle: PeerHandle) -> Option<ValidPieceIndex> {