Mercurial > hg > trac > TreeVNC > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java @ 80:2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 30 Apr 2014 13:43:10 +0900 |
parents | cdf7bbe45885 |
children | ca39d762805e |
rev | line source |
---|---|
12 | 1 package jp.ac.u_ryukyu.treevnc.client; |
1 | 2 |
3 import java.io.*; | |
4 import java.net.*; | |
12 | 5 |
65 | 6 import com.glavsoft.viewer.ViewerImpl; |
1 | 7 |
8 public class EchoClient { | |
45 | 9 private String proxyName; |
1 | 10 private BufferedReader is = null; |
11 private DataOutputStream os = null; | |
12 private Socket echoSocket = null; | |
13 private boolean runflag = false; | |
14 private WaitReply waitReply; | |
15 private Socket clientSocket = null; | |
45 | 16 private int echoPort = 9999; |
65 | 17 public ViewerImpl client; |
48 | 18 private String parentAddress; |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
19 public String parentNum; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
20 public String treeNum; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
21 public String leaderFlag; |
45 | 22 private String myAddress; |
1 | 23 |
3 | 24 public EchoClient(String name, int echoPort) { |
25 this.echoPort = echoPort; | |
45 | 26 this.proxyName = name; |
3 | 27 } |
1 | 28 |
32 | 29 public void openport() { |
1 | 30 try { |
45 | 31 echoSocket = new Socket(proxyName, echoPort); |
42 | 32 echoSocket.setReuseAddress(true); |
1 | 33 os = new DataOutputStream(echoSocket.getOutputStream()); |
34 is = new BufferedReader(new InputStreamReader( | |
35 echoSocket.getInputStream())); | |
36 } catch (UnknownHostException e) { | |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
37 System.err.println("Don't know about host: "+proxyName); |
1 | 38 } catch (IOException e) { |
45 | 39 System.out.println(proxyName + " Connection Faild"); |
1 | 40 System.exit(0); |
41 } | |
42 | |
43 } | |
44 | |
45 /** | |
46 * @param args | |
47 * select connect port | |
48 * @return | |
49 */ | |
32 | 50 public EchoClient requestHostName(String args) { |
1 | 51 if (echoSocket != null && os != null && is != null) { |
52 try { | |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
53 InetAddress addr = echoSocket.getLocalAddress(); |
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
54 myAddress = addr.getHostAddress(); |
45 | 55 os.writeBytes(myAddress + "\n"); |
1 | 56 os.writeBytes(args + "\n"); |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
57 getProxyData(is, echoSocket); |
1 | 58 |
59 streamClose(); | |
60 } catch (UnknownHostException e) { | |
61 System.err.println("Trying to connect to unknown host: " + e); | |
62 } catch (IOException e) { | |
63 System.err.println("IOException: " + e); | |
64 } | |
43 | 65 waitReply = new WaitReply(treeNum, this); |
1 | 66 waitReply.start(); |
67 } | |
68 return this; | |
69 } | |
70 | |
71 /** | |
72 * Call at lost host | |
73 */ | |
35 | 74 public boolean lostHost() { |
1 | 75 if (echoSocket != null && os != null && is != null) { |
76 try { | |
77 if (runflag) { | |
78 return true; | |
79 } | |
80 sendDataProxy(); | |
54 | 81 reConnectionMain(echoSocket); |
82 streamClose(); | |
1 | 83 } catch (UnknownHostException e) { |
84 System.err.println("Trying to connect to unknown host: " + e); | |
85 } catch (IOException e) { | |
86 return false; | |
87 } catch (NullPointerException e) { | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
88 //openport(); |
1 | 89 System.out.println("notFoundParents"); |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
90 //notfoundParent(); |
1 | 91 } |
92 } | |
93 return true; | |
94 } | |
95 | |
36 | 96 public boolean notfoundParent() { |
1 | 97 if (echoSocket != null && os != null && is != null) { |
98 runflag = true; | |
99 try { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
100 sendDataProxy("2", parentNum, null); |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
101 getProxyData(is, echoSocket); |
1 | 102 reConnectionMain(echoSocket); |
103 streamClose(); | |
104 } catch (UnknownHostException e) { | |
105 System.err.println("Trying to connect to unknown host: " + e); | |
106 } catch (IOException e) { | |
107 System.err.println("IOException: " + e); | |
108 } | |
109 } | |
110 return true; | |
111 } | |
112 | |
32 | 113 public EchoClient Interruption(Socket _clientSocket) { |
1 | 114 clientSocket = _clientSocket; |
115 try { | |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
116 BufferedReader lostis = new BufferedReader(new InputStreamReader( |
1 | 117 clientSocket.getInputStream())); |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
118 getProxyData(lostis, clientSocket); |
32 | 119 clientSocket.close(); |
1 | 120 } catch (IOException e) { |
121 System.out.println(e); | |
122 } | |
123 return this; | |
124 } | |
125 | |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
126 void getProxyData(BufferedReader is, Socket soc) throws IOException { |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
127 if ((parentAddress = is.readLine()) != null) { |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
128 System.out.println("Server received: " + parentAddress); |
1 | 129 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
130 if ((parentNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
131 System.out.println("parent: " + parentNum); |
1 | 132 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
133 if ((treeNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
134 System.out.println("treenum: " + treeNum); |
1 | 135 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
136 if ((leaderFlag = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
137 System.out.println("leaderflag: " + leaderFlag); |
1 | 138 } |
80
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
139 InetAddress parent = soc.getInetAddress(); |
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
140 parentAddress = parent.getHostName(); |
2951dd85e9fc
connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
77
diff
changeset
|
141 System.out.println("Actual Server: " + parentAddress); |
1 | 142 } |
143 | |
144 void reConnectionMain(Socket echoSocket) { | |
43 | 145 while (true) { |
146 try { | |
59 | 147 client.closeApp(); |
52 | 148 // set Socket for connection in VncViewer. |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
149 Socket soc = createSocketForClient(echoSocket, false); |
54 | 150 client.setSocket(soc); |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
151 if (soc != null) |
54 | 152 client.run(); |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
153 |
43 | 154 break; |
155 } catch (IOException e) { | |
54 | 156 break; |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
157 // continue; |
43 | 158 } |
36 | 159 } |
1 | 160 } |
161 | |
162 void streamClose() throws IOException { | |
163 os.close(); | |
164 is.close(); | |
165 echoSocket.close(); | |
166 } | |
167 | |
168 void sendDataProxy() { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
169 if ("1".equals(leaderFlag)) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
170 sendDataProxy("1", parentNum, treeNum); |
1 | 171 System.out.println("---------------------------------------------"); |
172 } else { | |
52 | 173 // sendDataProxy("3", parentNum, treeNum); |
174 // System.out.println("---------------------------------------------"); | |
1 | 175 } |
176 } | |
177 | |
178 void sendDataProxy(String type, String num, String treenum) { | |
179 try { | |
180 if (treenum != null) { | |
181 os.writeBytes(type + "\n"); | |
182 os.writeBytes(num + "\n"); | |
183 os.writeBytes(treenum + "\n"); | |
184 } else { | |
185 os.writeBytes(type + "\n"); | |
186 os.writeBytes(num + "\n"); | |
187 } | |
188 } catch (UnknownHostException e) { | |
189 System.err.println("Trying to connect to unknown host: " + e); | |
190 } catch (IOException e) { | |
191 System.err.println("IOException: " + e); | |
192 } | |
193 } | |
43 | 194 |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
195 public void getParentName() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
196 if (clientSocket == null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
197 openport(); |
42 | 198 requestHostName("1"); // 1 is normal connection type. |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
199 } else { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
200 Interruption(clientSocket); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
201 } |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
202 } |
36 | 203 |
65 | 204 public void setViewer(ViewerImpl v) { |
36 | 205 client = v; |
206 } | |
65 | 207 |
208 public ViewerImpl getViewer() { | |
209 return client; | |
210 } | |
52 | 211 |
45 | 212 public String getMyAddress() { |
213 return myAddress; | |
214 } | |
36 | 215 |
53 | 216 // create socket for ReConnection. |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
217 public Socket createSocketForClient(Socket soc, boolean flag) throws IOException { |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
218 Socket socket = null; |
52 | 219 String parentAddress; |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
220 int count = 0; |
54 | 221 System.out.println("########################PATH************************"); |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
222 BufferedReader is = new BufferedReader(new InputStreamReader(soc.getInputStream())); |
53 | 223 parentAddress = is.readLine(); |
56 | 224 String port = is.readLine(); |
54 | 225 // It is called when the screen changes. |
53 | 226 if ("reconnection".equals(parentAddress)) { |
227 while (true) { | |
228 try { | |
229 client.setOpenPort(Integer.parseInt(port)); | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
230 System.out.println("----------:"+this.parentAddress+"----------:"+port); |
53 | 231 return new Socket(this.parentAddress, |
232 Integer.parseInt(port)); | |
233 } catch (IOException e) { | |
234 try { | |
235 Thread.sleep(1000); | |
236 } catch (InterruptedException e1) { | |
237 e1.printStackTrace(); | |
238 } | |
239 if (count++ > 5) | |
240 break; | |
241 continue; | |
242 } | |
243 } | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
244 } else { |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
245 System.out.println("###########################faild" + parentAddress+"port::"+port); |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
246 if (parentAddress == null) |
54 | 247 return null; |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
248 is.readLine();// parentNum |
52 | 249 } |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
250 socket = new Socket(parentAddress, Integer.parseInt(port)); |
52 | 251 socket.setReuseAddress(true); |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
252 System.out.println("Return Soket"); |
52 | 253 return socket; |
36 | 254 } |
52 | 255 |
48 | 256 public String getParentsAddress() { |
257 return parentAddress; | |
258 } | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
259 |
56 | 260 public void reportConnectionFinished(Socket soc) throws IOException { |
261 DataOutputStream os = new DataOutputStream(soc.getOutputStream()); | |
262 os.writeBytes("finished"); | |
263 } | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
264 |
1 | 265 } |