Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
VxDxK committed Sep 3, 2024
1 parent fdc13ff commit 5034455
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
8 changes: 2 additions & 6 deletions torrent-client/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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![];
Expand Down
19 changes: 17 additions & 2 deletions torrent-client/src/client/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:?}")
Expand Down
17 changes: 17 additions & 0 deletions torrent-client/src/peer/connection.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<u8> = message.into();
self.tcp_connection.write_all(bytes.as_slice())?;
Ok(())
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -210,6 +217,16 @@ impl Display for Message {
}
}

impl From<Message> for Vec<u8> {
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;

Expand Down
6 changes: 3 additions & 3 deletions torrent-client/src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl AnnounceResponse {
pub struct ScrapeResponse;

pub trait TrackerClient {
fn announce(&self, url: Url, params: AnnounceParameters) -> Result<AnnounceResponse>;
fn announce(&self, url: &Url, params: AnnounceParameters) -> Result<AnnounceResponse>;
fn scrape(&self) -> Result<ScrapeResponse>;
}

Expand Down Expand Up @@ -285,13 +285,13 @@ impl HttpTracker {
}

impl TrackerClient for HttpTracker {
fn announce(&self, url: Url, params: AnnounceParameters) -> Result<AnnounceResponse> {
fn announce(&self, url: &Url, params: AnnounceParameters) -> Result<AnnounceResponse> {
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}")))?;

Expand Down

0 comments on commit 5034455

Please # to comment.