Mercurial > hg > Members > nobuyasu > tightVNCClient
view src/myVncClient/EchoClient.java @ 97:19aff5293769
large update befor
author | one |
---|---|
date | Fri, 14 Oct 2011 17:16:20 +0900 |
parents | 3c4bebe9bf15 |
children | 1d1bb77d871b |
line wrap: on
line source
package myVncClient; import java.io.*; import java.net.*; import myVncClient.MulticastQueue.Client; public class EchoClient { private String name; private BufferedReader is = null; private DataOutputStream os = null; private Socket echoSocket = null; private boolean runflag = false; private WaitReply waitReply; private Socket clientSocket = null; // MyVncClient client; private InterfaceForViewer client; private int echoPort = 9999; IpV6 ipV6; MyVncClient checkMove; VncViewer vncV; String responseLine; String parent;// 親の番号 String treenum;// 自分の番号 String leaderflag;// リーダフラグ boolean passflag; // WaitReplyに自分自身を渡している public EchoClient() { } public EchoClient(EchoClient echo) { this.name = echo.name; leaderflag= echo.leaderflag; parent = echo.parent; treenum = echo.treenum; client = echo.client; checkMove = (MyVncClient)echo.client; } // VncViewerから引数をもらってきてproxy役を認識する public EchoClient(String name,MyVncClient client) { this.client = client; this.name = name; } public EchoClient(String name,MyVncClient client, int echoPort) { this.client = client; this.name = name; this.echoPort = echoPort; } public EchoClient(EchoClient echo,MyVncClient client) { this.client = client; this.name = echo.name; leaderflag = echo.leaderflag; parent = echo.parent; treenum = echo.treenum; waitReply = echo.waitReply; } public EchoClient(String name,CuiMyVncClient client) { this.client = client; this.name = name; } public EchoClient(String name,CuiMyVncClient client, int echoPort) { this.client = client; this.name = name; this.echoPort = echoPort; } public EchoClient(EchoClient echo,CuiMyVncClient client) { this.client = client; this.name = echo.name; leaderflag = echo.leaderflag; parent = echo.parent; treenum = echo.treenum; } // void hostn(String args){ void openport() { try { if (name != null) { echoSocket = new Socket(name, echoPort); } else { echoSocket = new Socket("133.13.48.18", echoPort); } os = new DataOutputStream(echoSocket.getOutputStream()); is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost"); } catch (IOException e) { System.out.println(name + " Connection Faild"); System.exit(0); } } /** * @param args * select connect port * @return */ EchoClient requestHostName(String args) { // サーバーにメッセージを送る if (echoSocket != null && os != null && is != null) { try { InetAddress addr = InetAddress.getLocalHost(); String add = new String(addr.getHostAddress()); //add = getIpV6(); os.writeBytes(add + "\n"); os.writeBytes(args + "\n"); getProxyData(is); streamClose(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } waitReply = new WaitReply(treenum,client); waitReply.start(); } return this; } /** * Call at lost host */ boolean losthost() { if (echoSocket != null && os != null && is != null) { try { if(runflag){ return true; } sendDataProxy(); String checkRepetition = getProxyData2(is); if(checkRepetition.equals("stop")){ return true; } if(!(checkRepetition.equals("skip")) || "1".equals(leaderflag)) { Thread.sleep(1000); reConnectionMain(); streamClose(); } Thread.sleep(1000); if(!(checkMove.vncFrame.isShowing())&&"skip".equals(checkRepetition)) { openport(); notfoundParent(); } } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { return false; } catch (InterruptedException e) { e.printStackTrace(); } catch (NullPointerException e) { openport(); notfoundParent(); } } return true; } boolean notfoundParent() { if (echoSocket != null && os != null && is != null) { runflag = true; try { sendDataProxy("2", parent,null); getProxyData(is); reConnectionMain(); streamClose(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } return true; } EchoClient Interruption(Socket _clientSocket) { clientSocket = _clientSocket; BufferedReader lostis = null;//あとで修正する try { lostis = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); getProxyData(lostis); clientSocket.close();// WaitReplyのacceptを終了させる } catch (IOException e) { System.out.println(e); } return this; } void getProxyData(BufferedReader is) throws IOException { if ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); } if ((parent = is.readLine()) != null) { System.out.println("parent: " + parent); } if ((treenum = is.readLine()) != null) { System.out.println("treenum: " + treenum); } if ((leaderflag = is.readLine()) != null) { System.out.println("leaderflag: " + leaderflag); } } String getProxyData2(BufferedReader is) throws IOException { String checkRepetition; if ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); } if ((parent = is.readLine()) != null) { System.out.println("parent: " + parent); } if ((checkRepetition = is.readLine()) != null) { System.out.println("checkRepetition: " + checkRepetition); } return checkRepetition; } void reConnectionMain() { client.init(); client.setEchoValue(this); client.getParentName(); client.start_threads(); client.start(); } void streamClose() throws IOException { os.close(); is.close(); echoSocket.close(); } void sendDataProxy() { if ("1".equals(leaderflag)) { sendDataProxy("1",parent,treenum); } else { sendDataProxy("3",parent,treenum); } } void sendDataProxy(String type,String num,String treenum) { try { if(treenum!=null) { os.writeBytes(type + "\n"); os.writeBytes(num + "\n"); os.writeBytes(treenum + "\n"); } else { os.writeBytes(type + "\n"); os.writeBytes(num + "\n"); } } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } String getIpV6() { ipV6 = new IpV6(); ipV6.getInterface(); return ipV6.getV6(); } }