Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add on_piece_completed method on TorrentStorage #219

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/librqbit/src/file_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ impl<'a> FileOps<'a> {
format!("error reading {to_read_in_file} bytes, file_id: {file_idx} (\"{name:?}\")")
})?;

self.files.on_piece_completed(piece_index)?;

piece_remaining_bytes -= to_read_in_file;

if piece_remaining_bytes == 0 {
Expand Down
5 changes: 4 additions & 1 deletion crates/librqbit/src/peer_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use librqbit_core::{
};
use parking_lot::RwLock;
use peer_binary_protocol::{
extended::{handshake::{ExtendedHandshake, YourIP}, ExtendedMessage},
extended::{
handshake::{ExtendedHandshake, YourIP},
ExtendedMessage,
},
serialize_piece_preamble, Handshake, Message, MessageOwned, PIECE_MESSAGE_DEFAULT_LEN,
};
use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions crates/librqbit/src/storage/examples/inmemory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl StorageFactory for InMemoryExampleStorageFactory {

fn create(
&self,
info: &crate::torrent_state::ManagedTorrentInfo,
info: &crate::torrent_state::ManagedTorrentShared,
) -> anyhow::Result<InMemoryExampleStorage> {
InMemoryExampleStorage::new(info.lengths, info.file_infos.clone())
}
Expand Down Expand Up @@ -111,7 +111,7 @@ impl TorrentStorage for InMemoryExampleStorage {
}))
}

fn init(&mut self, _meta: &crate::ManagedTorrentInfo) -> anyhow::Result<()> {
fn init(&mut self, _meta: &crate::ManagedTorrentShared) -> anyhow::Result<()> {
Ok(())
}

Expand Down
10 changes: 7 additions & 3 deletions crates/librqbit/src/storage/examples/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use parking_lot::RwLock;

use crate::{
storage::{StorageFactory, StorageFactoryExt, TorrentStorage},
FileInfos, ManagedTorrentInfo,
FileInfos, ManagedTorrentShared,
};

#[derive(Default, Clone)]
Expand All @@ -18,7 +18,7 @@ pub struct MmapStorage {
impl StorageFactory for MmapStorageFactory {
type Storage = MmapStorage;

fn create(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Self::Storage> {
fn create(&self, info: &ManagedTorrentShared) -> anyhow::Result<Self::Storage> {
Ok(MmapStorage {
mmap: RwLock::new(
MmapOptions::new()
Expand Down Expand Up @@ -63,11 +63,15 @@ impl TorrentStorage for MmapStorage {
anyhow::bail!("not implemented")
}

fn init(&mut self, _meta: &ManagedTorrentInfo) -> anyhow::Result<()> {
fn init(&mut self, _meta: &ManagedTorrentShared) -> anyhow::Result<()> {
Ok(())
}

fn remove_directory_if_empty(&self, _path: &std::path::Path) -> anyhow::Result<()> {
Ok(())
}

fn on_piece_completed(&self, _file_id: usize, _offset: u64) -> anyhow::Result<()> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably make sense to implement default in the trait itself not to add this to every single storage example that exists.

There's also "storage_middleware" feature that will break if turned on (although I do understand it will break already anyway because ManagedTorrentInfo was renamed, but that's a separate thing)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added default (empty) implementation in the trait itself, can still be override in trait implementations.

I checked and a cargo check --all-features build (apart from an error of multiple definition of Sha1

error[E0428]: the name `Sha1` is defined multiple times
  --> crates/sha1w/src/lib.rs:79:1
   |
76 | pub type Sha1 = crypto_hash_impl::Sha1CryptoHash;
   | ------------------------------------------------- previous definition of the type `Sha1` here
...
79 | pub type Sha1 = ring_impl::Sha1Ring;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Sha1` redefined here
   |
   = note: `Sha1` must be defined only once in the type namespace of this module

Ok(())
}
}
12 changes: 12 additions & 0 deletions crates/librqbit/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::{
path::Path,
};

use librqbit_core::lengths::ValidPieceIndex;

use crate::torrent_state::ManagedTorrentShared;

pub trait StorageFactory: Send + Sync + Any {
Expand Down Expand Up @@ -97,6 +99,12 @@ pub trait TorrentStorage: Send + Sync {
/// Replace the current storage with a dummy, and return a new one that should be used instead.
/// This is used to make the underlying object useless when e.g. pausing the torrent.
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>>;

/// Callback called every time a piece has completed and has been validated.
/// Default implementation does nothing, but can be override in trait implementations.
fn on_piece_completed(&self, _piece_index: ValidPieceIndex) -> anyhow::Result<()> {
Ok(())
}
}

impl<U: TorrentStorage + ?Sized> TorrentStorage for Box<U> {
Expand Down Expand Up @@ -127,4 +135,8 @@ impl<U: TorrentStorage + ?Sized> TorrentStorage for Box<U> {
fn init(&mut self, meta: &ManagedTorrentShared) -> anyhow::Result<()> {
(**self).init(meta)
}

fn on_piece_completed(&self, piece_id: ValidPieceIndex) -> anyhow::Result<()> {
(**self).on_piece_completed(piece_id)
}
}
3 changes: 2 additions & 1 deletion crates/librqbit/src/tests/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
time::Duration,
};

use anyhow::{bail, Context};
use anyhow::bail;
use librqbit_core::Id20;
use parking_lot::RwLock;
use rand::{thread_rng, Rng, RngCore, SeedableRng};
Expand Down Expand Up @@ -97,6 +97,7 @@ impl TestPeerMetadata {

#[cfg(feature = "http-api")]
async fn debug_server() -> anyhow::Result<()> {
use anyhow::Context;
use axum::{response::IntoResponse, routing::get, Router};
async fn backtraces() -> impl IntoResponse {
#[cfg(feature = "async-bt")]
Expand Down
2 changes: 1 addition & 1 deletion crates/peer_binary_protocol/src/extended/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Serialize for YourIP {
IpAddr::V6(ipv6) => {
let buf = ipv6.octets();
serializer.serialize_bytes(&buf)
},
}
}
}
}
Expand Down