view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 188:f176bffcdc4a

add showTreeNode option.
author oc
date Tue, 24 Jun 2014 16:49:29 +0900
parents 2e1530139169
children a204b53a30c7
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand;
import com.glavsoft.transport.Reader;
import com.glavsoft.transport.Writer;

public class TreeVncCommand {
    MyRfbProto rfb;
    int port;
    String hostname;
    String myHostName;
    Writer os;
    Reader is;
    private TreeCommand command ;
    private Socket connection;
	private short value;

    public TreeVncCommand(MyRfbProto rfb,String myHostName, TreeCommand command, int port, String hostname, short value) {
        this.rfb = rfb;
        this.myHostName = myHostName;
        this.hostname = hostname;
        this.port = port;
        this.command = command;
        this.value = value;
    }

    public TreeVncCommand(MyRfbProto rfb, String myHostAddress, TreeCommand newNode,
            Writer os, Reader is, Socket connection) {
        this.rfb = rfb;
        this.myHostName = myHostAddress; 
        command = newNode;
        this.os = os;
        this.is = is;
        this.connection = connection;
    }

    public void handleTreeVncCommand()  {
        switch (command) {
    	case FIND_ROOT_REPLY :
    		handleFindRootReply(port,hostname,myHostName);
    		break;
        case CONNECT_TO_AS_LEADER :
            handleConnectTo( port,hostname,myHostName,true,this.value);
            break;
        case CONNECT_TO :
            handleConnectTo( port,hostname,myHostName,false,this.value);
            break;
    	case FIND_ROOT :
    	    // this is a multicast message, it is handled in FindRootListener
    	    break;
        case WHERE_TO_CONNECT : 
            handleWhereToConnect(port,hostname, myHostName);
            break;
        case LOST_PARENT :
            handleLostParent(port,hostname);
            break;
        case NEW_NODE :
            rfb.newClient(connection, os, is);
            break;
        case   QUIT_LOOP:
            break;
    	    default:
    	        System.out.println("unknown treeVNC command" + command);
    	}
    }

 /**
     * new clients ask root to where to connect
     * tell him his parent
     * @param port
     * @param hostname
     * @param myHostName 
     */
    void handleWhereToConnect(int port, String hostname, String myHostName) {
        rfb.getTreeManager().decideWhereToConnect(hostname,port,myHostName);
    }

    /**
     * set new parent address
     * @param port
     * @param hostname
     * @param leader
     * @param id 
     * @throws IOException 
     * @throws SocketException 
     * @throws UnknownHostException 
     */
    void handleConnectTo(int port, String hostname, String myHostName, boolean leader, short id) {
        if (rfb.isRoot()) {
            return; // we don't have parent
        }
        rfb.setId(id);
        rfb.setMyAddress(myHostName);
        rfb.setLeader(leader);
        try {
            rfb.getViewer().connectToParenet(port, hostname);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * Accept FIND_ROOT_REPLY
     *     add replying TreeVNC root to RootSelection Panel
     * @param port
     * @param hostname
     */
    void handleFindRootReply(int port, String hostname,String myHostname) {
    	rfb.addHostToSelectionPanel(port, hostname,myHostname);
    }
    
    /**
     * client node lost parent connection,  send reconnection message.
     * if root is not here, clients die themselves.
     * @param port
     * @param hostname
     */
    private void handleLostParent(int port, String hostname) {
        rfb.getTreeManager().fixLostParent(hostname,port);
    }
    
    public TreeCommand getCommand () {
    	return command;
    }


}