Mercurial > hg > Members > riono > TreeVNC_ja_comment
view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 138:c3761c896607
minor fix
author | oc |
---|---|
date | Tue, 10 Jun 2014 13:15:59 +0900 |
parents | src/main/java/jp/ac/u_ryukyu/treevnc/TReeVNCCommand.java@41f049cebcb5 |
children | 4547543ca73c |
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 * @throws TransportException * @throws IOException */ void treeVncCommand(byte[] b, Reader is, Writer os) 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>12) { byte namebuf[] = new byte[length-12]; try { is.readBytes(namebuf); } catch (TransportException e) { return; } hostname = new String(namebuf); } switch (command) { case ProtocolContext.FIND_ROOT_REPLY : handleFindRootReply(port,hostname); 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; case ProtocolContext.NOT_FOUND_PARENT : handleNotFoundParent(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) { viewer.addHostToSelectionPanel(port, hostname); } /** * 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); } /** * Some parent is missing. Reorganize tree * @param port * @param hostname */ private void handleNotFoundParent(int port, String hostname) { treeProtocol.notfoundParent(hostname, port); } }