summaryrefslogtreecommitdiff
path: root/NET/worlds/network/WSConnecting.java
blob: 5437a86d721b2a4ae174e240f1dabba01b29a544 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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<String> _hosts;
   private int _unresolvedHosts;
   private int _port;
   private int _timeout;
   private int _error;
   private boolean _calledBack;
   private Vector<String> _resolvedHosts;

   public WSConnecting(WorldServer serv, String host, int port, int timeout) {
      this._serv = serv;
      this._host = host;
      this._hosts = new Vector<String>();
      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<String> getBackupHosts() {
      this._resolvedHosts = new Vector<String>();

      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();
         }
      }
   }
}