summaryrefslogtreecommitdiff
path: root/NET/worlds/network/WSConnecting.java
blob: c1af50916f3b6b13d57662f543348d7944d4ef94 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*     */ package NET.worlds.network;
/*     */ 
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ 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)
/*     */   {
/*  40 */     this._serv = serv;
/*  41 */     this._host = host;
/*  42 */     this._hosts = new Vector();
/*  43 */     this._port = port;
/*  44 */     this._timeout = timeout;
/*  45 */     this._error = 0;
/*     */     
/*     */ 
/*  48 */     makeThread(-1);
/*     */     
/*     */ 
/*     */ 
/*  52 */     StringTokenizer tok = new StringTokenizer(host, ";");
/*  53 */     while (tok.hasMoreTokens())
/*  54 */       this._hosts.addElement(tok.nextToken());
/*  55 */     this._unresolvedHosts = this._hosts.size();
/*     */     
/*     */ 
/*  58 */     for (int i = 0; i < this._unresolvedHosts; i++)
/*     */     {
/*     */ 
/*  61 */       makeThread(i);
/*     */     }
/*     */   }
/*     */   
/*     */   private void makeThread(int index) {
/*  66 */     Thread t = new Thread(this, Integer.toString(index));
/*  67 */     t.setDaemon(true);
/*  68 */     t.start();
/*     */   }
/*     */   
/*     */   public void run()
/*     */   {
/*  73 */     int index = Integer.parseInt(Thread.currentThread().getName());
/*     */     
/*     */ 
/*  76 */     if (index == -1) {
/*     */       try {
/*  78 */         Thread.sleep(this._timeout * 1000);
/*     */       }
/*     */       catch (InterruptedException localInterruptedException) {}
/*  81 */       synchronized (this) {
/*  82 */         if (!this._calledBack) {
/*  83 */           this._calledBack = true;
/*  84 */           if (this._error == 0)
/*  85 */             this._error = 106;
/*  86 */           this._serv.setSocket(null, new VarErrorException(this._error), null);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  93 */     if (index < this._unresolvedHosts) {
/*     */       try {
/*  95 */         String host = (String)this._hosts.elementAt(index);
/*  96 */         String[] addresses = DNSLookup.lookupAll(host);
/*  97 */         if (addresses.length > 1)
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 116 */           for (int i = 0; i < addresses.length; i++)
/*     */           {
/* 118 */             synchronized (this._hosts) {
/* 119 */               int newIndex = this._hosts.size();
/*     */               
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 125 */               this._hosts.addElement(addresses[i]);
/*     */             }
/*     */             
/*     */             int newIndex;
/*     */             
/* 130 */             makeThread(newIndex);
/*     */           }
/*     */           
/*     */         } else
/* 134 */           connectTo(addresses[0], false);
/*     */       } catch (UnknownHostException uhe) {
/* 136 */         synchronized (this) {
/* 137 */           if (this._error == 0) {
/* 138 */             this._error = 107;
/*     */ 
/*     */           }
/*     */           
/*     */ 
/*     */         }
/*     */         
/*     */ 
/*     */       }
/*     */       
/*     */ 
/*     */     }
/*     */     else
/*     */     {
/*     */ 
/* 153 */       connectTo((String)this._hosts.elementAt(index), false);
/*     */     }
/*     */   }
/*     */   
/*     */   public Vector<String> getBackupHosts()
/*     */   {
/* 159 */     this._resolvedHosts = new Vector();
/* 160 */     for (int i = this._unresolvedHosts; i < this._hosts.size(); i++) {
/* 161 */       this._resolvedHosts.addElement((String)this._hosts.elementAt(i));
/*     */     }
/* 163 */     return this._resolvedHosts;
/*     */   }
/*     */   
/*     */   private void connectTo(String host, boolean delay) {
/* 167 */     Socket sock = null;
/*     */     
/*     */     try
/*     */     {
/* 171 */       sock = new Socket(host, this._port);
/*     */       
/* 173 */       synchronized (this)
/*     */       {
/*     */ 
/*     */ 
/* 177 */         if (delay) {
/*     */           try {
/* 179 */             wait(this._timeout * 1000 * 2 / 3);
/*     */           }
/*     */           catch (InterruptedException localInterruptedException) {}
/*     */         }
/*     */         
/* 184 */         System.out.println("Connected to " + host);
/*     */         
/* 186 */         if (!this._calledBack) {
/* 187 */           this._calledBack = true;
/* 188 */           this._serv.setSocket(sock, null, host);
/*     */         } else {
/*     */           try {
/* 191 */             sock.close();
/*     */           }
/*     */           catch (IOException localIOException1) {}
/*     */         }
/* 195 */         notifyAll();
/*     */       }
/*     */     }
/*     */     catch (IOException e) {
/* 199 */       synchronized (this) {
/* 200 */         if (this._error == 0) {
/* 201 */           if (((e instanceof NoRouteToHostException)) || 
/* 202 */             ((e instanceof UnknownHostException))) {
/* 203 */             this._error = 107;
/*     */           } else
/* 205 */             this._error = 104;
/*     */         }
/* 207 */         notifyAll();
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\network\WSConnecting.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */