Mercurial > hg > Members > riono > TreeVNC_ja_comment
view src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java @ 56:913d0f663e74
12/9
author | one |
---|---|
date | Mon, 09 Dec 2013 16:09:06 +0900 |
parents | 9fdb8ff88497 |
children | 433c79184c05 |
line wrap: on
line source
package jp.ac.u_ryukyu.treevnc.client; import java.io.*; import java.net.*; import com.glavsoft.viewer.Viewer; public class EchoClient { private String proxyName; private BufferedReader is = null; private DataOutputStream os = null; private Socket echoSocket = null; private boolean runflag = false; private WaitReply waitReply; private Socket clientSocket = null; private int echoPort = 9999; public Viewer client; private String parentAddress; public String parentNum; public String treeNum; public String leaderFlag; private String myAddress; public EchoClient(String name, int echoPort) { this.echoPort = echoPort; this.proxyName = name; } public void openport() { try { // if(echoSocket==null) echoSocket = new Socket(proxyName, echoPort); echoSocket.setReuseAddress(true); 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(proxyName + " Connection Faild"); System.exit(0); } } /** * @param args * select connect port * @return */ public EchoClient requestHostName(String args) { if (echoSocket != null && os != null && is != null) { try { InetAddress addr = InetAddress.getLocalHost(); myAddress = new String(addr.getHostAddress()); // add = getIpV6(); os.writeBytes(myAddress + "\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, this); waitReply.start(); } return this; } /** * Call at lost host */ public boolean lostHost() { if (echoSocket != null && os != null && is != null) { try { if (runflag) { return true; } sendDataProxy(); reConnectionMain(echoSocket); streamClose(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { return false; } catch (NullPointerException e) { openport(); System.out.println("notFoundParents"); notfoundParent(); } } return true; } public boolean notfoundParent() { if (echoSocket != null && os != null && is != null) { runflag = true; try { sendDataProxy("2", parentNum, 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; } public EchoClient Interruption(Socket _clientSocket) { clientSocket = _clientSocket; BufferedReader lostis = null; try { lostis = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); getProxyData(lostis); clientSocket.close(); } catch (IOException e) { System.out.println(e); } return this; } void getProxyData(BufferedReader is) throws IOException { if ((parentAddress = is.readLine()) != null) { System.out.println("Server: " + parentAddress); } if ((parentNum = is.readLine()) != null) { System.out.println("parent: " + parentNum); } 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 = null; if (!("1".equals(leaderFlag))) { ServerSocket waitReplyServer = new ServerSocket(9999); Socket socketForWait = waitReplyServer.accept(); BufferedReader isNotLeader = new BufferedReader(new InputStreamReader( socketForWait.getInputStream())); System.out.println("-------------------renode----------------------------"); if ((parentAddress = isNotLeader.readLine()) != null) { System.out.println("Server: " + parentAddress); } if ((parentNum = isNotLeader.readLine()) != null) { System.out.println("parent:test " + parentNum); } if ((checkRepetition = isNotLeader.readLine()) != null) { System.out.println("checkRepetition: " + checkRepetition); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } else { System.out.println("-------------------re----------------------------"); if ((parentAddress = is.readLine()) != null) { System.out.println("Server: " + parentAddress); } if ((parentNum = is.readLine()) != null) { System.out.println("parent:test " + parentNum); } if ((checkRepetition = is.readLine()) != null) { System.out.println("checkRepetition: " + checkRepetition); } } return checkRepetition; } void reConnectionMain(Socket echoSocket) { while (true) { try { client.close(); // set Socket for connection in VncViewer. Socket soc = createSocketForClient(echoSocket,false); client.setSocket(soc); if(soc != null) client.run(); break; } catch (IOException e) { break; //continue; } } } void streamClose() throws IOException { os.close(); is.close(); echoSocket.close(); } void sendDataProxy() { if ("1".equals(leaderFlag)) { sendDataProxy("1", parentNum, treeNum); System.out.println("---------------------------------------------"); } else { // sendDataProxy("3", parentNum, 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); } } public void getParentName() { if (clientSocket == null) { // echo = new EchoClient(pHost, this); openport(); requestHostName("1"); // 1 is normal connection type. } else { Interruption(clientSocket); } } public void setViewer(Viewer v) { client = v; } public String getMyAddress() { return myAddress; } // create socket for ReConnection. public Socket createSocketForClient(Socket soc,boolean flag) throws IOException { Socket socket = null; String parentAddress; int count = 0; System.out.println("########################PATH************************"); BufferedReader is = new BufferedReader(new InputStreamReader( soc.getInputStream())); parentAddress = is.readLine(); String port = is.readLine(); // It is called when the screen changes. if ("reconnection".equals(parentAddress)) { while (true) { try { client.setOpenPort(Integer.parseInt(port)); return new Socket(this.parentAddress, Integer.parseInt(port)); } catch (IOException e) { try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } if (count++ > 5) break; continue; } } } else { System.out.println("###########################faild"+parentAddress); if(parentAddress==null) return null; is.readLine();// parentNum } socket = new Socket(parentAddress,Integer.parseInt(port)); socket.setReuseAddress(true); return socket; } public String getParentsAddress() { return parentAddress; } public void reportConnectionFinished(Socket soc) throws IOException { DataOutputStream os = new DataOutputStream(soc.getOutputStream()); os.writeBytes("finished"); } }