view src/test/VncClientTest.java @ 182:8a0e30e527e7

add test/Rfb.java test/VncClient.java test/VncServer.java
author e085711
date Tue, 25 Oct 2011 04:51:23 +0900
parents
children
line wrap: on
line source

package test;

import java.io.IOException;

public class VncClientTest implements java.lang.Runnable {
	String host;
	int port;
	Rfb rfb;
	Thread th;

	VncClientTest(String[] argv) {
		host = argv[0];
		port = Integer.parseInt(argv[1]);
	}

	public static void main(String[] argv) {
		VncClientTest c = new VncClientTest(argv);
		c.init();
		c.startThread();

	}

	public void init() {
		th = new Thread(this);
	}

	public void startThread() {
		th.start();
	}

	public void run() {

		try {
			connectAndAuthenticate();
			doProtocolInitialisation();
			processNormalProtocol();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	void connectAndAuthenticate() throws Exception {
		showConnectionStatus("Connecting to " + host + ", port " + port + "...");
		rfb = new Rfb(host, port);
		showConnectionStatus("Connected server");

		rfb.readVersionMsg();
		showConnectionStatus("RFB Server supports protocol version"
				+ rfb.serverMajor + "." + rfb.serverMinor);

		rfb.writeVersionMsg();
		showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
				+ "." + rfb.clientMinor);

		int authType = rfb.negotiateSecurity();
		showConnectionStatus("security type is " + authType);

		switch (authType) {
		case Rfb.AuthAccess:
			rfb.authenticationRequestAccess();
			System.out.println("authenticateion Request right of Access");
			break;
		default:
			throw new Exception("Unknown authentication scheme " + authType);
		}

	}

	void doProtocolInitialisation() throws IOException {
		rfb.writeClientInit();
		rfb.readServerInit();

		System.out.println("Desktop name is " + rfb.desktopName);
		System.out.println("Desktop size is " + rfb.framebufferWidth + " x "
				+ rfb.framebufferHeight);
		setEncodings();

	}

	void setEncodings() throws IOException {
		int[] encodings = new int[2];
		int nEncodings = 0;

		encodings[nEncodings++] = Rfb.EncodingZRLE;
		encodings[nEncodings++] = Rfb.EncodingRaw;

		rfb.writeSetEncodings(encodings, nEncodings);

	}

	void processNormalProtocol() {

		try {
			rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
					rfb.framebufferHeight, false);

			// main loop
			while (true) {

				int msgType = rfb.readServerMessageType();
				System.out.println("msgType = " + msgType);

				switch (msgType) {
				case Rfb.FramebufferUpdate:
					rfb.readFramebufferUpdate();
					for (int i = 0; i < rfb.updateNRects; i++) {

						rfb.readFramebufferUpdateRectHdr();
						rfb.printFrameBufferUpdateRec();

						switch (rfb.updateRectEncoding) {
						case Rfb.EncodingRaw:
							readRawEncodingData();
							break;
						case Rfb.EncodingZRLE:
						case Rfb.EncodingZRLEE:
							readZRLEData();
							break;
						default:
							throw new Exception(
									"Unknown RFB rectangle encoding "
											+ rfb.updateRectEncoding);
						}
					}
					break;
				default:
					throw new Exception("Unknown RFB message type " + msgType);
				}
				rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
						rfb.framebufferHeight, true);			
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	void readRawEncodingData() throws IOException {
		int nBytes = rfb.updateRectW * rfb.updateRectH * 4 + 16;
		byte[] b = new byte[nBytes];
		rfb.readFully(b);

	}

	void readZRLEData() throws IOException {
		int nBytes = rfb.readU32();
		byte[] b = new byte[nBytes];
		rfb.readFully(b);

	}

	void showConnectionStatus(String msg) {
		System.out.println(msg);
	}

}