diff --git a/torrent-client/src/client/mod.rs b/torrent-client/src/client/mod.rs index 3115d9c..8a3ab22 100644 --- a/torrent-client/src/client/mod.rs +++ b/torrent-client/src/client/mod.rs @@ -1,16 +1,12 @@ mod worker; +use crate::client::worker::PeerWorker; use crate::file::TorrentFile; -use crate::peer::connection::PeerConnection; use crate::peer::PeerId; use crate::tracker::{AnnounceParameters, RequestMode, TrackerClient, TrackerError}; use rand::seq::SliceRandom; -use std::collections::VecDeque; -use std::net::TcpStream; use std::sync::{Arc, Mutex}; -use std::time::Duration; use thiserror::Error; -use crate::client::worker::PeerWorker; #[derive(Error, Debug)] pub enum ClientError { @@ -54,7 +50,7 @@ impl Client { .set_port(6881) .set_num_want(Some(100)) .set_request_mode(RequestMode::Verbose); - let mut torrent_info = self.tracker_client.announce(meta.announce.clone(), params)?; + let mut torrent_info = self.tracker_client.announce(&meta.announce, params)?; torrent_info.peers.shuffle(&mut rand::thread_rng()); let peers = Arc::new(Mutex::new(torrent_info.peers)); let mut handles = vec![]; diff --git a/torrent-client/src/client/worker.rs b/torrent-client/src/client/worker.rs index 3829786..2073b5d 100644 --- a/torrent-client/src/client/worker.rs +++ b/torrent-client/src/client/worker.rs @@ -2,7 +2,7 @@ use std::net::TcpStream; use std::sync::{Arc, Mutex}; use std::time::Duration; use crate::file::TorrentFile; -use crate::peer::connection::PeerConnection; +use crate::peer::connection::{Message, PeerConnection}; use crate::peer::{Peer, PeerId}; pub struct PeerWorker { @@ -33,7 +33,22 @@ impl PeerWorker { loop { match connection.recv() { Ok(message) => { - println!("message {message}") + println!("message {message}"); + match message { + Message::KeepAlive => {} + Message::Choke => {} + Message::UnChoke => { + + } + Message::Interested => {} + Message::NotInterested => {} + Message::Have(_) => {} + Message::Bitfield(_) => {} + Message::Request(_) => {} + Message::Piece(_) => {} + Message::Cancel(_) => {} + Message::Port(_) => {} + } } Err(err) => { println!("error {err:?}") diff --git a/torrent-client/src/peer/connection.rs b/torrent-client/src/peer/connection.rs index 0efb56e..2d0eb73 100644 --- a/torrent-client/src/peer/connection.rs +++ b/torrent-client/src/peer/connection.rs @@ -1,3 +1,4 @@ +use std::cmp::PartialEq; use std::fmt::{format, Display, Formatter}; use crate::peer::connection::ConnectionError::{HandshakeFailed, MessageId, PayloadLength, Todo, UnexpectedEOF}; use crate::peer::connection::HandshakeMessageError::{ProtocolString, ProtocolStringLen}; @@ -127,6 +128,12 @@ impl PeerConnection { let message = Message::try_from(data.as_slice())?; Ok(message) } + + pub fn send(&mut self, message: Message) -> Result<()> { + let bytes: Vec = message.into(); + self.tcp_connection.write_all(bytes.as_slice())?; + Ok(()) + } } #[derive(Debug, Clone)] @@ -210,6 +217,16 @@ impl Display for Message { } } +impl From for Vec { + fn from(value: Message) -> Self { + let mut result = Vec::new(); + result.extend_from_slice(1u32.to_ne_bytes().as_slice()); + + + result + } +} + impl TryFrom<&[u8]> for Message { type Error = ConnectionError; diff --git a/torrent-client/src/tracker/mod.rs b/torrent-client/src/tracker/mod.rs index 11eb202..d0e08b9 100644 --- a/torrent-client/src/tracker/mod.rs +++ b/torrent-client/src/tracker/mod.rs @@ -216,7 +216,7 @@ impl AnnounceResponse { pub struct ScrapeResponse; pub trait TrackerClient { - fn announce(&self, url: Url, params: AnnounceParameters) -> Result; + fn announce(&self, url: &Url, params: AnnounceParameters) -> Result; fn scrape(&self) -> Result; } @@ -285,13 +285,13 @@ impl HttpTracker { } impl TrackerClient for HttpTracker { - fn announce(&self, url: Url, params: AnnounceParameters) -> Result { + fn announce(&self, url: &Url, params: AnnounceParameters) -> Result { if !(url.scheme() != "http" || url.scheme() != "https") { return Err(UnsupportedProtocol(String::from(url.scheme()))); } let tracker_response = self .http_client - .get(self.build_announce_url(url, params)) + .get(self.build_announce_url(url.clone(), params)) .send() .map_err(|e| AnnounceRequestError(format!("send request to tracker failed {e}")))?;