view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 157:7cea8789387b

thread base command listening loop
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 13 Jun 2014 23:12:28 +0900
parents e68dfd1972ac
children 1c9f6acdfeb2
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;
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 int command;
    private Socket connection;

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

    public TreeVncCommand(MyRfbProto rfb, String myHostAddress, int 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 ProtocolContext.FIND_ROOT_REPLY :
    		handleFindRootReply(port,hostname,myHostName);
    		break;
        case ProtocolContext.CONNECT_TO_AS_LEADER :
            handleConnectTo( port,hostname,myHostName,true);
            break;
        case ProtocolContext.CONNECT_TO :
            handleConnectTo( port,hostname,myHostName,false);
            break;
    	case ProtocolContext.FIND_ROOT :
    	    // this is a multicast message, it is handled in FindRootListener
    	    break;
        case ProtocolContext.WHERE_TO_CONNECT : 
            handleWhereToConnect(port,hostname, myHostName);
            break;
        case ProtocolContext.LOST_PARENT :
            handleLostParent(port,hostname);
            break;
        case ProtocolContext.NEW_NODE :
            rfb.newClient(connection, os, is);
    	    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.viewer.decideWhereToConnect(hostname,port,myHostName);
    }

    /**
     * set new parent address
     * @param port
     * @param hostname
     * @param leader
     * @throws IOException 
     * @throws SocketException 
     * @throws UnknownHostException 
     */
    void handleConnectTo(int port, String hostname, String myHostName, boolean leader) {
        if (rfb.isRoot()) {
            return; // we don't have parent
        }
        try {
            rfb.treeProtocol.connectToParenet(port, hostname, myHostName, leader);
        } 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.viewer.fixLostParent(hostname,port);
    }


}