view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 145:649794dfb9d5

add my hostname to handle multiple network
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 12 Jun 2014 22:01:05 +0900
parents 4547543ca73c
children 703db66138b5
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc;

import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

import com.glavsoft.exceptions.TransportException;
import com.glavsoft.rfb.protocol.ProtocolContext;
import com.glavsoft.transport.Reader;
import com.glavsoft.transport.Writer;

import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol;
import jp.ac.u_ryukyu.treevnc.server.VncProxyService;

public class TreeVncCommand {
    public VncProxyService viewer;
    MyRfbProto rfb;
    private TreeVncProtocol treeProtocol;

    public TreeVncCommand(VncProxyService viewer,MyRfbProto rfb, TreeVncProtocol treeProtocol) {
        this.viewer = viewer;
        this.rfb = rfb;
        this.treeProtocol = treeProtocol;
    }

    /**
     * handle TreeVNC Command
     * @param b   12byte header ( command 4byte, length 4byte, port 4byte, option String )
     * @param is
     * @param os
     * @param myHostName 
     * @throws TransportException 
     * @throws IOException 
     */
    void treeVncCommand(byte[] b, Reader is, Writer os, String myHostName) throws TransportException, IOException {
    	ByteBuffer buf = ByteBuffer.wrap(b);
        int command = buf.get()&0xff;  // make it unsigned
        buf.position(buf.position()+3);
        int length = buf.getInt();
        int port = buf.getInt();
        String hostname = null;
        if (length>4) {
             byte namebuf[] = new byte[length-4];
             try {
                is.readBytes(namebuf);
            } catch (TransportException e) {
                return;
            }
             hostname = new String(namebuf);
        }
    	switch (command) {
    	case ProtocolContext.FIND_ROOT_REPLY :
    		handleFindRootReply(port,hostname,myHostName);
    		break;
        case ProtocolContext.CONNECT_TO_AS_LEADER :
            handleConnectTo( port,hostname,true);
            break;
        case ProtocolContext.CONNECT_TO :
            handleConnectTo( port,hostname,false);
            break;
    	case ProtocolContext.FIND_ROOT :
    	    // this is a multicast message, cannot happen
    	    break;
        case ProtocolContext.WHERE_TO_CONNECT : 
            handleWhereToConnect(port,hostname);
            break;
        case ProtocolContext.LOST_PARENT :
            handleLostParent(port,hostname);
            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
     */
    void handleWhereToConnect(int port, String hostname) {
        viewer.replyCreateTree(hostname,port);
    }

    /**
     * set new parent address
     * @param port
     * @param hostname
     * @param leader
     * @throws IOException 
     * @throws SocketException 
     * @throws UnknownHostException 
     */
    void handleConnectTo(int port, String hostname, boolean leader) throws UnknownHostException, SocketException, IOException {
        if (rfb.isRoot()) {
            return; // we don't have parent
        }
        treeProtocol.connectToParenet(port, hostname,leader);
    }

    /**
     * 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) {
        viewer.fixLostParent(hostname,port);
    }

	public void setVncProtocol(TreeVncProtocol _echo) {
		this.treeProtocol = _echo;
	}

	public void setViewer(VncProxyService v) {
		this.viewer = v;
	}

}