view src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java @ 454:432e2967eaab

All screen sharing request information is now in ConnectionPresenter only
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 22 Jun 2016 11:55:45 +0900
parents 9db8862ef200
children af958194248b
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc;

import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand;
import com.glavsoft.viewer.ViewerInterface;
import com.glavsoft.viewer.swing.ConnectionParams;

import java.io.IOException;
import java.net.*;

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 boolean stopFlag = false;
	private ViewerInterface vps;
	private MulticastSocket soc;
	private SecurityManager securityManager;

	public TreeRootFinderListener(ViewerInterface vncProxyService) {
		vps = vncProxyService;
	}

    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) {
					continue;
				}
				if ((reply[0]&0xff) != TreeCommand.FIND_ROOT.cmd) {
					continue;
				}
				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());
				if(stopFlag) break;
			}
		} 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;
	}
}