⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cachefilemanagerimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      					
      					CacheFileWithCache	file = (CacheFileWithCache)it.next();

      					try{
      						
      						TOTorrentFile	tf = file.getTorrentFile();
      						
      						long	min_flush_size	= -1;
      						
      						if ( tf != null ){
      							
      							min_flush_size	= tf.getTorrent().getPieceLength();
      							
      						}
      						
      						file.flushOldDirtyData( oldest, min_flush_size );
      						
      					}catch( CacheFileManagerException e ){
      						
      						file.setPendingException( e );
      						
      							// if this fails then the error should reoccur on a "proper"
      							// flush later and be reported
      						
      						Debug.printStackTrace( e );
      						
      					}catch( Throwable e ){
      						
      						Debug.printStackTrace( e );
      					}
      				}
      			}
      			
          }
        }
     );
		
	}
	
		// must be called when the cachefileimpl is synchronised to ensure that the file's
		// cache view and our cache view are consistent
	
	protected void
	addCacheSpace(
		CacheEntry		new_entry )
	
		throws CacheFileManagerException
	{
		try{
			this_mon.enter();
			
			cache_space_free	-= new_entry.getLength();
			
				// 	System.out.println( "Total cache space = " + cache_space_free );
		
			cache_entries.put( new_entry, new_entry );
			
			if ( DEBUG ){
				
				CacheFileWithCache	file	= new_entry.getFile();
								
				long	total_cache_size	= 0;
				
				int		my_count = 0;

				Iterator it = cache_entries.keySet().iterator();
				
				while( it.hasNext()){
					
					CacheEntry	entry = (CacheEntry)it.next();
					
					total_cache_size	+= entry.getLength();
					
					if ( entry.getFile() == file ){
						
						my_count++;
					}
				}
			
				if ( my_count != file.cache.size()){
					
					Debug.out( "Cache inconsistency: my count = " + my_count + ", file = " + file.cache.size());
					
					throw( new CacheFileManagerException( null, "Cache inconsistency: counts differ"));
					
				}else{
					
					//System.out.println( "Cache: file_count = " + my_count );
				}
				
				if ( total_cache_size != cache_size - cache_space_free ){
					
					Debug.out( "Cache inconsistency: used_size = " + total_cache_size + ", free = " + cache_space_free + ", size = " + cache_size );
					
					throw( new CacheFileManagerException( null, "Cache inconsistency: sizes differ"));
					
				}else{
					
					//System.out.println( "Cache: usage = " + total_cache_size );
				}
			}
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected void
	cacheEntryUsed(
		CacheEntry		entry )
	
		throws CacheFileManagerException
	{
		try{
			this_mon.enter();
		
				// note that the "get" operation update the MRU in cache_entries
			
			if ( cache_entries.get( entry ) == null ){
				
				Debug.out( "Cache inconsistency: entry missing on usage" );
				
				throw( new CacheFileManagerException( null, "Cache inconsistency: entry missing on usage"));
				
			}else{
				
				entry.used();
			}
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected void
	releaseCacheSpace(
		CacheEntry		entry )
	
		throws CacheFileManagerException
	{
		entry.getBuffer().returnToPool();
		
		try{
			this_mon.enter();
			
			cache_space_free	+= entry.getLength();
			
			if ( cache_entries.remove( entry ) == null ){
				
				Debug.out( "Cache inconsistency: entry missing on removal" );

				throw( new CacheFileManagerException( null, "Cache inconsistency: entry missing on removal"));
			}

			/*
			if ( 	entry.getType() == CacheEntry.CT_READ_AHEAD ){
				
				if ( entry.getUsageCount() < 2 ){
				
					System.out.println( "ra: not used" );
				
				}else{
				
					System.out.println( "ra: used" );
				}
			}
			*/
			
			// System.out.println( "Total cache space = " + cache_space_free );
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected long
	getCacheSize()
	{
		return( cache_size );
	}
	
	protected long
	getCacheUsed()
	{
		long free = cache_space_free;
		
		if ( free < 0 ){
			
			free	= 0;
		}
		
		return( cache_size - free );
	}
	
	protected void
	cacheBytesWritten(
		long		num )
	{
		try{
			this_mon.enter();
			
			cache_bytes_written	+= num;
			
			cache_write_count++;
			
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected void
	cacheBytesRead(
		int		num )
	{
		try{
			this_mon.enter();
			
			cache_bytes_read	+= num;
			
			cache_read_count++;
			
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected void
	fileBytesWritten(
		long	num )
	{
		try{
			this_mon.enter();
			
			file_bytes_written	+= num;
			
			file_write_count++;
			
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected void
	fileBytesRead(
		int		num )
	{
		try{
			this_mon.enter();
			
			file_bytes_read	+= num;
			
			file_read_count++;
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected long
	getBytesWrittenToCache()
	{
		return( cache_bytes_written );
	}
	
	protected long
	getBytesWrittenToFile()
	{
		return( file_bytes_written );
	}
	
	protected long
	getBytesReadFromCache()
	{
		return( cache_bytes_read );
	}
	
	protected long
	getBytesReadFromFile()
	{
		return( file_bytes_read );
	}
	
	public long
	getCacheReadCount()
	{
		return( cache_read_count );
	}
	
	public long
	getCacheWriteCount()
	{
		return( cache_write_count );
	}
	
	public long
	getFileReadCount()
	{
		return( file_read_count );
	}
	
	public long
	getFileWriteCount()
	{
		return( file_write_count );
	}
	
	protected void
	closeFile(
		CacheFileWithCache	file )
	{
		TOTorrentFile	tf = file.getTorrentFile();
		
		if ( tf != null && torrent_to_cache_file_map.get( tf ) != null ){

			try{
				this_mon.enter();
						
				Map	new_map = new HashMap( torrent_to_cache_file_map );
				
				new_map.remove( tf );
	
				torrent_to_cache_file_map	= new_map;
				
			}finally{
				
				this_mon.exit();
			}
		}
	}
	
	protected long
	getBytesInCache(
		TOTorrent		torrent,
		int				piece_number,
		int				offset,
		long			length )
	{
			// copied on update, grab local ref to access
		
		Map	map = torrent_to_cache_file_map;
		
		TOTorrentFile[]	files = torrent.getFiles();
		
		long	piece_size = torrent.getPieceLength();
		
		long	target_start 	= piece_number*piece_size + offset;
		long	target_end		= target_start + length;
		
		long	pos = 0;
		
		long	result	= 0;
		
		for (int i=0;i<files.length;i++){
			
			TOTorrentFile	tf = files[i];
			
			long	len = tf.getLength();
			
			long	this_start 	= pos;
			
			pos	+= len;
			
			long	this_end	= pos;
				
			if ( this_end <= target_start ){
				
				continue;
			}
			
			if ( target_end <= this_start ){
				
				break;
			}
			
			long	bit_start	= target_start>this_start?target_start:this_start;
			long	bit_end		= target_end<this_end?target_end:this_end;
			
			CacheFileWithCache	cache_file = (CacheFileWithCache)map.get( tf );
			
			if ( cache_file != null ){
				
				result	+= cache_file.getBytesInCache( bit_start - this_start, bit_end - bit_start );
			}
		}
		
		return( result );
	}
	
	protected void
	rethrow(
		CacheFile				file,
		FMFileManagerException 	e )
	
		throws CacheFileManagerException
	{
		Throwable 	cause = e.getCause();
		
		if ( cause != null ){
			
			throw( new CacheFileManagerException( file, e.getMessage(), cause ));
		}
		
		throw( new CacheFileManagerException( file, e.getMessage(), e ));
	}
	
	public void
	generate(
		IndentWriter		writer )
	{
		writer.println( "Cache Manager" );
			
		try{
			writer.indent();
			
			try{
				this_mon.enter();

				writer.println( "Entries = " + cache_entries.size());
				
				Iterator it = cache_entries.keySet().iterator();
				
				Set	files = new HashSet();
				
				while( it.hasNext()){
					
					CacheEntry	entry = (CacheEntry)it.next();
					
					CacheFileWithCache file = entry.getFile();
					
					if( !files.contains( file )){
						
						files.add( file );
						
						TOTorrentFile torrentFile = file.getTorrentFile();
						String fileLength = "";
						try {
							fileLength = "" + file.getLength();
						} catch (Exception e) {
							if (torrentFile != null)
								fileLength = "" + torrentFile.getLength();
						}
						String	hash = "<unknown>";
						
						try{
							if (torrentFile != null)
								hash = ByteFormatter.encodeString( torrentFile.getTorrent().getHash());
							
						}catch( Throwable e ){
						}
						
						String name = file.getName();

						writer.println("File: " + Debug.secretFileName(name) + ", size "
								+ fileLength + ", torrent " + hash + ", access = "
								+ file.getAccessMode());
					}
				}
				
			}finally{
				
				this_mon.exit();
			}
		}finally{
			
			writer.exdent();
		}
	}
	
	public void
	setFileLinks(
		TOTorrent				torrent,
		CaseSensitiveFileMap	links )
	{
		file_manager.setFileLinks( torrent, links );
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -