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

📄 bandwidthmanager.java

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


package gps.network;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.logging.Logger;
import java.awt.Color;
import java.awt.Graphics;

import gps.network.graph.Link;
import gps.util.LogFormatter;


/**
 * bandwidth manager on each link
 *
 * @author  Weishuai Yang
 * @version 1.2,  6/20/2005
 */
public class BandwidthManager {
	/**
	 * singleton reference to debug log
	 */
	protected static Logger mDebugLog = Logger.getLogger("Debug");
	/**
	 * the link to manage bandwidth for
	 */
    protected Link mLink;
	/**
	 * total bandwidth (2 ways in total)
	 */
    protected double mBandwidth=0;
	/**
	 * available bandwidth (2 ways in total)
	 */
    protected double mAvailableBandwidth=0;
	/**
	 * active connections on this link
	 */
    protected LinkedHashSet mActiveConnectionList=new LinkedHashSet();
    /**
	 * inactive connections on this link
	 */
    protected LinkedHashSet mInActiveConnectionList=new LinkedHashSet();
    

	/** 
	 * constructor
	 * initiates a bandwidth on link
	 * @param l link
	 */
    public BandwidthManager(Link l){
        mLink=l;
        setBandwidth(l.getProperties().getBandwidth());
    }
	/** 
	 * reset to initial value
	 */
	public void reset(){
		setBandwidth(mLink.getProperties().getBandwidth());
		mActiveConnectionList.clear();
		mInActiveConnectionList.clear();
	}
	
	/** 
	 * gets physical bandwidth
	 * @return maximum bandwidth possible
	 */
    public double getBandwidth(){
        return mBandwidth;
    }

	/** 
	 * sets physical bandwidth
	 * @param i maximum bandwidth possible
	 */
    public void setBandwidth(double i){
        mBandwidth=i;
        mAvailableBandwidth=i;
    }

	/**
	 * gets available bandwidth
	 * 
	 * @return available bandwidth
	 */
	public double getAvailableBandwidth(){
		return mAvailableBandwidth;
	}
	
	/** 
	 * gets active connections on this link
	 * @return active connection list
	 */
    public LinkedHashSet getActiveConnectionList(){
        return mActiveConnectionList;
    }

	/** 
	 * gets inactive connections on this link
	 * @return inactive connection list
	 */
    public LinkedHashSet getInActiveConnectionList(){
        return mInActiveConnectionList;
    }
	
	
	/** 
	 * add a connection on this link
	 * @param c new connection
	 */
    public void addConnection(Connection c){
    		mInActiveConnectionList.add(c);
    }

	/** 
	 * remove a connection on this link
	 * @param c connection to be removed
	 */
    public void removeConnection(Connection c){
    		mInActiveConnectionList.remove(c);
    }

	/** 
	 * active a existing connection on this link
	 * @param c connection to be activated
	 */
    public void activeConnection(Connection c){
    	mInActiveConnectionList.remove(c);
    	//if(!mActiveConnectionList.contains(c)){
    		mAvailableBandwidth-=c.mBandwidth;
    		if(Math.abs(mAvailableBandwidth-0)<0.000001)
					mAvailableBandwidth=0;
    		if(mAvailableBandwidth<0)
    				mDebugLog.severe("available bandwidth on "+ mLink+ "becomes less than 0 after active connection "+c);
			
    	//}
    	mActiveConnectionList.add(c);
    }

	/** 
	 * inactive a existing connection on this link
	 * @param c connection to be inactivated
	 */
    public void inActiveConnection(Connection c){
    	if(mActiveConnectionList.contains(c)){
    		mAvailableBandwidth+=c.mBandwidth;
    	}
    	mActiveConnectionList.remove(c);
    	mInActiveConnectionList.add(c);
    	
		if(Math.abs(mAvailableBandwidth-mBandwidth)<0.000001)
			mAvailableBandwidth=mBandwidth;
		if(mAvailableBandwidth>mBandwidth)
			mDebugLog.severe("available bandwidth "+mAvailableBandwidth+" on "+ mLink+ "out of bound after inactive "+c);
    }

	/** 
	 * return bandwidth occupied by connection to available bandwidth
	 * @param c connection returning bandwidth
	 */
	public void returnBandwidth(Connection c){
		mAvailableBandwidth+=c.mBandwidth;
		if(Math.abs(mAvailableBandwidth-mBandwidth)<0.000001)
			mAvailableBandwidth=mBandwidth;
		if(mAvailableBandwidth>mBandwidth)
			mDebugLog.severe("available bandwidth "+mAvailableBandwidth+" on "+ mLink+ "out of bound after return bandwidth from "+c);
	}

	/** 
	 * get estimated amount of bandwidth that can be allocated to a connection. 
	 * After negotiation, this connection become inactive
	 * @param c connection that wants bandwidth
	 */
	public double negotiateForConnection(Connection c){

    	int connections=0;
    	if(mActiveConnectionList.contains(c)){
			connections=mActiveConnectionList.size();
    	}
    	else
    		connections=mActiveConnectionList.size()+1;
    	
    	//put it at inactive list
    	double average=mBandwidth/(double)connections;
		//System.out.println("average is "+average+" mBandwidth is "+ mBandwidth + " connections is "+ connections);
    	double ret=0;
		if(mAvailableBandwidth>average)
			ret=mAvailableBandwidth;
		else
			ret=average;
		if(ret==0)
			mDebugLog.warning("Negotiated bandwidth on Link"+mLink+" for connection "+c+"is 0!");

		return ret;

	}
	
	/** 
	 * adjucst bandwidth for a connection
	 * @param c connection that needs adjust bandwidth
	 * @param adj adjust amount, positive for increase, and negative for decrease
	 */
	public void adjustBandwidthForConnection(Connection c, double adj){
		if(!mActiveConnectionList.contains(c)){
			mDebugLog.severe("trying to adjust bandwidth for connection "+c+"but it's not in active connection list of link "+mLink);	
    	}
		mAvailableBandwidth-=adj;

		if(Math.abs(mAvailableBandwidth-mBandwidth)<0.000001)
			mAvailableBandwidth=mBandwidth;
		else if(Math.abs(mAvailableBandwidth-0)<0.000001)
			mAvailableBandwidth=0;

		if((mAvailableBandwidth<0)||(mAvailableBandwidth>mBandwidth))
					mDebugLog.severe("available bandwidth "+mAvailableBandwidth+" on "+ mLink+ "out of bound after adjusting "+adj+" for "+c);
	}
	

	/** 
	 * reserve bandwidth for a connection
	 * @param c connection that needs reserve bandwidth
	 */
    public void reserveFor(Connection c){
 
    	if(mAvailableBandwidth>=c.mReservedBandwidth)
    		return;

    	int connections=0;
    	if(mActiveConnectionList.contains(c)){
			connections=mActiveConnectionList.size();
    	}
    	else
    		connections=mActiveConnectionList.size()+1;
    		
    	double average=mBandwidth/(double)connections;
    	
    	//let every connection adjust their bandwidth
    	
    	Iterator it=mActiveConnectionList.iterator();
    	while(it.hasNext()){
    		Connection cc=(Connection)it.next();
    		if(cc==c) continue;
    		if(cc.mBandwidth>average)
    			cc.adjustBandwidth(average-cc.mBandwidth);
    	}
    	
    }
	
	/**
	 * draws bandwidth information on GUI component
	 * 
	 * @param g Graphics object
	 * @param x x position
	 * @param y y position
	 */

	public void bandwidthDraw(Graphics g, int x, int y){
	    //if there is traffic on this link
	    if(mAvailableBandwidth!=mBandwidth){
	    	StringBuffer sb=new StringBuffer(LogFormatter.sprintf("%.1f%",
	    		(mBandwidth-mAvailableBandwidth)/mBandwidth*100));
	    	/*
	    	sb.append('(');
	    	sb.append(mActiveConnectionList.size());
	    	sb.append('/');
	    	sb.append(mActiveConnectionList.size()+mInActiveConnectionList.size());
	    	sb.append(')');
	    	*/
	    	g.setColor(Color.black);
	    	g.drawString(sb.toString(),x,y);
	    }
	}

	/**
	 * gets string description
	 * 
	 * @return string description
	 */

	public String toString() {
		String ret = "BandwidthManager("+mLink+")";

		ret += " ActiveList:"+mActiveConnectionList.size();
		ret += " InActiveList:"+mInActiveConnectionList.size();
		ret += " Bandwidth:"+mBandwidth;
		ret += " Available Bandwidth:"+mAvailableBandwidth;

		return ret;
	}	
}

⌨️ 快捷键说明

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