Mercurial > hg > Applications > TreeVNC
view src/main/java/jp/ac/u_ryukyu/treevnc/FindRoot.java @ 434:aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 11 Jun 2016 22:42:58 +0900 |
parents | b3ef6d14b2b7 |
children | 9db8862ef200 |
line wrap: on
line source
package jp.ac.u_ryukyu.treevnc; import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import com.glavsoft.viewer.swing.ConnectionParams; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.*; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class FindRoot implements Runnable { final int BUFSIZE = 1024; private MulticastSocket soc; private InetAddress mAddr; private ServerSocket server = null; private BufferedReader is; private int port; private boolean stopFlag = false; private TreeVncRootSelectionPanel rootSelectionPanel = new TreeVncRootSelectionPanel(this); private String proxyAddr; /** * To find vnc root, a client sends a multicast packet. * @param _port * @param createConnectionParam */ public FindRoot(int _port, CreateConnectionParam createConnectionParam) { port = _port; CreateConnectionParam cp = createConnectionParam; rootSelectionPanel.setCp(cp); try { soc = TreeRootFinderListener.createMulticastSocket(); soc.setTimeToLive(1); mAddr = InetAddress.getByName(TreeRootFinderListener.McastAddr); System.out.println("FindRoot socket on " + TreeRootFinderListener.McastAddr + " " + mAddr + " port " + _port); } catch (IOException e) { System.out.println("cannot create FindRoot socket. " + e); return; } } /** * 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) { System.out.println("cannot send FindRoot packet."); e.printStackTrace(); } } public String textPort() { return rootSelectionPanel.getPort(); } void socketClose() { try { rootSelectionPanel.unVisible(); is.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("cannot is close."); } } /** * TREEVNC_ROOT_FINDER is sent, wait for reply */ @Override public void run() { try { rootSelectionPanel.visible(); 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:hostname: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(); } } catch (IOException e) { System.out.println("an I/O error occurs " + e.getMessage()); } } 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. * * unused. */ 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; } }