package NET.worlds.network; import java.io.IOException; import java.net.NoRouteToHostException; import java.net.Socket; import java.net.UnknownHostException; import java.util.StringTokenizer; import java.util.Vector; class WSConnecting implements Runnable { private WorldServer _serv; private String _host; private Vector _hosts; private int _unresolvedHosts; private int _port; private int _timeout; private int _error; private boolean _calledBack; private Vector _resolvedHosts; public WSConnecting(WorldServer serv, String host, int port, int timeout) { this._serv = serv; this._host = host; this._hosts = new Vector(); this._port = port; this._timeout = timeout; this._error = 0; this.makeThread(-1); StringTokenizer tok = new StringTokenizer(host, ";"); while (tok.hasMoreTokens()) { this._hosts.addElement(tok.nextToken()); } this._unresolvedHosts = this._hosts.size(); for (int i = 0; i < this._unresolvedHosts; i++) { this.makeThread(i); } } private void makeThread(int index) { Thread t = new Thread(this, Integer.toString(index)); t.setDaemon(true); t.start(); } @Override public void run() { int index = Integer.parseInt(Thread.currentThread().getName()); if (index == -1) { try { Thread.sleep(this._timeout * 1000); } catch (InterruptedException var10) { } synchronized (this) { if (!this._calledBack) { this._calledBack = true; if (this._error == 0) { this._error = 106; } this._serv.setSocket(null, new VarErrorException(this._error), null); } } } else if (index < this._unresolvedHosts) { try { String host = this._hosts.elementAt(index); String[] addresses = DNSLookup.lookupAll(host); if (addresses.length > 1) { for (int i = 0; i < addresses.length; i++) { int newIndex; synchronized (this._hosts) { newIndex = this._hosts.size(); this._hosts.addElement(addresses[i]); } this.makeThread(newIndex); } } else { this.connectTo(addresses[0], false); } } catch (UnknownHostException var11) { synchronized (this) { if (this._error == 0) { this._error = 107; } } } } else { this.connectTo(this._hosts.elementAt(index), false); } } public Vector getBackupHosts() { this._resolvedHosts = new Vector(); for (int i = this._unresolvedHosts; i < this._hosts.size(); i++) { this._resolvedHosts.addElement(this._hosts.elementAt(i)); } return this._resolvedHosts; } private void connectTo(String host, boolean delay) { Socket sock = null; try { sock = new Socket(host, this._port); synchronized (this) { if (delay) { try { this.wait(this._timeout * 1000 * 2 / 3); } catch (InterruptedException var7) { } } System.out.println("Connected to " + host); if (!this._calledBack) { this._calledBack = true; this._serv.setSocket(sock, null, host); } else { try { sock.close(); } catch (IOException var6) { } } this.notifyAll(); } } catch (IOException var10) { IOException e = var10; synchronized (this) { if (this._error == 0) { if (!(e instanceof NoRouteToHostException) && !(e instanceof UnknownHostException)) { this._error = 104; } else { this._error = 107; } } this.notifyAll(); } } } }