changeset 345:47e351909227

add stop children sender when change server.
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Tue, 10 Feb 2015 18:18:49 +0900
parents f2921fd4ac96
children 89c41f9870ba 1f86df6504b7
files src/main/java/com/glavsoft/rfb/protocol/Protocol.java src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java src/viewer_swing/java/com/glavsoft/viewer/Viewer.java
diffstat 5 files changed, 46 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/com/glavsoft/rfb/protocol/Protocol.java	Tue Feb 10 11:27:21 2015 +0900
+++ b/src/main/java/com/glavsoft/rfb/protocol/Protocol.java	Tue Feb 10 18:18:49 2015 +0900
@@ -298,6 +298,7 @@
 
     public synchronized void cleanUpSession() {
         if (senderTask != null) {
+            // stop senderTask to VNCServer task.
             senderTask.stopTask(); 
             try {
                 senderThread.join();
--- a/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java	Tue Feb 10 11:27:21 2015 +0900
+++ b/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java	Tue Feb 10 18:18:49 2015 +0900
@@ -252,6 +252,7 @@
                     rfb.setCuiVersion(false);
                     boolean visible = true;
                     if (rect.getEncodingType() == EncodingType.INIT_DATA) {
+                        // VNCServer is changed, initiarize new screen.
                         int length = reader.readInt32();
                         byte[] initData = new byte[length];
                         reader.read(initData);
@@ -260,6 +261,8 @@
                         rfb.getContext().setInitData(initData);
                         repaintController.updateRemoteDesktopName(rfb.getContext());
                         reader.reset();
+                        // All children multicastqueue should be discarded hear.
+                        rfb.clearChildrenTransmission();
                         rfb.readSendData(length + 20, reader, null, rect); // size of UpdateRectangleMessage with initData.
                         short id = (short) rect.x;
                         visible = (id != rfb.getId());
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java	Tue Feb 10 11:27:21 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java	Tue Feb 10 18:18:49 2015 +0900
@@ -41,15 +41,6 @@
         echo.whereToConnect(myHostName,rfb.getAcceptPort());
     }
 
-    public void firstSendWhereToConnect(ViewerInterface v) {
-        try {
-            myHostName = InetAddress.getLocalHost().getHostAddress();
-        } catch (UnknownHostException e) {
-            e.printStackTrace();
-        }
-
-    }
-	
 	public synchronized void setHostName(String _hostName, int port, String _myHostName) {
 	    hostName = _hostName;
 	    portNumber = port;
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java	Tue Feb 10 11:27:21 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java	Tue Feb 10 18:18:49 2015 +0900
@@ -77,6 +77,7 @@
     private DatagramSocket socket = null;
 
     private byte[] originalInitData = null;
+    private boolean childrenMulticast = true;
 
     public TreeRFBProto(boolean isTreeManager, ViewerInterface viewer) {
         rThread = new RequestScreenThread(this);
@@ -109,9 +110,6 @@
      * @throws TransportException
      */
     public void newClient(final Socket newCli,final Writer os, final Reader is) {
-        if (clients >= 2) {
-            clients = 0;
-        }
         final int myId = clients;
         final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient();
         final AtomicInteger writerRunning = new AtomicInteger();
@@ -163,7 +161,7 @@
                                         myHostPort = rootHostPort;
                                     } catch (UnknownHostException e) {
                                         e.printStackTrace();
-                                        System.out.println("cannot be resolved into an root address");
+                                        System.out.println("cannot resolve localhost");
                                     }
                                 } else {
                                     rootHostName = getConnectionParam().getHostName();
@@ -171,7 +169,6 @@
                                     myHostName = getMyAddress();
                                     myHostPort = getAcceptPort();
                                 }
-
                                 TreeVncProtocol echo = new TreeVncProtocol(rootHostName, rootHostPort);
                                 clients = myId;
                                 echo.lostChild(myHostName, myHostPort, myId);
@@ -318,20 +315,27 @@
             public void run() {
                 writerRunning.set(1);
                 try {
-                    requestThreadNotify();
+                    requestThreadNotify(); // send full screen request via fullScreenRequestThread
                     
                     // after this, we discard upward packet.
                     new Thread(reader, "upward-packet-processing").start();
-                    
+
                     for (;;) {
                         LinkedList<ByteBuffer> bufs = c.poll();
-                        
                         int inputIndex = 0;
                         ByteBuffer header = bufs.get(inputIndex);
                         if (header == null)
                             continue;
                         writeToClient(os, bufs, inputIndex);
                         writerRunning.set(1); // yes my client is awaking.
+                        if (!childrenMulticast) {
+                            for (;;) {
+                                LinkedList<ByteBuffer> flag = c.poll();
+                                if (flag.size() == 0) {
+                                    break;
+                                }
+                            }
+                        }
                     }
                 } catch (Exception e) {
                     try {
@@ -347,12 +351,11 @@
             public void writeToClient(final Writer os,
                     LinkedList<ByteBuffer> bufs, int inputIndex)
                             throws TransportException {
-                while (inputIndex < bufs.size()) {
+                while (inputIndex < bufs.size() && childrenMulticast) {
                     ByteBuffer b = bufs.get(inputIndex++);
                     os.write(b.array(), b.position(), b.limit());
                 }
                 os.flush();
-                bufs = null;
                 multicastqueue.heapAvailable();
             }
         };
@@ -490,13 +493,9 @@
     }
 
     public synchronized void vncConnected(boolean ready) {
+        enableChildrenTransmission();
         if (ready && reconnecting) {
-            Socket vncSocket = viewer.getVNCSocket();
-            NetworkInterface ni = nets.getInterface(vncSocket);
-            if (ni!=null) {
-                vncInterface = ni.getName();
-                System.out.println("VNCNetworkInterface :" + vncInterface);
-            }
+            printNetworkInterface();
             sendDesktopSizeChange(reconnectingId);
             reconnecting = false;
             /*
@@ -511,7 +510,16 @@
                 notifyAll();
             }
         }
-    }	
+    }
+
+    private void printNetworkInterface() {
+        Socket vncSocket = viewer.getVNCSocket();
+        NetworkInterface ni = nets.getInterface(vncSocket);
+        if (ni!=null) {
+            vncInterface = ni.getName();
+            System.out.println("VNCNetworkInterface :" + vncInterface);
+        }
+    }
 
 
     public synchronized void waitForVNCConnection() {
@@ -568,13 +576,28 @@
     }
 
     public void stopReceiverTask() {
-        if (context!=null)
+        if (context!=null) {
             context.cleanUpSession(null);
+            context.getRfb().clearChildrenTransmission();
+        }
         // cleanup zlib decoder for new VNCServer
         if (isTreeManager())
             inflater = new Inflater();
     }
 
+    public void clearChildrenTransmission() {
+        // set discard flag
+        childrenMulticast = false;
+        // put gurd erement to restart multicastqueue.
+        multicastqueue.put(new LinkedList<ByteBuffer>());
+    }
+
+    public void enableChildrenTransmission() {
+        // a child senderTask may skip this flag
+        // durling write waiting. in this case, remaining bufferes are sent.
+        childrenMulticast = true;
+    }
+
     public String getMyAddress() {
         return myAddress;
     }
@@ -664,6 +687,7 @@
     }
 
     /**
+     * Multicast framebufferUpdate to children.
      * 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
--- a/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java	Tue Feb 10 11:27:21 2015 +0900
+++ b/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java	Tue Feb 10 18:18:49 2015 +0900
@@ -348,7 +348,6 @@
         } else {
             cp.findTreeVncRoot();
         }
-        cp.firstSendWhereToConnect(this);
         cp.sendWhereToConnect(this);
         isTreeVNC = true;
         myRfb =  rfb;