changeset 113:8424f64dd736

time out and discarding. kill time out thread after client death.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 05 Aug 2011 14:33:42 +0900
parents 1a5afcf100a5
children 02016fcb9105
files src/myVncProxy/MyRfbProto.java src/myVncProxy/ProxyVncCanvas.java
diffstat 2 files changed, 27 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Fri Aug 05 12:45:06 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Fri Aug 05 14:33:42 2011 +0900
@@ -27,6 +27,7 @@
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.zip.DataFormatException;
 import java.util.zip.Deflater;
 import java.util.zip.Inflater;
@@ -310,7 +311,7 @@
 		rectW = readU16();           //  8
 		rectH = readU16();           //  10
 		encoding = readU32();      //   12
-		System.out.println("encoding = "+encoding);
+		// System.out.println("encoding = "+encoding);
 		if (encoding == EncodingZRLE|| encoding==EncodingZRLEE||encoding==EncodingZlib)
 			zLen = readU32();
 		else
@@ -453,9 +454,6 @@
 		System.out.println("checkMillis: " + time);
 	}
 
-	void printStatus() {
-		System.out.println();
-	}
 
 	synchronized void changeStatusFlag() {
 		printStatusFlag = true;
@@ -642,23 +640,35 @@
 		//		addSock(newCli);
 		final int myId = clients; 
 		final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient();
-		final AtomicBoolean writerRunning = new AtomicBoolean();
+		final AtomicInteger writerRunning = new AtomicInteger();
+		writerRunning.set(1);
 		/**
 		 * Timeout thread. If a client is suspended, it has top of queue indefinitely, which caused memory
 		 * overflow. After the timeout, we poll the queue and discard it. Start long wait if writer is running.
 		 */
 		final Runnable timer = new Runnable() {
 			public void run() {
+				int count = 0;
 				for(;;) {
-					long timeout = 30000;
+					long timeout = 30000/4;
 					try {
 						synchronized(this) {
+							int state,flag;
+							writerRunning.set(0);
 							wait(timeout);
-							writerRunning.set(false);
-							while (!writerRunning.get()) {
+							flag = 0;
+							while((state=writerRunning.get())==0) {
 								c.poll(); // discard, should be timeout
-								System.out.println("Discarded "+myId);
-								wait(10); // if this is too short, writer cannot take the poll, if this is too long, memory will overflow... 
+								count++;
+								if (flag==0) {
+									System.out.println("Discarding "+myId + " count="+ count); flag = 1;
+								}
+								wait(10); // if this is too short, writer cannot take the poll, if this is too long, memory will overflow...
+							}
+							if (flag==1) System.out.println("Resuming "+myId + " count="+count);
+							if (state!=1) {
+								System.out.println("Client died "+myId);
+								break;
 							}
 						}
 					} catch (InterruptedException e) {
@@ -677,9 +687,10 @@
 					try {
 						int c = is.read(b);
 						if (c<=0) throw new IOException();
-						System.out.println("client read "+c);
+						// System.out.println("client read "+c);
 					} catch (IOException e) {
 						try {
+							writerRunning.set(2);
 							os.close();
 							is.close();
 						} catch (IOException e1) {
@@ -694,6 +705,7 @@
 		 */
 		Runnable sender = new Runnable() {
 			public void run() {
+				writerRunning.set(1);
 				try {
 					/**
 					 *  initial connection of RFB protocol
@@ -712,13 +724,14 @@
 						ByteBuffer header = bufs.get(inputIndex);
 						if (header==null) continue;
 						if (header.get(0)==RfbProto.FramebufferUpdate) {
-							System.out.println("client "+ myId);
+							// System.out.println("client "+ myId);
 						}
 						writeToClient(os, bufs, inputIndex);
-						writerRunning.set(true);  // yes my client is awaking.
+						writerRunning.set(1);  // yes my client is awaking.
 					}
 				} catch (IOException e) {
 					try {
+						writerRunning.set(2);
 						os.close();
 					} catch (IOException e1) {
 					}
--- a/src/myVncProxy/ProxyVncCanvas.java	Fri Aug 05 12:45:06 2011 +0900
+++ b/src/myVncProxy/ProxyVncCanvas.java	Fri Aug 05 14:33:42 2011 +0900
@@ -363,7 +363,7 @@
 		
 		long count = 0;
 		while (true) {
-			System.out.println("\ncount=" + count);
+			// System.out.println("\ncount=" + count);
 			
 			count++;