diff src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 135:ada4d850a820

lostParent and notFoundParenet
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 07 Jun 2014 19:54:27 +0900
parents 128cce60c43c
children c3761c896607
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sat Jun 07 15:57:03 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sat Jun 07 19:54:27 2014 +0900
@@ -5,8 +5,6 @@
 import java.net.BindException;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.net.SocketException;
-import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.LinkedList;
@@ -39,18 +37,19 @@
 	public MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
 	private RequestScreenThread rThread;
 	private boolean proxyFlag = true;
-	public TreeVncProtocol treeProtocol;
 	public int acceptPort = 0;
 	protected boolean readyReconnect = false;
 	private boolean cuiVersion;
 	private long counter = 0; // packet serial number
-	private VncProxyService viewer = null;
+    TreeVncProtocol treeProtocol;
+	public TReeVNCCommand treeVncCommand;
     public ServerSocket servSock;
     private boolean permitChangeScreen = true;
     private static final int INFLATE_BUFSIZE = 1024 * 100;
 
     private Inflater inflater = new Inflater();
     private Deflater deflater = new Deflater();
+    private VncProxyService viewer;
 
 
 	public MyRfbProto() {
@@ -58,7 +57,8 @@
 	}
 	
 	public void setVncProxy(VncProxyService viewer) {
-		this.viewer = viewer;
+	    this.viewer = viewer;
+	    treeVncCommand = new TReeVNCCommand(viewer,this,treeProtocol);
 	}
 
 	abstract public boolean isRoot() ;
@@ -71,7 +71,7 @@
 		sendRfbVersion(os);
 		byte[] b;
 		if ((b = readVersionMsg(is, os))!=null) {
-			treeVncCommand(b,is,os);
+			treeVncCommand.treeVncCommand(b,is,os);
 			return true;
 		}
 		sendSecurityType(os);
@@ -83,86 +83,6 @@
 	}
 	
 	/**
-	 * handle TreeVNC Command
-	 * @param b   12byte header ( command 4byte, length 4byte, port 4byte, option String )
-	 * @param is
-	 * @param os
-	 * @throws TransportException 
-	 * @throws IOException 
-	 */
-	private void treeVncCommand(byte[] b, Reader is, Writer os) throws TransportException, IOException {
-		ByteBuffer buf = ByteBuffer.wrap(b);
-        int command = buf.get()&0xff;
-        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);
-		case ProtocolContext.LOST_PARENT :
-			System.out.println("get treeVNC command" + command);
-		}
-	}
-
-	/**
-	 * new clients ask root to where to connect
-	 * tell him his parent
-	 * @param port
-	 * @param hostname
-	 */
-	private 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 
-	 */
-	private void handleConnectTo(int port, String hostname, boolean leader) throws UnknownHostException, SocketException, IOException {
-        if (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
-	 */
-	private void handleFindRootReply(int port, String hostname) {
-		viewer.addHostToSelectionPanel(port, hostname);
-	}
-
-	/**
 	 * handle new client accept 
 	 *     it also handle TreeVNC Command
 	 * @param acceptThread
@@ -235,9 +155,10 @@
 		};
 		new Thread(timer, "timer-discard-multicastqueue").start();
 		/**
-		 * send all incoming from clients to parent.
+		 * handle command from lower node
 		 */
 		final Runnable reader = new Runnable() {
+
             public void run() {
 				for (;;) {
 					try {
@@ -291,7 +212,7 @@
 
 		};
 		/**
-		 * send packets to a client
+		 * send packets to a client (one thread for each client )
 		 */
 		Runnable sender = new Runnable() {
 			public void run() {
@@ -301,8 +222,7 @@
 
 					// after this, we discard upward packet.
 					new Thread(reader, "upward-packet-processing").start(); 
-					// writeFramebufferUpdateRequest(0,0, framebufferWidth,
-					// framebufferHeight, false );
+
 					for (;;) {
 						LinkedList<ByteBuffer> bufs = c.poll();
 						int inputIndex = 0;
@@ -703,6 +623,13 @@
         return len;
     }
 
+    /**
+     * read FrameBuffferUpdate. If it is ZLE, make it ZLEE which is self contained compressed packet.
+     * put the packet to the multicastqueue. Then normal rendering engine read the same stream using is.reset().
+     * @param dataLen
+     * @param reader
+     * @throws TransportException
+     */
     public void readSendData(int dataLen, Reader reader)
             throws TransportException {
         LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();