📄 connection.java
字号:
/*
* @(#)Connection.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.ArrayList;
import java.util.logging.Logger;
import gps.network.graph.Link;
import gps.network.graph.Node;
import gps.protocol.Agent;
/**
* high level directed connection between nodes
*
* @author Weishuai Yang
* @version 1.2, 6/20/2005
*/
public class Connection {
/**
* singleton reference to topology
*/
protected static Topology mTopology=Topology.getInstance();
/**
* singleton reference to debug log
*/
protected static Logger mDebugLog = Logger.getLogger("Debug");
//public Agent mSource;
//public Agent mDestination;
/**
* source node of the connection, i.e. initiator
*/
protected Node mSource;
/**
* destination of the connection
*/
protected Node mDestination;
/**
* all the links along the connection
*/
protected Link[] mLinks=null;
/**
* bandwidth consumed by this connection
*/
protected double mBandwidth=0;
/**
* bandwidth from source to destination
*/
protected double mBandwidthAtoB=0;
/**
* bandwidth from destination to source
*/
protected double mBandwidthBtoA=0;
/**
* bandwidth reserved for this connection along links, but not allocated yet
*/
protected double mReservedBandwidth=0;
/**
* active connection or not
*/
protected boolean mActive=false;
/**
* locked means it is adjusting
*/
protected boolean mLocked=false;
/**
* all the connections in the system
*/
protected static ArrayList mConnections=new ArrayList();
/**
* constructs a connection between agents
* @param a source agent
* @param b destination agent
*/
public Connection(Agent a, Agent b){
mSource=a.getNode();
mDestination=b.getNode();
mLinks=mTopology.getLinksBetween(a.getNode(), b.getNode());
mConnections.add(this);
//System.out.println("setting up connection from node "+mSource.getID()+" to node "+mDestination.getID());
}
/**
* constructs a connection between nodes
* @param a source node
* @param b destination node
*/
public Connection(Node a, Node b){
mSource=a;
mDestination=b;
mLinks=mTopology.getLinksBetween(a, b);
mConnections.add(this);
}
/**
* get all the connections in the system, class method
* @return connection list
*/
public static ArrayList getAllConnections(){
return mConnections;
}
/**
* print all connections in system, just for debug purpose
*/
public static void printConnections(){
System.out.println("\n");
for(int i=0;i<mConnections.size();i++){
System.out.println((Connection)mConnections.get(i));
}
Link[] links=Topology.getInstance().getLinks();
for(int i=0; i<links.length; i++){
double d=links[i].getBandwidthManager().getAvailableBandwidth();
System.out.println(links[i].fromNode()+"->"+links[i].toNode()+":"+d);
}
}
/**
* setup this connection along links
*/
public void setUp(){
//always add it to inactive list
mActive=false;
mBandwidth=0;
for(int i=0;i<mLinks.length;i++)
mLinks[i].getBandwidthManager().addConnection(this);
}
/**
* reserve bandwidth for this connection along links
*/
protected void reserveBandwidth(){
//after reserve, it's alwasy inactive, even if it is active before reserve
returnBandwidth();
negotiateBandwidth();
for(int i=0;i<mLinks.length;i++)
mLinks[i].getBandwidthManager().reserveFor(this);
}
/**
* return bandwidth for this connection along links
*/
protected void returnBandwidth(){
for(int i=0;i<mLinks.length;i++)
mLinks[i].getBandwidthManager().returnBandwidth(this);
mBandwidth=0;
}
protected void negotiateBandwidth(){
double minimum=mLinks[0].getBandwidthManager().negotiateForConnection(this);
for(int i=0;i<mLinks.length;i++){
double tmp=mLinks[i].getBandwidthManager().negotiateForConnection(this);
if(tmp<minimum){
minimum=tmp;
}
}
mReservedBandwidth=minimum;
}
/**
* apply reserved to be real bandwidth
*/
protected void applyReservedBandwidth(){
mBandwidth=mReservedBandwidth;
mReservedBandwidth=0;
for(int i=0;i<mLinks.length;i++)
mLinks[i].getBandwidthManager().activeConnection(this);
}
/**
* make connection active
*/
public void setActive(){
if(mActive) return;
reserveBandwidth();
mActive=true;
applyReservedBandwidth();
}
/**
* get active status
* @return true if active, otherwise false
*/
public boolean isActive(){
return mActive;
}
/**
* maximizes bandwidth for this connection
*/
protected void maximize(){
reserveBandwidth();
applyReservedBandwidth();
}
/**
* finds bottle neck along links
* @param forward true means search forward, otherwise backward
*/
public Link findBottleNeck(boolean forward){
double minimum=mLinks[0].getBandwidthManager().getAvailableBandwidth();
Link bottleNeck=mLinks[0];
if(forward)
for(int i=0;i<mLinks.length;i++){
if(mLinks[i].getBandwidthManager().getAvailableBandwidth()<minimum){
minimum=mLinks[i].getBandwidthManager().negotiateForConnection(this);
bottleNeck=mLinks[i];
}
}
else
for(int i=mLinks.length-1;i>=0;i--){
if(mLinks[i].getBandwidthManager().getAvailableBandwidth()<minimum){
minimum=mLinks[i].getBandwidthManager().negotiateForConnection(this);
bottleNeck=mLinks[i];
}
}
return bottleNeck;
}
/**
* gets bandwidth allocated for this connection
* @return allocated bandwidth
*/
public double getBandwidth(){
return mBandwidth;
}
/**
* gets one way bandwidth
* @param n source
* @return bandwidth from source
*/
public double getBandwidthFrom(Node n){
if(n==mSource)
return mBandwidthAtoB;
else if(n==mDestination)
return mBandwidthBtoA;
return 0;
}
/**
* sets one way bandwidth
* @param n source
* @param b new bandwidth value
*/
public void setBandwidthFrom(Node n, double b){
if(n==mSource)
mBandwidthAtoB=b;
else if(n==mDestination)
mBandwidthBtoA=b;
}
/**
* makes connection inactive
*/
public void setInActive(){
if(!mActive) return;
mActive=false;
for(int i=0;i<mLinks.length;i++)
mLinks[i].getBandwidthManager().inActiveConnection(this);
relaxBandwidthAlongTheWay();
mBandwidth=0;
}
public void relaxBandwidthAlongTheWay(){
//find bottleneck first
Link bottleNeck1=findBottleNeck(true);
Link bottleNeck2=null;
int count=0;
while(bottleNeck1!=bottleNeck2){
count++;
LinkedHashSet connections=bottleNeck1.getBandwidthManager().getActiveConnectionList();
ArrayList connlist=new ArrayList(connections);
Iterator it=connlist.iterator();
while(it.hasNext()){
Connection c=(Connection)it.next();
if(c==this){
mDebugLog.warning("relaxing self!");
return;
}
if(c.mActive){
if(c.getBandwidth()==0) return;
c.maximize();
}
else
mDebugLog.warning("connection get from active connection list is not active!");
}
bottleNeck2=bottleNeck1;
bottleNeck1=findBottleNeck(true);
if(count>100){
mDebugLog.warning("Finding bottleneck forward more than 100 times!");
}
}
//find bottleneck first
bottleNeck1=findBottleNeck(false);
bottleNeck2=null;
count=0;
while(bottleNeck1!=bottleNeck2){
count++;
LinkedHashSet connections=bottleNeck1.getBandwidthManager().getActiveConnectionList();
ArrayList connlist=new ArrayList(connections);
Iterator it=connlist.iterator();
while(it.hasNext()){
Connection c=(Connection)it.next();
if(c==this){
mDebugLog.warning("relaxing self!");
return;
}
if(c.mActive){
if(c.getBandwidth()==0) return;
c.maximize();
}
else
mDebugLog.warning("connection get from active connection list is not active!");
}
bottleNeck2=bottleNeck1;
bottleNeck1=findBottleNeck(false);
if(count>100){
mDebugLog.warning("Finding bottleneck reverse more than 100 times!");
}
}
}
/**
* tear down the connection
*/
public void tearDown(){
if(mActive)
setInActive();
for(int i=0;i<mLinks.length;i++)
mLinks[i].getBandwidthManager().removeConnection(this);
//System.out.println("tearing down connection from node "+mSource.getID()+" to node "+mDestination.getID());
mConnections.remove(this);
}
/**
* adjust bandwidth for provided amount
* @param adj adjust amount
*/
public void adjustBandwidth(double adj){
//request more or less bandwidth
if(!mActive)
mDebugLog.warning("Trying to adjust bandwidth for inactive connection "+ this);
mBandwidth+=adj;
for(int i=0;i<mLinks.length;i++)
mLinks[i].getBandwidthManager().adjustBandwidthForConnection(this, adj);
}
/**
* gets string description
*
* @return string description
*/
public String toString() {
String ret = "Connection("+mSource.getID()+"->"+mDestination.getID()+")";
if (mActive)
ret += ": Active, Bandwidth: "+mBandwidth;
else
ret += ": InActive";
if(mReservedBandwidth!=0)
ret +=" ReservedBandwidth: "+mReservedBandwidth;
ret+=" ";
return ret;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -