changeset 29:57eb5575e6c4

MyRfbProto for client and proxy.
author one
date Sat, 01 Sep 2012 20:08:03 +0900
parents 18fad65bc447
children 0c08cdc4b572
files src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEESender.java src/main/java/com/glavsoft/rfb/protocol/TreeTask.java src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java src/main/java/jp/ac/u_ryukyu/treevnc/test/MyRfbProto.java
diffstat 4 files changed, 176 insertions(+), 123 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEESender.java	Sat Sep 01 19:24:07 2012 +0900
+++ b/src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEESender.java	Sat Sep 01 20:08:03 2012 +0900
@@ -1,6 +1,7 @@
 package com.glavsoft.rfb.encoding.decoder;
 
 import jp.ac.u_ryukyu.treevnc.server.MyRfbProtoProxy;
+import jp.ac.u_ryukyu.treevnc.test.MyRfbProto;
 import jp.ac.u_ryukyu.treevnc.client.MyRfbProtoClient;
 
 import com.glavsoft.drawing.Renderer;
@@ -10,9 +11,9 @@
 
 public class ZRLEESender extends Decoder {
 	
-	private MyRfbProtoProxy rfb;
+	private MyRfbProto rfb;
 
-	public ZRLEESender(MyRfbProtoProxy rfb) {
+	public ZRLEESender(MyRfbProto rfb) {
 		this.rfb = rfb;
 	}
 	
--- a/src/main/java/com/glavsoft/rfb/protocol/TreeTask.java	Sat Sep 01 19:24:07 2012 +0900
+++ b/src/main/java/com/glavsoft/rfb/protocol/TreeTask.java	Sat Sep 01 20:08:03 2012 +0900
@@ -2,6 +2,7 @@
 
 import jp.ac.u_ryukyu.treevnc.client.MyRfbProtoClient;
 import jp.ac.u_ryukyu.treevnc.server.MyRfbProtoProxy;
+import jp.ac.u_ryukyu.treevnc.test.MyRfbProto;
 
 import com.glavsoft.rfb.ClipboardController;
 import com.glavsoft.rfb.IRepaintController;
@@ -16,7 +17,7 @@
 
 	public TreeTask(Reader reader, IRepaintController repaintController,
 			ClipboardController clipboardController,
-			DecodersContainer decoders, ProtocolContext context, MyRfbProtoProxy rfb) {
+			DecodersContainer decoders, ProtocolContext context, MyRfbProto rfb) {
 		super(reader, repaintController, clipboardController, decoders, context);
 		//super(reader, new NullRepaintController(), clipboardController, decoders,context, true);
 		Decoder decoder = new ZRLEESender(rfb);
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java	Sat Sep 01 19:24:07 2012 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java	Sat Sep 01 20:08:03 2012 +0900
@@ -17,7 +17,6 @@
 import jp.ac.u_ryukyu.treevnc.MulticastQueue;
 import jp.ac.u_ryukyu.treevnc.test.MyRfbProto;
 
-
 import com.glavsoft.exceptions.TransportException;
 import com.glavsoft.rfb.encoding.EncodingType;
 import com.glavsoft.rfb.protocol.ProtocolContext;
@@ -29,12 +28,12 @@
 	final static int FramebufferUpdate = 0;
 	final static int CheckDelay = 11;
 	final static String versionMsg_3_855 = "RFB 003.855\n";
-	private static final int INFLATE_BUFSIZE = 1024*100;
+	private static final int INFLATE_BUFSIZE = 1024 * 100;
 	private Reader reader;
 	private EchoClient echoValue = new EchoClient();
-	private String host,treenum,parent,pHost,leaderflag;
-	private int echoPort,port,acceptPort;
-	Socket clientSocket,sock;
+	private String host, treenum, parent, pHost, leaderflag;
+	private int echoPort, port, acceptPort;
+	Socket clientSocket, sock;
 	DataInputStream is;
 	OutputStream os;
 	private ContainerManager containerManager;
@@ -45,34 +44,34 @@
 	boolean proxyFlag = false;
 	int serverMajor, serverMinor;
 	int clientMajor, clientMinor;
-	
+
 	private Inflater inflater = new Inflater();
 	private Deflater deflater = new Deflater();
-	
-	public MyRfbProtoClient(Reader reader,String host,String port) {
+
+	public MyRfbProtoClient(Reader reader, String host, String port) {
 		this.reader = reader;
 	}
-	
+
 	public void setParam(ConnectionParams connectionParams) {
 		pHost = connectionParams.hostName;
 		echoPort = connectionParams.portNumber;
 	}
-	
+
 	public boolean readProxyFlag() throws TransportException {
 		int flag = reader.readUInt8();
-		if(flag == 1)
+		if (flag == 1)
 			return true;
 		else
 			return false;
 	}
-	
+
 	public byte[] readEchoPort() throws Exception {
 		byte[] b = new byte[4];
 		reader.readBytes(b, 0, b.length);
-		//readFully(b);
+		// readFully(b);
 		return b;
 	}
-	
+
 	public void getParentName() {
 		if (echoValue == null) {
 
@@ -96,34 +95,34 @@
 		} else {
 			treenum = echoValue.treenum;
 		}
-		
+
 		if (echoValue.leaderflag != null) {
 			leaderflag = echoValue.leaderflag;
 		} else {
 			leaderflag = echoValue.leaderflag;
 		}
 	}
-	
+
 	int castByteInt(byte[] b) {
 		ByteBuffer bb = ByteBuffer.wrap(b);
 		int value = bb.getInt();
 		return value;
 	}
-	
+
 	Socket changeParent(String host, int port) throws IOException {
 		sock = new Socket(host, port);
 		return sock;
 	}
-	
+
 	public Socket accept() throws IOException {
 		return servSock.accept();
 	}
-	
+
 	void initServSock(int port) throws IOException {
 		servSock = new ServerSocket(port);
 		acceptPort = port;
 	}
-	
+
 	public void selectPort(int p) {
 		int port = p;
 		while (true) {
@@ -139,42 +138,50 @@
 		}
 		System.out.println("accept port = " + port);
 	}
-	
+
 	public void newClient(AcceptThread acceptThread, final Socket newCli,
 			final OutputStream os, final InputStream is) throws IOException {
 		// createBimgFlag = true;
 		// rfb.addSockTmp(newCli);
-		//		addSock(newCli);
-		final int myId = clients; 
-		final MulticastQueue.Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient();
+		// addSock(newCli);
+		final int myId = clients;
+		final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue
+				.newClient();
 		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.
+		 * 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 = 40000/8;
+				for (;;) {
+					long timeout = 40000 / 8;
 					try {
-						synchronized(this) {
-							int state,flag;
+						synchronized (this) {
+							int state, flag;
 							writerRunning.set(0);
 							wait(timeout);
 							flag = 0;
-							while((state=writerRunning.get())==0) {
+							while ((state = writerRunning.get()) == 0) {
 								c.poll(); // discard, should be timeout
 								count++;
-								if (flag==0) {
-									System.out.println("Discarding "+myId + " count="+ count); flag = 1;
+								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...
+								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);
+							if (flag == 1)
+								System.out.println("Resuming " + myId
+										+ " count=" + count);
+							if (state != 1) {
+								System.out.println("Client died " + myId);
 								break;
 							}
 						}
@@ -190,10 +197,11 @@
 		final Runnable reader = new Runnable() {
 			public void run() {
 				byte b[] = new byte[4096];
-				for(;;) {
+				for (;;) {
 					try {
 						int c = is.read(b);
-						if (c<=0) throw new IOException();
+						if (c <= 0)
+							throw new IOException();
 						// System.out.println("client read "+c);
 					} catch (IOException e) {
 						try {
@@ -215,46 +223,48 @@
 				writerRunning.set(1);
 				try {
 					/**
-					 *  initial connection of RFB protocol
+					 * initial connection of RFB protocol
 					 */
 					sendRfbVersion(os);
-//					readVersionMsg(is);
-					int rfbMinor = readVersionMsg(is,os);
+					// readVersionMsg(is);
+					int rfbMinor = readVersionMsg(is, os);
 					sendSecurityType(os);
 					readSecType(is);
 					sendSecResult(os);
 					readClientInit(is);
 					sendInitData(os);
-					new Thread(reader).start(); // discard incoming packet here after.
-					if(rfbMinor == 855){
-						//checkDilay(os);
+					new Thread(reader).start(); // discard incoming packet here
+												// after.
+					if (rfbMinor == 855) {
+						// checkDilay(os);
 						// send jpeg data of full screen.
 						// sendFullScreen("jpeg" ,os);
 					} else {
 						// send raw data of full screen.
-						
+
 					}
 					for (;;) {
 						LinkedList<ByteBuffer> bufs = c.poll();
 						int inputIndex = 0;
 						ByteBuffer header = bufs.get(inputIndex);
-						if (header==null) continue;
-						if (header.get(0)==CheckDelay) {
+						if (header == null)
+							continue;
+						if (header.get(0) == CheckDelay) {
 							System.out.println("--------------------");
-							//writeToClient(os, bufs, inputIndex);
+							// writeToClient(os, bufs, inputIndex);
 						}
-						if (header.get(0)==FramebufferUpdate) {
+						if (header.get(0) == FramebufferUpdate) {
 							// System.out.println("client "+ myId);
 						}
 						writeToClient(os, bufs, inputIndex);
-						writerRunning.set(1);  // 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) {
-						/* if socket closed 	cliList.remove(newCli); */
+						/* if socket closed cliList.remove(newCli); */
 					}
 				}
 			}
@@ -262,7 +272,7 @@
 			public void writeToClient(final OutputStream os,
 					LinkedList<ByteBuffer> bufs, int inputIndex)
 					throws IOException {
-				while(inputIndex < bufs.size()) {
+				while (inputIndex < bufs.size()) {
 					ByteBuffer b = bufs.get(inputIndex++);
 					os.write(b.array(), b.position(), b.limit());
 				}
@@ -273,11 +283,12 @@
 		new Thread(sender).start();
 
 	}
-	
+
 	void sendRfbVersion(OutputStream os) throws IOException {
 		os.write(versionMsg_3_855.getBytes());
-//		os.write(versionMsg_3_8.getBytes());
+		// os.write(versionMsg_3_8.getBytes());
 	}
+
 	int readVersionMsg(InputStream is, OutputStream os) throws IOException {
 
 		byte[] b = new byte[12];
@@ -298,16 +309,18 @@
 
 		if (rfbMajor < 3) {
 			throw new IOException(
-			"RFB server does not support protocol version 3");
+					"RFB server does not support protocol version 3");
 		}
 
 		if (rfbMinor == 855) {
 			sendProxyFlag(os);
-//			if(proxyFlag)sendPortNumber(os);
+			// if(proxyFlag)sendPortNumber(os);
 		}
 		return rfbMinor;
-		
-	}	void readVersionMsg(InputStream is) throws IOException {
+
+	}
+
+	void readVersionMsg(InputStream is) throws IOException {
 
 		byte[] b = new byte[12];
 
@@ -331,7 +344,7 @@
 		}
 
 	}
-	
+
 	void sendSecurityType(OutputStream os) throws IOException {
 		// number-of-security-types
 		os.write(1);
@@ -339,31 +352,34 @@
 		// 1:None
 		os.write(1);
 	}
+
 	void readSecType(InputStream is) throws IOException {
 		byte[] b = new byte[1];
 		is.read(b);
 
 	}
-	
+
 	void sendSecResult(OutputStream os) throws IOException {
 		byte[] b = castIntByte(0);
 		os.write(b);
 	}
-	
+
 	void readClientInit(InputStream in) throws IOException {
 		byte[] b = new byte[0];
 		in.read(b);
 	}
-	
+
 	void sendInitData(OutputStream os) throws IOException {
 		os.write(context.getInitData());
 	}
-	
+
 	void sendProxyFlag(OutputStream os) throws IOException {
-		if(proxyFlag) os.write(1);
-		else os.write(0);
+		if (proxyFlag)
+			os.write(1);
+		else
+			os.write(0);
 	}
-	
+
 	byte[] castIntByte(int len) {
 		byte[] b = new byte[4];
 		b[0] = (byte) ((len >>> 24) & 0xFF);
@@ -372,60 +388,67 @@
 		b[3] = (byte) ((len >>> 0) & 0xFF);
 		return b;
 	}
-	
+
 	/**
 	 * gzip byte arrays
+	 * 
 	 * @param deflater
 	 * @param inputs
 	 *            byte data[]
-	 * @param inputIndex 
+	 * @param inputIndex
 	 * @param outputs
 	 *            byte data[]
-	 * @return  byte length in last byte array
+	 * @return byte length in last byte array
 	 * @throws IOException
 	 */
-	public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
+	public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs,
+			int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
 		int len = 0;
-		ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE);
-		while(inputIndex < inputs.size() ) {
+		ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
+		while (inputIndex < inputs.size()) {
 			ByteBuffer b1 = inputs.get(inputIndex++);
-			deflater.setInput(b1.array(),b1.position(),b1.remaining());
+			deflater.setInput(b1.array(), b1.position(), b1.remaining());
 			/**
-			 * If we finish() stream and reset() it, Deflater start new gzip stream, this makes continuous zlib reader unhappy.
-			 * if we remove finish(), Deflater.deflate() never flushes its output. The original zlib deflate has flush flag. I'm pretty
-			 * sure this a kind of bug of Java library.
+			 * If we finish() stream and reset() it, Deflater start new gzip
+			 * stream, this makes continuous zlib reader unhappy. if we remove
+			 * finish(), Deflater.deflate() never flushes its output. The
+			 * original zlib deflate has flush flag. I'm pretty sure this a kind
+			 * of bug of Java library.
 			 */
-			if (inputIndex==inputs.size())	
+			if (inputIndex == inputs.size())
 				deflater.finish();
 			int len1 = 0;
 			do {
-				len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining());
-				if (len1>0) {
+				len1 = deflater.deflate(c1.array(), c1.position(),
+						c1.remaining());
+				if (len1 > 0) {
 					len += len1;
-					c1.position(c1.position()+len1); 
-					if (c1.remaining()==0) {
-						c1.flip();	outputs.addLast(c1);
+					c1.position(c1.position() + len1);
+					if (c1.remaining() == 0) {
+						c1.flip();
+						outputs.addLast(c1);
 						c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
 					}
 				}
-			} while (len1 >0 || !deflater.needsInput()); // &&!deflater.finished());
+			} while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished());
 		}
-		if (c1.position()!=0) {
-			c1.flip();	outputs.addLast(c1);
+		if (c1.position() != 0) {
+			c1.flip();
+			outputs.addLast(c1);
 		}
 		deflater.reset();
 		return len;
 	}
-	
-	
+
 	/**
 	 * gunzip byte arrays
+	 * 
 	 * @param inflater
 	 * @param inputs
 	 *            byte data[]
 	 * @param outputs
 	 *            byte data[]
-	 *@return  number of total bytes            
+	 * @return number of total bytes
 	 * @throws IOException
 	 */
 	public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs,
@@ -459,52 +482,75 @@
 		}
 		return len;
 	}
-	
-	void readSendData(int dataLen,Reader reader) throws IOException, DataFormatException, TransportException {
-		LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>();
+
+	public void readSendData(int dataLen, Reader reader)
+			throws TransportException {
+		LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
 		ByteBuffer header = ByteBuffer.allocate(16);
-		reader.readBytes(header.array(),0,16); 
+		reader.readBytes(header.array(), 0, 16);
 		header.limit(16);
-		if (header.get(0)==FramebufferUpdate) {
+		if (header.get(0) == FramebufferUpdate) {
 			int encoding = header.getInt(12);
-			if (encoding==EncodingType.ZRLE.getId()||encoding==EncodingType.ZLIB.getId()) { // ZRLEE is already recompressed
+			if (encoding == EncodingType.ZRLE.getId()
+					|| encoding == EncodingType.ZLIB.getId()) { // ZRLEE is
+																// already
+																// recompressed
 				ByteBuffer len = ByteBuffer.allocate(4);
-				reader.readBytes(len.array(),0,4); len.limit(4);
-				ByteBuffer inputData = ByteBuffer.allocate(dataLen-20);
-				reader.readBytes(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20);
-				LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>();
+				reader.readBytes(len.array(), 0, 4);
+				len.limit(4);
+				ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
+				reader.readBytes(inputData.array(), 0, inputData.capacity());
+				inputData.limit(dataLen - 20);
+				LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>();
 				inputs.add(inputData);
 
-				header.putInt(12, EncodingType.ZRLEE.getId()); // means recompress every time
-				// using new Deflecter every time is incompatible with the protocol, clients have to be modified.
+				header.putInt(12, EncodingType.ZRLEE.getId()); // means
+																// recompress
+																// every time
+				// using new Deflecter every time is incompatible with the
+				// protocol, clients have to be modified.
 				Deflater nDeflater = deflater; // new Deflater();
 				LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
-				unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE);
-				// dump32(inputs);
-				int len2 = zip(nDeflater, out, 0, bufs);
-				ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip();
-				bufs.addFirst(blen);
+				try {
+					unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE);
+					// dump32(inputs);
+					int len2 = zip(nDeflater, out, 0, bufs);
+					ByteBuffer blen = ByteBuffer.allocate(4);
+					blen.putInt(len2);
+					blen.flip();
+					bufs.addFirst(blen);
 
-				bufs.addFirst(header);
-				multicastqueue.put(bufs);
-				is.reset();
-				return ;
+					bufs.addFirst(header);
+					multicastqueue.put(bufs);
+					is.reset();
+				} catch (DataFormatException e) {
+					throw new TransportException(e);
+				} catch (IOException e) {
+					throw new TransportException(e);
+				}
+				return;
 			}
-		} 
+		}
 		bufs.add(header);
-		if (dataLen>16) {
-			ByteBuffer b = ByteBuffer.allocate(dataLen-16);
-			reader.readBytes(b.array(),0,dataLen-16); b.limit(dataLen-16);
+		if (dataLen > 16) {
+			ByteBuffer b = ByteBuffer.allocate(dataLen - 16);
+			reader.readBytes(b.array(), 0, dataLen - 16);
+			b.limit(dataLen - 16);
 			bufs.add(b);
 		}
 		multicastqueue.put(bufs);
-		is.reset();
+		try {
+			is.reset();
+		} catch (IOException e) {
+			throw new TransportException(e);
+		}
 
-		// It may be compressed. We can inflate here to avoid repeating clients decompressing here,
-		// but it may generate too many large data. It is better to do it in each client.
-		// But we have do inflation for all input data, so we have to do it here.
+		// It may be compressed. We can inflate here to avoid repeating clients
+		// decompressing here,
+		// but it may generate too many large data. It is better to do it in
+		// each client.
+		// But we have do inflation for all input data, so we have to do it
+		// here.
 	}
 
-
-	
 }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/test/MyRfbProto.java	Sat Sep 01 19:24:07 2012 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/test/MyRfbProto.java	Sat Sep 01 20:08:03 2012 +0900
@@ -273,4 +273,9 @@
     public void setProtocolContext(Protocol workingProtocol) {
         context = workingProtocol;
     }
+
+
+	public void readSendData(int dataLen, Reader reader) throws TransportException {
+		
+	}
 }