Mercurial > hg > Applications > TreeVNC
changeset 82:cc72fdbb7c72
on going ...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 30 Apr 2014 17:45:45 +0900 |
parents | f93d0286c2ab |
children | 40d400a7ec84 |
files | src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java src/main/java/jp/ac/u_ryukyu/treevnc/client/GetDataClient.java src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java src/viewer_swing/java/com/glavsoft/viewer/swing/ConnectionParams.java |
diffstat | 6 files changed, 125 insertions(+), 117 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java Wed Apr 30 17:11:15 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java Wed Apr 30 17:45:45 2014 +0900 @@ -5,7 +5,6 @@ import com.glavsoft.viewer.ViewerImpl; import jp.ac.u_ryukyu.treevnc.client.EchoClient; -import jp.ac.u_ryukyu.treevnc.client.GetDataClient; import jp.ac.u_ryukyu.treevnc.client.GetHostClient; import jp.ac.u_ryukyu.treevnc.server.GetBroadCastProxy; @@ -23,15 +22,14 @@ } public void communicationToProxy() { - GetHostClient bCast = new GetHostClient(GetBroadCastProxy.MULTICAST_FIND_TREEVNC_ROOT); - bCast.getHost(); - GetDataClient getBcast = new GetDataClient(); + GetHostClient getBcast = new GetHostClient(GetBroadCastProxy.MULTICAST_FIND_TREEVNC_ROOT); + getBcast.getHost(); portNumber = 0; do { try { Thread runBcast = new Thread(getBcast); runBcast.start(); - hostName = getBcast.textAddress(); + hostName = getBcast.textAddress(); // wait for root reply to our broadcast portNumber = Integer.parseInt(getBcast.textPort()); } catch (Exception e) { logger.info("can't get parent address or port");
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/GetDataClient.java Wed Apr 30 17:11:15 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,96 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.client; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.BindException; -import java.net.InetAddress; -import java.net.ServerSocket; -import java.net.Socket; - - -public class GetDataClient implements Runnable { - private ServerSocket server = null; - private BufferedReader is; - private int port = 8182; - private boolean stopFlag; - private TextBoxClient text = new TextBoxClient(); - private String proxyAddr; - - public String textAddress() { - return text.getAddress(); - } - - public String textPort() { - return text.getPort(); - } - - void socketClose() { - try { - text.unVisible(); - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public void run() { - try { - do { - try { - server = new ServerSocket(port); - } catch (BindException e){ - port ++; - } - } while (server == null); - while(!stopFlag) { - Socket socket = server.accept(); - is = new BufferedReader(new InputStreamReader( - socket.getInputStream())); - proxyAddr = is.readLine(); - // proxyAddr format - // 5999:localhost:localhost:133.13.59.210: - // port:hostnae:hostname:ip address: - // GetBroadCastProxy create this string on VNC root - InetAddress adr = socket.getInetAddress(); - String proxyAddr1 = adr.getHostName(); - if (proxyAddr1 != null) { - int portIndex = proxyAddr.indexOf(":"); - int restIndex = proxyAddr.indexOf(":", portIndex+1); - if (portIndex>0) { - proxyAddr = proxyAddr.substring(0, portIndex)+":"+proxyAddr1 + ":" + proxyAddr.substring(restIndex+1); - } - } - if(proxyAddr!=null) - text.checkBox(proxyAddr); - text.setButton(); - text.visible(); - } - System.out.println("stop"); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void setStopFlag(boolean stopFlag) { - this.stopFlag = stopFlag; - - } - - public void interrupt() { - Thread.currentThread().interrupt(); - } - - public boolean isStopFlag() { - return stopFlag; - } - - public void ipRegister() { - text.ipRegister(); - } - - public String getProxyAddr() { - return proxyAddr; - } -}
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java Wed Apr 30 17:11:15 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java Wed Apr 30 17:45:45 2014 +0900 @@ -1,26 +1,50 @@ package jp.ac.u_ryukyu.treevnc.client; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.net.BindException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; +import java.net.ServerSocket; +import java.net.Socket; -public class GetHostClient { +import com.glavsoft.viewer.swing.ConnectionParams; + +import jp.ac.u_ryukyu.treevnc.server.GetBroadCastProxy; + +public class GetHostClient implements Runnable { final int BUFSIZE = 1024; - final String MCASTADDR = "224.0.0.1"; - final int PORT = 8183; private byte[] buf = new byte[BUFSIZE]; private InetAddress mAddr; private MulticastSocket soc; private String str; + private ServerSocket server = null; + private BufferedReader is; + private int port = ConnectionParams.DEFAULT_VNC_ROOT_FINDER; + private boolean stopFlag; + private TextBoxClient text = new TextBoxClient(); + private String proxyAddr; + public GetHostClient(String _str) { str = _str; + + do { + try { + server = new ServerSocket(port); + } catch (BindException e){ + port ++; + } catch (IOException e) { + // can't open socket + } + } while (server == null); } public void createSocket() { try { - mAddr = InetAddress.getByName(MCASTADDR); + mAddr = InetAddress.getByName(GetBroadCastProxy.McastAddr); soc = new MulticastSocket(); soc.setTimeToLive(1); } catch (IOException e) { @@ -29,8 +53,9 @@ } public void sendData() { - buf = str.getBytes(); - DatagramPacket sendPacket = new DatagramPacket(buf, str.length(), mAddr, PORT); + String s = str + Integer.toString(port); + buf = s.getBytes(); + DatagramPacket sendPacket = new DatagramPacket(buf, s.length(), mAddr, ConnectionParams.DEFAULT_VNC_ROOT_FINDER); try { soc.send(sendPacket); } catch (IOException e) { @@ -42,4 +67,78 @@ createSocket(); sendData(); } + + + public String textAddress() { + return text.getAddress(); + } + + public String textPort() { + return text.getPort(); + } + + void socketClose() { + try { + text.unVisible(); + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * TREEVNC_ROOT_FINDER is sent, wait for reply + */ + @Override + public void run() { + try { + while(!stopFlag) { + Socket socket = server.accept(); + is = new BufferedReader(new InputStreamReader( + socket.getInputStream())); + proxyAddr = is.readLine(); + // proxyAddr format + // 5999:localhost:localhost:133.13.59.210: + // port:hostnae:hostname:ip address: + // GetBroadCastProxy create this string on VNC root + InetAddress adr = socket.getInetAddress(); + String proxyAddr1 = adr.getHostName(); + if (proxyAddr1 != null) { + int portIndex = proxyAddr.indexOf(":"); + int restIndex = proxyAddr.indexOf(":", portIndex+1); + if (portIndex>0) { + proxyAddr = proxyAddr.substring(0, portIndex)+":"+proxyAddr1 + ":" + proxyAddr.substring(restIndex+1); + } + } + if(proxyAddr!=null) + text.checkBox(proxyAddr); + text.setButton(); + text.visible(); + } + System.out.println("stop"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void setStopFlag(boolean stopFlag) { + this.stopFlag = stopFlag; + + } + + public void interrupt() { + Thread.currentThread().interrupt(); + } + + public boolean isStopFlag() { + return stopFlag; + } + + public void ipRegister() { + text.ipRegister(); + } + + public String getProxyAddr() { + return proxyAddr; + } }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java Wed Apr 30 17:11:15 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java Wed Apr 30 17:45:45 2014 +0900 @@ -10,6 +10,8 @@ import java.net.SocketAddress; import java.net.UnknownHostException; +import com.glavsoft.viewer.swing.ConnectionParams; + //import TextBoxProxy; public class GetBroadCastProxy implements Runnable { @@ -36,23 +38,23 @@ */ private void replyToRootSearchMulticast() { byte[] buf = new byte[BufSize]; - byte[] reply = new byte[BufSize]; try { InetAddress mAddr = InetAddress.getByName(McastAddr); - soc = new MulticastSocket(Port); + soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER); DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); soc.joinGroup(mAddr); while (!stopFlag) { soc.receive(recvPacket); String address = getAddress(recvPacket.getSocketAddress()); - inputStream = new ByteArrayInputStream(recvPacket.getData()); - inputStream.read(reply); + byte[] reply = recvPacket.getData(); + int len = recvPacket.getLength(); int i = 0; for(byte b : MULTICAST_FIND_TREEVNC_ROOT.getBytes()) { if (b != reply[i]) return; i++; + if (i>len) return; } - int port = parse_code(reply,i); + int port = parse_code(reply,i,len); replyToClient(address,port,bCast); if(stopFlag) break; } @@ -61,11 +63,13 @@ } } - public int parse_code( byte[] bs,int offset ) + public int parse_code( byte[] bs,int offset,int len ) { int intval = 0; - for( int i = offset; i < bs.length ; i++ ) + for( int i = offset; i < len ; i++ ) { + if (bs[i]==0 ) return intval; intval = intval * 10 + ( bs[ i ] - '0' ); + } return intval; }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java Wed Apr 30 17:11:15 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java Wed Apr 30 17:45:45 2014 +0900 @@ -17,6 +17,7 @@ import com.glavsoft.rfb.protocol.ProtocolSettings; import com.glavsoft.viewer.Viewer; import com.glavsoft.viewer.cli.Parser; +import com.glavsoft.viewer.swing.ConnectionParams; import com.glavsoft.viewer.swing.ParametersHandler; public class VncProxyService extends Viewer implements Runnable, @@ -27,7 +28,6 @@ */ private static final long serialVersionUID = 1L; - public static final int DEFAULT_PORT = 5900; public static Logger logger = Logger.getLogger("com.glavsoft"); private boolean forceReconnection; private String reconnectionReason; @@ -40,6 +40,8 @@ private boolean firstTime = true; private ServerSocket echoServerSocket; + private int vncport = ConnectionParams.DEFAULT_RFB_PORT; + public VncProxyService() { ProtocolSettings.getDefaultSettings(); uiSettings = super.uiSettings; @@ -79,7 +81,7 @@ aClient = new AcceptClient(); isApplet = false; setIsTreeVNC(true); - setConnectionParam(hostName,5900); + setConnectionParam(hostName,vncport); isApplet = true; run(); createConnectionAndStart(); @@ -239,7 +241,7 @@ // newVps.fbWidth = width; // newVps.fbHeight = height; // run call and change workingProtocol - newVps.setConnectionParam(hostName, 5900); + newVps.setConnectionParam(hostName, vncport); newVps.initProxy1(hostName); orderRecconection(newVps ,hostName); socketClose();
--- a/src/viewer_swing/java/com/glavsoft/viewer/swing/ConnectionParams.java Wed Apr 30 17:11:15 2014 +0900 +++ b/src/viewer_swing/java/com/glavsoft/viewer/swing/ConnectionParams.java Wed Apr 30 17:45:45 2014 +0900 @@ -33,6 +33,7 @@ public class ConnectionParams implements Model { public static final int DEFAULT_SSH_PORT = 22; public static final int DEFAULT_RFB_PORT = 5900; + public static final int DEFAULT_VNC_ROOT_FINDER = 8182; public String hostName; private int portNumber;