Mercurial > hg > Members > riono > TreeVNC_ja_comment
changeset 159:1c9f6acdfeb2
TreeCommand enum
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 14 Jun 2014 09:22:52 +0900 |
parents | 7ffb3fc44105 |
children | b9052986bbb3 |
files | src/main/java/com/glavsoft/rfb/protocol/ProtocolContext.java src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommandChannelListener.java src/main/java/jp/ac/u_ryukyu/treevnc/client/FindRoot.java src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeRootFinderListener.java src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java src/viewer_swing/java/com/glavsoft/viewer/Viewer.java |
diffstat | 8 files changed, 91 insertions(+), 58 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/com/glavsoft/rfb/protocol/ProtocolContext.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/main/java/com/glavsoft/rfb/protocol/ProtocolContext.java Sat Jun 14 09:22:52 2014 +0900 @@ -27,6 +27,7 @@ import com.glavsoft.rfb.IPasswordRetriever; import com.glavsoft.rfb.client.ClientToServerMessage; import com.glavsoft.rfb.encoding.PixelFormat; +import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import com.glavsoft.rfb.protocol.state.ProtocolState; import com.glavsoft.transport.Reader; import com.glavsoft.transport.Writer; @@ -84,14 +85,36 @@ * length : text * */ - public static final int FIND_ROOT = 220; - public static final int FIND_ROOT_REPLY = 221; - public static final int WHERE_TO_CONNECT = 222; - public static final int CONNECT_TO = 223; - public static final int CONNECT_TO_AS_LEADER = 224; - public static final int LOST_PARENT = 225; - public static final int NOT_FOUND_PARENT = 226; - public static final int NEW_NODE = 227; - - + public enum TreeCommand { + FIND_ROOT ( 220), + FIND_ROOT_REPLY (221), + WHERE_TO_CONNECT (222), + CONNECT_TO (223), + CONNECT_TO_AS_LEADER (224), + LOST_PARENT (225), + NOT_FOUND_PARENT (226), + NEW_NODE (227), + QUIT_LOOP (228); + + public final int cmd; + TreeCommand(int cmd) { + this.cmd = cmd; + } + + public static TreeCommand create(int i) { + switch (i) { + case 220: return FIND_ROOT ; + case 221: return FIND_ROOT_REPLY; + case 222: return WHERE_TO_CONNECT; + case 223: return CONNECT_TO ; + case 224: return CONNECT_TO_AS_LEADER ; + case 225: return LOST_PARENT ; + case 226: return NOT_FOUND_PARENT ; + case 227: return NEW_NODE ; + case 228: default: + return QUIT_LOOP; + } + } + } + }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java Sat Jun 14 09:22:52 2014 +0900 @@ -5,7 +5,7 @@ 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; @@ -16,10 +16,10 @@ String myHostName; Writer os; Reader is; - private int command; + private TreeCommand command ; private Socket connection; - public TreeVncCommand(MyRfbProto rfb,String myHostName, int command, int port, String hostname) { + public TreeVncCommand(MyRfbProto rfb,String myHostName, TreeCommand command, int port, String hostname) { this.rfb = rfb; this.myHostName = myHostName; this.hostname = hostname; @@ -27,7 +27,7 @@ this.command = command; } - public TreeVncCommand(MyRfbProto rfb, String myHostAddress, int newNode, + public TreeVncCommand(MyRfbProto rfb, String myHostAddress, TreeCommand newNode, Writer os, Reader is, Socket connection) { this.rfb = rfb; this.myHostName = myHostAddress; @@ -39,26 +39,29 @@ public void handleTreeVncCommand() { switch (command) { - case ProtocolContext.FIND_ROOT_REPLY : + case FIND_ROOT_REPLY : handleFindRootReply(port,hostname,myHostName); break; - case ProtocolContext.CONNECT_TO_AS_LEADER : + case CONNECT_TO_AS_LEADER : handleConnectTo( port,hostname,myHostName,true); break; - case ProtocolContext.CONNECT_TO : + case CONNECT_TO : handleConnectTo( port,hostname,myHostName,false); break; - case ProtocolContext.FIND_ROOT : + case FIND_ROOT : // this is a multicast message, it is handled in FindRootListener break; - case ProtocolContext.WHERE_TO_CONNECT : + case WHERE_TO_CONNECT : handleWhereToConnect(port,hostname, myHostName); break; - case ProtocolContext.LOST_PARENT : + case LOST_PARENT : handleLostParent(port,hostname); break; - case ProtocolContext.NEW_NODE : + case NEW_NODE : rfb.newClient(connection, os, is); + break; + case QUIT_LOOP: + break; default: System.out.println("unknown treeVNC command" + command); }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommandChannelListener.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommandChannelListener.java Sat Jun 14 09:22:52 2014 +0900 @@ -9,6 +9,7 @@ import com.glavsoft.exceptions.TransportException; import com.glavsoft.rfb.protocol.ProtocolContext; +import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import com.glavsoft.rfb.protocol.state.HandshakeState; import com.glavsoft.transport.Reader; import com.glavsoft.transport.Writer; @@ -18,25 +19,30 @@ byte[] imageBytes; int port; LinkedBlockingDeque<TreeVncCommand> cmdQueue = new LinkedBlockingDeque<TreeVncCommand>(); + private Thread acceptThread; protected final static String versionMsg_3_856 = "RFB 003.856\n"; public TreeVncCommandChannelListener(MyRfbProto _rfb, int p) { rfb = _rfb; port = p; + } - public void changeRfb(MyRfbProto _rfb) { - rfb = _rfb; - } - public void commandMainLoop() { - while(true) { - TreeVncCommand cmd = cmdQueue.poll(); - cmd.handleTreeVncCommand(); - } + acceptThread = new Thread(new Runnable() { + @Override + public void run() { + TreeVncCommand cmd; + do { + cmd = cmdQueue.poll(); + cmd.handleTreeVncCommand(); + } while (cmd!=null); + } + },"root-command-loop"); } public void run() { + commandMainLoop(); while (true) { try { final Socket newCli = rfb.accept(); @@ -93,7 +99,7 @@ sendSecResult(os); readClientInit(is); sendInitData(os); - return new TreeVncCommand(rfb, myHostAddress, ProtocolContext.NEW_NODE, os,is,connection); + return new TreeVncCommand(rfb, myHostAddress, TreeCommand.NEW_NODE, os,is,connection); } /** @@ -114,7 +120,7 @@ */ TreeVncCommand 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 + TreeCommand command = TreeCommand.create(buf.get()&0xff); // make it unsigned buf.position(buf.position()+3); int length = buf.getInt(); int port = buf.getInt(); @@ -146,7 +152,7 @@ reader.readBytes(b); - if ((b[0]&0xff)>=220) return b; // TreeVNC extention command. + if ((b[0]&0xff)>=220) return b; // TreeVNC extension command. if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') @@ -208,4 +214,12 @@ os.write(initData); } } + + public void waitForShutdown() { + if (acceptThread!=null) + try { + acceptThread.join(); + } catch (InterruptedException e) { + } + } }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/FindRoot.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/FindRoot.java Sat Jun 14 09:22:52 2014 +0900 @@ -12,6 +12,7 @@ import java.nio.ByteOrder; import com.glavsoft.rfb.protocol.ProtocolContext; +import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import com.glavsoft.viewer.swing.ConnectionParams; import jp.ac.u_ryukyu.treevnc.CreateConnectionParam; @@ -58,7 +59,7 @@ public void findRoot() { ByteBuffer buf = ByteBuffer.allocate(12); buf.order(ByteOrder.BIG_ENDIAN); - buf.put((byte) ProtocolContext.FIND_ROOT); + buf.put((byte) TreeCommand.FIND_ROOT.cmd); buf.put((byte) 0); buf.put((byte) 0); buf.put((byte) 0);
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java Sat Jun 14 09:22:52 2014 +0900 @@ -6,6 +6,7 @@ import java.nio.ByteOrder; import com.glavsoft.rfb.protocol.ProtocolContext; +import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import com.glavsoft.viewer.ViewerInterface; public class TreeVncProtocol { @@ -44,23 +45,23 @@ } public void findRootReply(int port) throws IOException { - sendWithHostAndPort(ProtocolContext.FIND_ROOT_REPLY, null, port); + sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port); } public void whereToConnect(String hostname, int port) throws IOException { - sendWithHostAndPort(ProtocolContext.WHERE_TO_CONNECT, hostname, port); + sendWithHostAndPort(TreeCommand.WHERE_TO_CONNECT, hostname, port); } public void connectTo(String hostname, int port,int leaderFlag) throws IOException{ - int command = leaderFlag == 1 ? ProtocolContext.CONNECT_TO_AS_LEADER : ProtocolContext.CONNECT_TO; + TreeCommand command = leaderFlag == 1 ? TreeCommand.CONNECT_TO_AS_LEADER : TreeCommand.CONNECT_TO; sendWithHostAndPort(command , hostname, port); } public void lostParent(String hostname, int port) throws IOException { - sendWithHostAndPort(ProtocolContext.LOST_PARENT, hostname, port); + sendWithHostAndPort(TreeCommand.LOST_PARENT, hostname, port); } - public void sendWithHostAndPort(int command, String hostname, int port) + public void sendWithHostAndPort(TreeCommand command, String hostname, int port) throws IOException { openport(); if (hostname == null) { @@ -72,7 +73,7 @@ if (cmdlen < 12) cmdlen=12; ByteBuffer buf = ByteBuffer.allocate(cmdlen); buf.order(ByteOrder.BIG_ENDIAN); - buf.put((byte) command); + buf.put((byte) command.cmd); buf.put((byte) 0); buf.put((byte) 0);
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeRootFinderListener.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeRootFinderListener.java Sat Jun 14 09:22:52 2014 +0900 @@ -7,8 +7,7 @@ import java.net.UnknownHostException; import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol; - -import com.glavsoft.rfb.protocol.ProtocolContext; +import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import com.glavsoft.viewer.swing.ConnectionParams; public class TreeRootFinderListener implements Runnable { @@ -41,7 +40,7 @@ if (len != 12) { continue; } - if ((reply[0]&0xff) != ProtocolContext.FIND_ROOT) { + if ((reply[0]&0xff) != TreeCommand.FIND_ROOT.cmd) { continue; } int port = 0;
--- a/src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java Sat Jun 14 09:22:52 2014 +0900 @@ -17,19 +17,13 @@ } private void vncStart(String[] args) { - Thread root = null; modeSelect(args); - if (treeProxy) { - final VncProxyService vps = new VncProxyService(); + VncProxyService vps = null; + if (treeProxy) { + vps = new VncProxyService(); vps.setNoConnection(noConnection); vps.proxyStart(args,width,height); if (!permitChangeScreen ) vps.getRfb().setPermitChangeScreen(false); - root = new Thread(new Runnable() { - @Override - public void run() { - vps.getAcceptThread().commandMainLoop(); - } - },"root-command-loop"); } ViewerInterface v; if (viewer) { @@ -40,11 +34,9 @@ v.startTreeViewer(); } } - if (root!=null) - try { - root.join(); - } catch (InterruptedException e) { - } + if (vps!=null && vps.getAcceptThread()!=null) { + vps.getAcceptThread().waitForShutdown(); + } } private void modeSelect(String[] args) {
--- a/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java Fri Jun 13 23:17:25 2014 +0900 +++ b/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java Sat Jun 14 09:22:52 2014 +0900 @@ -300,7 +300,7 @@ settings.setViewOnly(true); // too avoid unnecessary upward traffic cp.createConnectionParam(this); rfb.setViewer(this); - cp.getAcceptThread().commandMainLoop(); + cp.getAcceptThread().waitForShutdown(); } /** @@ -317,7 +317,7 @@ rfb.setViewer(this); myRfb = rfb; settings.setViewOnly(true); // too avoid unnecessary upward traffic - cp.getAcceptThread().commandMainLoop(); + cp.getAcceptThread().waitForShutdown(); }