📄 downloadmanagerimpl.java
字号:
}
}
public void
saveDownload()
{
DiskManager disk_manager = diskManager;
if ( disk_manager != null ){
disk_manager.storeFilePriorities();
}
download_manager_state.save();
}
public void setState(int _state){
// note: there is a DIFFERENCE between the state held on the DownloadManager and
// that reported via getState as getState incorporated DiskManager states when
// the DownloadManager is INITIALIZED
//System.out.println( "DM:setState - " + _state );
if ( state != _state ) {
state = _state;
// sometimes, downloadEnded() doesn't get called, so we must check here too
if (state == STATE_SEEDING) {
setOnlySeeding(true);
} else if (state == STATE_QUEUED) {
if (onlySeeding && !filesExist())
return;
}
informStateChanged( state );
}
}
public int getNbSeeds() {
if (peerManager != null)
return peerManager.getNbSeeds();
return 0;
}
public int getNbPeers() {
if (peerManager != null)
return peerManager.getNbPeers();
return 0;
}
public String getTrackerStatus() {
if (tracker_client != null)
return tracker_client.getStatusString();
// to tracker, return scrape
if (torrent != null && globalManager != null) {
TRTrackerScraperResponse response = getTrackerScrapeResponse();
if (response != null) {
return response.getStatusString();
}
}
return "";
}
// this is called asynchronously when a response is received
public void
setTrackerScrapeResponse(
TRTrackerScraperResponse response )
{
tracker_listeners.dispatch( LDT_TL_SCRAPERESULT, response );
}
public TRTrackerClient
getTrackerClient()
{
return( tracker_client );
}
/**
* @return
*/
public int getNbPieces() {
return nbPieces;
}
public int getTrackerTime() {
if (tracker_client != null)
return tracker_client.getTimeUntilNextUpdate();
// no tracker, return scrape
if (torrent != null && globalManager != null) {
TRTrackerScraperResponse response = getTrackerScrapeResponse();
if (response != null) {
if (response.getStatus() == TRTrackerScraperResponse.ST_SCRAPING)
return -1;
return (int)((response.getNextScrapeStartTime() - SystemTime.getCurrentTime()) / 1000);
}
}
return TRTrackerClient.REFRESH_MINIMUM_SECS;
}
/**
* @return
*/
public TOTorrent
getTorrent()
{
return( torrent );
}
public String
getTorrentSaveDirAndFile()
{
return( torrent_save_dir + ( torrent_save_file==null?"":(File.separator + torrent_save_file )));
}
public String
getTorrentSaveDir()
{
return( torrent_save_dir );
}
public String
getTorrentSaveFile()
{
return( torrent_save_file );
}
public void
setTorrentSaveDir(
String sPath)
{
// assumption here is that the caller really knows what they are doing. You can't
// just change this willy nilly, it must be synchronised with reality. For example,
// the disk-manager calls it after moving files on completing
// The UI can call it as long as the torrent is stopped.
// Calling it while a download is active will in general result in unpredictable behaviour!
torrent_save_dir = sPath;
}
public String getPieceLength(){
if ( torrent != null ){
return( DisplayFormatters.formatByteCountToKiBEtc(torrent.getPieceLength()));
}
return( "" );
}
/**
* @return
*/
public String getTorrentFileName() {
return torrentFileName;
}
/**
* @param string
*/
public void setTorrentFileName(String string) {
torrentFileName = string;
}
public TRTrackerScraperResponse
getTrackerScrapeResponse()
{
TRTrackerScraperResponse r = null;
if (globalManager != null) {
TRTrackerScraper scraper = globalManager.getTrackerScraper();
if ( tracker_client != null ){
r = scraper.scrape(tracker_client);
}
if ( r == null && torrent != null){
// torrent not running. For multi-tracker torrents we need to behave sensibly
// here
TRTrackerScraperResponse non_null_response = null;
TOTorrentAnnounceURLSet[] sets = torrent.getAnnounceURLGroup().getAnnounceURLSets();
if ( sets.length == 0 ){
r = scraper.scrape(torrent);
}else{
// we use a fixed seed so that subsequent scrapes will randomise
// in the same order, as required by the spec. Note that if the
// torrent's announce sets are edited this all works fine (if we
// cached the randomised URL set this wouldn't work)
Random scrape_random = new Random(scrape_random_seed);
for (int i=0;r==null && i<sets.length;i++){
TOTorrentAnnounceURLSet set = sets[i];
URL[] urls = set.getAnnounceURLs();
List rand_urls = new ArrayList();
for (int j=0;j<urls.length;j++ ){
URL url = urls[j];
int pos = (int)(scrape_random.nextDouble() * (rand_urls.size()+1));
rand_urls.add(pos,url);
}
for (int j=0;r==null && j<rand_urls.size();j++){
r = scraper.scrape(torrent, (URL)rand_urls.get(j));
if ( r!= null ){
// treat bad scrapes as missing so we go on to
// the next tracker
if ( (!r.isValid()) || r.getStatus() == TRTrackerScraperResponse.ST_ERROR ){
if ( non_null_response == null ){
non_null_response = r;
}
r = null;
}
}
}
}
if ( r == null ){
r = non_null_response;
}
}
}
}
return r;
}
/**
* @param string
*/
public void setErrorDetail(String string) {
errorDetail = string;
}
/**
* Stops the current download, then restarts it again.
*/
public void
restartDownload(
boolean use_fast_resume)
{
try{
if (!use_fast_resume) {
//invalidate resume info
diskManager.dumpResumeDataToDisk(false, true);
readTorrent( torrent==null?null:torrent.getHash(),false, false );
}
stopIt( DownloadManager.STATE_STOPPED, false, false );
try {
while (state != DownloadManager.STATE_STOPPED) Thread.sleep(50);
} catch (Exception ignore) {/*ignore*/}
initialize();
}catch( Exception e ){
setFailed( "Resume data save fails: " + Debug.getNestedExceptionMessage(e));
}
}
public void startDownloadInitialized(boolean initStoppedDownloads) {
if (getState() == DownloadManager.STATE_WAITING || initStoppedDownloads && getState() == DownloadManager.STATE_STOPPED) {
initialize();
}
if (getState() == DownloadManager.STATE_READY) {
startDownload();
}
}
/** @retun true, if the other DownloadManager has the same size and hash
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj)
{
// check for object equivalence first!
if ( this == obj ){
return( true );
}
if(null != obj && obj instanceof DownloadManager) {
DownloadManager other = (DownloadManager) obj;
TOTorrent t1 = getTorrent();
TOTorrent t2 = other.getTorrent();
if ( t1 == null || t2 == null ){
return( false ); // broken torrents - treat as different so shown
// as broken
}
try{
return( Arrays.equals(t1.getHash(), t2.getHash()));
}catch( TOTorrentException e ){
// only get here is serious problem with hashing process
Debug.printStackTrace( e );
}
}
return false;
}
public void
checkTracker()
{
checkTracker(false);
}
protected void
checkTracker(
boolean force )
{
if( tracker_client != null)
tracker_client.update( force );
}
/**
* @return
*/
public String
getTorrentComment() {
return torrent_comment;
}
public String
getTorrentCreatedBy() {
return torrent_created_by;
}
public long
getTorrentCreationDate() {
if (torrent==null){
return(0);
}
return( torrent.getCreationDate());
}
/**
* @return
*/
public int getIndex() {
if(globalManager != null)
return globalManager.getIndexOf(this);
return -1;
}
public boolean isMoveableUp() {
if(globalManager != null)
return globalManager.isMoveableUp(this);
return false;
}
public boolean isMoveableDown() {
if(globalManager != null)
return globalManager.isMoveableDown(this);
return false;
}
public void moveUp() {
if(globalManager != null)
globalManager.moveUp(this);
}
public void moveDown() {
if(globalManager != null)
globalManager.moveDown(this);
}
public GlobalManager
getGlobalManager()
{
return( globalManager );
}
public DiskManager
getDiskManager()
{
return( diskManager );
}
public PEPeerManager
getPeerManager()
{
return( peerManager );
}
public boolean
isDownloadComplete()
{
return( onlySeeding );
}
public void
addListener(
DownloadManagerListener listener )
{
listeners.addListener(listener);
// pick up the current state
listener.stateChanged( this, state );
// we DON'T dispatch a downloadComplete event here as this event is used to mark the
// transition between downloading and seeding, NOT purely to inform of seeding status
}
public void
removeListener(
DownloadManagerListener listener )
{
listeners.removeListener(listener);
}
protected void
informStateChanged(
int new_state )
{
listeners.dispatch( LDT_STATECHANGED, new Integer( new_state ));
}
protected void
informDownloadEnded()
{
listeners.dispatch( LDT_DOWNLOADCOMPLETE, null );
}
protected void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -