// #22 package ooos; import java.net.*; public class NetGraph { public static int N = 6; public static int BASE_PORT = 5800; public static final int INF = 99999; public static int nextPort = BASE_PORT; public static final int DECODE_ERR = -99999; public static boolean useNet = false; public PortIF ports[] = new PortIF[N+1]; public int w[][] = new int[N+1][N+1]; public String hosts[] = new String[N+1]; public int portNums[] = new int[N+1]; private String localHost; public NetGraph() { localHost = getLocalHost(); initIP(); initPortNum(); initWeights(); initPorts(); pause(); } public String getLocalHost() { String s = "UNKNOWN"; if (useNet) try { InetAddress inet = InetAddress.getLocalHost(); s = inet.getHostName(); System.out.println("LocalHost: "+s); } catch (Exception e) {} return s; } private void initIP() { //hosts[1] = "computer1"; // hosts[1] = localHost; for home PC //hosts[2] = "computer2"; //hosts[3] = "computer3"; //hosts[4] = "computer4"; //hosts[5] = "computer5"; //hosts[6] = "computer6"; hosts[1] = "gold"; hosts[2] = "gold"; hosts[3] = "gold"; hosts[4] = "gold"; hosts[5] = "gold"; hosts[6] = "gold"; } private void initPortNum() { for (int i=1; i<=N; i++) portNums[i] = nextPort++; } private void initWeights() { for (int i=1; i<=N; i++) for (int j=1; j<=N; j++) w[i][j] = INF; w[1][2] = 7; w[2][1] = 7; w[1][3] = 0; w[3][1] = 0; w[1][4] = 6; w[4][1] = 6; w[2][3] = 4; w[3][2] = 4; w[2][5] = 2; w[5][2] = 2; w[3][4] = 5; w[4][3] = 5; w[3][6] = 3; w[6][3] = 3; w[4][6] = 1; w[6][4] = 1; w[5][6] = 8; w[6][5] = 8; } private void initPorts() { for (int i=1; i<=N; i++) if (useNet) ports[i] = new NetPort("NetPort"+i,hosts[i],portNums[i]); else ports[i] = new Port("Port"+i); } public boolean isLocal(int i) { if (useNet) return (localHost.equals(hosts[i])); else return true; } private void pause() { if (useNet) { System.out.println("Type after all network systems are up."); try { int i = System.in.read(); } catch (Exception e) {} } } public static String encode(String s, int i) { return (s+i+","); } public static int decode(String s, int index) { int count=0; int lastindex = 0; int nextindex, result; while (true) { nextindex = s.indexOf(',',lastindex); if (nextindex < 0) return DECODE_ERR; else if (count==index) { try { String substr = s.substring(lastindex,nextindex); result = Integer.parseInt(substr); } catch (Exception e) {result = DECODE_ERR;} return result; } else { count++; lastindex = nextindex+1; } } } }