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

📄 btdocument.java

📁 p2p仿真
💻 JAVA
字号:
/*
 * @(#)BTDocument.java	ver 1.2  6/20/2005
 *
 * Copyright 2005 Weishuai Yang (wyang@cs.binghamton.edu). 
 * All rights reserved.
 * 
 */

package gps.protocol.BT;

import gps.protocol.Document;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
/**
 * BT Document
 * 
 * @author  Weishuai Yang
 * @version 1.2,  6/20/2005
 */
public class BTDocument extends Document {
	
	public static double DEFAULT_PIECE_LENGTH = Math.pow(2,18);
	public static double DEFAULT_BLOCK_LENGTH = Math.pow(2,14);
	
	protected double mPieceLength=DEFAULT_PIECE_LENGTH;
	protected double mBlockLength=DEFAULT_BLOCK_LENGTH;
	protected int mPieceNum = 0;
	protected boolean mPieces[]=null;
	protected LinkedHashMap mPartialList=new LinkedHashMap();
	protected LinkedHashSet mDownloadingSet=new LinkedHashSet();

	
	
	/**
	 * the initializer for new a document using next available id
	 */
	public BTDocument(){
		this(getNextAvailableID());
	}
	
	/**
	 * the initializer for new a document with a specified id
	 * @param id is the specified int id
	 */
	public BTDocument(int id){
		super(id);
	}

	/**
	 * the initializer for new a document 
	 * without a incremental default id, and set size and key
	 * @param size is the size of the document
	 * @param key is document key
	 */
	public BTDocument(double size, String key){
		this();
		setSize(size);
		setKey(key);
	}
	/**
	 * the initializer for new a document 
	 * without a incremental default id, piece length, and set size and key
	 * @param plength is piece length
	 * @param size is the size of the document
	 * @param key is document key
	 */
	public BTDocument(double plength, double size, String key){
		this();
		setPieceLength(plength);
		setSize(size);
		setKey(key);
	}
	/**
	 * gets piece index in partial list
	 * @return all index in partial list
	 */
	public ArrayList getPartialIndex(){
		return new ArrayList(mPartialList.keySet());
	}

	/**
	 * gets piece index in downloading set
	 * @return all index in downloading set
	 */
	public LinkedHashSet getDownloadingSet(){
		return mDownloadingSet;
	}
	
	/**
	 * check if a piece is in partial list
	 * @param index piece index
	 * @return true if in partial list, otherwise false
	 */
	public boolean inPartialList(int index){
		return mPartialList.containsKey(new Integer(index));
	}

	/**
	 * check if a piece is in downloading set
	 * @param index piece index
	 * @return true if in downloading set, otherwise false
	 */
	public boolean inDownloadingSet(int index){
		return mDownloadingSet.contains(new Integer(index));
	}

	/**
	 * add a piece into downloading set
	 * @param index piece index
	 */
	public void addPieceDownloading(int index){
		mDownloadingSet.add(new Integer(index));
	}

	/**
	 * remove a piece from downloading set
	 * @param index piece index
	 */
	public void removePieceDownloading(int index){
		mDownloadingSet.remove(new Integer(index));
	}
	
	/**
	 * finish a piece
	 * @param index piece index
	 */
	public boolean finishPiece(int index){
		if(!mDownloadingSet.contains(new Integer(index)))
			return false;
		
	    if(!mPartialList.containsKey(new Integer(index)))
			return false;
		
	    if(mStatus==EMPTY) mStatus=PARTIAL;
		mPartialList.remove(new Integer(index));
		mDownloadingSet.remove(new Integer(index));
		mPieces[index]=true;
		boolean finished=true;
		for(int i=0;i<mPieceNum;i++)
			finished=mPieces[i]&&finished;
		if(finished)
			super.setWhole();
		return true;
	}
	/**
	 * set piece progress
	 * @param index piece index
	 * @param offset new progress
	 */
	public void pieceProgress(int index, int offset){
		if(!mDownloadingSet.contains(new Integer(index))){
			mDebugLog.warning("partial is not being downloaded when adding piece progress!");
			return;
		}
	    if(offset>=mPieceLength){
	        finishPiece(index);
	    }
	    else
		    mPartialList.put(new Integer(index), new Integer(offset));
	}

	/**
	 * add piece progress
	 * @param index piece index
	 * @param add increment
	 */
	
	public void pieceProgressAdd(int index, int add){
		if(!mDownloadingSet.contains(new Integer(index))){
			mDebugLog.warning("partial is not being downloaded when adding piece progress!");
			return;
		}
		
		if(mPartialList.get(new Integer(index))==null){
			mDebugLog.warning("partial doesn't exist when adding piece progress!");
			return;
		}
		
		int origoff=((Integer)mPartialList.get(new Integer(index))).intValue();
		pieceProgress(index,origoff+add);
		
	}
	/**
	 * gets finished progress for a piece
	 * @param index of piece
	 * @return the finished offset within that piece
	 */
	public int getPieceProgress(int index){
		//not supposed to be 0
		if(!mPartialList.containsKey(new Integer(index))){
			mDebugLog.warning("sub piece doesn't exist!");
			return -1;
		}
		else
			return ((Integer)mPartialList.get(new Integer(index))).intValue();
	}
	/**
	 * remove the piece record from parital list
	 * @param index of piece
	 */
	public void removePieceProgress(int index){
		mPartialList.remove(new Integer(index));
	}
	

	
	/**
	 * sets the size of the document and adjust pieces
	 * @param size the size of the document
	 */
	public void setSize(double size){
		super.setSize(size);
		mPieceNum=(int)Math.ceil(mSize/mPieceLength);
		mPieces=new boolean[mPieceNum];
		for(int i=0;i<mPieceNum;i++)
			mPieces[i]=false;
	}
	
	/**
	 * gets the size of total unfinished pieces
	 * @return left size
	 */
	public double getLeftSize(){
		if(isEmpty())
			return mSize;
		if(isWhole())
			return 0;
			
		//Note: the last piece may have different piece length
		int finished=0;
		double finishedSize=0;
		for(int i=0;i<mPieceNum-2;i++)
			if(mPieces[i]==true)
				finished++;
		finishedSize=finished*mPieceLength;
		
		if(mPieces[mPieceNum-1])
		    finishedSize+=(mSize-(mPieceNum-1)*mPieceLength);
		return mSize-finishedSize;
		
	}
	
	/**
	 * Method getLeftPieceNum gets the unfinished pieces number
	 * @return left piece number
	 */
	public int getLeftPieceNum(){
		if(isEmpty())
			return mPieceNum;
		if(isWhole())
			return 0;
		int left=0;
		for(int i=0;i<mPieceNum;i++)
			if(mPieces[i]==false)
				left++;
		return left;
	}

	/**
	 * Method getFinishedPercent gets the percent finished
	 * @return finished percent
	 */
	public double getFinishedPercent(){
	    return (1.0*(mPieceNum-getLeftPieceNum()))/mPieceNum;
	}
	
	
	/**
	 * sets whole flag of the document and each pieces
	 */
	public void setWhole(){
		super.setWhole();
		for(int i=0;i<mPieceNum;i++)
			mPieces[i]=true;
	}
	
	/**
	 * gets piece length
	 * @return piece length for this document
	 */
	public double getPieceLength(){
		return mPieceLength;
	}
	
	/**
	 * gets block length
	 * @return block length for this document
	 */
	public double getBlockLength(){
		return mBlockLength;
	}
	/**
	 * gets piece length for a specific piece, it's the same 
	 * as getPieceLength() except for the last piece
	 * @param index is the piece index
	 * @return piece length
	 */
	public double getPieceLength(int index){
		
		if(index<0||index>mPieceNum-1){
			mDebugLog.warning("piece index out of bound from getPieceLength!");
			return 0;
		}
		if(index==mPieceNum-1)
			return mSize-(mPieceNum-1)*mPieceLength;
		else
			return mPieceLength;
	}
	
	/**
	 * sets piece length
	 * @param plength new piece length to be set
	 */
	 
	public void setPieceLength(double plength){
		mPieceLength=plength;
	}
	
	/**
	 * gets piece number
	 * @return total piece number
	 */
	public int getPieceNum(){
		return mPieceNum;
	}
	
	/**
	 * gets piece status array for setting whole or not
	 * @return piece bit field array
	 */
	public boolean[] getPieces(){
		return mPieces;
	}
	
	/**
	 * checks if the document has that piece or not
	 * @param index is piece index
	 * @return true if have
	 */
	public boolean havePiece(int index){
		if(index<0||index>mPieceNum-1){
			mDebugLog.warning("piece index out of bound from havePiece!");
			return false;
		}
		return mPieces[index];
	}
}

⌨️ 快捷键说明

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