view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 329:230038d5127d

change reconnect new node instead of lost child node.
author oc
date Mon, 02 Feb 2015 20:18:53 +0900
parents 1a2ab6bd5ba3
children dff2f92ae3ff
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.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;
            case LOST_LEFT_CHILD :
                handleLostChild(port, hostname, myHostName, 0);
                break;
            case LOST_RIGHT_CHILD :
                handleLostChild(port, hostname, myHostName, 1);
                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.stopReceiverTask();
        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 void handleLostChild(int port, String hostname, String myHostName, int clientId) {
        rfb.getTreeManager(intf).fixLostChild(hostname, port, myHostName, clientId);
    }


    public TreeCommand getCommand () {
        return command;
    }


}