comparison src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java @ 12:12c3a73be47f

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