Mercurial > hg > Members > riono > TreeVNC_ja_comment
view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 153:e68dfd1972ac
fix bad names.
author | oc |
---|---|
date | Fri, 13 Jun 2014 18:04:42 +0900 |
parents | 4096a8408fdb |
children | 7cea8789387b |
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 byte [] command * @param is * @param os * @param myHostName * @throws TransportException * @throws IOException * * TreeVNC Command is sent as a possible replied version message. 12 bytes is already read. * Command 4 byte (including padding) * lenght 4 byte * port 4 byte * rest of data ( add padding if it is shorter than 12 byte) */ 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) { if (length>1024) { System.out.println("Too long TreeVncCommand "); return; } 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,myHostName,true); break; case ProtocolContext.CONNECT_TO : handleConnectTo( port,hostname,myHostName,false); break; case ProtocolContext.FIND_ROOT : // this is a multicast message, it is handled in FindRootListener break; case ProtocolContext.WHERE_TO_CONNECT : handleWhereToConnect(port,hostname, myHostName); 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 * @param myHostName */ void handleWhereToConnect(int port, String hostname, String myHostName) { viewer.decideWhereToConnect(hostname,port,myHostName); } /** * set new parent address * @param port * @param hostname * @param leader * @throws IOException * @throws SocketException * @throws UnknownHostException */ void handleConnectTo(int port, String hostname, String myHostName, boolean leader) throws UnknownHostException, SocketException, IOException { if (rfb.isRoot()) { return; // we don't have parent } treeProtocol.connectToParenet(port, hostname, myHostName, 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; } }