changeset 92:405ead268439

merge 81
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Thu, 22 Sep 2011 05:16:18 +0900
parents d89e03d99b7f (diff) d3e806372657 (current diff)
children 71bfbb5241bf
files src/myVncClient/CuiMyVncClient.java src/myVncClient/EchoClient.java src/myVncClient/MyVncClient.java
diffstat 6 files changed, 264 insertions(+), 124 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncClient/CuiMyVncClient.java	Sat Sep 10 04:04:09 2011 +0900
+++ b/src/myVncClient/CuiMyVncClient.java	Thu Sep 22 05:16:18 2011 +0900
@@ -316,22 +316,24 @@
 				+ "." + rfb.clientMinor);
 
 
-		if(rfb.serverMinor == 998) {
-			byte[] b = new byte[4];
-			b = rfb.readEchoPort();
-			echoPort = castByteInt(b);
+		if (rfb.serverMinor == 855) {
+			boolean useEchoPortFlag = rfb.readProxyFlag();
+			if (useEchoPortFlag) {
+				byte[] b = new byte[4];
+				b = rfb.readEchoPort();
+				echoPort = castByteInt(b);
 
-			InetAddress addr = InetAddress.getByName(pHost);
-			String h = new String(addr.getHostAddress());
-			
-			getParentName();
-			if(!(h.equals(host))) {
-				rfb.changeParent(host, port);
-				rfb.readVersionMsg();
-				rfb.writeVersionMsg();
+				InetAddress addr = InetAddress.getByName(pHost);
+				String h = new String(addr.getHostAddress());
+
+				getParentName();
+				if (!(h.equals(host))) {
+					rfb.changeParent(host, port);
+					rfb.readVersionMsg();
+					rfb.writeVersionMsg();
+				}
 			}
-		}
-		
+		}	
 		
 		int secType = rfb.negotiateSecurity();
 		int authType;
@@ -381,23 +383,24 @@
 		showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
 				+ "." + rfb.clientMinor);
 
-		if(rfb.serverMinor == 998) {
-			byte[] b = new byte[4];
-			b = rfb.readEchoPort();
-			echoPort = castByteInt(b);
+		if (rfb.serverMinor == 855) {
+			boolean useEchoPortFlag = rfb.readProxyFlag();
+			if (useEchoPortFlag) {
+				byte[] b = new byte[4];
+				b = rfb.readEchoPort();
+				echoPort = castByteInt(b);
 
-			String hs = host;
-			InetAddress addr = InetAddress.getByName(host);
-			String h = new String(addr.getHostAddress());
-			
-			getParentName();
-			if(!(h.equals(host))) {
-				rfb.changeParent(host, port);
-				rfb.readVersionMsg();
-				rfb.writeVersionMsg();
+				InetAddress addr = InetAddress.getByName(pHost);
+				String h = new String(addr.getHostAddress());
+
+				getParentName();
+				if (!(h.equals(host))) {
+					rfb.changeParent(host, port);
+					rfb.readVersionMsg();
+					rfb.writeVersionMsg();
+				}
 			}
-		}
-		
+		}	
 		
 		int secType = rfb.negotiateSecurity();
 		int authType;
@@ -1048,5 +1051,18 @@
 		int echoValue = bb.getInt();
 		return echoValue;
 	}
+	public Image getScreenImage() {
+		return vc.rawPixelsImage;
+	}
+
+	public void writeScreenData(byte[] b, String imageFormat) {
+/*
+		try{
+			vc.drawBufferedImage(b, imageFormat);
+		}catch(IOException e){
+			e.printStackTrace();
+		}
+*/
+	}
 	
 }
--- a/src/myVncClient/EchoClient.java	Sat Sep 10 04:04:09 2011 +0900
+++ b/src/myVncClient/EchoClient.java	Thu Sep 22 05:16:18 2011 +0900
@@ -193,7 +193,7 @@
 
 				if(!(checkRepetition.equals("skip")) || "1".equals(leaderflag)) {
 
-					Thread.sleep(1000);
+					//Thread.sleep(1000);
 					client.init();
 					client.setEchoValue(this);
 					client.getParentName();
--- a/src/myVncClient/InterfaceForViewer.java	Sat Sep 10 04:04:09 2011 +0900
+++ b/src/myVncClient/InterfaceForViewer.java	Thu Sep 22 05:16:18 2011 +0900
@@ -1,6 +1,6 @@
 package myVncClient;
 
-import java.awt.Graphics; 
+import java.awt.Image;
 import java.net.Socket;
 
 public interface InterfaceForViewer extends java.lang.Runnable{
@@ -26,5 +26,6 @@
 
 	public void setClientSocket(Socket sock);
 	public void close();
-	
+	public Image getScreenImage();
+	public void writeScreenData(byte[] b, String imageFormat);
 }
--- a/src/myVncClient/MyRfbProto.java	Sat Sep 10 04:04:09 2011 +0900
+++ b/src/myVncClient/MyRfbProto.java	Thu Sep 22 05:16:18 2011 +0900
@@ -16,7 +16,6 @@
 import java.net.BindException;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -35,11 +34,14 @@
 
 public
 class MyRfbProto extends RfbProto {
-	final static String versionMsg_3_998 = "RFB 003.998\n";
+	final static String versionMsg_3_855 = "RFB 003.855\n";
+
 	/**
-	 * CheckMillis is one of new msgType for RFB 3.998. 
+	 * CheckMillis is one of new msgType for RFB 3.855. 
 	 */
 	final static byte SpeedCheckMillis = 4;
+	final static byte WriteJpegData = 5;
+	
 	private static final int INFLATE_BUFSIZE = 1024*100;
 	boolean printStatusFlag = false;
 	long startCheckTime;
@@ -60,6 +62,8 @@
 	private LinkedList<Socket> cliListTmp;
 	private LinkedList<Socket> cliList;
 	boolean createBimgFlag;
+	boolean proxyFlag = false;
+	
 	//override
 	InterfaceForViewer viewer;
 
@@ -72,7 +76,7 @@
 	private int clients = 0;
 	private Inflater inflater = new Inflater();
 	private Deflater deflater = new Deflater();
-
+	
 	public
 	MyRfbProto() throws IOException {
 	}
@@ -86,7 +90,18 @@
 		// executor = Executors.newCachedThreadPool();
 		// executor = Executors.newSingleThreadExecutor();
 	}
-	
+
+	MyRfbProto(String h, int p, MyVncClient v) throws IOException {
+		super(h, p, v);
+		this.viewer = v;
+		cliList = new LinkedList<Socket>();
+		cliListTmp = new LinkedList<Socket>();
+		createBimgFlag = false;
+		proxyFlag = false;
+		//		sendThreads = new LinkedList<Thread>();
+		// executor = Executors.newCachedThreadPool();
+		// executor = Executors.newSingleThreadExecutor();
+	}
 	MyRfbProto(String h, int p) throws IOException {
 		super(h, p);
 		cliList = new LinkedList<Socket>();
@@ -98,34 +113,27 @@
 	}
 
 	
-	void readVersionMsg() throws Exception {
-
-		byte[] b = new byte[12];
-
-		readFully(b);
+	void sendProxyFlag(OutputStream os) throws IOException {
+		if(proxyFlag) os.write(1);
+		else os.write(0);
+	}
+	
+	boolean readProxyFlag() throws IOException{
+		int flag = readU8();
+		if(flag == 1)
+			return true;
+		else
+			return false;
+	}
 
-		if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
-				|| (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
-				|| (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
-				|| (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
-				|| (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
-			throw new Exception("Host " + host + " port " + port
-					+ " is not an RFB server");
-		}
-
-		serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
-		serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
-
-		if (serverMajor < 3) {
-			throw new Exception(
-					"RFB server does not support protocol version 3");
-		}
-
-		if (serverMinor == 998) {
-			
-		}
-		
+/*	
+	void sendPortNumber(OutputStream os) throws IOException {
+		byte[] b = new byte[4];
+		b = castIntByte(geth.port);
+		os.write(b);
 	}
+*/	
+	
 	byte[] readEchoPort() throws Exception {
 		byte[] b = new byte[4];
 		readFully(b);
@@ -152,15 +160,12 @@
 	// over write
 	void writeVersionMsg() throws IOException {
 		clientMajor = 3;
-		if (serverMinor >= 9) {
-			clientMinor = 9;
-			os.write(versionMsg_3_998.getBytes());
-		} else if (serverMajor > 3 || serverMinor >= 8) {
+		if (serverMinor == 855) {
+			clientMinor = 855;
+			os.write(versionMsg_3_855.getBytes());
+			} else if (serverMajor > 3 || serverMinor >= 8) {
 			clientMinor = 8;
 			os.write(versionMsg_3_8.getBytes());
-		} else if (serverMinor >= 9) {
-			clientMinor = 9;
-			os.write(versionMsg_3_998.getBytes());
 		} else if (serverMinor >= 7) {
 			clientMinor = 7;
 			os.write(versionMsg_3_7.getBytes());
@@ -266,11 +271,40 @@
 	}
 
 	void sendRfbVersion(OutputStream os) throws IOException {
-//		os.write(versionMsg_3_998.getBytes());
-		os.write(versionMsg_3_8.getBytes());
+		os.write(versionMsg_3_855.getBytes());
+//		os.write(versionMsg_3_8.getBytes());
 	}
 
-	void readVersionMsg(InputStream is) throws IOException {
+	int readVersionMsg(InputStream is, OutputStream os) throws IOException {
+
+		byte[] b = new byte[12];
+
+		is.read(b);
+
+		if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
+				|| (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
+				|| (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
+				|| (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
+				|| (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
+			throw new IOException("Host " + host + " port " + port
+					+ " is not an RFB server");
+		}
+
+		int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
+		int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
+
+		if (rfbMajor < 3) {
+			throw new IOException(
+			"RFB server does not support protocol version 3");
+		}
+
+		if (rfbMinor == 855) {
+			sendProxyFlag(os);
+//			if(proxyFlag)sendPortNumber(os);
+		}
+		return rfbMinor;
+		
+	}	void readVersionMsg(InputStream is) throws IOException {
 
 		byte[] b = new byte[12];
 
@@ -489,6 +523,46 @@
 		}
 	}
 	
+	void sendFullScreen(String imageFormat, OutputStream os) {
+		BufferedImage bimg = getBufferedImage(viewer.getScreenImage());
+		try {
+			byte[] b = getImageBytes(bimg, imageFormat);
+//			int len = b.length;
+			byte[] length = castIntByte(b.length);
+			System.out.println("jpeg length = " + b.length);
+
+			byte c = 0x05;
+			os.write((byte)WriteJpegData);
+			os.write(length); // length of jpeg data
+			os.write(b); // jpeg data
+			os.flush();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	void readWriteJpegData() throws IOException {
+		byte[] b = readJpegData();
+		viewer.writeScreenData(b, "jpeg");
+	}
+	
+	byte[] readJpegData() throws IOException {
+		int len = readU32();
+		byte[] b = new byte[len];
+		readFully(b);
+		return b;
+	}
+	
+	
+	public BufferedImage getBufferedImage(Image img) {
+		BufferedImage bimg = new BufferedImage(framebufferWidth, framebufferHeight, BufferedImage.TYPE_INT_RGB);
+		Graphics g = bimg.getGraphics();
+		g.drawImage(img, 0, 0, null);
+		g.dispose();
+		return bimg;
+	}
+
+	
 	void readSpeedCheck() throws IOException {
 		byte[] b = new byte[1];
 		readFully(b);
@@ -768,13 +842,21 @@
 					 *  initial connection of RFB protocol
 					 */
 					sendRfbVersion(os);
-					readVersionMsg(is);
+//					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){
+						// 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;
--- a/src/myVncClient/MyVncClient.java	Sat Sep 10 04:04:09 2011 +0900
+++ b/src/myVncClient/MyVncClient.java	Thu Sep 22 05:16:18 2011 +0900
@@ -2,14 +2,15 @@
 
 import java.awt.*;
 import java.awt.event.*;
+import java.awt.image.BufferedImage;
 import java.io.*;
 import java.net.*;
 import java.util.Random;
 
 import java.nio.ByteBuffer;
 
-public class MyVncClient extends VncViewer implements InterfaceForViewer, java.lang.Runnable,
-		WindowListener {
+public class MyVncClient extends VncViewer implements InterfaceForViewer,
+		java.lang.Runnable, WindowListener {
 
 	/**
 	 * 
@@ -51,7 +52,7 @@
 			port = Integer.parseInt(mainArgs[1]);
 		else
 			port = 5999;
-		
+
 		init();
 		start_threads();
 		start();
@@ -129,7 +130,7 @@
 				accThread = new Thread(new AcceptThread(rfb, 5999));
 				accThread.start();
 				first = false;
-			}else {
+			} else {
 				System.out.println("reConnectAndAuthenticate() ");
 				reConnectAndAuthenticate();
 				accThread = new Thread(new AcceptThread(rfb, 5999));
@@ -144,10 +145,10 @@
 			// vc.drawFirstImage();
 
 		} catch (IOException e) {
-			try{
+			try {
 				rfb.sock.close();
 
-			}catch(IOException e2){
+			} catch (IOException e2) {
 				e2.printStackTrace();
 			}
 			System.out.println("Socket error");
@@ -161,17 +162,17 @@
 			 */
 
 			int counter = 0;
-			 vncFrame.setVisible(false); 
-			 vncFrame.dispose();
-			 
+			vncFrame.setVisible(false);
+			vncFrame.dispose();
+
 			while (true) {
 				/**
 				 * if my last node case reconnectoion stop
 				 */
 
 				echoValue = new EchoClient(echoValue, this);
-				//echoValue = new EchoClient(echoValue);
-				
+				// echoValue = new EchoClient(echoValue);
+
 				try {
 					Thread.sleep(ran);
 				} catch (InterruptedException e1) {
@@ -190,7 +191,7 @@
 				}
 				counter++;
 			}
-			
+
 			// System.exit(0);
 		} catch (Exception e) {
 			System.out.println(e);
@@ -246,13 +247,13 @@
 			fatalError("Network error: could not connect to server: " + host
 					+ ":" + port, e);
 		} catch (EOFException e) {
-			
+
 			vncFrame.setVisible(false);
 			vncFrame.dispose();
 			// num4
-			if ( leaderflag != null) {
+			if (leaderflag != null) {
 				while (true) {
-					//echoValue = new EchoClient(echoValue, this);
+					// echoValue = new EchoClient(echoValue, this);
 					echoValue = new EchoClient(echoValue);
 					echoValue.openport();
 					// runflag = echo.losthost();
@@ -378,24 +379,26 @@
 		showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
 				+ "." + rfb.clientMinor);
 
-
-		if(rfb.serverMinor == 998) {
-			byte[] b = new byte[4];
-			b = rfb.readEchoPort();
-			echoPort = castByteInt(b);
+		if (rfb.serverMinor == 855) {
+			boolean useEchoPortFlag = rfb.readProxyFlag();
+			if (useEchoPortFlag) {
+				byte[] b = new byte[4];
+				b = rfb.readEchoPort();
+				echoPort = castByteInt(b);
 
-			InetAddress addr = InetAddress.getByName(pHost);
-			String h = new String(addr.getHostAddress());
-			
-			getParentName();
-			if(!(h.equals(host))) {
-				rfb.changeParent(host, port);
-				rfb.readVersionMsg();
-				rfb.writeVersionMsg();
+				InetAddress addr = InetAddress.getByName(pHost);
+				String h = new String(addr.getHostAddress());
+
+				getParentName();
+				if (!(h.equals(host))) {
+					rfb.changeParent(host, port);
+					rfb.readVersionMsg();
+					rfb.writeVersionMsg();
+					boolean flag = rfb.readProxyFlag();					
+				}
 			}
 		}
 
-		
 		int secType = rfb.negotiateSecurity();
 		int authType;
 		if (secType == RfbProto.SecTypeTight) {
@@ -434,7 +437,7 @@
 			vncFrame.setVisible(true);
 		} else {
 			validate();
-		}		
+		}
 
 		showConnectionStatus("Connecting to " + host + ", port " + port + "...");
 
@@ -450,24 +453,24 @@
 		showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
 				+ "." + rfb.clientMinor);
 
-
-		if(rfb.serverMinor == 998) {
-			byte[] b = new byte[4];
-			b = rfb.readEchoPort();
-			int echoPort = castByteInt(b);
+		if (rfb.serverMinor == 855) {
+			boolean useEchoPortFlag = rfb.readProxyFlag();
+			if (useEchoPortFlag) {
+				byte[] b = new byte[4];
+				b = rfb.readEchoPort();
+				echoPort = castByteInt(b);
 
-			InetAddress addr = InetAddress.getByName(host);
-			String h = new String(addr.getHostAddress());
-			
-			getParentName();
-			if(!(h.equals(host))) {
-				rfb.changeParent(host, port);
-				rfb.readVersionMsg();
-				rfb.writeVersionMsg();
+				InetAddress addr = InetAddress.getByName(pHost);
+				String h = new String(addr.getHostAddress());
+
+				getParentName();
+				if (!(h.equals(host))) {
+					rfb.changeParent(host, port);
+					rfb.readVersionMsg();
+					rfb.writeVersionMsg();
+				}
 			}
 		}
-		
-		
 		int secType = rfb.negotiateSecurity();
 		int authType;
 		if (secType == RfbProto.SecTypeTight) {
@@ -495,7 +498,8 @@
 		default:
 			throw new Exception("Unknown authentication scheme " + authType);
 		}
-	}	
+	}
+
 	//
 	// Show a message describing the connection status.
 	// To hide the connection status label, use (msg == null).
@@ -823,7 +827,7 @@
 		 * 
 		 * port = readIntParameter("PORT", 5550);
 		 */
-		
+
 		// Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
 		readPasswordParameters();
 
@@ -1027,7 +1031,7 @@
 			System.exit(1);
 		}
 	}
-	
+
 	//
 	// Show message text and optionally "Relogin" and "Close" buttons.
 	//
@@ -1138,7 +1142,7 @@
 
 	public void windowDeiconified(WindowEvent evt) {
 	}
-	
+
 	public void getParentName() {
 		if (echoValue == null) {
 
@@ -1177,6 +1181,7 @@
 	public void setEchoValue(EchoClient value) {
 		this.echoValue = value;
 	}
+
 	int castByteInt(byte[] b) {
 		ByteBuffer bb = ByteBuffer.wrap(b);
 		int value = bb.getInt();
@@ -1186,12 +1191,25 @@
 	public void setClientSocket(Socket sock) {
 		clientSocket = sock;
 	}
-	
+
 	public void close() {
 		rfb.close();
 		vncFrame.setVisible(false);
 		vncFrame.dispose();
 	}
 
+	@Override
+	public Image getScreenImage() {
+//		return vc.memImage;
+		return vc.rawPixelsImage;
+	}
+	
+	public void writeScreenData(byte[] b, String imageFormat) {
+		try{
+			vc.drawBufferedImage(b, imageFormat);
+		}catch(IOException e){
+			e.printStackTrace();
+		}
+	}
 
 }
--- a/src/myVncClient/VncCanvas.java	Sat Sep 10 04:04:09 2011 +0900
+++ b/src/myVncClient/VncCanvas.java	Thu Sep 22 05:16:18 2011 +0900
@@ -30,6 +30,10 @@
 import java.util.zip.*;
 import java.net.*;
 
+import javax.imageio.ImageIO;
+
+import test.TestComet;
+
 
 //
 // VncCanvas is a subclass of Canvas which draws a VNC desktop on it.
@@ -384,6 +388,8 @@
 
 		long count = 0;
 		
+//		new Thread(){public void run() {TestComet.main(null);}}.start();
+		
 		
 		while (true) {
 //			System.out.println("\ncount=" + count);
@@ -405,6 +411,13 @@
 
 			// Process the message depending on its type.
 			switch (msgType) {
+			case MyRfbProto.SpeedCheckMillis:
+				rfb.readSpeedCheck();
+				break;
+			case MyRfbProto.WriteJpegData:
+				rfb.readWriteJpegData();
+				
+				break;
 			case RfbProto.FramebufferUpdate:
 
 				if (statNumUpdates == viewer.debugStatsExcludeUpdates
@@ -1546,6 +1559,8 @@
 		memGraphics.setClip(x, y, w, h);
 		memGraphics.drawImage(rawPixelsImage, 0, 0, null);
 		memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight);
+
+//		test.TestComet.imageUpdate(rawPixelsImage, x, y, w, h);
 	}
 
 	//
@@ -1931,6 +1946,7 @@
 		memGraphics.drawImage( bimg, 0,0, null);
 		
 	}
+	
 
 	BufferedImage createBufferedImage(Image img){
 		BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB  );
@@ -1941,6 +1957,13 @@
 		return bimg;
 	}
 	
+	void drawBufferedImage(byte[] b, String imageFormat) throws IOException{
+		BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(b));
+//		ImageIO.write(bimg, "jpeg", new File("sample.jpeg"));
+		memGraphics.setClip(0,0, rfb.framebufferWidth, rfb.framebufferHeight );
+		memGraphics.drawImage( bimg, 0,0, null);
+		scheduleRepaint(0, 0, rfb.framebufferWidth, rfb.framebufferHeight );
+	}
 	
 	
 }