package ibis.util;

import java.util.Properties;
import java.util.StringTokenizer;
import java.net.InetAddress;

public class PoolInfo {

	private static int total_hosts;
	private  static int host_number;
	private  static String [] host_names;
	private  static InetAddress [] hosts;

	static {
		String temp;
		
		Properties p = System.getProperties();
		
		total_hosts = getIntProperty(p, "ibis.pool.total_hosts");
		host_number = getIntProperty(p, "ibis.pool.host_number");
		
		temp = p.getProperty("ibis.pool.host_names");
		if(temp == null) {
			throw new RuntimeException("Property ibis.pool.host_names not set!");
		}
		
		if (host_number >= total_hosts || host_number < 0 || total_hosts < 1) {
			throw new RuntimeException("Sanity check on host numbers failed!");
		}
		
		host_names = new String[total_hosts];
		hosts      = new InetAddress[total_hosts];
		
		StringTokenizer tok = new StringTokenizer(temp, " ", false);
		
		for (int i=0;i<total_hosts;i++) {
			
			String t = tok.nextToken();       
			
			try {
				/*
				  This looks weird, but is required to get the entire hostname
				  ie. 'java.sun.com' instead of just 'java'.
				*/
				
				InetAddress adres = InetAddress.getByName(t);
				adres             = InetAddress.getByName(adres.getHostAddress());
				host_names[i]     = adres.getHostName();
				if (! host_names[i].equals(t) &&
				    host_names[i].toUpperCase().equals(t.toUpperCase())) {
				    System.err.println("This is probably M$ Windows. Restore lower case in host name " + t);
				    host_names[i] = t;
				}
				hosts[i]          = adres;
				
			} catch (Exception e) {
				throw new RuntimeException("Could not find host name " + t);
			}		       			
		}
	}

	public static int size() {
		return total_hosts;
	}

	public static int rank() {
		return host_number;
	}

	public static String hostName() {
		return host_names[host_number];
	}

	public static String hostName(int rank) {
		return host_names[rank];
	}

	public static InetAddress hostAddress(int rank) {
		return hosts[rank];
	}

	public static InetAddress hostAddress() {
		return hosts[host_number];
	}

	public static InetAddress[] hostAddresses() {
		return hosts;
	}

	public static String[] hostNames() {
		return host_names;
	}

	private static int getIntProperty(Properties p, String name) throws RuntimeException {

		String temp = p.getProperty(name);
		
		if (temp == null) { 
			throw new RuntimeException("Property " + name + " not found !");
		}
		
		return Integer.parseInt(temp);
	}	


    public static void printTime(String id, long time) {
	System.out.println("Application: " + id + "; Ncpus: " + total_hosts +
			   "; time: " + time/1000.0 + " seconds\n");
    }

}

@@ -136,160 +136,811 @@
 %7B%0A%0A%09
-private static int total_hosts;%0A%09private  static int host_number;%0A%09private  static String %5B%5D host_names;%0A%09private  static InetAddress %5B%5D hosts;%0A%0A%09static
+int total_hosts;%0A%09int host_number;%0A%09String %5B%5D host_names;%0A%09InetAddress %5B%5D hosts;%0A%0A%09public PoolInfo() %7B%0A%09    this(false);%0A%09%7D%0A%0A%09public PoolInfo(boolean forceSequential) %7B%0A%09    if (forceSequential) %7B%0A%09%09sequentialPool();%0A%09    %7D else %7B%0A%09%09propertiesPool();%0A%09    %7D%0A%09%7D%0A%0A%0A%09private void sequentialPool() %7B%0A%09%09String temp;%0A%09%09%0A%09%09total_hosts = 1;%0A%09%09host_number = 0;%0A%09%09%0A%09%09host_names = new String%5Btotal_hosts%5D;%0A%09%09hosts      = new InetAddress%5Btotal_hosts%5D;%0A%09%09%09%09%0A%09%09try %7B%0A%09%09%09InetAddress adres = InetAddress.getLocalHost();%0A%09%09%09adres             = InetAddress.getByName(adres.getHostAddress());%0A%09%09%09host_names%5Bhost_number%5D = adres.getHostName();%0A%09%09%09hosts%5Bhost_number%5D      = adres;%0A%09%09%09%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09throw new RuntimeException(%22Could not find my host name%22);%0A%09%09%7D%09%09       %09%09%09%0A%09%7D%0A%0A%0A%09private void propertiesPool()
  %7B%0A%09
@@ -2357,31 +2357,24 @@
 %09%7D%0A%0A%09public 
-static 
 int size() %7B
@@ -2408,23 +2408,16 @@
 %09public 
-static 
 int rank
@@ -2447,39 +2447,32 @@
 er;%0A%09%7D%0A%0A%09public 
-static 
 String hostName(
@@ -2517,31 +2517,24 @@
 %09%7D%0A%0A%09public 
-static 
 String hostN
@@ -2580,39 +2580,32 @@
 k%5D;%0A%09%7D%0A%0A%09public 
-static 
 InetAddress host
@@ -2650,39 +2650,32 @@
 k%5D;%0A%09%7D%0A%0A%09public 
-static 
 InetAddress host
@@ -2727,23 +2727,16 @@
 %09public 
-static 
 InetAddr
@@ -2787,23 +2787,16 @@
 %09public 
-static 
 String%5B%5D
@@ -3112,23 +3112,16 @@
  public 
-static 
 void pri

Make PoolInfo fields non-static (why was that ever?)