Mercurial > hg > Applications > TightVNC_orginal
annotate src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java @ 38:c2f0b6907448
add doc directory
author | one |
---|---|
date | Thu, 27 Sep 2012 19:32:25 +0900 |
parents | b7d4d0349f99 |
children | fe38611c4b2b |
rev | line source |
---|---|
12 | 1 package jp.ac.u_ryukyu.treevnc.client; |
1 | 2 |
3 import java.io.*; | |
4 import java.net.*; | |
12 | 5 |
36 | 6 import com.glavsoft.viewer.Viewer; |
1 | 7 |
8 public class EchoClient { | |
9 private String name; | |
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; | |
36 | 16 Viewer client; |
1 | 17 private int echoPort = 9999; |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
18 public String parentAddress; |
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; |
1 | 22 |
23 | |
24 public EchoClient() { | |
25 } | |
26 | |
27 public EchoClient(EchoClient echo) { | |
28 this.name = echo.name; | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
29 this.leaderFlag = echo.leaderFlag; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
30 this.parentNum = echo.parentNum; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
31 this.treeNum = echo.treeNum; |
1 | 32 this.client = echo.client; |
33 this.waitReply = echo.waitReply; | |
34 // checkMove = (MyVncClient)echo.client; | |
35 } | |
36 | |
37 public EchoClient(String name, MyVncClient client) { | |
35 | 38 this.client = client; |
1 | 39 this.name = name; |
40 } | |
41 | |
3 | 42 public EchoClient(String name, int echoPort) { |
43 this.echoPort = echoPort; | |
44 this.name = name; | |
45 } | |
46 | |
1 | 47 public EchoClient(String name, MyVncClient client, int echoPort) { |
35 | 48 this.client = client; |
1 | 49 this.name = name; |
50 this.echoPort = echoPort; | |
51 } | |
52 | |
53 public EchoClient(EchoClient echo, MyVncClient client) { | |
35 | 54 this.client = client; |
1 | 55 this.name = echo.name; |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
56 leaderFlag = echo.leaderFlag; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
57 parentNum = echo.parentNum; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
58 treeNum = echo.treeNum; |
1 | 59 waitReply = echo.waitReply; |
60 } | |
61 | |
32 | 62 public void openport() { |
1 | 63 try { |
64 if (name != null) { | |
65 echoSocket = new Socket(name, echoPort); | |
66 } else { | |
67 echoSocket = new Socket("133.13.48.18", echoPort); | |
68 } | |
69 os = new DataOutputStream(echoSocket.getOutputStream()); | |
70 is = new BufferedReader(new InputStreamReader( | |
71 echoSocket.getInputStream())); | |
72 } catch (UnknownHostException e) { | |
73 System.err.println("Don't know about host: localhost"); | |
74 } catch (IOException e) { | |
75 System.out.println(name + " Connection Faild"); | |
76 System.exit(0); | |
77 } | |
78 | |
79 } | |
80 | |
81 /** | |
82 * @param args | |
83 * select connect port | |
84 * @return | |
85 */ | |
32 | 86 public EchoClient requestHostName(String args) { |
1 | 87 if (echoSocket != null && os != null && is != null) { |
88 try { | |
89 | |
90 InetAddress addr = InetAddress.getLocalHost(); | |
91 String add = new String(addr.getHostAddress()); | |
92 // add = getIpV6(); | |
93 | |
94 os.writeBytes(add + "\n"); | |
95 os.writeBytes(args + "\n"); | |
96 getProxyData(is); | |
97 | |
98 streamClose(); | |
99 } catch (UnknownHostException e) { | |
100 System.err.println("Trying to connect to unknown host: " + e); | |
101 } catch (IOException e) { | |
102 System.err.println("IOException: " + e); | |
103 } | |
36 | 104 waitReply = new WaitReply(treeNum,this); |
1 | 105 waitReply.start(); |
106 } | |
107 return this; | |
108 } | |
109 | |
110 /** | |
111 * Call at lost host | |
112 */ | |
35 | 113 public boolean lostHost() { |
1 | 114 if (echoSocket != null && os != null && is != null) { |
115 try { | |
116 if (runflag) { | |
117 return true; | |
118 } | |
119 sendDataProxy(); | |
120 String checkRepetition = getProxyData2(is); | |
121 if (checkRepetition.equals("stop")) { | |
122 return true; | |
123 } | |
124 if (!(waitReply.checkPath())) { | |
125 Thread.sleep(1000); | |
126 reConnectionMain(echoSocket); | |
127 streamClose(); | |
128 } | |
129 } catch (UnknownHostException e) { | |
130 System.err.println("Trying to connect to unknown host: " + e); | |
131 } catch (IOException e) { | |
132 return false; | |
133 } catch (InterruptedException e) { | |
134 e.printStackTrace(); | |
135 } catch (NullPointerException e) { | |
136 openport(); | |
137 System.out.println("notFoundParents"); | |
138 notfoundParent(); | |
139 } | |
140 } | |
141 return true; | |
142 } | |
143 | |
36 | 144 public boolean notfoundParent() { |
1 | 145 if (echoSocket != null && os != null && is != null) { |
146 runflag = true; | |
147 try { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
148 sendDataProxy("2", parentNum, null); |
1 | 149 getProxyData(is); |
150 reConnectionMain(echoSocket); | |
151 streamClose(); | |
152 } catch (UnknownHostException e) { | |
153 System.err.println("Trying to connect to unknown host: " + e); | |
154 } catch (IOException e) { | |
155 System.err.println("IOException: " + e); | |
156 } | |
157 } | |
158 return true; | |
159 } | |
160 | |
32 | 161 public EchoClient Interruption(Socket _clientSocket) { |
1 | 162 clientSocket = _clientSocket; |
32 | 163 BufferedReader lostis = null; |
1 | 164 |
165 try { | |
166 lostis = new BufferedReader(new InputStreamReader( | |
167 clientSocket.getInputStream())); | |
168 getProxyData(lostis); | |
32 | 169 clientSocket.close(); |
1 | 170 } catch (IOException e) { |
171 System.out.println(e); | |
172 } | |
173 return this; | |
174 } | |
175 | |
176 void getProxyData(BufferedReader is) throws IOException { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
177 if ((parentAddress = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
178 System.out.println("Server: " + parentAddress); |
1 | 179 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
180 if ((parentNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
181 System.out.println("parent: " + parentNum); |
1 | 182 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
183 if ((treeNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
184 System.out.println("treenum: " + treeNum); |
1 | 185 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
186 if ((leaderFlag = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
187 System.out.println("leaderflag: " + leaderFlag); |
1 | 188 } |
189 } | |
190 | |
191 String getProxyData2(BufferedReader is) throws IOException { | |
192 String checkRepetition; | |
193 System.out.println("-------------------re----------------------------"); | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
194 if ((parentAddress = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
195 System.out.println("Server: " + parentAddress); |
1 | 196 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
197 if ((parentNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
198 System.out.println("parent:test " + parentNum); |
1 | 199 } |
200 if ((checkRepetition = is.readLine()) != null) { | |
201 System.out.println("checkRepetition: " + checkRepetition); | |
202 } | |
203 return checkRepetition; | |
204 } | |
205 | |
206 void reConnectionMain(Socket echoSocket) { | |
36 | 207 try { |
208 client.close(); | |
209 client.setSocket(createSocketForClient(echoSocket)); | |
210 Thread.sleep(1000); | |
211 client.run(); | |
212 } catch (IOException e) { | |
213 e.printStackTrace(); | |
214 } catch (InterruptedException e) { | |
215 e.printStackTrace(); | |
216 } | |
1 | 217 } |
218 | |
219 void streamClose() throws IOException { | |
220 os.close(); | |
221 is.close(); | |
222 echoSocket.close(); | |
223 } | |
224 | |
225 void sendDataProxy() { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
226 if ("1".equals(leaderFlag)) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
227 sendDataProxy("1", parentNum, treeNum); |
1 | 228 System.out.println("---------------------------------------------"); |
229 } else { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
230 sendDataProxy("3", parentNum, treeNum); |
1 | 231 System.out.println("---------------------------------------------"); |
232 } | |
233 } | |
234 | |
235 void sendDataProxy(String type, String num, String treenum) { | |
236 try { | |
237 if (treenum != null) { | |
238 os.writeBytes(type + "\n"); | |
239 os.writeBytes(num + "\n"); | |
240 os.writeBytes(treenum + "\n"); | |
241 } else { | |
242 os.writeBytes(type + "\n"); | |
243 os.writeBytes(num + "\n"); | |
244 } | |
245 | |
246 } catch (UnknownHostException e) { | |
247 System.err.println("Trying to connect to unknown host: " + e); | |
248 } catch (IOException e) { | |
249 System.err.println("IOException: " + e); | |
250 } | |
251 } | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
252 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
253 public void getParentName() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
254 if (clientSocket == null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
255 // echo = new EchoClient(pHost, this); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
256 openport(); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
257 requestHostName("1"); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
258 } else { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
259 Interruption(clientSocket); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
260 } |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
261 } |
36 | 262 |
263 public void setViewer(Viewer v) { | |
264 client = v; | |
265 } | |
266 | |
267 public Socket createSocketForClient(Socket soc) throws IOException { | |
268 BufferedReader is = new BufferedReader(new InputStreamReader( | |
269 soc.getInputStream())); | |
38 | 270 String tempParentAddress = is.readLine(); |
271 parentNum = is.readLine(); | |
272 if("reconnection".equals(tempParentAddress)) { | |
273 System.out.println("into function \n" + parentNum); | |
274 return new Socket(parentAddress,Integer.parseInt(parentNum)); | |
275 } | |
276 treeNum = is.readLine(); | |
277 leaderFlag = is.readLine(); | |
278 return new Socket(tempParentAddress,5999); | |
36 | 279 } |
1 | 280 } |