diff src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java @ 157:7cea8789387b

thread base command listening loop
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 13 Jun 2014 23:12:28 +0900
parents e68dfd1972ac
children 1c9f6acdfeb2
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java	Fri Jun 13 19:18:47 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommand.java	Fri Jun 13 23:12:28 2014 +0900
@@ -1,65 +1,44 @@
 package jp.ac.u_ryukyu.treevnc;
 
 import java.io.IOException;
+import java.net.Socket;
 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;
+    int port;
+    String hostname;
+    String myHostName;
+    Writer os;
+    Reader is;
+    private int command;
+    private Socket connection;
 
-    public TreeVncCommand(VncProxyService viewer,MyRfbProto rfb, TreeVncProtocol treeProtocol) {
-        this.viewer = viewer;
+    public TreeVncCommand(MyRfbProto rfb,String myHostName, int command, int port, String hostname) {
         this.rfb = rfb;
-        this.treeProtocol = treeProtocol;
+        this.myHostName = myHostName;
+        this.hostname = hostname;
+        this.port = port;
+        this.command = command;
     }
 
-    /**
-     * 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) {
+    public TreeVncCommand(MyRfbProto rfb, String myHostAddress, int newNode,
+            Writer os, Reader is, Socket connection) {
+        this.rfb = rfb;
+        this.myHostName = myHostAddress; 
+        command = newNode;
+        this.os = os;
+        this.is = is;
+        this.connection = connection;
+    }
+
+    public void handleTreeVncCommand()  {
+        switch (command) {
     	case ProtocolContext.FIND_ROOT_REPLY :
     		handleFindRootReply(port,hostname,myHostName);
     		break;
@@ -78,14 +57,13 @@
         case ProtocolContext.LOST_PARENT :
             handleLostParent(port,hostname);
             break;
+        case ProtocolContext.NEW_NODE :
+            rfb.newClient(connection, os, is);
     	    default:
     	        System.out.println("unknown treeVNC command" + command);
     	}
     }
 
-
-
-
  /**
      * new clients ask root to where to connect
      * tell him his parent
@@ -94,7 +72,7 @@
      * @param myHostName 
      */
     void handleWhereToConnect(int port, String hostname, String myHostName) {
-        viewer.decideWhereToConnect(hostname,port,myHostName);
+        rfb.viewer.decideWhereToConnect(hostname,port,myHostName);
     }
 
     /**
@@ -106,12 +84,15 @@
      * @throws SocketException 
      * @throws UnknownHostException 
      */
-    void handleConnectTo(int port, String hostname, String myHostName, boolean leader) 
-    		throws UnknownHostException, SocketException, IOException {
+    void handleConnectTo(int port, String hostname, String myHostName, boolean leader) {
         if (rfb.isRoot()) {
             return; // we don't have parent
         }
-        treeProtocol.connectToParenet(port, hostname, myHostName, leader);
+        try {
+            rfb.treeProtocol.connectToParenet(port, hostname, myHostName, leader);
+        } catch (IOException e) {
+            System.out.println(e.getMessage());
+        }
     }
 
     /**
@@ -131,15 +112,8 @@
      * @param hostname
      */
     private void handleLostParent(int port, String hostname) {
-        viewer.fixLostParent(hostname,port);
+        rfb.viewer.fixLostParent(hostname,port);
     }
 
-	public void setVncProtocol(TreeVncProtocol _echo) {
-		this.treeProtocol = _echo;
-	}
-
-	public void setViewer(VncProxyService v) {
-		this.viewer = v;
-	}
 
 }
\ No newline at end of file