Mercurial > hg > Applications > TreeVNC
view src/main/java/jp/ac/u_ryukyu/treevnc/FindRoot.java @ 287:61938de1dd06
fix sendSingleDisplayWidth() timing
author | oc |
---|---|
date | Mon, 12 Jan 2015 16:30:46 +0900 |
parents | 2b3eb4a9492f |
children | 0b61b7214ece |
line wrap: on
line source
package jp.ac.u_ryukyu.treevnc; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.InetAddress; 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.TreeCommand; import com.glavsoft.viewer.swing.ConnectionParams; public class FindRoot implements Runnable { final int BUFSIZE = 1024; private InetAddress mAddr; private MulticastSocket soc; private ServerSocket server = null; private BufferedReader is; private int port; private boolean stopFlag; private TreeVncRootSelectionPanel rootSelectionPanel = new TreeVncRootSelectionPanel(); private String proxyAddr; /** * To find vnc root, a client sends a multicast packet. * @param _port * @param createConnectionParam */ public FindRoot(int _port, CreateConnectionParam createConnectionParam) { CreateConnectionParam cp = createConnectionParam; port = _port; rootSelectionPanel.setCp(cp); createSocket(); } public void createSocket() { try { mAddr = InetAddress.getByName(TreeRootFinderListener.McastAddr); soc = new MulticastSocket(); soc.setTimeToLive(1); } catch (IOException e) { e.printStackTrace(); } } /** * send find root message. * */ public void findRoot() { ByteBuffer buf = ByteBuffer.allocate(12); buf.order(ByteOrder.BIG_ENDIAN); buf.put((byte) TreeCommand.FIND_ROOT.cmd); 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(); } } public String textPort() { return rootSelectionPanel.getPort(); } void socketClose() { try { rootSelectionPanel.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(); String proxyAddr2 = null; if (proxyAddr1 != null) { int portIndex = proxyAddr.indexOf(":"); int restIndex = proxyAddr.indexOf(":", portIndex+1); if (portIndex>0) { proxyAddr2 = proxyAddr.substring(0, portIndex)+":"+proxyAddr1 + ":" + proxyAddr.substring(restIndex+1); } } if(proxyAddr2!=null) rootSelectionPanel.checkBox(proxyAddr2); rootSelectionPanel.setButton(); rootSelectionPanel.visible(); } System.err.println("stop"); } catch (IOException e) { } } public void setStopFlag(boolean stopFlag) { this.stopFlag = stopFlag; } /** * the thread is waiting accept system call, * close the socket causes exception, * which will terminated the thread. * */ public void interrupt() { stopFlag = true; try { server.close(); soc.close(); } catch (IOException e) { } Thread.currentThread().interrupt(); } public boolean isStopFlag() { return stopFlag; } public void ipRegister() { rootSelectionPanel.ipRegister(); } public String getProxyAddr() { return proxyAddr; } }