//----------------------------------------------------------------------
//This code is developed as part of the Java CoG Kit project
//The terms of the license can be found at http://www.cogkit.org/license
//This message may not be removed or altered.
//----------------------------------------------------------------------

/*
 * Created on Jun 21, 2005
 */
package org.globus.cog.karajan.scheduler;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.globus.cog.karajan.util.BoundContact;

public class WeightedHost implements Comparable {
	private BoundContact host;
	private Double score;
	private double tscore;
	private int load;
	private double delayedDelta;

	public WeightedHost(BoundContact contact) {
		this(contact, 0.0);
	}

	public WeightedHost(BoundContact contact, double score) {
		this(contact, score, 0);
	}
	
	public WeightedHost(BoundContact contact, double score, int load) {
		this.host = contact;
		setScore(score);
		this.load = load;
	}

	protected void setScore(double score) {
		this.score = new Double(score);
		this.tscore = smooth(score);
	}

	public static final double T = 100;
	public static final double B = 2.0*Math.log(T)/Math.PI;
	public static final double C = 0.2;

	public double smooth(double score) {
		return Math.exp(B*Math.atan(C*score));
	}

	public final double getScore() {
		return score.doubleValue();
	}

	public final Double getScoreAsDouble() {
		return score;
	}

	public final double getTScore() {
		return tscore;
	}

	public final BoundContact getHost() {
		return host;
	}

	public int getLoad() {
		return load;
	}

	public void setLoad(int load) {
		this.load = load;
	}

	public synchronized void changeLoad(int dl) {
		load += dl;
		if (load < 0) {
			load = 0;
		}
	}

	public boolean equals(Object obj) {
		if (obj instanceof WeightedHost) {
			WeightedHost wh = (WeightedHost) obj;
			return host.equals(wh.host);
		}
		return false;
	}

	public int hashCode() {
		return host.hashCode();
	}

	public static final NumberFormat D4;
	static {
		D4 = DecimalFormat.getInstance();
		D4.setMaximumFractionDigits(3);
		D4.setMinimumFractionDigits(3);
	}

	public String toString() {
		return host.toString() + ":" + D4.format(score) + "(" + D4.format(tscore) + "):"+load;
	}

	public int compareTo(Object o) {
		WeightedHost other = (WeightedHost) o;
		int r = score.compareTo(other.score);
		if (r == 0) {
			// arbitrary ordering on the contact
			return System.identityHashCode(host) - System.identityHashCode(other.host);
		}
		else {
			return r;
		}
	}

	public double getDelayedDelta() {
		return delayedDelta;
	}

	public void setDelayedDelta(double delayedDelta) {
		this.delayedDelta = delayedDelta;
	}
	
	
}
@@ -684,16 +684,42 @@
 edDelta;
+%0A%09private int jobThrottle;
 %0A%0A%09publi
@@ -753,16 +753,33 @@
  contact
+, int jobThrottle
 ) %7B%0A%09%09th
@@ -793,16 +793,29 @@
 act, 0.0
+, jobThrottle
 );%0A%09%7D%0A%0A%09
@@ -860,32 +860,49 @@
 ct, double score
+, int jobThrottle
 ) %7B%0A%09%09this(conta
@@ -917,15 +917,27 @@
 e, 0
+, jobThrottle
 );%0A%09%7D%0A
-%09
 %0A%09pu
@@ -990,32 +990,49 @@
  score, int load
+, int jobThrottle
 ) %7B%0A%09%09this.host 
@@ -1077,24 +1077,58 @@
 oad = load;%0A
+%09%09this.jobThrottle = jobThrottle;%0A
 %09%7D%0A%0A%09protect
@@ -1301,17 +1301,19 @@
  B = 2.0
-*
+ * 
 Math.log
@@ -1315,17 +1315,19 @@
 h.log(T)
-/
+ / 
 Math.PI;
@@ -1422,17 +1422,19 @@
 th.exp(B
-*
+ * 
 Math.ata
@@ -1436,17 +1436,19 @@
 h.atan(C
-*
+ * 
 score));
@@ -2392,13 +2392,99 @@
 %22):%22
-+
+ + 
 load
+%0A%09%09%09%09+ %22/%22 + (int)(jobThrottle * tscore + 2) + (isOverloaded() ? %22 overloaded%22 : %22%22)
 ;%0A%09%7D
@@ -2926,9 +2926,144 @@
 %0A%09%7D%0A
-%09
 %0A%09
+public boolean isOverloaded() %7B%0A%09%09return !(load %3C jobThrottle * tscore + 2);%0A%09%7D%0A%0A%09public int getJobThrottle() %7B%0A%09%09return jobThrottle;%0A%09%7D
 %0A%7D

move the overloaded logic into the wh