view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 423:ffe01c959cdd

Fix LostChild for root node
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Fri, 22 Jan 2016 07:51:57 +0900
parents 0bbfc23ef8c4
children f7210f834403
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, String intf) {
        this.rfb = rfb;
        this.myHostName = myHostAddress; 
        command = newNode;
        this.os = os;
        this.is = is;
        this.connection = connection;
        this.intf = intf;
    }




    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(os, is, intf);
                break;
            case QUIT_LOOP :
                break;
            case LOST_CHILD :
                handleLostChild(port, hostname, myHostName, this.value);
                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);
    }

    /**
     * Connect to parent.
     * @param port
     * @param hostname
     * @param leader
     * @param id 
     * @throws IOException 
     * @throws SocketException 
     * @throws UnknownHostException
     *
     * Even if this is a tree root,
     * connect to the node anyway.
     * This enables internet wide connection.
     */
    void handleConnectTo(int port, String hostname, String myHostName, boolean leader, short id) {
        rfb.stopReceiverTask();
        rfb.setId(id);
        rfb.setMyAddress(myHostName);
        rfb.setLeader(leader);
        if (rfb.isTreeManager()) {
            // markers tree root has a parent on a different network.
            rfb.setId((short) -1);
        }
        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;
    }


}