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

📄 btsocket.java

📁 p2p仿真
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 		return new ArrayList(0);
	 		
	 	/*
	 	StringBuffer sb=new StringBuffer(mSession.getAgent()+"Pieces that "+mSession.getAgent()+" have: ");
		for(int i=0;i<pieces.length;i++){
		    if(pieces[i]){
		        sb.append(i+" ");
		    }
		}
		mDebugLog.info(mScheduler.getCurrent()+": "+sb.toString());
	 	*/
	 	
	 	//construct a list with pure ready pieces on the counterpart
	 	ArrayList morePieces=new ArrayList();
	 	for(int i=0;i<peerPieces.length;i++){
	 	    if((peerPieces[i])&&(!pieces[i])) {
	 	          morePieces.add(new Integer(i));
	 	    }
	 	}
		return morePieces;
	}
	
	/**
	 * when a block is finished downloading, this event is triggered
	 * @param e the event object
	 */
	public void handleBlockFinished(BTEvent e){

	    BTDocument btd=mSession.getDocument();
	    BTPeer p=(BTPeer)mCounterpart.mSession.getAgent();


		mSession.setDownloadBetweenSnubbingDetection(mLength+mSession.getDownloadBetweenSnubbingDetection());

		if(mRequestingPieceIndex<0)
		    mDebugLog.severe(mScheduler.getCurrent()+": "+mSession.getAgent()
	            +" when block finished mRequestingPieceIndex is "+mRequestingPieceIndex);

		if(!btd.havePiece(mRequestingPieceIndex)){
			    double piecepercent=1.0*(mBegin+mLength)/btd.getPieceLength(mRequestingPieceIndex);  
			    double totalpercent=btd.getFinishedPercent();
	
			    
				if(piecepercent<1.0){
				    /*
				    mDebugLog.info(mScheduler.getCurrent()+": "+mSession.getAgent()
				            +" block finished("+(piecepercent*100)+"%) within piece "+mRequestingPieceIndex
				            +"from "+mCounterpart+", total finished percent("+((int)(totalpercent*10000)/(double)100)+"%)");
					*/
					mSession.getDocument().pieceProgressAdd(mRequestingPieceIndex, mLength);
				}
				else{			
				    btd.finishPiece(mRequestingPieceIndex);

				    totalpercent=btd.getFinishedPercent();
					mDebugLog.info(mScheduler.getCurrent()+": "+mSession.getAgent()
								+" finished downloading piece "+mRequestingPieceIndex+" of "+mSession.getDocument().getKey()+" from "
								+mCounterpart+", total finished percent("
								+((int)(totalpercent*10000)/(double)100)+"%), left piece number:"+btd.getLeftPieceNum());
					
					if(btd.isWhole()){
						mAmInterested=false;
						mSession.sendInterested(false, p);
						mSession.mStatus=BTSession.COMPLETED;
						mSession.announceTracker();		
						//finished whole file
						/////debug begin
						mDebugLog.info(mScheduler.getCurrent()+": "+mSession.getAgent()+" finished downloading document "+mSession.getDocument().getKey());
						/////debug end
					}
			
					mSession.announcePeer(mRequestingPieceIndex);
					
					//update document information on screen
					ProtocolPanel pp=Simulator.getInstance().getPP();
					if(pp!=null){
					    if(((totalpercent-mPreviousPercent)*100>1)
					            ||((totalpercent!=0)&&(mPreviousPercent==0))) 
					    pp.getSimGuiControl().getGraphPanel().repaint();
					}
					
					mRequestingPieceIndex=-1;
			}
		}
	    
	    else{
	    	//already have that piece, consider next request, select new piece to download
	    	btd.removePieceDownloading(mRequestingPieceIndex);
	    	mRequestingPieceIndex=-1;
	    }

		
		//recover this end's initial value
		mDownloading=false;
		//if(mSession.getAgent().getID()==3)
		//System.out.println(""+mScheduler.getCurrent()+this+"stops downloading2");
		//recover the other end's initial value
		mCounterpart.mUploading=false;
		
		mBegin=0;
		mLength=0;
		mDownloadLeft=0;
		mStartTime=0;
		mEstimateFinish=null;
		
		
		if(mCounterpart.mDownloading){

			mConnection.setBandwidthFrom(this, mConnection.getBandwidth());
			mConnection.setBandwidthFrom(mCounterpart, 0);
			
			mCounterpart.adjustInBandwidth(mConnection.getBandwidth()/2);
			adjustOutBandwidth(mConnection.getBandwidth()/2);
		}

		if(mRequestTimeout!=null){
		    mScheduler.cancel(mRequestTimeout);
			mRequestTimeout=null;
		}
		else
		    mDebugLog.warning(mScheduler.getCurrent()+": "+mSession.getAgent()+" after finish a block, request timeout is supposed to be on!");


		if(btd.isWhole()){
			mConnection.setInActive();
		    return;
		}
		
		if(mRequestingPieceIndex!=-1){
			btd.removePieceDownloading(mRequestingPieceIndex);
		}
		//if not choked, request next block
		if(!mPeerChoking)
			mSession.sendRequestTo(p);
		else{
		    //this connection is just get choked by the peer
		    //the other side should already set it to inactive
			mDebugLog.info(mScheduler.getCurrent()+": "+mSession.getAgent()+" after finish a block, because being choked by "+ p);
		    mConnection.setInActive();
		    mRequestingPieceIndex=-1;
		    return;
		}
	}

	/**
	 * adjusts bandwidth during download, called by counterpart
	 * @param adj the adjustment amount
	 */
	public void adjustInBandwidth(double adj){
		
		//it's possible that I'm not downloading
	    
		if(!mDownloading) return;
		if(mEstimateFinish==null)	return;
		//else if(mDownloadLeft==0) return;
	
		if(mRequestingPieceIndex==-1) return;
		else if(mSession.getDocument().havePiece(mRequestingPieceIndex))
			return;
		
		double preRate=((Double)(mEstimateFinish.getAddParam())).doubleValue();
		//System.out.println(mScheduler.getCurrent()+ ": "+ this + " preRate is "+preRate+", adj is "+adj);
		
		//update progress
		int addDown=(int)((mScheduler.getCurrent() - mStartTime)*preRate);
		
		mSession.getDocument().pieceProgressAdd(mRequestingPieceIndex, (int)addDown);
		
		if(addDown>mDownloadLeft){
		    //XXX
			mDebugLog.warning(mScheduler.getCurrent()+":"+mSession+" adjust in Bandwidth encounter piece "+mRequestingPieceIndex+" should already finished!");
			mDownloadLeft=0;
		}
		else mDownloadLeft-=(int)addDown;
		
		//if(mConnection.mBandwidth==0)	return;
		
		double finish=mScheduler.getCurrent()+(mDownloadLeft/mConnection.getBandwidthFrom(mCounterpart));
		mScheduler.cancel(mEstimateFinish);
		mEstimateFinish.setTimeStamp(finish);
		mEstimateFinish.setAddParam(new Double(mConnection.getBandwidthFrom(mCounterpart)));
		
		//mDebugLog.info(mScheduler.getCurrent()+": "+this+" estimated finish time adjusted to"+finish+" download left is "+mDownloadLeft+", and bandwidth is "+mConnection.mBandwidth);
		
		mScheduler.enqueue(mEstimateFinish);
		mStartTime=mScheduler.getCurrent();
		mInBandwidthHistory.add(new BandwidthHistoryItem(mStartTime, mConnection.getBandwidthFrom(mCounterpart)));
		
	}

	/**
	 * adjust out bandwidth
	 * @param adj the adjustment amount
	 */
	public void adjustOutBandwidth(double adj){
		if(!mUploading) return;
		mOutBandwidthHistory.add(new BandwidthHistoryItem(mScheduler.getCurrent(), mConnection.getBandwidthFrom(this)));
	}


	/**
	 * calculate average input bandwidth since a time spot
	 * @param time time spot
	 * @return average bandwidth
	 */
	public double calcInAverageSince(double time){
	    //if no record yet
	    if(mInBandwidthHistory.size()==0)
	        return 0;
	    
	    //since size != 0, bhi must not be null
	    BandwidthHistoryItem bhi=(BandwidthHistoryItem)mInBandwidthHistory.peek();
	    BandwidthHistoryItem tmp=null;
	    
	    if(time<=0) time=bhi.mTimeStamp;
	    
	    if((time>0)){
	    	//if the time is long enough, remove old
		    while((bhi!=null)&&(bhi.mTimeStamp<time)){
		        //since bhi!=null, tmp can't be null
		        tmp=(BandwidthHistoryItem)mInBandwidthHistory.poll();
		        bhi=(BandwidthHistoryItem)mInBandwidthHistory.peek();
		    }
		    if(tmp!=null){
		        tmp.mTimeStamp=time;
		        mInBandwidthHistory.addFirst(tmp);
		    }
		    bhi=(BandwidthHistoryItem)mInBandwidthHistory.peek();
		    
	    }
	    
	    
	    //calculate the average bandwidth to each second
	    double t=time;
	    double b=0;
	    double sum=0;
	    
	    while(bhi!=null){
	        sum=(bhi.mTimeStamp-t)*bhi.mBandwidth;
	        t=bhi.mTimeStamp;
	        tmp=(BandwidthHistoryItem)mInBandwidthHistory.poll();
	        bhi=(BandwidthHistoryItem)mInBandwidthHistory.peek();
	    }
	    sum+=(mScheduler.getCurrent()-tmp.mTimeStamp)*tmp.mBandwidth;
	    tmp.mTimeStamp=mScheduler.getCurrent();
	    mInBandwidthHistory.addFirst(tmp);
	    return sum/(mScheduler.getCurrent()-time);
	}

	/**
	 * calculate average output bandwidth since a time spot
	 * @param time time spot
	 * @return average bandwidth
	 */
	public double calcOutAverageSince(double time){
	    //if no record yet
	    if(mOutBandwidthHistory.size()==0)
	        return 0;
	    
	    BandwidthHistoryItem bhi=(BandwidthHistoryItem)mOutBandwidthHistory.peek();
	    BandwidthHistoryItem tmp=null;
	    
	    if(time<=0) time=bhi.mTimeStamp;
	    
	    if(time>0){
	    	//if the time is long enough, remove old
	    	
		    while((bhi!=null)&&(bhi.mTimeStamp<=time)){
		        tmp=(BandwidthHistoryItem)mOutBandwidthHistory.removeFirst();
		        bhi=(BandwidthHistoryItem)mOutBandwidthHistory.peek();
		    }
		    if(tmp!=null){
		        tmp.mTimeStamp=time;
		        mOutBandwidthHistory.addFirst(tmp);
		    }
		    bhi=(BandwidthHistoryItem)mOutBandwidthHistory.peek();
	    
	    }
	    //calculate the average bandwidth to each second
	    double t=time;
	    double b=0;
	    double sum=0;
	    
	    while(bhi!=null){
	        sum=(bhi.mTimeStamp-t)*bhi.mBandwidth;
	        t=bhi.mTimeStamp;
	        tmp=(BandwidthHistoryItem)mOutBandwidthHistory.removeFirst();
	        bhi=(BandwidthHistoryItem)mOutBandwidthHistory.peek();
	    }
	    sum+=(mScheduler.getCurrent()-tmp.mTimeStamp)*tmp.mBandwidth;
	    tmp.mTimeStamp=mScheduler.getCurrent();
	    mOutBandwidthHistory.addFirst(tmp);
	    return sum/(mScheduler.getCurrent()-time);
	}
	



	/**
	 * gets string description
	 * @return a string describing this session
	 */
	public String toString() {
	    if(mCounterpart==null)
	        return "BTC()@"+mSession.getAgent();
	    else
	        return "BTC("+mCounterpart.mSession.getAgent()+")@"+mSession.getAgent();
	}

	//Internal class for recording bandwidth history
	private class BandwidthHistoryItem{
		 public double mTimeStamp=0;
		 public double mBandwidth=0;
		 public BandwidthHistoryItem(double t, double b){
		     mTimeStamp=t;
		     mBandwidth=b;
		 }
	}
}

⌨️ 快捷键说明

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