# HG changeset patch # User oc # Date 1401960645 -32400 # Node ID 32c6563492f390a29af5a2a220cfec69d34211c0 # Parent 214d4f2064312539b117119b4ab90aa1af7718c5 add vnc protocol command sender. diff -r 214d4f206431 -r 32c6563492f3 src/main/java/com/glavsoft/rfb/protocol/ProtocolContext.java --- a/src/main/java/com/glavsoft/rfb/protocol/ProtocolContext.java Thu Jun 05 17:01:34 2014 +0900 +++ b/src/main/java/com/glavsoft/rfb/protocol/ProtocolContext.java Thu Jun 05 18:30:45 2014 +0900 @@ -88,7 +88,8 @@ public static final int FIND_ROOT_REPLY = 221; public static final int WHERE_TO_CONNECT = 222; public static final int CONNECT_TO = 223; - public static final int LOST_PARENT = 224; + public static final int CONNECT_TO_AS_LEADER = 224; + public static final int LOST_PARENT = 225; } diff -r 214d4f206431 -r 32c6563492f3 src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java --- a/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java Thu Jun 05 17:01:34 2014 +0900 +++ b/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java Thu Jun 05 18:30:45 2014 +0900 @@ -46,7 +46,7 @@ import java.util.logging.Logger; import jp.ac.u_ryukyu.treevnc.MyRfbProto; -import jp.ac.u_ryukyu.treevnc.client.EchoClient; +import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol; public class ReceiverTask implements Runnable { private static final byte FRAMEBUFFER_UPDATE = 0; @@ -151,7 +151,7 @@ if(!rfb.isRoot() && !(rfb.getTerminationType())) { System.out.println("task stop"); int counter = 0; // static int ? - EchoClient echo = rfb.getEcho(); + TreeVncProtocol echo = rfb.getEcho(); echo.openport(); while(true) { // after set time out. not counter. diff -r 214d4f206431 -r 32c6563492f3 src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java Thu Jun 05 17:01:34 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java Thu Jun 05 18:30:45 2014 +0900 @@ -4,7 +4,7 @@ import com.glavsoft.viewer.ViewerImpl; -import jp.ac.u_ryukyu.treevnc.client.EchoClient; +import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol; import jp.ac.u_ryukyu.treevnc.client.GetHostClient; import jp.ac.u_ryukyu.treevnc.server.GetBroadCastProxy; @@ -40,7 +40,7 @@ } public void createConnectionParam(ViewerImpl v) { - EchoClient echo = new EchoClient(hostName, 9999); + TreeVncProtocol echo = new TreeVncProtocol(hostName, 9999); rfb.setEcho(echo); rfb.setProxyAddr(hostName); echo.getParentName(); diff -r 214d4f206431 -r 32c6563492f3 src/main/java/jp/ac/u_ryukyu/treevnc/InterfaceForViewer.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/InterfaceForViewer.java Thu Jun 05 17:01:34 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/InterfaceForViewer.java Thu Jun 05 18:30:45 2014 +0900 @@ -7,7 +7,7 @@ public interface InterfaceForViewer extends java.lang.Runnable{ - public void setEchoValue(EchoClient value); + public void setEchoValue(TreeVncProtocol value); public String readParameter(String name, boolean required); public void getParentName(); diff -r 214d4f206431 -r 32c6563492f3 src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java Thu Jun 05 17:01:34 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java Thu Jun 05 18:30:45 2014 +0900 @@ -14,7 +14,7 @@ import java.util.zip.Deflater; import java.util.zip.Inflater; -import jp.ac.u_ryukyu.treevnc.client.EchoClient; +import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol; import jp.ac.u_ryukyu.treevnc.server.RequestScreenThread; import jp.ac.u_ryukyu.treevnc.server.VncProxyService; @@ -38,7 +38,7 @@ public MulticastQueue> multicastqueue = new MulticastQueue>(); private RequestScreenThread rThread; private boolean proxyFlag = true; - private EchoClient echo; + private TreeVncProtocol echo; private String proxyAddr; public int acceptPort = 0; protected boolean readyReconnect = false; @@ -424,7 +424,7 @@ proxyFlag = false; } - public void setEcho(EchoClient _echo) { + public void setEcho(TreeVncProtocol _echo) { echo = _echo; } @@ -436,7 +436,7 @@ return echo.getViewer(); } - public EchoClient getEcho() { + public TreeVncProtocol getEcho() { return echo; } diff -r 214d4f206431 -r 32c6563492f3 src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java Thu Jun 05 17:01:34 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,260 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.client; - -import java.io.*; -import java.net.*; - -import com.glavsoft.viewer.ViewerImpl; - -public class EchoClient { - private String proxyName; - private BufferedReader is = null; - private DataOutputStream os = null; - private Socket echoSocket = null; - private boolean runflag = false; - private ReconnectionWaiter waitReply; - private Socket clientSocket = null; - private int echoPort = 9999; - public ViewerImpl 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 { - 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: "+proxyName); - } 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 = echoSocket.getLocalAddress(); - myAddress = addr.getHostAddress(); - os.writeBytes(myAddress + "\n"); - os.writeBytes(args + "\n"); - getProxyData(is, echoSocket); - - streamClose(); - } catch (UnknownHostException e) { - System.err.println("Trying to connect to unknown host: " + e); - } catch (IOException e) { - System.err.println("IOException: " + e); - } - waitReply = new ReconnectionWaiter(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, echoSocket); - 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; - try { - BufferedReader lostis = new BufferedReader(new InputStreamReader( - clientSocket.getInputStream())); - getProxyData(lostis, clientSocket); - clientSocket.close(); - } catch (IOException e) { - System.out.println(e); - } - return this; - } - - void getProxyData(BufferedReader is, Socket soc) throws IOException { - if ((parentAddress = is.readLine()) != null) { - System.out.println("Server received: " + 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); - } - InetAddress parent = soc.getInetAddress(); - parentAddress = parent.getHostName(); - System.out.println("Actual Server: " + parentAddress); - } - - void reConnectionMain(Socket echoSocket) { - while (true) { - try { - client.closeApp(); - // 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) { - openport(); - requestHostName("1"); // 1 is normal connection type. - } else { - Interruption(clientSocket); - } - } - - public void setViewer(ViewerImpl v) { - client = v; - } - - public ViewerImpl getViewer() { - return client; - } - - 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)); - System.out.println("----------:"+this.parentAddress+"----------:"+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+"port::"+port); - if (parentAddress == null) - return null; - is.readLine();// parentNum - } - socket = new Socket(parentAddress, Integer.parseInt(port)); - socket.setReuseAddress(true); - System.out.println("Return Soket"); - return socket; - } - - public String getParentsAddress() { - return parentAddress; - } - -} \ No newline at end of file diff -r 214d4f206431 -r 32c6563492f3 src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java Thu Jun 05 17:01:34 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java Thu Jun 05 18:30:45 2014 +0900 @@ -9,7 +9,10 @@ import java.net.MulticastSocket; import java.net.ServerSocket; import java.net.Socket; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import com.glavsoft.rfb.protocol.ProtocolContext; import com.glavsoft.viewer.swing.ConnectionParams; import jp.ac.u_ryukyu.treevnc.server.GetBroadCastProxy; @@ -28,6 +31,10 @@ private TextBoxClient text = new TextBoxClient(); private String proxyAddr; + /** + * To find vnc root, a client sends a multicast packet. + * @param _str + */ public GetHostClient(String _str) { str = _str; } @@ -72,6 +79,25 @@ sendData(); } + public void findRoot() { + ByteBuffer buf = ByteBuffer.allocate(12); + buf.order(ByteOrder.BIG_ENDIAN); + buf.put((byte) ProtocolContext.FIND_ROOT); + buf.put((byte) 0); + buf.put((byte) 0); + buf.put((byte) 0); + buf.putInt(4); // length + buf.putInt(port); + buf.flip(); + + DatagramPacket sendPacket = new DatagramPacket(buf.array(), buf.limit(), mAddr, ConnectionParams.DEFAULT_VNC_ROOT_FINDER); + try { + soc.send(sendPacket); + } catch (IOException e) { + e.printStackTrace(); + } + + } /** diff -r 214d4f206431 -r 32c6563492f3 src/main/java/jp/ac/u_ryukyu/treevnc/client/ReconnectionWaiter.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/ReconnectionWaiter.java Thu Jun 05 17:01:34 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/ReconnectionWaiter.java Thu Jun 05 18:30:45 2014 +0900 @@ -11,9 +11,9 @@ * Close current application and reopen it. */ public class ReconnectionWaiter extends Thread { - private EchoClient echo; + private TreeVncProtocol echo; - public ReconnectionWaiter(String treenum, EchoClient echo) { + public ReconnectionWaiter(String treenum, TreeVncProtocol echo) { this.echo = echo; } diff -r 214d4f206431 -r 32c6563492f3 src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java Thu Jun 05 18:30:45 2014 +0900 @@ -0,0 +1,325 @@ +package jp.ac.u_ryukyu.treevnc.client; + +import java.io.*; +import java.net.*; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import jp.ac.u_ryukyu.treevnc.server.VncProxyService; + +import com.glavsoft.rfb.protocol.ProtocolContext; +import com.glavsoft.viewer.ViewerImpl; + +public class TreeVncProtocol { + private String proxyName; + private BufferedReader is = null; + private DataOutputStream os = null; + private Socket echoSocket = null; + private boolean runflag = false; + private ReconnectionWaiter waitReply; + private Socket clientSocket = null; + private int echoPort = 9999; + public ViewerImpl client; + private String parentAddress; + public String parentNum; + public String treeNum; + public String leaderFlag; + private String myAddress; + private int rootPort; + + + public TreeVncProtocol(String name, int echoPort) { + this.echoPort = echoPort; + this.proxyName = name; + } + + public void setRootPort(int port) { + rootPort = port; + } + + + public void openport() { + try { + 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: "+proxyName); + } catch (IOException e) { + System.out.println(proxyName + " Connection Faild"); + System.exit(0); + } + + } + + public void findRootReply(String hostname, int port) throws IOException { + sendWithHostAndPort(ProtocolContext.FIND_ROOT_REPLY, hostname, port); + } + + public void whereToConnect(String hostname, int port) throws IOException { + sendWithHostAndPort(ProtocolContext.WHERE_TO_CONNECT, hostname, port); + } + + public void connectTo(String hostname, int port) throws IOException{ + sendWithHostAndPort(ProtocolContext.CONNECT_TO, hostname, port); + } + + public void connectToAsLeader(String hostname, int port) throws IOException { + sendWithHostAndPort(ProtocolContext.CONNECT_TO_AS_LEADER, hostname, port); + } + + public void lostParent(String hostname, int port) throws IOException { + sendWithHostAndPort(ProtocolContext.LOST_PARENT, hostname, port); + } + + + public void sendWithHostAndPort(int command, String hostname, int port) + throws IOException { + + int savePort = echoPort; + echoPort = rootPort; + openport(); + + ByteBuffer buf = ByteBuffer.allocate(4+4+4+hostname.length()); + buf.order(ByteOrder.BIG_ENDIAN); + buf.put((byte) command); + + buf.put((byte) 0); + buf.put((byte) 0); + buf.put((byte) 0); + buf.putInt(4+hostname.length()); // length + buf.putInt(port); + buf.put(hostname.getBytes(), 0, hostname.length()); + buf.flip(); + + char[] charBuf = new char[12]; + is.read(charBuf , 0, 12); + os.write(buf.array(), 0, buf.limit()); + + echoPort = savePort; + streamClose(); + } + + + /** + * @param args + * select connect port + * @return + */ + public TreeVncProtocol requestHostName(String args) { + + if (echoSocket != null && os != null && is != null) { + try { + InetAddress addr = echoSocket.getLocalAddress(); + myAddress = addr.getHostAddress(); + os.writeBytes(myAddress + "\n"); + os.writeBytes(args + "\n"); + getProxyData(is, echoSocket); + + streamClose(); + } catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + waitReply = new ReconnectionWaiter(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, echoSocket); + 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 TreeVncProtocol Interruption(Socket _clientSocket) { + clientSocket = _clientSocket; + try { + BufferedReader lostis = new BufferedReader(new InputStreamReader( + clientSocket.getInputStream())); + getProxyData(lostis, clientSocket); + clientSocket.close(); + } catch (IOException e) { + System.out.println(e); + } + return this; + } + + void getProxyData(BufferedReader is, Socket soc) throws IOException { + if ((parentAddress = is.readLine()) != null) { + System.out.println("Server received: " + 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); + } + InetAddress parent = soc.getInetAddress(); + parentAddress = parent.getHostName(); + System.out.println("Actual Server: " + parentAddress); + } + + void reConnectionMain(Socket echoSocket) { + while (true) { + try { + client.closeApp(); + // 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) { + openport(); + requestHostName("1"); // 1 is normal connection type. + } else { + Interruption(clientSocket); + } + } + + public void setViewer(ViewerImpl v) { + client = v; + } + + public ViewerImpl getViewer() { + return client; + } + + 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)); + System.out.println("----------:"+this.parentAddress+"----------:"+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+"port::"+port); + if (parentAddress == null) + return null; + is.readLine();// parentNum + } + socket = new Socket(parentAddress, Integer.parseInt(port)); + socket.setReuseAddress(true); + System.out.println("Return Soket"); + return socket; + } + + public String getParentsAddress() { + return parentAddress; + } + +} \ No newline at end of file