Mercurial > hg > Members > nobuyasu > tightVNCProxy
comparison 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 |
comparison
equal
deleted
inserted
replaced
181:f0dc6e6e85dc | 182:8a0e30e527e7 |
---|---|
1 package test; | |
2 | |
3 import java.io.IOException; | |
4 | |
5 public class VncClientTest implements java.lang.Runnable { | |
6 String host; | |
7 int port; | |
8 Rfb rfb; | |
9 Thread th; | |
10 | |
11 VncClientTest(String[] argv) { | |
12 host = argv[0]; | |
13 port = Integer.parseInt(argv[1]); | |
14 } | |
15 | |
16 public static void main(String[] argv) { | |
17 VncClientTest c = new VncClientTest(argv); | |
18 c.init(); | |
19 c.startThread(); | |
20 | |
21 } | |
22 | |
23 public void init() { | |
24 th = new Thread(this); | |
25 } | |
26 | |
27 public void startThread() { | |
28 th.start(); | |
29 } | |
30 | |
31 public void run() { | |
32 | |
33 try { | |
34 connectAndAuthenticate(); | |
35 doProtocolInitialisation(); | |
36 processNormalProtocol(); | |
37 } catch (Exception e) { | |
38 e.printStackTrace(); | |
39 } | |
40 | |
41 } | |
42 | |
43 void connectAndAuthenticate() throws Exception { | |
44 showConnectionStatus("Connecting to " + host + ", port " + port + "..."); | |
45 rfb = new Rfb(host, port); | |
46 showConnectionStatus("Connected server"); | |
47 | |
48 rfb.readVersionMsg(); | |
49 showConnectionStatus("RFB Server supports protocol version" | |
50 + rfb.serverMajor + "." + rfb.serverMinor); | |
51 | |
52 rfb.writeVersionMsg(); | |
53 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor | |
54 + "." + rfb.clientMinor); | |
55 | |
56 int authType = rfb.negotiateSecurity(); | |
57 showConnectionStatus("security type is " + authType); | |
58 | |
59 switch (authType) { | |
60 case Rfb.AuthAccess: | |
61 rfb.authenticationRequestAccess(); | |
62 System.out.println("authenticateion Request right of Access"); | |
63 break; | |
64 default: | |
65 throw new Exception("Unknown authentication scheme " + authType); | |
66 } | |
67 | |
68 } | |
69 | |
70 void doProtocolInitialisation() throws IOException { | |
71 rfb.writeClientInit(); | |
72 rfb.readServerInit(); | |
73 | |
74 System.out.println("Desktop name is " + rfb.desktopName); | |
75 System.out.println("Desktop size is " + rfb.framebufferWidth + " x " | |
76 + rfb.framebufferHeight); | |
77 setEncodings(); | |
78 | |
79 } | |
80 | |
81 void setEncodings() throws IOException { | |
82 int[] encodings = new int[2]; | |
83 int nEncodings = 0; | |
84 | |
85 encodings[nEncodings++] = Rfb.EncodingZRLE; | |
86 encodings[nEncodings++] = Rfb.EncodingRaw; | |
87 | |
88 rfb.writeSetEncodings(encodings, nEncodings); | |
89 | |
90 } | |
91 | |
92 void processNormalProtocol() { | |
93 | |
94 try { | |
95 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, | |
96 rfb.framebufferHeight, false); | |
97 | |
98 // main loop | |
99 while (true) { | |
100 | |
101 int msgType = rfb.readServerMessageType(); | |
102 System.out.println("msgType = " + msgType); | |
103 | |
104 switch (msgType) { | |
105 case Rfb.FramebufferUpdate: | |
106 rfb.readFramebufferUpdate(); | |
107 for (int i = 0; i < rfb.updateNRects; i++) { | |
108 | |
109 rfb.readFramebufferUpdateRectHdr(); | |
110 rfb.printFrameBufferUpdateRec(); | |
111 | |
112 switch (rfb.updateRectEncoding) { | |
113 case Rfb.EncodingRaw: | |
114 readRawEncodingData(); | |
115 break; | |
116 case Rfb.EncodingZRLE: | |
117 case Rfb.EncodingZRLEE: | |
118 readZRLEData(); | |
119 break; | |
120 default: | |
121 throw new Exception( | |
122 "Unknown RFB rectangle encoding " | |
123 + rfb.updateRectEncoding); | |
124 } | |
125 } | |
126 break; | |
127 default: | |
128 throw new Exception("Unknown RFB message type " + msgType); | |
129 } | |
130 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, | |
131 rfb.framebufferHeight, true); | |
132 } | |
133 | |
134 } catch (IOException e) { | |
135 e.printStackTrace(); | |
136 } catch (Exception e) { | |
137 e.printStackTrace(); | |
138 } | |
139 | |
140 } | |
141 | |
142 void readRawEncodingData() throws IOException { | |
143 int nBytes = rfb.updateRectW * rfb.updateRectH * 4 + 16; | |
144 byte[] b = new byte[nBytes]; | |
145 rfb.readFully(b); | |
146 | |
147 } | |
148 | |
149 void readZRLEData() throws IOException { | |
150 int nBytes = rfb.readU32(); | |
151 byte[] b = new byte[nBytes]; | |
152 rfb.readFully(b); | |
153 | |
154 } | |
155 | |
156 void showConnectionStatus(String msg) { | |
157 System.out.println(msg); | |
158 } | |
159 | |
160 } |