Mercurial > hg > Members > riono > TreeVNC_ja_comment
view src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java @ 505:af958194248b
change replyToRootSearchMulticast
author | oshiro |
---|---|
date | Mon, 18 Feb 2019 18:46:23 +0900 |
parents | 432e2967eaab |
children | 3fe7e1a372df |
line wrap: on
line source
package jp.ac.u_ryukyu.treevnc; import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import com.glavsoft.rfb.protocol.ReceiverTask; import com.glavsoft.transport.Reader; import com.glavsoft.viewer.ViewerInterface; import com.glavsoft.viewer.swing.ConnectionParams; import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.*; import static com.glavsoft.rfb.protocol.ReceiverTask.FRAMEBUFFER_UPDATE; public class TreeRootFinderListener implements Runnable { public static final String Ipv4McastAddr = "224.0.0.1"; public static final String Ipv6McastAddr = "ff02::1"; public static String McastAddr = Ipv4McastAddr; static final int BufSize = 1024; private ReceiverTask receiverTask; private boolean stopFlag = false; private ViewerInterface vps; private MulticastSocket soc; private SecurityManager securityManager; public TreeRootFinderListener(ViewerInterface vncProxyService) { vps = vncProxyService; } public TreeRootFinderListener(ViewerInterface vncProxyService, ReceiverTask receiverTask) { vps = vncProxyService; this.receiverTask = receiverTask; } public static MulticastSocket createMulticastSocket() throws IOException { MulticastSocket soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER); try { soc.joinGroup(InetAddress.getByName(McastAddr)); } catch (SocketException e) { System.out.println("join to " + Ipv4McastAddr + " failed."); } try { soc.joinGroup(InetAddress.getByName(Ipv6McastAddr)); } catch (SocketException e) { System.out.println("join to " + Ipv6McastAddr + " failed."); } return soc; } /** * To find TreeVNC root, a client sends me a multicast, reply our address to him. * It contains a port to receive, so multiple TREEVNC clients can run on a PC. */ private void replyToRootSearchMulticast() { byte[] buf = new byte[BufSize]; try { soc = createMulticastSocket(); System.out.println("FindRoot listening on "+ InetAddress.getByName(McastAddr)); DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); while (!stopFlag) { soc.receive(recvPacket); String hostname = recvPacket.getAddress().getHostAddress(); byte[] reply = recvPacket.getData(); int len = recvPacket.getLength(); if (len == 12 && (reply[0] & 0xff) == TreeCommand.FIND_ROOT.cmd) { int port = reply[8]; port = port * 256 + reply[9]; port = port * 256 + reply[10]; port = port * 256 + reply[11]; TreeVncProtocol t = new TreeVncProtocol(hostname, port); t.findRootReply(vps.getRfb().getAcceptPort()); } else { if (receiverTask != null) { receiverTask.setReader(new Reader(new ByteArrayInputStream(reply))); if (receiverTask.getMessageId() == FRAMEBUFFER_UPDATE) { receiverTask.framebufferUpdateMessage(); } } } } } catch (Exception e) { System.out.println("tree-root-find-listener :" + e.getMessage()); } } public void run() { replyToRootSearchMulticast(); } // it looks like that we never stop public void setStopFlag(boolean stopFlag) { this.stopFlag = stopFlag; } public boolean isStopFlag() { return stopFlag; } }