view src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java @ 326:1d4d5055a288

add error message, add assure stream close.
author oc
date Sun, 01 Feb 2015 15:30:17 +0900
parents b31903e5b02d
children 293c35aa902b
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc;

import java.io.IOException;

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

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

	public synchronized void findTreeVncRoot() throws InterruptedException {
		rfb.createRootSelectionPanel(this);
		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 sendWhereToConnect(ViewerInterface v) {
        rfb.setConnectionParam(this);
		TreeVncProtocol echo = new TreeVncProtocol(hostName,portNumber);
		try {
            echo.whereToConnect(myHostName,rfb.getAcceptPort());
        } catch (IOException e) {
            System.out.println("sendWhereToConnect : cannot connect to root "+e.getMessage());
        }
	}
	
	public synchronized void setHostName(String _hostName, int port, String _myHostName) {
	    hostName = _hostName;
	    portNumber = port;
	    myHostName = _myHostName;
	    notify();
	}

	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;
	    // who sets myHostName?
	}

	public String getMyHostName() {
	    return myHostName;
	}

    public int getPort() {
        return portNumber;
    }

    public String getHostName() {
        return hostName;
    }

}