Skip to content

Commit

Permalink
Set state.pieces properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Nov 7, 2023
1 parent 788f603 commit 4640011
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
16 changes: 5 additions & 11 deletions src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
metainfo::Metainfo,
peer_metainfo::{PeerMetainfoMessage, METAINFO_PIECE_SIZE},
sha1,
state::{Block, Peer, PeerInfo, PeerStatus, State, TorrentStatus, BLOCK_SIZE},
state::{init_pieces, Block, Peer, PeerInfo, PeerStatus, State, TorrentStatus, BLOCK_SIZE},
torrent::write_piece,
types::ByteString,
};
Expand Down Expand Up @@ -467,15 +467,16 @@ pub async fn write_loop(
if let (Some(info_dict), _) = parse_bencoded(data) {
debug!("bencoded metainfo: {:?}", info_dict);
// since peer metainfo protocol only transfers info dict, it needs
// to be inserted into full metainfo dict
// to be inserted into full metainfo dict to parse properly
let metainfo_dict = BencodeValue::Dict(
[("info".into(), info_dict)].into_iter().collect(),
);
match Metainfo::try_from(metainfo_dict) {
Ok(metainfo) => {
state.pieces = Some(init_pieces(&metainfo.info));
state.metainfo = Ok(metainfo);
info!("metainfo is downloaded: {:?}", state.metainfo);
state.status = TorrentStatus::Downloading;
info!("metainfo is downloaded: {:?}", state.metainfo);
}
Err(e) => {
panic!("unable to parse metainfo from bencoded: {:#}", e);
Expand Down Expand Up @@ -597,14 +598,7 @@ async fn read_loop(
}
piece.status = TorrentStatus::Downloaded;
info!(
"piece {}/{}/{}",
state
.pieces
.as_ref()
.unwrap()
.values()
.filter(|p| p.status != TorrentStatus::Downloading)
.count(),
"piece {}/{}",
state
.pieces
.as_ref()
Expand Down
6 changes: 3 additions & 3 deletions src/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
peer::peer_loop,
persist::PersistState,
sha1,
state::{init_pieces, Peer, PeerInfo, State, TorrentStatus},
state::{Peer, PeerInfo, State, TorrentStatus},
tracker::tracker_loop,
};

Expand All @@ -38,7 +38,7 @@ pub async fn download_torrent(
p_state: Arc<Mutex<PersistState>>,
) -> Result<()> {
let started = Instant::now();
let (metainfo, info_hash) = metainfo_from_path(path)?;
let (_, info_hash) = metainfo_from_path(path)?;
let (dht_peers, peer_id) = {
let p_state = p_state.lock().await;
(
Expand All @@ -62,7 +62,7 @@ pub async fn download_torrent(
tracker_response: None,
info_hash,
peer_id: p_state.lock().await.peer_id.to_vec(),
pieces: Some(init_pieces(&metainfo.info)),
pieces: None,
peers: peers
.into_iter()
.map(|p| (p.clone(), Peer::new(p)))
Expand Down

0 comments on commit 4640011

Please sign in to comment.