comparison src/myVncClient/EchoClient.java @ 100:961a5915834f

merge
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Mon, 24 Oct 2011 23:31:34 +0900
parents 405ead268439
children 2f2f8da71d60
comparison
equal deleted inserted replaced
95:53236c2c1229 100:961a5915834f
15 private WaitReply waitReply; 15 private WaitReply waitReply;
16 private Socket clientSocket = null; 16 private Socket clientSocket = null;
17 // MyVncClient client; 17 // MyVncClient client;
18 private InterfaceForViewer client; 18 private InterfaceForViewer client;
19 private int echoPort = 9999; 19 private int echoPort = 9999;
20 MyVncClient checkMove; 20 private IpV6 ipV6;
21 VncViewer vncV; 21 private MyVncClient checkMove;
22 private VncViewer vncV;
22 String responseLine; 23 String responseLine;
23 String parent;// 親の番号 24 String parent;// 親の番号
24 String treenum;// 自分の番号 25 String treenum;// 自分の番号
25 String leaderflag;// リーダフラグ 26 String leaderflag;// リーダフラグ
26 boolean passflag; 27 //boolean passflag;
27 28
28 29
29 // WaitReplyに自分自身を渡している 30 // WaitReplyに自分自身を渡している
30 public EchoClient() { 31 public EchoClient() {
31 } 32 }
32 33
33 34
34 public EchoClient(EchoClient echo) { 35 public EchoClient(EchoClient echo) {
35 this.name = echo.name; 36 this.name = echo.name;
36 leaderflag= echo.leaderflag; 37 this.leaderflag= echo.leaderflag;
37 parent = echo.parent; 38 this.parent = echo.parent;
38 treenum = echo.treenum; 39 this.treenum = echo.treenum;
39 client = echo.client; 40 this.client = echo.client;
41 this.waitReply = echo.waitReply;
40 checkMove = (MyVncClient)echo.client; 42 checkMove = (MyVncClient)echo.client;
41 } 43 }
42 // VncViewerから引数をもらってきてproxy役を認識する 44 // VncViewerから引数をもらってきてproxy役を認識する
43 public EchoClient(String name,MyVncClient client) { 45 public EchoClient(String name,MyVncClient client) {
44 this.client = client; 46 this.client = client;
81 treenum = echo.treenum; 83 treenum = echo.treenum;
82 } 84 }
83 85
84 // void hostn(String args){ 86 // void hostn(String args){
85 void openport() { 87 void openport() {
86 //  ソケットや入出力用のストリームの宣言
87 /*
88 Random rnd = new Random();
89 long ran = rnd.nextInt(1000) + 1000;
90 */
91 // ポート9999番に接続
92 try { 88 try {
93 if (name != null) { 89 if (name != null) {
94 echoSocket = new Socket(name, echoPort); 90 echoSocket = new Socket(name, echoPort);
95 } else { 91 } else {
96 echoSocket = new Socket("133.13.48.18", echoPort); 92 echoSocket = new Socket("133.13.48.18", echoPort);
98 os = new DataOutputStream(echoSocket.getOutputStream()); 94 os = new DataOutputStream(echoSocket.getOutputStream());
99 is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); 95 is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
100 } catch (UnknownHostException e) { 96 } catch (UnknownHostException e) {
101 System.err.println("Don't know about host: localhost"); 97 System.err.println("Don't know about host: localhost");
102 } catch (IOException e) { 98 } catch (IOException e) {
103 System.out.println(name + " への接続に失敗しました"); 99 System.out.println(name + " Connection Faild");
104 System.exit(0); 100 System.exit(0);
105 } 101 }
106 } 102 }
107 103
108 /** 104 /**
112 */ 108 */
113 EchoClient requestHostName(String args) { 109 EchoClient requestHostName(String args) {
114 // サーバーにメッセージを送る 110 // サーバーにメッセージを送る
115 if (echoSocket != null && os != null && is != null) { 111 if (echoSocket != null && os != null && is != null) {
116 try { 112 try {
117 // ip情報を取得する 113
118 InetAddress addr = InetAddress.getLocalHost(); 114 InetAddress addr = InetAddress.getLocalHost();
119 // System.out.println(addr.getHostAddress());
120 String add = new String(addr.getHostAddress()); 115 String add = new String(addr.getHostAddress());
121 116 //add = getIpV6();
122 // メッセージを送ります 117
123 os.writeBytes(add + "\n"); 118 os.writeBytes(add + "\n");
124 os.writeBytes(args + "\n"); 119 os.writeBytes(args + "\n");
125 120 getProxyData(is);
126 if ((responseLine = is.readLine()) != null) { 121
127 System.out.println("Server: " + responseLine); 122 streamClose();
128 }
129 if ((parent = is.readLine()) != null) {
130 System.out.println("parent: " + parent);
131 }
132 if ((treenum = is.readLine()) != null) {
133 System.out.println("treenum: " + treenum);
134 }
135 if ((leaderflag = is.readLine()) != null) {
136 System.out.println("leaderflag: " + leaderflag);
137 }
138
139 // 開いたソケットなどをクローズ
140 os.close();
141 is.close();
142 echoSocket.close();
143 } catch (UnknownHostException e) { 123 } catch (UnknownHostException e) {
144 System.err.println("Trying to connect to unknown host: " + e); 124 System.err.println("Trying to connect to unknown host: " + e);
145 } catch (IOException e) { 125 } catch (IOException e) {
146 System.err.println("IOException: " + e); 126 System.err.println("IOException: " + e);
147 127 }
148 }
149
150 waitReply = new WaitReply(treenum,client); 128 waitReply = new WaitReply(treenum,client);
151 waitReply.start(); 129 waitReply.start();
152
153 } 130 }
154 return this; 131 return this;
155 } 132 }
156 133
157 134
158 /** 135 /**
159 * Call at lost host 136 * Call at lost host
160 */ 137 */
161 boolean losthost() { 138 boolean losthost() {
162 String checkRepetition;
163 if (echoSocket != null && os != null && is != null) { 139 if (echoSocket != null && os != null && is != null) {
164 try { 140 try {
165 if(runflag){ 141 if(runflag){
166 return true; 142 return true;
167 } 143 }
168 // echoSocketの情報を呼び出す 144 sendDataProxy();
169 if ("1".equals(leaderflag)) { 145 String checkRepetition = getProxyData2(is);
170 senddataProxy("1",parent,treenum);
171 } else {
172 senddataProxy("3",parent,treenum);
173 }
174
175 if ((responseLine = is.readLine()) != null) {
176 System.out.println("Server: " + responseLine);
177 }
178 if ((parent = is.readLine()) != null) {
179 System.out.println("parent: " + parent);
180 }
181 if ((checkRepetition = is.readLine()) != null) {
182 System.out.println("checkRepetition: " + checkRepetition);
183 }
184 if(checkRepetition.equals("stop")){ 146 if(checkRepetition.equals("stop")){
185 return true; 147 return true;
186 } 148 }
187 149 //if(!(checkRepetition.equals("skip")) || "1".equals(leaderflag)) {
188 /** 150 if(!(waitReply.checkPath())) {
189 * if ((value.treenum = is.readLine()) != null) { 151 Thread.sleep(1000);
190 * System.out.println("treenum: " + value.treenum); } 152 reConnectionMain();
191 */ 153 streamClose();
192 154 }
193 155 //Thread.sleep(1000);
194 if(!(checkRepetition.equals("skip")) || "1".equals(leaderflag)) { 156 /*
195
196 //Thread.sleep(1000);
197 client.init();
198 client.setEchoValue(this);
199 client.getParentName();
200 client.start_threads();
201 client.start();
202 // MyVncClient.main(this);
203
204 // 開いたソケットなどをクローズ
205 os.close();
206 is.close();
207 echoSocket.close();
208 //runflag = true;
209 }
210
211
212 Thread.sleep(1000);
213 if(!(checkMove.vncFrame.isShowing())&&"skip".equals(checkRepetition)) { 157 if(!(checkMove.vncFrame.isShowing())&&"skip".equals(checkRepetition)) {
214 System.out.println("in");
215 openport(); 158 openport();
216 notfoundParent(); 159 notfoundParent();
217 } 160 }
218 161 */
219
220 } catch (UnknownHostException e) { 162 } catch (UnknownHostException e) {
221 System.err.println("Trying to connect to unknown host: " + e); 163 System.err.println("Trying to connect to unknown host: " + e);
222 } catch (IOException e) { 164 } catch (IOException e) {
223 165 return false;
224 return false; 166 } catch (InterruptedException e) {
225
226 /*
227 while (true) {
228
229 try {
230 Thread.sleep(1000);
231 System.out.println("once connection proxy");
232 } catch (InterruptedException e1) {
233 e1.printStackTrace();
234 }
235
236 openport();
237 losthost();
238 System.out.println("faild in this area");
239 System.err.println("IOException: " + e);
240
241 if(runflag == true){
242 break;
243 }
244 }
245 */
246 }
247 catch (InterruptedException e) {
248 e.printStackTrace(); 167 e.printStackTrace();
249 } 168
250 169 } catch (NullPointerException e) {
170 openport();
171 System.out.println("notFoundParents");
172 notfoundParent();
173 }
251 } 174 }
252 return true; 175 return true;
253 } 176 }
254 177
255 boolean notfoundParent() { 178 boolean notfoundParent() {
256 if (echoSocket != null && os != null && is != null) { 179 if (echoSocket != null && os != null && is != null) {
257 runflag = true; 180 runflag = true;
258 try { 181 try {
259 senddataProxy("2", parent,null); 182 sendDataProxy("2", parent,null);
260 183 getProxyData(is);
261 if ((responseLine = is.readLine()) != null) { 184 reConnectionMain();
262 System.out.println("Server: " + responseLine); 185 streamClose();
263 }
264 if ((parent = is.readLine()) != null) {
265 System.out.println("parent: " + parent);
266 }
267 if ((treenum = is.readLine()) != null) {
268 System.out.println("treenum: " + treenum);
269 }
270 if ((leaderflag = is.readLine()) != null) {
271 System.out.println("leaderflag: " + leaderflag);
272 }
273 client.init();
274 client.setEchoValue(this);
275 client.getParentName();
276 client.start_threads();
277 client.start();
278 os.close();
279 is.close();
280 echoSocket.close();
281 } catch (UnknownHostException e) { 186 } catch (UnknownHostException e) {
282 System.err.println("Trying to connect to unknown host: " + e); 187 System.err.println("Trying to connect to unknown host: " + e);
283 } catch (IOException e) { 188 } catch (IOException e) {
284 System.err.println("IOException: " + e); 189 System.err.println("IOException: " + e);
285 } 190 }
286 } 191 }
287 return true; 192 return true;
288 } 193 }
289 /* 194
290 void lostchild() { 195
291
292 // ßSystem.out.println(name);
293 if (echoSocket != null && os != null && is != null) {
294 try {
295
296 // echoSocketなどの情報を呼び出す(追加)
297
298 // 自分の番号を報告
299 os.writeBytes("4\n");
300 os.writeBytes(treenum + "\n");
301
302 os.close();
303 is.close();
304 echoSocket.close();
305
306 } catch (UnknownHostException e) {
307 System.err.println("Trying to connect to unknown host: " + e);
308 } catch (IOException e) {
309 System.err.println("IOException: " + e);
310 }
311 }
312 }
313 */
314 EchoClient Interruption(Socket _clientSocket) { 196 EchoClient Interruption(Socket _clientSocket) {
315 clientSocket = _clientSocket; 197 clientSocket = _clientSocket;
316 BufferedReader lostis = null;//あとで修正する 198 BufferedReader lostis = null;//あとで修正する
317 199
318 200
319 try { 201 try {
320 lostis = new BufferedReader(new InputStreamReader( 202 lostis = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
321 clientSocket.getInputStream())); 203 getProxyData(lostis);
322 // while (true){ 204 clientSocket.close();// WaitReplyのacceptを終了させる
323 // line = lostis.readLine();
324 // System.out.println(line);
325
326 try {
327 if ((responseLine = lostis.readLine()) != null) {
328 System.out.println("newServer: " + responseLine);
329 }
330 if ((parent = lostis.readLine()) != null) {
331 System.out.println("newtparent: " + parent);
332 }
333 if ((treenum = lostis.readLine()) != null) {
334 System.out.println("newtreenum: " + treenum);
335 }
336 if ((leaderflag = lostis.readLine()) != null) {
337 System.out.println("newreaderflag: " + leaderflag);
338 }
339
340 } catch (UnknownHostException e) {
341 System.err.println("Trying to connect to unknown host: " + e);
342 } catch (IOException e) {
343 System.err.println("IOException: " + e);
344 }
345
346 // }
347 } catch (IOException e) { 205 } catch (IOException e) {
348 System.out.println(e); 206 System.out.println(e);
349 } 207 }
350 try {
351 clientSocket.close();// WaitReplyのacceptを終了させる
352 } catch (IOException e) {
353 System.out.println(e);
354 }
355 return this; 208 return this;
356 } 209 }
357 210
358 void senddataProxy(String type,String num,String treenum) { 211 void getProxyData(BufferedReader is) throws IOException {
212 if ((responseLine = is.readLine()) != null) {
213 System.out.println("Server: " + responseLine);
214 }
215 if ((parent = is.readLine()) != null) {
216 System.out.println("parent: " + parent);
217 }
218 if ((treenum = is.readLine()) != null) {
219 System.out.println("treenum: " + treenum);
220 }
221 if ((leaderflag = is.readLine()) != null) {
222 System.out.println("leaderflag: " + leaderflag);
223 }
224 }
225
226 String getProxyData2(BufferedReader is) throws IOException {
227 String checkRepetition;
228 if ((responseLine = is.readLine()) != null) {
229 System.out.println("Server: " + responseLine);
230 }
231 if ((parent = is.readLine()) != null) {
232 System.out.println("parent: " + parent);
233 }
234 if ((checkRepetition = is.readLine()) != null) {
235 System.out.println("checkRepetition: " + checkRepetition);
236 }
237 return checkRepetition;
238 }
239
240 void reConnectionMain() {
241 client.init();
242 client.setEchoValue(this);
243 client.getParentName();
244 client.start_threads();
245 client.start();
246 }
247
248 void streamClose() throws IOException {
249 os.close();
250 is.close();
251 echoSocket.close();
252 }
253
254 void sendDataProxy() {
255 if ("1".equals(leaderflag)) {
256 sendDataProxy("1",parent,treenum);
257 } else {
258 sendDataProxy("3",parent,treenum);
259 }
260 }
261
262 void sendDataProxy(String type,String num,String treenum) {
359 try { 263 try {
360 if(treenum!=null) { 264 if(treenum!=null) {
361 os.writeBytes(type + "\n"); 265 os.writeBytes(type + "\n");
362 os.writeBytes(num + "\n"); 266 os.writeBytes(num + "\n");
363 os.writeBytes(treenum + "\n"); 267 os.writeBytes(treenum + "\n");
370 System.err.println("Trying to connect to unknown host: " + e); 274 System.err.println("Trying to connect to unknown host: " + e);
371 } catch (IOException e) { 275 } catch (IOException e) {
372 System.err.println("IOException: " + e); 276 System.err.println("IOException: " + e);
373 } 277 }
374 } 278 }
279
280 String getIpV6() {
281 ipV6 = new IpV6();
282 ipV6.getInterface();
283 return ipV6.getV6();
284 }
375 } 285 }