r262 - /debtorrent/trunk/DebTorrent/BT1/FileSelector.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Fri Aug 17 07:07:25 UTC 2007


Author: camrdale-guest
Date: Fri Aug 17 07:07:24 2007
New Revision: 262

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=262
Log:
Make sure the FileSelector priorities aren't updated before the initialization is complete.

Modified:
    debtorrent/trunk/DebTorrent/BT1/FileSelector.py

Modified: debtorrent/trunk/DebTorrent/BT1/FileSelector.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/FileSelector.py?rev=262&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/FileSelector.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/FileSelector.py Fri Aug 17 07:07:24 2007
@@ -46,6 +46,8 @@
          0 -- highest priority
          1 -- medium priority
          2 -- lowest priority
+    @type init_priority: C{list} of C{int}
+    @ivar init_priority: the initial unpickled priority of each file in the download
     @type new_priority: C{list} of C{int}
     @ivar new_priority: the new priority of each file in the download
     @type new_partials: C{list} of C{int}
@@ -64,8 +66,6 @@
         newly enabled piece download
     @type rerequestfunc: C{method}
     @ivar rerequestfunc: method to call to request more peers
-    @type new_piece_priority: C{list} of C{int}
-    @ivar new_piece_priority: the new priority for each piece in the download
     
     """
     
@@ -99,11 +99,15 @@
         self.failfunc = failfunc
         self.downloader = None
         self.picker = picker
+        self.cancelfunc = None
+        self.requestmorefunc = None
+        self.rerequestfunc = None
 
         storage.set_bufferdir(bufferdir)
         
         self.numfiles = len(files)
         self.priority = [-1] * self.numfiles
+        self.init_priority = None
         self.new_priority = None
         self.new_partials = None
         self.filepieces = []
@@ -131,8 +135,8 @@
         
 
 
-    def init_priority(self, new_priority):
-        """Initialize the priorities of all the files.
+    def init_priorities(self, init_priority):
+        """Initialize the priorities of all the files from the unpickled state.
         
         @type new_priority: C{list} of C{int}
         @param new_priority: the new file priorities
@@ -142,8 +146,8 @@
         """
         
         try:
-            assert len(new_priority) == self.numfiles
-            for v in new_priority:
+            assert len(init_priority) == self.numfiles
+            for v in init_priority:
                 assert type(v) in (type(0),type(0L))
                 assert v >= -1
                 assert v <= 2
@@ -152,12 +156,12 @@
             return False
         try:
             for f in xrange(self.numfiles):
-                if new_priority[f] < 0:
+                if init_priority[f] < 0:
                     self.storage.disable_file(f)
                 else:
                     self.storage.enable_file(f)
             self.storage.reset_file_status()
-            self.new_priority = new_priority
+            self.init_priority = init_priority
         except (IOError, OSError), e:
             self.failfunc("can't open partial file for "
                           + self.files[f][0] + ': ' + str(e))
@@ -184,13 +188,13 @@
         """
         
         if d.has_key('priority'):
-            if not self.init_priority(d['priority']):
+            if not self.init_priorities(d['priority']):
                 return
         pieces = self.storage.unpickle(d)
-        new_piece_priority = self._get_piece_priority_list(self.new_priority)
-        self.storagewrapper.reblock([i == -1 for i in new_piece_priority])
+        init_piece_priority = self._get_piece_priority_list(self.init_priority)
+        self.storagewrapper.reblock([i == -1 for i in init_piece_priority])
         self.new_partials = self.storagewrapper.unpickle(d, pieces)
-        self.piece_priority = self._initialize_piece_priority(self.new_priority)
+        self.piece_priority = self._initialize_piece_priority(self.init_priority)
 
 
     def tie_in(self, cancelfunc, requestmorefunc, rerequestfunc):
@@ -210,52 +214,22 @@
         self.requestmorefunc = requestmorefunc
         self.rerequestfunc = rerequestfunc
 
-        if self.new_priority:
-            self.priority = self.new_priority
-            self.new_priority = None
-            self.new_piece_priority = self._set_piece_priority(self.priority)
-
+        # Set up the unpickled initial priorities
+        if self.init_priority:
+            self.priority = self.init_priority
+            self.init_priority = None
+
+        # Set up the unpickled list of partially completed pieces
         if self.new_partials:
             shuffle(self.new_partials)
             for p in self.new_partials:
                 self.picker.requested(p)
         self.new_partials = None
         
-
-    def _initialize_files_disabled(self, old_priority, new_priority):
-        """Initialize the disabled files on startup.
-        
-        @type old_priority: C{list} of C{int}
-        @param old_priority: the old file priorities
-        @type new_priority: C{list} of C{int}
-        @param new_priority: the new file priorities
-        @rtype: C{boolean}
-        @return: whether the initialization was successful
-        
-        """
-        
-        old_disabled = [p == -1 for p in old_priority]
-        new_disabled = [p == -1 for p in new_priority]
-        files_updated = False        
-        try:
-            for f in xrange(self.numfiles):
-                if new_disabled[f] and not old_disabled[f]:
-                    self.storage.disable_file(f)
-                    files_updated = True
-                if old_disabled[f] and not new_disabled[f]:
-                    self.storage.enable_file(f)
-                    files_updated = True
-        except (IOError, OSError), e:
-            if new_disabled[f]:
-                msg = "can't open partial file for "
-            else:
-                msg = 'unable to open '
-            self.failfunc(msg + self.files[f][0] + ': ' + str(e))
-            return False
-        if files_updated:
-            self.storage.reset_file_status()
-        return True        
-
+        # Schedule the processing of any early arrivals of new priorities
+        if self.new_priority:
+            self.sched(self.set_priorities_now)
+        
 
     def _set_files_disabled(self, old_priority, new_priority):
         """Disable files based on a new priority setting.
@@ -390,23 +364,6 @@
         return new_piece_priority        
 
 
-    def initialize_priorities_now(self, new_priority = None):
-        """Initialize the priorities on startup.
-        
-        @type new_priority: C{list} of C{int}
-        @param new_priority: the new file priorities
-            (optional, defaults to not initializing anything)
-        
-        """
-
-        if not new_priority:
-            return
-        old_priority = self.priority
-        self.priority = new_priority
-        if not self._initialize_files_disabled(old_priority, new_priority):
-            return
-        self.piece_priority = self._initialize_piece_priority(new_priority)
-
     def set_priorities_now(self, new_priority = None):
         """Set the new priorities.
         
@@ -436,7 +393,9 @@
         """
 
         self.new_priority = new_priority
-        self.sched(self.set_priorities_now)
+        # Only set the new priorities if the tie_in initialization is complete
+        if self.requestmorefunc:
+            self.sched(self.set_priorities_now)
         
     def set_priority(self, f, p):
         """Set the priority of a single file.
@@ -462,7 +421,9 @@
         
         priority = self.new_priority
         if not priority:
-            priority = self.priority    # potential race condition
+            priority = self.init_priority
+            if not priority:
+                priority = self.priority    # potential race condition
         return [i for i in priority]
 
     def __setitem__(self, index, val):
@@ -490,7 +451,10 @@
         try:
             return self.new_priority[index]
         except:
-            return self.priority[index]
+            try:
+                return self.init_priority[index]
+            except:
+                return self.priority[index]
 
 
     def finish(self):




More information about the Debtorrent-commits mailing list