view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 231:0e0e6744432c

get new nodeList for checkDelay.
author oc
date Wed, 08 Oct 2014 17:44:39 +0900
parents 1badb2ce838f
children 1819e9c5d0cd
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 {
    TreeRFBProto rfb;
    int port;
    String hostname;
    String myHostName;
    Writer os;
    Reader is;
    private TreeCommand command ;
    private Socket connection;
    private short value;
    public String intf;

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

    public TreeVncCommand(TreeRFBProto 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, myHostName);
            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(intf).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.isTreeManager()) {
            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
     * @param myHostName 
     */
    private void handleLostParent(int port, String hostname, String myHostName) {
        rfb.getTreeManager(intf).fixLostParent(hostname,port,myHostName);
    }

    public TreeCommand getCommand () {
        return command;
    }


}