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

📄 downloadjob.java

📁 peeranha42是jxta的 p2p程序核心
💻 JAVA
字号:
package de.uni_bremen.informatik.p2p.plugins.filesharing.data;

import java.io.File;
import java.util.Hashtable;
import java.util.LinkedList;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import de.uni_bremen.informatik.p2p.plugins.filesharing.control.FileUtilities;
import de.uni_bremen.informatik.p2p.plugins.filesharing.control.TransferObserver;
import de.uni_bremen.informatik.p2p.plugins.filesharing.network.download.DownloadProtocol;
import net.jxta.protocol.PipeAdvertisement;


/**
 * This class represents a download entry of the filesharingplugin.
 *
 * @author Lars Kordes
 */
public class DownloadJob {
	public static final int D_FINISHED = -2;
	
	public static final int D_NOT_FINISHED_YET = -1;
	
    /** State */
    public static final int NO_CONNECTION = 0;

    /** State */
    public static final int STOPPED = 1;

    /** State */
    public static final int CANCELT = 2;

    /** State */
    public static final int DOWNLOAD = 3;

    /** State */
    public static final int FINISHED = 4;
    
    /** State */
    public static final int QUEUED = 5;
    
    private JProgressBar progressbar = new JProgressBar(0, 100);

    private TransferObserver observer;
    
    /** Name of the file which gets downloaded. */
    public String filename;

    /** Size of the file which gets downloaded. Measured in kbytes. */
    public long size;

    /** Nr of chunks of the download **/
    public long nrOfChunks;
    
    /** Actual download progress. Measured in kbytes. */
    public long progress = 0;
    
    /** Hold received chunks */
    private Hashtable receivedChunks;
    
    /** Hold ordered chunks */
    private Hashtable orderedChunks;
    
    /**
     * Downloadstatus. Can be NO_CONNECTION, STOPPED, CONNECTING, DOWNLOAD,
     * FINISHED.
     */
    private int status;

    /** Filedescriptor of file with received data. */
    public File filedes;
    
    /** Downloadrate measured in kbit/s. */
    private long downloadrate = 0;

    public LinkedList sources;
    
    public String hash;
    
    public LinkedList con;
    
    /**
     * Classconstructor
     */
    public DownloadJob() {
        status = NO_CONNECTION;
    }
    
    /**
     * Classconstructor
     * @param sr The Searchresult this DownloadJob is created of
     */
    public DownloadJob(SearchResult sr) {
        this.filename = sr.filename;
        this.size = sr.size;
        this.nrOfChunks = sr.nrOfChunks; 
        this.hash = sr.hash;
        this.sources = new LinkedList(sr.sources);
        this.con = new LinkedList();
            
        receivedChunks = new Hashtable(); // All elements are initalized to false by default
        orderedChunks = new Hashtable(); // All elements are initalized to false by default
    }
    
    /**
     * Sets a chunk as received
     * @param packetNr
     */
    public void addReceivedChunkNumber(long packetNr) {
        if(receivedChunks.get(new Long(packetNr)) == null) {
        	receivedChunks.put(new Long(packetNr), new Boolean(true));
	        progress += DownloadProtocol.CHK_SIZE;
	        
			Runnable update = new Runnable() {
				public void run() {
					if(FilesharingState.downloadpanel != null) FilesharingState.downloadpanel.getTable().updateUI();
				}
			};
			SwingUtilities.invokeLater(update);
        }
    }
    
    /**
     * Returns the nr of the next chunk which has not yet been downloaded
     * 
     * @return nr of next chunk
     */
    public long getNextChunkNumber() {
    	if(status != CANCELT && status != FINISHED) {
	        for (long cnt = 0; cnt < nrOfChunks; cnt++) {
	            if (receivedChunks.get(new Long(cnt)) == null) {
	            	if(orderedChunks.get(new Long(cnt)) == null) {
	            		orderedChunks.put(new Long(cnt), new Boolean(true));
	            		return cnt;
	            	}
	            }
	        }
	        for (long i = 0; i < nrOfChunks; i++) {
	        	if(receivedChunks.get(new Long(i)) == null) return D_NOT_FINISHED_YET;
	        }
	        FileUtilities.moveToDownloadDir(filedes);
	        status = FINISHED;
    	}
        
        return D_FINISHED; // all chunks have been downloaded
    }
    
    /**
     * Returns the downloadrate.
     * 
     * @return Downloadrate
     */
    public long getDownloadRate() {
    	return downloadrate;
    }
    
    /**
     * Sets the downloadrate.
     * 
     * @param rate New downloadrate;
     */
    public void setDownloadRate(long rate) {
    	downloadrate = rate;
    	Runnable update = new Runnable() {
			public void run() {
				if(FilesharingState.downloadpanel != null) FilesharingState.downloadpanel.getTable().updateUI();
			}
		};
		SwingUtilities.invokeLater(update);
    }
    
    /**
     * Returns Progress in precent
     * 
     * @return Progress in precent.
     */
    public int getProgressInPrecent() {
    	double i = ((new Long(progress).doubleValue()/size) * 100);
    	return new Double(i).intValue();
    }
    
    /**
     * Returns progress bar.
     * 
     * @return Progress bar.
     */
    public JProgressBar getProgressBar() {
    	progressbar.setValue((new Double((new Long(progress).doubleValue() / size) * 100)).intValue());
    	return progressbar;
    }

	/**
	 * Returns next PipeAdvertisement in source queue.
	 *  
	 * @return Next PipeAdvertisement in source queue.
	 */
	public PipeAdvertisement getNextPipeAdvertisement() {
		PipeAdvertisement padv = (PipeAdvertisement) sources.removeLast();
		sources.add(padv);
		return padv;
	}
	
	public void setStatus(int i) {
		status = i;
		if(status == CANCELT) {
			try {
				filedes.delete();
			}
			catch(Exception e) {
			}
		}
	}
	
	public int getStatus() {
		return status;
	}
	
	public void startObserver() {
		observer = new TransferObserver(this);
		observer.start();
	}
	
	public long getProgress() {
		return progress;
	}
}

⌨️ 快捷键说明

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