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

📄 sharemanagerimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	{
		return( COConfigurationManager.getBooleanParameter( "Sharing Add Hashes", true ));
	}
	
	public ShareResource[]
	getShares()
	{
		ShareResource[]	res = new ShareResource[shares.size()];
		
		shares.values().toArray( res );
		
		return( res );
	}
	
	protected ShareResourceImpl
	getResource(
		File		file )
	
		throws ShareException
	{
		try{
			return((ShareResourceImpl)shares.get(file.getCanonicalFile().toString()));
			
		}catch( IOException e ){
			
			throw( new ShareException( "getCanonicalFile fails", e ));
		}
	}
	
	public ShareResource
	getShare(
		File	file_or_dir )
	{
		try{
			return( getResource( file_or_dir ));
			
		}catch( ShareException e ){
						
			return( null );
		}
	}
	
	public ShareResourceFile
	addFile(
		File	file )
	
		throws ShareException, ShareResourceDeletionVetoException
	{
		return( addFile( null, file ));
	}
	
	protected ShareResourceFile
	addFile(
		ShareResourceDirContentsImpl	parent,
		File							file )

		throws ShareException, ShareResourceDeletionVetoException
	{
		if (Logger.isEnabled())
			Logger.log(new LogEvent(LOGID, "ShareManager: addFile '"
					+ file.toString() + "'"));

		try{
			return( (ShareResourceFile)addFileOrDir( parent, file, ShareResource.ST_FILE, false ));
			
		}catch( ShareException e ){
			
			reportError(e);
			
			throw(e);
		}
	}
	
	public ShareResourceFile
	getFile(
		File	file )
	
		throws ShareException
	{
		return( (ShareResourceFile)ShareResourceFileImpl.getResource( this, file ));
	}
	
	public ShareResourceDir
	addDir(
		File	dir )
	
		throws ShareException, ShareResourceDeletionVetoException
	{
		return( addDir( null, dir ));
	}
	
	public ShareResourceDir
	addDir(
		ShareResourceDirContentsImpl	parent,
		File							dir )
	
		throws ShareException, ShareResourceDeletionVetoException
	{
		if (Logger.isEnabled())
			Logger.log(new LogEvent(LOGID, "ShareManager: addDir '" + dir.toString()
					+ "'"));

		try{
			this_mon.enter();
			
			return( (ShareResourceDir)addFileOrDir( parent, dir, ShareResource.ST_DIR, false ));
			
		}catch( ShareException e ){
			
			reportError(e);
			
			throw(e);
			
		}finally{
			
			this_mon.exit();
		}
	}
	
	public ShareResourceDir
	getDir(
		File	file )
	
		throws ShareException
	{
		return( (ShareResourceDir)ShareResourceDirImpl.getResource( this, file ));
	}
	
	protected ShareResource
	addFileOrDir(
		ShareResourceDirContentsImpl	parent,
		File							file,
		int								type,
		boolean							modified )
	
		throws ShareException, ShareResourceDeletionVetoException
	{
		try{
			this_mon.enter();
		
			String	name = file.getCanonicalFile().toString();
			
			ShareResource	old_resource = (ShareResource)shares.get(name);
			
			if ( old_resource != null ){
		
				old_resource.delete();
			}
			
			ShareResourceImpl new_resource;
			
			if ( type == ShareResource.ST_FILE ){
		
				reportCurrentTask( "Adding file '" + name + "'");
				
				new_resource = new ShareResourceFileImpl( this, parent, file );
				
			}else{
				
				reportCurrentTask( "Adding dir '" + name + "'");
				
				new_resource = new ShareResourceDirImpl( this, parent, file );
			}
			
			shares.put(name, new_resource );
			
			config.saveConfig();
			
			for (int i=0;i<listeners.size();i++){
				
				try{
					
					if ( modified ){
						
						((ShareManagerListener)listeners.get(i)).resourceModified( new_resource );
					
					}else{
						
						((ShareManagerListener)listeners.get(i)).resourceAdded( new_resource );				
					}
				}catch( Throwable e ){
					
					Debug.printStackTrace( e );
				}
			}
			
			return( new_resource );
			
		}catch( IOException e ){
			
			throw( new ShareException( "getCanoncialFile fails", e ));
			
		}finally{
			
			this_mon.exit();
		}
	}
	

	
	public ShareResourceDirContents
	addDirContents(
		File		dir,
		boolean		recursive )
	
		throws ShareException, ShareResourceDeletionVetoException
	{
		if (Logger.isEnabled())
			Logger.log(new LogEvent(LOGID, "ShareManager: addDirContents '"
					+ dir.toString() + "'"));

		try{
			this_mon.enter();
			
			String	name = dir.getCanonicalFile().toString();
			
			reportCurrentTask( "Adding dir contents '" + name + "', recursive = " + recursive );
	
			ShareResource	old_resource = (ShareResource)shares.get( name );
			
			if ( old_resource != null ){
				
				old_resource.delete();
			}

			ShareResourceDirContents new_resource = new ShareResourceDirContentsImpl( this, dir, recursive );
						
			shares.put( name, new_resource );
			
			config.saveConfig();
			
			for (int i=0;i<listeners.size();i++){
				
				try{
					
					((ShareManagerListener)listeners.get(i)).resourceAdded( new_resource );
					
				}catch( Throwable e ){
					
					Debug.printStackTrace( e );
				}
			}
			
			return( new_resource );
			
		}catch( IOException e ){
			
			reportError(e);
			
			throw( new ShareException( "getCanoncialFile fails", e ));
			
		}catch( ShareException e ){
			
			reportError(e);
			
			throw(e);
			
		}finally{
			
			this_mon.exit();
		}
	}	
	
	protected void
	delete(
		ShareResourceImpl	resource )
	
		throws ShareException
	{
		if (Logger.isEnabled())
			Logger.log(new LogEvent(LOGID, "ShareManager: resource '"
					+ resource.getName() + "' deleted"));
		
		try{
			this_mon.enter();
		
			shares.remove(resource.getName());
			
			resource.deleteInternal();
			
			config.saveConfig();
			
			for (int i=0;i<listeners.size();i++){
				
				try{
					
					((ShareManagerListener)listeners.get(i)).resourceDeleted( resource );
					
				}catch( Throwable e ){
					
					Debug.printStackTrace( e );
				}
			}
		}finally{
			
			this_mon.exit();
		}
	}
	
	protected void
	scanShares()
	
		throws ShareException
	{
		if (Logger.isEnabled())
			Logger.log(new LogEvent(LOGID,
					"ShareManager: scanning resources for changes"));

		checkConsistency();
	}
	
		// bit of a hack this, but to do it properly would require extensive rework to decouple the
		// process of saying "share file" and then actually doing it 
	
	protected  void
	setTorrentCreator(
		TOTorrentCreator	_to_creator )
	{
		to_creator	= _to_creator;
	}
	
	public void
	cancelOperation()
	{
		TOTorrentCreator	temp = to_creator;
		
		if ( temp != null ){
			
			temp.cancel();
		}
	}
	
	public void
	reportProgress(
		int		percent_complete )
	{
		for (int i=0;i<listeners.size();i++){
			
			try{
				
				((ShareManagerListener)listeners.get(i)).reportProgress( percent_complete );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
			}
		}		
	}
	
	public void
	reportCurrentTask(
		String	task_description )
	{
		for (int i=0;i<listeners.size();i++){
			
			try{
				
				((ShareManagerListener)listeners.get(i)).reportCurrentTask( task_description );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
			}
		}			
	}

	protected void
	reportError(
		Throwable e )
	{
		String	message = e.getMessage();
		
		if ( message != null ){
			
			reportCurrentTask( Debug.getNestedExceptionMessage(e));
			
		}else{
			
			reportCurrentTask( e.toString());
		}
	}
	public void
	addListener(
		ShareManagerListener		l )
	{
		listeners.add(l);	
	}
	
	public void
	removeListener(
		ShareManagerListener		l )
	{
		listeners.remove(l);
	}
	
	protected class
	shareScanner
	{
		boolean	run = true;
		
		protected
		shareScanner()
		{
			current_scanner	= this;
			
			Thread t = 
				new AEThread( "ShareManager::scanner" )
				{
					public void
					runSupport()
					{
						while( current_scanner == shareScanner.this ){
						
							try{
								
								int		scan_period		= COConfigurationManager.getIntParameter( "Sharing Rescan Period" );

								if ( scan_period < 1 ){
									
									scan_period	= 1;
								}
								
								Thread.sleep( scan_period * 1000 );
								
								if ( current_scanner == shareScanner.this ){
									
									scanShares();
								}

							}catch( Throwable e ){
								
								Debug.printStackTrace(e);
							}
						}
					}
				};
				
			t.setDaemon(true);
			
			t.start();
			
		}
	}
}

⌨️ 快捷键说明

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