Mercurial > hg > Applications > TightVNC_orginal
diff src/main/java/ac/ryukyu/treevnc/client/EchoClient.java @ 1:75b54fd9e73a
add new package treevnc
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Jul 2012 13:34:43 +0900 |
parents | |
children | dca3bd61b830 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ac/ryukyu/treevnc/client/EchoClient.java Tue Jul 03 13:34:43 2012 +0900 @@ -0,0 +1,289 @@ +package ac.ryukyu.treevnc.client; + +import java.io.*; +import java.net.*; +import ac.ryukyu.treevnc.*; + +//import myVncProxy.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; + // private IpV6 ipV6; + // private MyVncClient checkMove; + // private VncViewer vncV; + String responseLine; + String parent;// 親の番号 + String treenum;// 自分の番号 + String leaderflag;// リーダフラグ + + // boolean passflag; + + // WaitReplyに自分自身を渡している + public EchoClient() { + } + + public EchoClient(EchoClient echo) { + this.name = echo.name; + this.leaderflag = echo.leaderflag; + this.parent = echo.parent; + this.treenum = echo.treenum; + this.client = echo.client; + this.waitReply = echo.waitReply; + // 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)) { + if (!(waitReply.checkPath())) { + Thread.sleep(1000); + reConnectionMain(echoSocket); + 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(); + System.out.println("notFoundParents"); + notfoundParent(); + } + } + return true; + } + + boolean notfoundParent() { + if (echoSocket != null && os != null && is != null) { + runflag = true; + try { + sendDataProxy("2", parent, null); + getProxyData(is); + reConnectionMain(echoSocket); + 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; + System.out.println("-------------------re----------------------------"); + if ((responseLine = is.readLine()) != null) { + System.out.println("Server: " + responseLine); + } + if ((parent = is.readLine()) != null) { + System.out.println("parent:test " + parent); + } + if ((checkRepetition = is.readLine()) != null) { + System.out.println("checkRepetition: " + checkRepetition); + } + return checkRepetition; + } + + void reConnectionMain(Socket echoSocket) { + client.close(); + client.setClientSocket(echoSocket); + client.run(); + /* + 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); + System.out.println("---------------------------------------------"); + } else { + sendDataProxy("3", parent, treenum); + System.out.println("---------------------------------------------"); + } + } + + 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(); } + */ +} \ No newline at end of file