view src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java @ 142:d2af9edb1803

fix selectRfbSelectPort.
author oc
date Thu, 12 Jun 2014 18:04:22 +0900
parents 1fa40e04f099
children 4547543ca73c
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc;

import java.io.IOException;

import com.glavsoft.viewer.ViewerImpl;
import com.glavsoft.viewer.swing.ConnectionParams;

import jp.ac.u_ryukyu.treevnc.client.FindRoot;
import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol;

public class CreateConnectionParam {
	private String hostName;
	private int portNumber = ConnectionParams.DEFAULT_VNC_ROOT;
	private MyRfbProto rfb;
	
	public CreateConnectionParam(MyRfbProto rfb) {
		this.rfb = rfb;
	}

	public synchronized void findTreeVncRoot() throws InterruptedException {
		FindRoot getBcast = new FindRoot(rfb.acceptPort,this);
		getBcast.findRoot();
		// wait for RootSelection
		wait();
	}

	/**
	 * To find parent, send WHERE_TO_CONNECT command to the TreeVNC root
	 * Incoming CONNECT_TO message is handled in MyRFBProto
	 * @param v
	 */
	public void createConnectionParam(ViewerImpl v) {
		TreeVncProtocol echo = new TreeVncProtocol(hostName,portNumber);
		rfb.setEcho(echo);
		try {
            echo.whereToConnect(rfb.getMyAddress(),rfb.getAcceptPort());
        } catch (IOException e) {
            // cannot send where to connect
        }
	}
	
	public void runAcceptThread() {
		rfb.selectPort(ConnectionParams.DEFAULT_VNC_ROOT);
		AcceptThread acceptThread = new AcceptThread(rfb, rfb.getAcceptPort());
		portNumber = rfb.getAcceptPort();
		Thread accThread = new Thread(acceptThread);
		accThread.start();
	}

	public void setHostName(String _hostName, int port) {
	    hostName = _hostName;
	    portNumber = port;
	}

	public void setHostName(String hostAndPort) {
	    int i = hostAndPort.indexOf(':'); 
	    if (i>0) {
	        portNumber = Integer.parseInt(hostAndPort.substring(i+1));
	        hostName = hostAndPort.substring(0,i);
	    } else  
	        hostName = hostAndPort;
	}

}