0
|
1 package treeVnc;
|
|
2
|
|
3 import java.io.*;
|
|
4 import java.net.*;
|
|
5
|
|
6 //import myVncProxy.MulticastQueue.Client;
|
|
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;
|
|
16 // MyVncClient client;
|
|
17 private InterfaceForViewer client;
|
|
18 private int echoPort = 9999;
|
|
19 // private IpV6 ipV6;
|
|
20 // private MyVncClient checkMove;
|
|
21 // private VncViewer vncV;
|
|
22 String responseLine;
|
|
23 String parent;// 親の番号
|
|
24 String treenum;// 自分の番号
|
|
25 String leaderflag;// リーダフラグ
|
|
26
|
|
27 // boolean passflag;
|
|
28
|
|
29 // WaitReplyに自分自身を渡している
|
|
30 public EchoClient() {
|
|
31 }
|
|
32
|
|
33 public EchoClient(EchoClient echo) {
|
|
34 this.name = echo.name;
|
|
35 this.leaderflag = echo.leaderflag;
|
|
36 this.parent = echo.parent;
|
|
37 this.treenum = echo.treenum;
|
|
38 this.client = echo.client;
|
|
39 this.waitReply = echo.waitReply;
|
|
40 // checkMove = (MyVncClient)echo.client;
|
|
41 }
|
|
42
|
|
43 // VncViewerから引数をもらってきてproxy役を認識する
|
|
44 public EchoClient(String name, MyVncClient client) {
|
|
45 this.client = client;
|
|
46 this.name = name;
|
|
47 }
|
|
48
|
|
49 public EchoClient(String name, MyVncClient client, int echoPort) {
|
|
50 this.client = client;
|
|
51 this.name = name;
|
|
52 this.echoPort = echoPort;
|
|
53 }
|
|
54
|
|
55 public EchoClient(EchoClient echo, MyVncClient client) {
|
|
56 this.client = client;
|
|
57 this.name = echo.name;
|
|
58 leaderflag = echo.leaderflag;
|
|
59 parent = echo.parent;
|
|
60 treenum = echo.treenum;
|
|
61 waitReply = echo.waitReply;
|
|
62 }
|
|
63
|
|
64 public EchoClient(String name, CuiMyVncClient client) {
|
|
65 this.client = client;
|
|
66 this.name = name;
|
|
67 }
|
|
68
|
|
69 public EchoClient(String name, CuiMyVncClient client, int echoPort) {
|
|
70 this.client = client;
|
|
71 this.name = name;
|
|
72 this.echoPort = echoPort;
|
|
73 }
|
|
74
|
|
75 public EchoClient(EchoClient echo, CuiMyVncClient client) {
|
|
76 this.client = client;
|
|
77 this.name = echo.name;
|
|
78 leaderflag = echo.leaderflag;
|
|
79 parent = echo.parent;
|
|
80 treenum = echo.treenum;
|
|
81 }
|
|
82
|
|
83 // void hostn(String args){
|
|
84 void openport() {
|
|
85 try {
|
|
86 if (name != null) {
|
|
87 echoSocket = new Socket(name, echoPort);
|
|
88 } else {
|
|
89 echoSocket = new Socket("133.13.48.18", echoPort);
|
|
90 }
|
|
91 os = new DataOutputStream(echoSocket.getOutputStream());
|
|
92 is = new BufferedReader(new InputStreamReader(
|
|
93 echoSocket.getInputStream()));
|
|
94 } catch (UnknownHostException e) {
|
|
95 System.err.println("Don't know about host: localhost");
|
|
96 } catch (IOException e) {
|
|
97 System.out.println(name + " Connection Faild");
|
|
98 System.exit(0);
|
|
99 }
|
|
100
|
|
101 }
|
|
102
|
|
103 /**
|
|
104 * @param args
|
|
105 * select connect port
|
|
106 * @return
|
|
107 */
|
|
108 EchoClient requestHostName(String args) {
|
|
109 // サーバーにメッセージを送る
|
|
110 if (echoSocket != null && os != null && is != null) {
|
|
111 try {
|
|
112
|
|
113 InetAddress addr = InetAddress.getLocalHost();
|
|
114 String add = new String(addr.getHostAddress());
|
|
115 // add = getIpV6();
|
|
116
|
|
117 os.writeBytes(add + "\n");
|
|
118 os.writeBytes(args + "\n");
|
|
119 getProxyData(is);
|
|
120
|
|
121 streamClose();
|
|
122 } catch (UnknownHostException e) {
|
|
123 System.err.println("Trying to connect to unknown host: " + e);
|
|
124 } catch (IOException e) {
|
|
125 System.err.println("IOException: " + e);
|
|
126 }
|
|
127 waitReply = new WaitReply(treenum, client);
|
|
128 waitReply.start();
|
|
129 }
|
|
130 return this;
|
|
131 }
|
|
132
|
|
133 /**
|
|
134 * Call at lost host
|
|
135 */
|
|
136 boolean lostHost() {
|
|
137 if (echoSocket != null && os != null && is != null) {
|
|
138 try {
|
|
139 if (runflag) {
|
|
140 return true;
|
|
141 }
|
|
142 sendDataProxy();
|
|
143 String checkRepetition = getProxyData2(is);
|
|
144 if (checkRepetition.equals("stop")) {
|
|
145 return true;
|
|
146 }
|
|
147 // if(!(checkRepetition.equals("skip")) ||
|
|
148 // "1".equals(leaderflag)) {
|
|
149 if (!(waitReply.checkPath())) {
|
|
150 Thread.sleep(1000);
|
|
151 reConnectionMain(echoSocket);
|
|
152 streamClose();
|
|
153 }
|
|
154 // Thread.sleep(1000);
|
|
155 /*
|
|
156 * if(!(checkMove.vncFrame.isShowing())&&"skip".equals(
|
|
157 * checkRepetition)) { openport(); notfoundParent(); }
|
|
158 */
|
|
159 } catch (UnknownHostException e) {
|
|
160 System.err.println("Trying to connect to unknown host: " + e);
|
|
161 } catch (IOException e) {
|
|
162 return false;
|
|
163 } catch (InterruptedException e) {
|
|
164 e.printStackTrace();
|
|
165
|
|
166 } catch (NullPointerException e) {
|
|
167 openport();
|
|
168 System.out.println("notFoundParents");
|
2
|
169 notFoundParent();
|
0
|
170 }
|
|
171 }
|
|
172 return true;
|
|
173 }
|
|
174
|
2
|
175 boolean notFoundParent() {
|
0
|
176 if (echoSocket != null && os != null && is != null) {
|
|
177 runflag = true;
|
|
178 try {
|
|
179 sendDataProxy("2", parent, null);
|
|
180 getProxyData(is);
|
|
181 reConnectionMain(echoSocket);
|
|
182 streamClose();
|
|
183 } catch (UnknownHostException e) {
|
|
184 System.err.println("Trying to connect to unknown host: " + e);
|
|
185 } catch (IOException e) {
|
|
186 System.err.println("IOException: " + e);
|
|
187 }
|
|
188 }
|
|
189 return true;
|
|
190 }
|
|
191
|
|
192 EchoClient Interruption(Socket _clientSocket) {
|
|
193 clientSocket = _clientSocket;
|
|
194 BufferedReader lostis = null;// あとで修正する
|
|
195
|
|
196 try {
|
|
197 lostis = new BufferedReader(new InputStreamReader(
|
|
198 clientSocket.getInputStream()));
|
|
199 getProxyData(lostis);
|
|
200 clientSocket.close();// WaitReplyのacceptを終了させる
|
|
201 } catch (IOException e) {
|
|
202 System.out.println(e);
|
|
203 }
|
|
204 return this;
|
|
205 }
|
|
206
|
|
207 void getProxyData(BufferedReader is) throws IOException {
|
|
208 if ((responseLine = is.readLine()) != null) {
|
|
209 System.out.println("Server: " + responseLine);
|
|
210 }
|
|
211 if ((parent = is.readLine()) != null) {
|
|
212 System.out.println("parent: " + parent);
|
|
213 }
|
|
214 if ((treenum = is.readLine()) != null) {
|
|
215 System.out.println("treenum: " + treenum);
|
|
216 }
|
|
217 if ((leaderflag = is.readLine()) != null) {
|
|
218 System.out.println("leaderflag: " + leaderflag);
|
|
219 }
|
|
220 }
|
|
221
|
|
222 String getProxyData2(BufferedReader is) throws IOException {
|
|
223 String checkRepetition;
|
|
224 System.out.println("-------------------re----------------------------");
|
|
225 if ((responseLine = is.readLine()) != null) {
|
|
226 System.out.println("Server: " + responseLine);
|
|
227 }
|
|
228 if ((parent = is.readLine()) != null) {
|
|
229 System.out.println("parent:test " + parent);
|
|
230 }
|
|
231 if ((checkRepetition = is.readLine()) != null) {
|
|
232 System.out.println("checkRepetition: " + checkRepetition);
|
|
233 }
|
|
234 return checkRepetition;
|
|
235 }
|
|
236
|
|
237 void reConnectionMain(Socket echoSocket) {
|
|
238 client.close();
|
|
239 client.setClientSocket(echoSocket);
|
|
240 client.init();
|
|
241 client.setEchoValue(this);
|
|
242 client.getParentName();
|
|
243 client.start_threads();
|
|
244 client.start();
|
|
245 }
|
|
246
|
|
247 void streamClose() throws IOException {
|
|
248 os.close();
|
|
249 is.close();
|
|
250 echoSocket.close();
|
|
251 }
|
|
252
|
|
253 void sendDataProxy() {
|
|
254 if ("1".equals(leaderflag)) {
|
|
255 sendDataProxy("1", parent, treenum);
|
|
256 System.out.println("---------------------------------------------");
|
|
257 } else {
|
|
258 sendDataProxy("3", parent, treenum);
|
|
259 System.out.println("---------------------------------------------");
|
|
260 }
|
|
261 }
|
|
262
|
|
263 void sendDataProxy(String type, String num, String treenum) {
|
|
264 try {
|
|
265 if (treenum != null) {
|
|
266 os.writeBytes(type + "\n");
|
|
267 os.writeBytes(num + "\n");
|
|
268 os.writeBytes(treenum + "\n");
|
|
269 } else {
|
|
270 os.writeBytes(type + "\n");
|
|
271 os.writeBytes(num + "\n");
|
|
272 }
|
|
273
|
|
274 } catch (UnknownHostException e) {
|
|
275 System.err.println("Trying to connect to unknown host: " + e);
|
|
276 } catch (IOException e) {
|
|
277 System.err.println("IOException: " + e);
|
|
278 }
|
|
279 }
|
|
280 /*
|
|
281 * String getIpV6() { ipV6 = new IpV6(); ipV6.getInterface(); return
|
|
282 * ipV6.getV6(); }
|
|
283 */
|
|
284 } |