Mercurial > hg > Applications > TightVNC_orginal
annotate src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java @ 43:6687aa9dd55e
add flag for screen change in ReciverTask.java.
author | one |
---|---|
date | Tue, 06 Nov 2012 15:36:46 +0900 |
parents | fe38611c4b2b |
children | d4663dfd91ae |
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 |
3 | 23 public EchoClient(String name, int echoPort) { |
24 this.echoPort = echoPort; | |
25 this.name = name; | |
26 } | |
1 | 27 |
32 | 28 public void openport() { |
1 | 29 try { |
43 | 30 // if(echoSocket==null) |
42 | 31 echoSocket = new Socket(name, echoPort); |
32 echoSocket.setReuseAddress(true); | |
1 | 33 os = new DataOutputStream(echoSocket.getOutputStream()); |
34 is = new BufferedReader(new InputStreamReader( | |
35 echoSocket.getInputStream())); | |
36 } catch (UnknownHostException e) { | |
37 System.err.println("Don't know about host: localhost"); | |
38 } catch (IOException e) { | |
39 System.out.println(name + " Connection Faild"); | |
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 { | |
53 | |
54 InetAddress addr = InetAddress.getLocalHost(); | |
55 String add = new String(addr.getHostAddress()); | |
56 // add = getIpV6(); | |
57 | |
58 os.writeBytes(add + "\n"); | |
59 os.writeBytes(args + "\n"); | |
60 getProxyData(is); | |
61 | |
62 streamClose(); | |
63 } catch (UnknownHostException e) { | |
64 System.err.println("Trying to connect to unknown host: " + e); | |
65 } catch (IOException e) { | |
66 System.err.println("IOException: " + e); | |
67 } | |
43 | 68 waitReply = new WaitReply(treeNum, this); |
1 | 69 waitReply.start(); |
70 } | |
71 return this; | |
72 } | |
73 | |
74 /** | |
75 * Call at lost host | |
76 */ | |
35 | 77 public boolean lostHost() { |
1 | 78 if (echoSocket != null && os != null && is != null) { |
79 try { | |
80 if (runflag) { | |
81 return true; | |
82 } | |
83 sendDataProxy(); | |
84 String checkRepetition = getProxyData2(is); | |
85 if (checkRepetition.equals("stop")) { | |
86 return true; | |
87 } | |
88 if (!(waitReply.checkPath())) { | |
89 Thread.sleep(1000); | |
90 reConnectionMain(echoSocket); | |
91 streamClose(); | |
92 } | |
93 } catch (UnknownHostException e) { | |
94 System.err.println("Trying to connect to unknown host: " + e); | |
95 } catch (IOException e) { | |
96 return false; | |
97 } catch (InterruptedException e) { | |
98 e.printStackTrace(); | |
99 } catch (NullPointerException e) { | |
100 openport(); | |
101 System.out.println("notFoundParents"); | |
102 notfoundParent(); | |
103 } | |
104 } | |
105 return true; | |
106 } | |
107 | |
36 | 108 public boolean notfoundParent() { |
1 | 109 if (echoSocket != null && os != null && is != null) { |
110 runflag = true; | |
111 try { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
112 sendDataProxy("2", parentNum, null); |
1 | 113 getProxyData(is); |
114 reConnectionMain(echoSocket); | |
115 streamClose(); | |
116 } catch (UnknownHostException e) { | |
117 System.err.println("Trying to connect to unknown host: " + e); | |
118 } catch (IOException e) { | |
119 System.err.println("IOException: " + e); | |
120 } | |
121 } | |
122 return true; | |
123 } | |
124 | |
32 | 125 public EchoClient Interruption(Socket _clientSocket) { |
1 | 126 clientSocket = _clientSocket; |
32 | 127 BufferedReader lostis = null; |
1 | 128 |
129 try { | |
130 lostis = new BufferedReader(new InputStreamReader( | |
131 clientSocket.getInputStream())); | |
132 getProxyData(lostis); | |
32 | 133 clientSocket.close(); |
1 | 134 } catch (IOException e) { |
135 System.out.println(e); | |
136 } | |
137 return this; | |
138 } | |
139 | |
140 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
|
141 if ((parentAddress = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
142 System.out.println("Server: " + parentAddress); |
1 | 143 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
144 if ((parentNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
145 System.out.println("parent: " + parentNum); |
1 | 146 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
147 if ((treeNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
148 System.out.println("treenum: " + treeNum); |
1 | 149 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
150 if ((leaderFlag = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
151 System.out.println("leaderflag: " + leaderFlag); |
1 | 152 } |
153 } | |
154 | |
155 String getProxyData2(BufferedReader is) throws IOException { | |
156 String checkRepetition; | |
157 System.out.println("-------------------re----------------------------"); | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
158 if ((parentAddress = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
159 System.out.println("Server: " + parentAddress); |
1 | 160 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
161 if ((parentNum = is.readLine()) != null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
162 System.out.println("parent:test " + parentNum); |
1 | 163 } |
164 if ((checkRepetition = is.readLine()) != null) { | |
165 System.out.println("checkRepetition: " + checkRepetition); | |
166 } | |
167 return checkRepetition; | |
168 } | |
169 | |
170 void reConnectionMain(Socket echoSocket) { | |
43 | 171 while (true) { |
172 try { | |
173 client.close(); | |
174 client.setSocket(createSocketForClient(echoSocket)); | |
175 client.run(); | |
176 break; | |
177 } catch (IOException e) { | |
178 continue; | |
179 } | |
36 | 180 } |
1 | 181 } |
182 | |
183 void streamClose() throws IOException { | |
184 os.close(); | |
185 is.close(); | |
186 echoSocket.close(); | |
187 } | |
188 | |
189 void sendDataProxy() { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
190 if ("1".equals(leaderFlag)) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
191 sendDataProxy("1", parentNum, treeNum); |
1 | 192 System.out.println("---------------------------------------------"); |
193 } else { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
194 sendDataProxy("3", parentNum, treeNum); |
1 | 195 System.out.println("---------------------------------------------"); |
196 } | |
197 } | |
198 | |
199 void sendDataProxy(String type, String num, String treenum) { | |
200 try { | |
201 if (treenum != null) { | |
202 os.writeBytes(type + "\n"); | |
203 os.writeBytes(num + "\n"); | |
204 os.writeBytes(treenum + "\n"); | |
205 } else { | |
206 os.writeBytes(type + "\n"); | |
207 os.writeBytes(num + "\n"); | |
208 } | |
209 | |
210 } catch (UnknownHostException e) { | |
211 System.err.println("Trying to connect to unknown host: " + e); | |
212 } catch (IOException e) { | |
213 System.err.println("IOException: " + e); | |
214 } | |
215 } | |
43 | 216 |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
217 public void getParentName() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
218 if (clientSocket == null) { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
219 // echo = new EchoClient(pHost, this); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
220 openport(); |
42 | 221 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
|
222 } else { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
223 Interruption(clientSocket); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
224 } |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
225 } |
36 | 226 |
227 public void setViewer(Viewer v) { | |
228 client = v; | |
229 } | |
230 | |
231 public Socket createSocketForClient(Socket soc) throws IOException { | |
232 BufferedReader is = new BufferedReader(new InputStreamReader( | |
233 soc.getInputStream())); | |
38 | 234 String tempParentAddress = is.readLine(); |
235 parentNum = is.readLine(); | |
43 | 236 if ("reconnection".equals(tempParentAddress)) { |
42 | 237 System.out.println("-----------------into function \n" + parentNum); |
43 | 238 return new Socket(parentAddress, Integer.parseInt(parentNum)); |
239 } | |
38 | 240 treeNum = is.readLine(); |
241 leaderFlag = is.readLine(); | |
43 | 242 return new Socket(tempParentAddress, 5999); |
36 | 243 } |
1 | 244 } |