6565from .resource import Resource
6666from .torrentfile import DelugeTorrentInput
6767
68- defer .setDebugging (True )
6968router .register_handler (DelugeTorrentInput .plugin_name , DelugeTorrentInput , True , False , False )
7069
7170VIDEO_STREAMABLE_EXTENSIONS = ['mkv' , 'mp4' , 'iso' , 'ogg' , 'ogm' , 'm4v' ]
@@ -119,7 +118,7 @@ def get_file_priorities(self):
119118 # Ensure file_priorities option is populated.
120119 self .set_file_priorities ([])
121120
122- return self .options ["file_priorities" ]
121+ return list ( self .options ["file_priorities" ])
123122
124123 torrent = component .get ("TorrentManager" ).torrents .get (infohash , None )
125124 if torrent and not hasattr (torrent , 'get_file_priorities' ):
@@ -200,7 +199,7 @@ def can_read(self, from_byte):
200199 self .torrent .handle .set_piece_deadline (needed_piece , 0 )
201200 self .torrent .handle .piece_priority (needed_piece , MAX_PIECE_PRIORITY )
202201
203- file_priorities = self .torrent .get_file_priorities ()
202+ file_priorities = list ( self .torrent .get_file_priorities () )
204203 if file_priorities [f ['index' ]] != MAX_FILE_PRIORITY :
205204 logger .debug ('Also setting file to max %r' % (f , ))
206205 file_priorities [f ['index' ]] = MAX_FILE_PRIORITY
@@ -224,7 +223,8 @@ def can_read(self, from_byte):
224223 logger .debug ('Calling read again to get the real number' )
225224 return self .can_read (from_byte )
226225 else :
227- return ((last_available_piece - needed_piece ) * self .piece_length ) + self .piece_length - rest
226+ logger .debug ('Really last available piece is %s' % (last_available_piece , ))
227+ return ((last_available_piece - needed_piece ) * self .piece_length ) + self .piece_length - rest , last_available_piece
228228
229229 def is_idle (self ):
230230 return not self .readers and self .last_activity + TORRENT_CLEANUP_INTERVAL < datetime .now ()
@@ -275,7 +275,7 @@ def _cycle(self):
275275 logger .debug ('We had a fileset not started, must_whitelist:%r first_files:%r cannot_blacklist:%r' % (must_whitelist , first_files , cannot_blacklist ))
276276 status = self .torrent .get_status (['files' , 'file_progress' ])
277277
278- file_priorities = self .torrent .get_file_priorities ()
278+ file_priorities = list ( self .torrent .get_file_priorities () )
279279 for f , progress in zip (status ['files' ], status ['file_progress' ]):
280280 i = f ['index' ]
281281 if progress == 1.0 :
@@ -314,7 +314,7 @@ def _cycle(self):
314314 else :
315315 fileset_ranges [fileset_hash ] = fileset ['files' ].index (path )
316316
317- file_priorities = self .torrent .get_file_priorities ()
317+ file_priorities = list ( self .torrent .get_file_priorities () )
318318 logger .debug ('Fileset heads: %r' % (fileset_ranges , ))
319319 for fileset_hash , first_file in fileset_ranges .items ():
320320 fileset = self .filesets [fileset_hash ]
@@ -387,6 +387,14 @@ def add_fileset(self, fileset):
387387 if fileset_hash not in self .filesets :
388388 self .filesets [fileset_hash ] = {'started' : False , 'files' : files }
389389
390+ def request_piece (self , piece ):
391+ self .torrent .handle .read_piece (piece )
392+
393+ def new_piece_available (self , piece , data ):
394+ logger .debug ("New pice available: %s" % (piece , ))
395+ for reader in self .readers .keys ():
396+ reader .new_piece_available (piece , data )
397+
390398
391399class TorrentHandler (object ):
392400 def __init__ (self , reset_priorities_on_finish , aggressive_prioritizing = False ):
@@ -397,6 +405,7 @@ def __init__(self, reset_priorities_on_finish, aggressive_prioritizing=False):
397405 self .alerts = component .get ("AlertManager" )
398406 self .alerts .register_handler ("torrent_removed_alert" , self .on_alert_torrent_removed )
399407 self .alerts .register_handler ("torrent_finished_alert" , self .on_alert_torrent_finished )
408+ self .alerts .register_handler ("read_piece_alert" , self .on_alert_read_piece )
400409
401410 self .cleanup_looping_call = task .LoopingCall (self .cleanup )
402411 self .cleanup_looping_call .start (60 )
@@ -427,6 +436,18 @@ def on_alert_torrent_finished(self, alert):
427436 if self .reset_priorities_on_finish :
428437 self .torrents [infohash ].reset_priorities ()
429438
439+ def on_alert_read_piece (self , alert ):
440+ try :
441+ infohash = str (alert .handle .info_hash ())
442+ except (RuntimeError , KeyError ):
443+ logger .warning ('Failed to handle on read piece alert' )
444+ return
445+
446+ if infohash not in self .torrents :
447+ return
448+
449+ self .torrents [infohash ].new_piece_available (alert .piece , alert .buffer )
450+
430451 def shutdown (self ):
431452 for torrent in self .torrents .values ():
432453 if self .reset_priorities_on_finish :
0 commit comments