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

📄 btalgorithmoptimisticunchoking.java

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

package gps.protocol.BT.algorithm;
import gps.event.SimEvent;
import gps.event.SimEventHandler;
import gps.event.SimEventScheduler;
import gps.protocol.BT.BT;
import gps.protocol.BT.BTPeer;
import gps.protocol.BT.BTSession;
import gps.protocol.BT.BTSocket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Random;
import java.util.logging.Logger;


/**
 * BT optimistic unchoking algorithm.
 * 
 * @author  Weishuai Yang
 * @version 1.2,  6/20/2005
 */
public class BTAlgorithmOptimisticUnchoking implements SimEventHandler {
	/**
	 * singleton reference to event scheduler
	 */
	protected static SimEventScheduler mScheduler=SimEventScheduler.getInstance();
	/**
	 * singleton reference to debug log
	 */
	protected static Logger mDebugLog = Logger.getLogger("Debug");
	
	/**
	 * default optimistic unchoking interval
	 */
	public static int DEFAULT_OPTIMISTIC_UNCHOKING_INTERVAL=30; //30 sec
	
	/**
	 * default snubbing detect interval
	 */
	public static int DEFAULT_SNUBBING_DETECT_INTERVAL=60; //60 sec
	/**
	 * default snubbing unchoke number
	 */
	public static int DEFAULT_SNUBBING_UNCHOKE_NUM=4;  //unchoke 4 if snubbing

	
	/**
	 * optimistic unchoking interval
	 */
	protected int mDefaultOptimisticUnchokingInterval=DEFAULT_OPTIMISTIC_UNCHOKING_INTERVAL;
	/**
	 * snubbing detect interval
	 */
	protected int mDevaultSnubbingDetectInterval=DEFAULT_SNUBBING_DETECT_INTERVAL;
	/**
	 * snubbing unchoke number
	 */
	protected int mDefaultSnubbingUnchokeNum=DEFAULT_SNUBBING_UNCHOKE_NUM;

	/**
	 * last run time, to check if interval is reached
	 */
	protected double mLastRunningTime=0;
	/**
	 * the session this optimistic unchoking is working for
	 */
	protected BTSession mSession=null;
	
	/**
	 * constructs optimistic unchoking algorithm object
	 * @param session the session this optimistic unchoking is working for
	 */
	public BTAlgorithmOptimisticUnchoking(BTSession session){
		mSession=session;	
	}

	/**
	 * constructs optimistic unchoking algorithm object
	  * @param session the session this optimistic unchoking is working for
	  * @param i1 optimistic unchoking interval
	  * @param i2 snubbing detect interval
	 */
	public BTAlgorithmOptimisticUnchoking(BTSession session, int i1, int i2){
		mSession=session;
		mDefaultOptimisticUnchokingInterval=i1;
		mDevaultSnubbingDetectInterval=i2;
	}
	
	/**
	 * handles optimistic unchoking events
	 * @param e optimistic unchoking event
	 * @return true if already handled
	 */
	public boolean handle(SimEvent e) {
	    
	    double now=mScheduler.getCurrent();
	    LinkedHashSet unchoked=mSession.getUnchokedList();
		LinkedList candidate=mSession.getPeerCandidateList();
	    
	    if(mLastRunningTime==0){
	        //first time run
	        BTPeer p=null;
	        if(candidate.size()!=0){
	        	p=(BTPeer)candidate.removeFirst();
	        	mDebugLog.info(mScheduler.getCurrent()+": "+mSession+" optimisticly unchokes "+p);
	        	mSession.connectTo(p, false);
	        }
	    }
		else if(now-mLastRunningTime<mDefaultOptimisticUnchokingInterval){
		    //not enough time, simply return
		    return true;
		}
		else{
		    //do optomistic unchoking
	        BTPeer p=null;
	        if(candidate.size()!=0){
	        	p=(BTPeer)candidate.removeFirst();
	        	//connect with unchoked flag
	        	mDebugLog.info(mScheduler.getCurrent()+": "+mSession+" optimisticly unchokes "+p);
	        	mSession.connectTo(p, false);
	        }
		}
		
	    
	    if(now-mLastRunningTime>=mDevaultSnubbingDetectInterval){
		    //do snubbing detection
		    //System.out.println("snubbing detection called by "+mSession);
		    if(mSession.getDocument().isWhole())
		    	return true;
		    
		    if(mSession.getDownloadBetweenSnubbingDetection()==0){
		        mDebugLog.info(mScheduler.getCurrent()+": "+mSession.getAgent()+" snubbing decteded.");
		    	ArrayList conList=new ArrayList(mSession.getConnections().values());

				Random r=new Random(mSession.getAgent().getID()+Integer.parseInt(BT.getInstance().getSimulatorProperties().getProperty("RandomSeed")));

				Collections.shuffle(conList, r);
		    	
		    	for(int i=0; ((i<mDefaultSnubbingUnchokeNum)&&(i<conList.size()));i++){
		    		BTSocket connection=(BTSocket)conList.get(i);
		    		BTPeer p=(BTPeer)connection.mSession.getAgent();
		    	    if(!unchoked.contains(p)){
			    		connection.mAmChoking=false;
						mSession.sendChoked(false, p);
					}
		    	}

		    }
		    mSession.setDownloadBetweenSnubbingDetection(0);
		    
		}
	    
	    mLastRunningTime=now;
	    return true;
	}	
}

⌨️ 快捷键说明

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