@@ -10,7 +10,7 @@ use std::collections::btree_map::Entry;
10
10
use crate :: database:: SqliteDatabase ;
11
11
use std:: sync:: Arc ;
12
12
use aquatic_udp_protocol:: { AnnounceEvent , NumberOfBytes } ;
13
- use log:: debug;
13
+ use log:: { debug} ;
14
14
use crate :: key_manager:: AuthKey ;
15
15
use r2d2_sqlite:: rusqlite;
16
16
use crate :: torrust_http_tracker:: AnnounceRequest ;
@@ -270,12 +270,10 @@ pub struct TorrentTracker {
270
270
}
271
271
272
272
impl TorrentTracker {
273
- pub fn new ( config : Arc < Configuration > ) -> TorrentTracker {
274
- let database = SqliteDatabase :: new ( & config. db_path ) . unwrap_or_else ( |error| {
275
- panic ! ( "Could not create SQLite database. Reason: {}" , error)
276
- } ) ;
273
+ pub fn new ( config : Arc < Configuration > ) -> Result < TorrentTracker , rusqlite:: Error > {
274
+ let database = SqliteDatabase :: new ( & config. db_path ) ?;
277
275
278
- TorrentTracker {
276
+ Ok ( TorrentTracker {
279
277
config,
280
278
torrents : RwLock :: new ( std:: collections:: BTreeMap :: new ( ) ) ,
281
279
database,
@@ -293,7 +291,7 @@ impl TorrentTracker {
293
291
udp6_announces_handled : 0 ,
294
292
udp6_scrapes_handled : 0 ,
295
293
} ) ,
296
- }
294
+ } )
297
295
}
298
296
299
297
fn is_public ( & self ) -> bool {
@@ -355,13 +353,20 @@ impl TorrentTracker {
355
353
}
356
354
357
355
// Loading the torrents into memory
358
- pub async fn load_torrents ( & self , tracker : Arc < TorrentTracker > ) -> Result < bool , rusqlite:: Error > {
359
- self . database . load_persistent_torrent_data ( tracker) . await
356
+ pub async fn load_torrents ( & self ) -> Result < ( ) , rusqlite:: Error > {
357
+ let torrents = self . database . load_persistent_torrent_data ( ) . await ?;
358
+
359
+ for torrent in torrents {
360
+ self . add_torrent ( torrent. 0 , 0 , torrent. 1 , 0 ) . await ;
361
+ }
362
+
363
+ Ok ( ( ) )
360
364
}
361
365
362
366
// Saving the torrents from memory
363
- pub async fn save_torrents ( & self , tracker : Arc < TorrentTracker > ) -> Result < bool , rusqlite:: Error > {
364
- self . database . save_persistent_torrent_data ( tracker) . await
367
+ pub async fn save_torrents ( & self ) -> Result < ( ) , rusqlite:: Error > {
368
+ let torrents = self . torrents . read ( ) . await ;
369
+ self . database . save_persistent_torrent_data ( & * torrents) . await
365
370
}
366
371
367
372
// Adding torrents is not relevant to public trackers.
@@ -419,12 +424,15 @@ impl TorrentTracker {
419
424
}
420
425
}
421
426
422
- pub async fn add_torrent ( & self , info_hash : & InfoHash , seeders : u32 , completed : u32 , leechers : u32 ) -> TorrentStats {
427
+ pub async fn add_torrent ( & self , info_hash : InfoHash , seeders : u32 , completed : u32 , leechers : u32 ) -> TorrentStats {
423
428
let mut torrents = self . torrents . write ( ) . await ;
424
429
425
430
if !torrents. contains_key ( & info_hash) {
426
- let mut torrent_entry = TorrentEntry :: new ( ) ;
427
- torrent_entry. completed = completed;
431
+ let torrent_entry = TorrentEntry {
432
+ peers : Default :: default ( ) ,
433
+ completed,
434
+ seeders
435
+ } ;
428
436
torrents. insert ( info_hash. clone ( ) , torrent_entry) ;
429
437
}
430
438
0 commit comments