All pastes #569097 Raw Edit

Someone

public text v1 · immutable
·published 2007-06-16 00:22 UTC
rendered paste body
Index: org/gudy/azureus2/core3/util/TorrentUtils.java
===================================================================
RCS file: /cvsroot/azureus/azureus2/org/gudy/azureus2/core3/util/TorrentUtils.java,v
retrieving revision 1.73
diff -u -r1.73 TorrentUtils.java
--- org/gudy/azureus2/core3/util/TorrentUtils.java	4 Jun 2007 00:33:55 -0000	1.73
+++ org/gudy/azureus2/core3/util/TorrentUtils.java	16 Jun 2007 00:22:11 -0000
@@ -77,8 +77,7 @@
 		COConfigurationManager.addAndFireParameterListener("Save Torrent Backup",
 				new ParameterListener() {
 					public void parameterChanged(String parameterName) {
-						bSaveTorrentBackup = COConfigurationManager.getBooleanParameter(
-								parameterName, false);
+						bSaveTorrentBackup = COConfigurationManager.getBooleanParameter(parameterName);
 					}
 				});
 		
@@ -116,6 +115,16 @@
 		 * @throws TOTorrentException
 		 */
 	
+	public static ExtendedTorrent
+	readDelegateFromFile(
+		File		file,
+		boolean		force_initial_discard )
+		
+		throws TOTorrentException
+	{
+		return((ExtendedTorrent)readFromFile( file, true, force_initial_discard ));
+	}
+	
 	public static TOTorrent
 	readFromFile(
 		File		file,
@@ -240,11 +249,18 @@
 	   try{
 	   		torrent.getMonitor().enter();
 	    	
-	   			// we've got to re-obtain the pieces here in case they've been thrown
+	   			// we've got to re-obtain the pieces etc. here in case they've been thrown
 	   			// away to save memory *before* we rename the torrent file!
-	   		
+	   		   		
 	   		torrent.getPieces();
 	   		
+	   			// restore fluff too
+	   		
+	   		if  ( torrent_map_fluff != null ){
+	   			
+	   			torrent.getAdditionalMapProperty( torrent_map_fluff );
+	   		}
+	   		
 	    	String str = torrent.getAdditionalStringProperty("torrent filename");
 	    	
 	    	if (str == null){
@@ -259,7 +275,7 @@
 	    	
 	    	File torrent_file = new File(str);
 	    	
-	    	if ( 	( force_backup ||COConfigurationManager.getBooleanParameter("Save Torrent Backup", false)) &&
+	    	if ( 	( force_backup ||COConfigurationManager.getBooleanParameter("Save Torrent Backup")) &&
 	    			torrent_file.exists()) {
 	    		
 	    		File torrent_file_bak = new File(str + ".bak");
@@ -335,20 +351,7 @@
 
 		throws TOTorrentException 
 	{
-		try{
-	   		torrent.getMonitor().enter();
-	    	
-	   			// we've got to re-obtain the pieces here in case they've been thrown
-	   			// away to save memory *before* we rename the torrent file!
-	   		
-	   		torrent.getPieces();
-	   			      
-	    	torrent.serialiseToBEncodedFile(file);
-			
-	   	}finally{
-	   		
-	   		torrent.getMonitor().exit();
-	   	}	
+	   	torrent.serialiseToBEncodedFile(file);
 	}
 	
 	public static void
@@ -1338,16 +1341,51 @@
 			});
 	}
 	
-	public static class
+	private static String	torrent_map_fluff;
+	private static Map		discarded_map = new HashMap(1);
+	
+	public static void
+	registerMapFluff(
+		String		_fluff )
+	{
+		synchronized( TorrentUtils.class ){
+			
+				// lazyness - currently only one fluff item 
+			
+			if ( torrent_map_fluff != null ){
+				
+				Debug.out( "Excessive fluff detected" );
+				
+			}else{
+				
+				torrent_map_fluff = _fluff;
+			}
+		}
+	}
+	
+	public interface
+	ExtendedTorrent
+		extends TOTorrent
+	{
+		public byte[][]
+		peekPieces()
+		    		
+			throws TOTorrentException;
+		
+		public void
+		setDiscardFluff(
+			boolean	discard );
+	}
+	
+	private static class
 	torrentDelegate
 		extends LogRelation
-		implements TOTorrent
+		implements ExtendedTorrent
 	{
 		private TOTorrent		delegate;
 		private File			file;
 		
 		private long			last_pieces_read_time	= SystemTime.getCurrentTime();
-		private byte[][]		pieces;
 		
 		protected
 		torrentDelegate(
@@ -1363,6 +1401,26 @@
 			}
 		}
 		
+		public void
+		setDiscardFluff(
+			boolean	discard )
+		{
+			if ( discard && torrent_map_fluff != null ){
+				
+				System.out.println( "Discarded fluff for " + new String(getName()));
+				
+				try{
+			   		getMonitor().enter();
+					
+					delegate.setAdditionalMapProperty( torrent_map_fluff, discarded_map );
+					
+				}finally{
+					
+					getMonitor().exit();
+				}
+			}
+		}
+		
 		public byte[]
 		getName()
 		{
@@ -1484,11 +1542,9 @@
 				try{
 			   		getMonitor().enter();
 
-			   		TOTorrent	temp = readFromFile( file, false );
-					
-			   		res	= temp.getPieces();
-					
-			   		delegate.setPieces( res );
+			   		restoreState( true, false );
+			   		
+			   		res = delegate.getPieces();
 			   		
 				}finally{
 					
@@ -1499,6 +1555,58 @@
 			return( res );
 		}
 
+		/**
+		 * monitor must be held before calling me
+		 * @param do_pieces
+		 * @param do_fluff
+		 * @throws TOTorrentException
+		 */
+		
+		protected boolean[]
+		restoreState(
+			boolean		do_pieces,
+			boolean		do_fluff )
+		
+			throws TOTorrentException
+		{
+	   		boolean	had_pieces = delegate.getPieces() != null;
+	   		
+	   		boolean	had_fluff = 
+	   			torrent_map_fluff == null || 
+	   			delegate.getAdditionalMapProperty( torrent_map_fluff ) != discarded_map;
+
+	   		if ( had_pieces ){
+	   			
+	   			do_pieces = false;
+	   		}
+	   		
+	   		if ( had_fluff ){
+	   			
+	   			do_fluff = false;
+	   		}
+	   		
+	   		if ( do_pieces || do_fluff ){
+	   		
+		   		TOTorrent	temp = readFromFile( file, false );
+				
+		   		if ( do_pieces ){
+		   		
+		   			byte[][] res	= temp.getPieces();
+				
+		   			delegate.setPieces( res );
+		   		}
+		   		
+		   		if ( do_fluff ){
+		   			
+		   			delegate.setAdditionalMapProperty(
+		   					torrent_map_fluff, 
+		   					temp.getAdditionalMapProperty( torrent_map_fluff ));
+		   		}
+	   		}
+	   		
+	   		return(new boolean[]{ do_pieces, do_fluff });
+		}
+		
 			/**
 			 * peeks the pieces, will return null if they are discarded
 			 * @return
@@ -1651,13 +1759,58 @@
 			String		name,
 			Map			value )
 		{
-			delegate.setAdditionalMapProperty( name, value );
+			if ( torrent_map_fluff != null && name.equals( torrent_map_fluff )){
+
+				System.out.println( "Set fluff for " + new String(getName()) + " to " + value );
+
+				try{
+					getMonitor().enter();
+
+					delegate.setAdditionalMapProperty( name, value );
+					
+				}finally{
+					
+					getMonitor().exit();
+				}
+			}else{
+				
+				delegate.setAdditionalMapProperty( name, value );	
+			}
 		}
 			
 		public Map
 		getAdditionalMapProperty(
 			String		name )
 		{
+			if ( torrent_map_fluff != null && name.equals( torrent_map_fluff )){
+				
+				try{
+					getMonitor().enter();
+
+					Map	result = delegate.getAdditionalMapProperty( name );
+					
+					if ( result == discarded_map ){
+					
+						try{
+							restoreState( false, true );
+							
+							Map res = delegate.getAdditionalMapProperty( name );
+
+							System.out.println( "Restored fluff for " + new String(getName()) + " to " + res );
+
+							return( res );
+							
+						}catch( Throwable e ){
+							
+							Debug.out( "Property '" + name + " lost due to torrent read error", e );
+						}
+					}
+				}finally{
+					
+					getMonitor().exit();
+				}
+			}
+			
 			return( delegate.getAdditionalMapProperty( name ));
 		}
 		
@@ -1681,15 +1834,13 @@
 			String name )
 		{
 			delegate.removeAdditionalProperty( name );
-		}
-		
+		}	
 		
 		public void
 		removeAdditionalProperties()
 		{
 			delegate.removeAdditionalProperties();
-		}
-		
+		}		
 
 		public void
 		serialiseToBEncodedFile(
@@ -1701,17 +1852,20 @@
 			
 			try{
 		   		getMonitor().enter();
+		   				   		
+		   		boolean[]	restored = restoreState( true, true );
 		   		
-		   		boolean	had_pieces = delegate.getPieces() != null;
-		   		
-		   		getPieces();
-			
 		   		delegate.serialiseToBEncodedFile( target_file );
 		   		
-		   		if ( !had_pieces ){
+		   		if ( restored[0] ){
 		   			
 		   			discardPieces( SystemTime.getCurrentTime(), true );
 		   		}
+		   		
+		   		if ( restored[1] ){
+		   			
+		   			delegate.setAdditionalMapProperty( torrent_map_fluff, discarded_map );
+		   		}
 			}finally{
 				
 				getMonitor().exit();
@@ -1729,17 +1883,20 @@
 			try{
 		   		getMonitor().enter();
 		   		
-		   		boolean	had_pieces = delegate.getPieces() != null;
-
-		   		getPieces();
+		   		boolean[]	restored = restoreState( true, true );
 			
 		   		Map	result = delegate.serialiseToMap();
 		   		
-		   		if ( !had_pieces ){
+		   		if ( restored[0] ){
 		   			
 		   			discardPieces( SystemTime.getCurrentTime(), true );
 		   		}
 		   		
+		   		if ( restored[1] ){
+		   			
+		   			delegate.setAdditionalMapProperty( torrent_map_fluff, discarded_map );
+		   		}
+		   		
 		   		return( result );
 		   		
 			}finally{
@@ -1751,7 +1908,7 @@
 
 		public void
 		serialiseToXMLFile(
-				File		target_file )
+			File		target_file )
 			  
 		   throws TOTorrentException
 		{
@@ -1760,16 +1917,19 @@
 			try{
 		   		getMonitor().enter();
 		   		
-		   		boolean	had_pieces = delegate.getPieces() != null;
-
-		   		getPieces();
+		   		boolean[]	restored = restoreState( true, true );
 			
 		   		delegate.serialiseToXMLFile( target_file );
 			
-		   		if ( !had_pieces ){
+		   		if ( restored[0] ){
 		   			
 		   			discardPieces( SystemTime.getCurrentTime(), true );
 		   		}
+		   		
+		   		if ( restored[1] ){
+		   			
+		   			delegate.setAdditionalMapProperty( torrent_map_fluff, discarded_map );
+		   		}
 			}finally{
 				
 				getMonitor().exit();
Index: org/gudy/azureus2/core3/download/DownloadManagerState.java
===================================================================
RCS file: /cvsroot/azureus/azureus2/org/gudy/azureus2/core3/download/DownloadManagerState.java,v
retrieving revision 1.49
diff -u -r1.49 DownloadManagerState.java
--- org/gudy/azureus2/core3/download/DownloadManagerState.java	12 Jun 2007 07:49:23 -0000	1.49
+++ org/gudy/azureus2/core3/download/DownloadManagerState.java	16 Jun 2007 00:22:11 -0000
@@ -284,6 +284,10 @@
 	public boolean hasAttribute(String name);
 	
 	public void
+	setActive(
+		boolean	active );
+	
+	public void
 	save();
 	
 		/**
Index: org/gudy/azureus2/core3/download/impl/DownloadManagerImpl.java
===================================================================
RCS file: /cvsroot/azureus/azureus2/org/gudy/azureus2/core3/download/impl/DownloadManagerImpl.java,v
retrieving revision 1.329
diff -u -r1.329 DownloadManagerImpl.java
--- org/gudy/azureus2/core3/download/impl/DownloadManagerImpl.java	10 May 2007 00:40:31 -0000	1.329
+++ org/gudy/azureus2/core3/download/impl/DownloadManagerImpl.java	16 Jun 2007 00:22:11 -0000
@@ -544,7 +544,11 @@
 				
 				 download_manager_state	= 
 					 	DownloadManagerStateImpl.getDownloadState(
-					 			this, torrentFileName, torrent_hash, initial_state == DownloadManager.STATE_STOPPED );
+					 			this, 
+					 			torrentFileName, 
+					 			torrent_hash, 
+					 			initial_state == DownloadManager.STATE_STOPPED || 
+					 			initial_state == DownloadManager.STATE_QUEUED );
 				 
 				 readParameters();
 				 
@@ -1409,6 +1413,8 @@
 			return;
 		}
    
+		download_manager_state.setActive( true );
+		
 		try{
 			try{
 				this_mon.enter();
@@ -1561,7 +1567,13 @@
   		boolean	remove_torrent,
   		boolean	remove_data )
   	{
-  		controller.stopIt( state_after_stopping, remove_torrent, remove_data );
+  		try{
+  			controller.stopIt( state_after_stopping, remove_torrent, remove_data );
+  			
+  		}finally{
+  			
+			download_manager_state.setActive( false );
+  		}
   	}
   	
 	public boolean
Index: org/gudy/azureus2/core3/download/impl/DownloadManagerStateImpl.java
===================================================================
RCS file: /cvsroot/azureus/azureus2/org/gudy/azureus2/core3/download/impl/DownloadManagerStateImpl.java,v
retrieving revision 1.66
diff -u -r1.66 DownloadManagerStateImpl.java
--- org/gudy/azureus2/core3/download/impl/DownloadManagerStateImpl.java	12 Jun 2007 07:49:22 -0000	1.66
+++ org/gudy/azureus2/core3/download/impl/DownloadManagerStateImpl.java	16 Jun 2007 00:22:11 -0000
@@ -89,6 +89,8 @@
 			
 			default_attributes.put( ATTRIBUTE_DEFAULTS[i][0], ATTRIBUTE_DEFAULTS[i][1] );
 		}
+		
+		TorrentUtils.registerMapFluff( TRACKER_CACHE_KEY );
 	}
 	
 	private static AEMonitor	class_mon	= new AEMonitor( "DownloadManagerState:class" );
@@ -99,7 +101,7 @@
 	
 	private DownloadManagerImpl			download_manager;
 	
-	private TOTorrent					torrent;
+	private TorrentUtils.ExtendedTorrent	torrent;
 	
 	private boolean						write_required;
 	
@@ -123,8 +125,8 @@
 	private static DownloadManagerState
 	getDownloadState(
 		DownloadManagerImpl	download_manager,
-		TOTorrent			original_torrent,
-		TOTorrent			target_torrent )
+		TOTorrent						original_torrent,
+		TorrentUtils.ExtendedTorrent	target_torrent )
 	
 		throws TOTorrentException
 	{
@@ -179,14 +181,14 @@
 		
 		// System.out.println( "getDownloadState: hash = " + ByteFormatter.encodeString(torrent_hash));
 		
-		TOTorrent saved_state	= null;
+		TorrentUtils.ExtendedTorrent saved_state	= null;
 				
 		File	saved_file = getStateFile( torrent_hash ); 
 		
 		if ( saved_file.exists()){
 			
 			try{
-				saved_state = TorrentUtils.readFromFile( saved_file, true );
+				saved_state = TorrentUtils.readDelegateFromFile( saved_file, false );
 				
 			}catch( Throwable e ){
 				
@@ -200,7 +202,7 @@
 		
 			TorrentUtils.copyToFile( original_torrent, saved_file );
 			
-			saved_state = TorrentUtils.readFromFile( saved_file, true );
+			saved_state = TorrentUtils.readDelegateFromFile( saved_file, false );
 		}
 
 		return( getDownloadState( null, original_torrent, saved_state ));
@@ -211,7 +213,7 @@
 		DownloadManagerImpl	download_manager,
 		String				torrent_file,
 		byte[]				torrent_hash,
-		boolean				force_piece_discard )
+		boolean				inactive )
 	
 		throws TOTorrentException
 	{
@@ -219,8 +221,8 @@
 		
 		// System.out.println( "getDownloadState: hash = " + (torrent_hash==null?"null":ByteFormatter.encodeString(torrent_hash) + ", file = " + torrent_file ));
 
-		TOTorrent	original_torrent	= null;
-		TOTorrent 	saved_state			= null;
+		TOTorrent						original_torrent	= null;
+		TorrentUtils.ExtendedTorrent 	saved_state			= null;
 		
 			// first, if we already have the hash then see if we can load the saved state
 		
@@ -235,14 +237,15 @@
 					
 					if ( cached_state != null ){
 						
-						CachedStateWrapper wrapper = new CachedStateWrapper( download_manager, torrent_file, torrent_hash, cached_state, force_piece_discard );
+						CachedStateWrapper wrapper = new CachedStateWrapper( download_manager, torrent_file, torrent_hash, cached_state, inactive );
 						
 						global_state_cache_wrappers.add( wrapper );
 						
 						saved_state	= wrapper;
+						
 					}else{
 						
-						saved_state = TorrentUtils.readFromFile( saved_file, true, discard_pieces );
+						saved_state = TorrentUtils.readDelegateFromFile( saved_file, discard_pieces );
 					}
 					
 				}catch( Throwable e ){
@@ -256,7 +259,7 @@
 		
 		if ( saved_state == null ){
 		
-			original_torrent = TorrentUtils.readFromFile( new File(torrent_file), true, discard_pieces );
+			original_torrent = TorrentUtils.readDelegateFromFile( new File(torrent_file), discard_pieces );
 			
 			torrent_hash = original_torrent.getHash();
 			
@@ -265,7 +268,7 @@
 			if ( saved_file.exists()){
 				
 				try{
-					saved_state = TorrentUtils.readFromFile( saved_file, true, discard_pieces );
+					saved_state = TorrentUtils.readDelegateFromFile( saved_file, discard_pieces );
 					
 				}catch( Throwable e ){
 					
@@ -282,11 +285,18 @@
 				
 				TorrentUtils.copyToFile( original_torrent, saved_file );
 				
-				saved_state = TorrentUtils.readFromFile( saved_file, true, discard_pieces );
+				saved_state = TorrentUtils.readDelegateFromFile( saved_file, discard_pieces );
 			}
 		}
 
-		return( getDownloadState( download_manager, original_torrent, saved_state ));
+		DownloadManagerState res = getDownloadState( download_manager, original_torrent, saved_state );
+		
+		if ( inactive ){
+			
+			res.setActive( false );
+		}
+		
+		return( res );
 	}
 	
 	protected static File
@@ -435,8 +445,8 @@
 
 	protected
 	DownloadManagerStateImpl(
-		DownloadManagerImpl	_download_manager,
-		TOTorrent			_torrent )
+		DownloadManagerImpl				_download_manager,
+		TorrentUtils.ExtendedTorrent	_torrent )
 	{
 		download_manager	= _download_manager;
 		torrent				= _torrent;
@@ -670,6 +680,13 @@
 	}
 	
 	public void
+	setActive(
+		boolean		active )
+	{
+		torrent.setDiscardFluff( !active );
+	}
+	
+	public void
 	save()
 	{
  		boolean do_write;
@@ -2391,6 +2408,11 @@
 			return( new CaseSensitiveFileMap());
 		}
 		
+		public void 
+		setActive(boolean active )
+		{
+		}
+		
 		public void
 		save()
 		{	
@@ -2453,8 +2475,8 @@
 	
 	protected static class
 	CachedStateWrapper
-		extends LogRelation
-		implements TOTorrent
+		extends 	LogRelation
+		implements 	TorrentUtils.ExtendedTorrent
 	{
 		private DownloadManagerImpl	download_manager;
 		
@@ -2463,12 +2485,14 @@
 		private Map			cache;	
 		private Map			cache_attributes;
 		
-		private TOTorrent			delegate;
-		private TOTorrentException	fixup_failure;
+		private TorrentUtils.ExtendedTorrent		delegate;
+		private TOTorrentException					fixup_failure;
 		
 		private boolean		discard_pieces;
 		private boolean		logged_failure;
 		
+		private volatile boolean		discard_fluff;
+		
 		protected
 		CachedStateWrapper(
 			DownloadManagerImpl		_download_manager,
@@ -2558,6 +2582,11 @@
 		
 					delegate = loadRealState();
 				
+					if ( discard_fluff ){
+					
+						delegate.setDiscardFluff( discard_fluff );
+					}
+
 					if ( cache != null ){
 						
 						Debug.out( "Cache miss forced fixup" );
@@ -2601,7 +2630,7 @@
 			return( false );
 		}
 		
-		protected TOTorrent
+		protected TorrentUtils.ExtendedTorrent
 		loadRealState()
 		
 			throws TOTorrentException
@@ -2614,7 +2643,7 @@
 				
 				try{
 					
-					return( TorrentUtils.readFromFile( saved_file, true, discard_pieces ));
+					return( TorrentUtils.readDelegateFromFile( saved_file, discard_pieces ));
 					
 				}catch( Throwable e ){
 					
@@ -2633,7 +2662,7 @@
 			if ( saved_file.exists()){
 				
 				try{
-					return( TorrentUtils.readFromFile( saved_file, true, discard_pieces ));
+					return( TorrentUtils.readDelegateFromFile( saved_file, discard_pieces ));
 					
 				}catch( Throwable e ){
 					
@@ -2648,7 +2677,7 @@
 			
 			TorrentUtils.copyToFile( original_torrent, saved_file );
 			
-			return( TorrentUtils.readFromFile( saved_file, true, discard_pieces ));
+			return( TorrentUtils.readDelegateFromFile( saved_file, discard_pieces ));
 		}
 		
 		
@@ -2835,18 +2864,25 @@
     		throws TOTorrentException
     	{
     		if ( fixup()){
-    			
-    			if ( delegate instanceof TorrentUtils.torrentDelegate ){
-    				
-    				return(((TorrentUtils.torrentDelegate)delegate).peekPieces());
-    			}
-    			
-    			return( delegate.getPieces());
+    			    				
+    			return( delegate.peekPieces());
     		}
     		
 	   		throw( fixup_failure );
     	}
     	
+    	public void 
+    	setDiscardFluff(
+    		boolean discard )
+    	{
+    		discard_fluff	= discard;
+    		
+    		if ( delegate != null ){
+    			
+    			delegate.setDiscardFluff( discard_fluff );
+    		}
+     	}
+    	
     	public long
     	getPieceLength()
        	{