17
|
1 package myVncClient;
|
0
|
2
|
4
|
3 import java.awt.*;
|
|
4 import java.awt.event.*;
|
|
5 import java.io.*;
|
|
6 import java.net.*;
|
0
|
7
|
4
|
8 public class VncViewer extends java.applet.Applet implements
|
|
9 java.lang.Runnable, WindowListener {
|
0
|
10
|
4
|
11 boolean inAnApplet = true;
|
|
12 boolean inSeparateFrame = false;
|
0
|
13
|
4
|
14 //
|
|
15 // main() is called when run as a java program from the command line.
|
|
16 // It simply runs the applet inside a newly-created frame.
|
|
17 //
|
0
|
18
|
4
|
19 public static void main(String[] argv) {
|
|
20 VncViewer v = new VncViewer();
|
|
21 v.mainArgs = argv;
|
|
22 v.inAnApplet = false;
|
|
23 v.inSeparateFrame = true;
|
18
|
24
|
4
|
25 v.init();
|
|
26 v.start();
|
|
27 }
|
0
|
28
|
4
|
29 String[] mainArgs;
|
|
30
|
15
|
31 MyRfbProto rfb;
|
4
|
32 Thread rfbThread;
|
0
|
33
|
4
|
34 Frame vncFrame;
|
|
35 Container vncContainer;
|
|
36 ScrollPane desktopScrollPane;
|
|
37 GridBagLayout gridbag;
|
|
38 ButtonPanel buttonPanel;
|
|
39 Label connStatusLabel;
|
|
40 VncCanvas vc;
|
|
41 OptionsFrame options;
|
|
42 ClipboardFrame clipboard;
|
|
43 RecordingFrame rec;
|
0
|
44
|
4
|
45 // Control session recording.
|
|
46 Object recordingSync;
|
|
47 String sessionFileName;
|
|
48 boolean recordingActive;
|
|
49 boolean recordingStatusChanged;
|
|
50 String cursorUpdatesDef;
|
|
51 String eightBitColorsDef;
|
0
|
52
|
4
|
53 // Variables read from parameter values.
|
|
54 String socketFactory;
|
|
55 String host;
|
|
56 int port;
|
|
57 String passwordParam;
|
|
58 boolean showControls;
|
|
59 boolean offerRelogin;
|
|
60 boolean showOfflineDesktop;
|
|
61 int deferScreenUpdates;
|
|
62 int deferCursorUpdates;
|
|
63 int deferUpdateRequests;
|
|
64 int debugStatsExcludeUpdates;
|
|
65 int debugStatsMeasureUpdates;
|
0
|
66
|
4
|
67 // Reference to this applet for inter-applet communication.
|
|
68 public static java.applet.Applet refApplet;
|
0
|
69
|
4
|
70 //
|
|
71 // init()
|
|
72 //
|
0
|
73
|
4
|
74 public void init() {
|
0
|
75
|
4
|
76 readParameters();
|
0
|
77
|
4
|
78 refApplet = this;
|
0
|
79
|
4
|
80 if (inSeparateFrame) {
|
|
81 vncFrame = new Frame("TightVNC");
|
|
82 if (!inAnApplet) {
|
|
83 vncFrame.add("Center", this);
|
|
84 }
|
|
85 vncContainer = vncFrame;
|
|
86 } else {
|
|
87 vncContainer = this;
|
|
88 }
|
0
|
89
|
4
|
90 recordingSync = new Object();
|
0
|
91
|
4
|
92 options = new OptionsFrame(this);
|
|
93 clipboard = new ClipboardFrame(this);
|
|
94 if (RecordingFrame.checkSecurity())
|
|
95 rec = new RecordingFrame(this);
|
0
|
96
|
4
|
97 sessionFileName = null;
|
|
98 recordingActive = false;
|
|
99 recordingStatusChanged = false;
|
|
100 cursorUpdatesDef = null;
|
|
101 eightBitColorsDef = null;
|
0
|
102
|
4
|
103 if (inSeparateFrame)
|
|
104 vncFrame.addWindowListener(this);
|
0
|
105
|
4
|
106 rfbThread = new Thread(this);
|
|
107 rfbThread.start();
|
|
108 }
|
0
|
109
|
4
|
110 public void update(Graphics g) {
|
|
111 }
|
0
|
112
|
4
|
113 //
|
|
114 // run() - executed by the rfbThread to deal with the RFB socket.
|
|
115 //
|
|
116
|
|
117 public void run() {
|
0
|
118
|
4
|
119 gridbag = new GridBagLayout();
|
|
120 vncContainer.setLayout(gridbag);
|
0
|
121
|
4
|
122 GridBagConstraints gbc = new GridBagConstraints();
|
|
123 gbc.gridwidth = GridBagConstraints.REMAINDER;
|
|
124 gbc.anchor = GridBagConstraints.NORTHWEST;
|
0
|
125
|
4
|
126 if (showControls) {
|
|
127 buttonPanel = new ButtonPanel(this);
|
|
128 gridbag.setConstraints(buttonPanel, gbc);
|
|
129 vncContainer.add(buttonPanel);
|
|
130 }
|
|
131
|
10
|
132 /*****************************************************************************/
|
18
|
133 if(inSeparateFrame) {
|
|
134 vncFrame.pack();
|
|
135 vncFrame.setVisible(true);
|
|
136 } else {
|
|
137 validate();
|
|
138 }
|
4
|
139 try {
|
16
|
140
|
6
|
141 rfb = new MyRfbProto(host, port, this);
|
10
|
142 rfb.readServerInit();
|
|
143
|
4
|
144 createCanvas(0, 0);
|
|
145 } catch (IOException e) {
|
|
146 System.out.println("Socket error");
|
|
147 System.exit(0);
|
|
148 }
|
|
149
|
|
150 gbc.weightx = 1.0;
|
|
151 gbc.weighty = 1.0;
|
|
152
|
10
|
153 if (inSeparateFrame) {
|
0
|
154
|
4
|
155 // Create a panel which itself is resizeable and can hold
|
|
156 // non-resizeable VncCanvas component at the top left corner.
|
|
157 Panel canvasPanel = new Panel();
|
|
158 canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
|
159 canvasPanel.add(vc);
|
0
|
160
|
4
|
161 // Create a ScrollPane which will hold a panel with VncCanvas
|
|
162 // inside.
|
|
163 desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
|
|
164 gbc.fill = GridBagConstraints.BOTH;
|
|
165 gridbag.setConstraints(desktopScrollPane, gbc);
|
|
166 desktopScrollPane.add(canvasPanel);
|
0
|
167
|
4
|
168 // Finally, add our ScrollPane to the Frame window.
|
|
169 vncFrame.add(desktopScrollPane);
|
|
170 vncFrame.setTitle(rfb.desktopName);
|
|
171 vncFrame.pack();
|
|
172 vc.resizeDesktopFrame();
|
0
|
173
|
4
|
174 } else {
|
0
|
175
|
4
|
176 // Just add the VncCanvas component to the Applet.
|
|
177 gridbag.setConstraints(vc, gbc);
|
|
178 add(vc);
|
|
179 validate();
|
0
|
180
|
4
|
181 }
|
10
|
182 /*****************************************************************************/
|
0
|
183
|
10
|
184
|
4
|
185 try {
|
0
|
186
|
4
|
187 if (showControls)
|
|
188 buttonPanel.enableButtons();
|
0
|
189
|
4
|
190 moveFocusToDesktop();
|
|
191 processNormalProtocol();// main loop
|
0
|
192
|
4
|
193 } catch (NoRouteToHostException e) {
|
|
194 fatalError("Network error: no route to server: " + host, e);
|
|
195 } catch (UnknownHostException e) {
|
|
196 fatalError("Network error: server name unknown: " + host, e);
|
|
197 } catch (ConnectException e) {
|
|
198 fatalError("Network error: could not connect to server: " + host
|
|
199 + ":" + port, e);
|
|
200 } catch (EOFException e) {
|
|
201 if (showOfflineDesktop) {
|
|
202 e.printStackTrace();
|
|
203 System.out
|
|
204 .println("Network error: remote side closed connection");
|
|
205 if (vc != null) {
|
|
206 vc.enableInput(false);
|
|
207 }
|
|
208 if (inSeparateFrame) {
|
|
209 vncFrame.setTitle(rfb.desktopName + " [disconnected]");
|
|
210 }
|
|
211 if (rfb != null && !rfb.closed())
|
|
212 rfb.close();
|
|
213 if (showControls && buttonPanel != null) {
|
|
214 buttonPanel.disableButtonsOnDisconnect();
|
|
215 if (inSeparateFrame) {
|
|
216 vncFrame.pack();
|
|
217 } else {
|
|
218 validate();
|
|
219 }
|
|
220 }
|
|
221 } else {
|
|
222 fatalError("Network error: remote side closed connection", e);
|
|
223 }
|
|
224 } catch (IOException e) {
|
|
225 String str = e.getMessage();
|
|
226 if (str != null && str.length() != 0) {
|
|
227 fatalError("Network Error: " + str, e);
|
|
228 } else {
|
|
229 fatalError(e.toString(), e);
|
|
230 }
|
|
231 } catch (Exception e) {
|
|
232 String str = e.getMessage();
|
|
233 if (str != null && str.length() != 0) {
|
|
234 fatalError("Error: " + str, e);
|
|
235 } else {
|
|
236 fatalError(e.toString(), e);
|
|
237 }
|
|
238 }
|
|
239
|
0
|
240 }
|
|
241
|
4
|
242 //
|
|
243 // Create a VncCanvas instance.
|
|
244 //
|
0
|
245
|
4
|
246 void createCanvas(int maxWidth, int maxHeight) throws IOException {
|
|
247 // Determine if Java 2D API is available and use a special
|
|
248 // version of VncCanvas if it is present.
|
|
249 vc = null;
|
|
250 try {
|
|
251 // This throws ClassNotFoundException if there is no Java 2D API.
|
|
252 Class cl = Class.forName("java.awt.Graphics2D");
|
|
253 // If we could load Graphics2D class, then we can use VncCanvas2D.
|
|
254 cl = Class.forName("VncCanvas2");
|
|
255 Class[] argClasses = { this.getClass(), Integer.TYPE, Integer.TYPE };
|
|
256 java.lang.reflect.Constructor cstr = cl.getConstructor(argClasses);
|
|
257 Object[] argObjects = { this, new Integer(maxWidth),
|
|
258 new Integer(maxHeight) };
|
|
259 vc = (VncCanvas) cstr.newInstance(argObjects);
|
|
260 } catch (Exception e) {
|
|
261 System.out.println("Warning: Java 2D API is not available");
|
|
262 }
|
0
|
263
|
4
|
264 // If we failed to create VncCanvas2D, use old VncCanvas.
|
|
265 if (vc == null)
|
|
266 vc = new VncCanvas(this, maxWidth, maxHeight);
|
|
267 }
|
0
|
268
|
4
|
269 //
|
|
270 // Process RFB socket messages.
|
|
271 // If the rfbThread is being stopped, ignore any exceptions,
|
|
272 // otherwise rethrow the exception so it can be handled.
|
|
273 //
|
0
|
274
|
4
|
275 void processNormalProtocol() throws Exception {
|
|
276 try {
|
|
277 vc.processNormalProtocol();// main loop
|
|
278 } catch (Exception e) {
|
|
279 if (rfbThread == null) {
|
|
280 System.out.println("Ignoring RFB socket exceptions"
|
|
281 + " because applet is stopping");
|
|
282 } else {
|
|
283 throw e;
|
|
284 }
|
|
285 }
|
|
286 }
|
0
|
287
|
4
|
288 //
|
|
289 // Connect to the RFB server and authenticate the user.
|
|
290 //
|
0
|
291
|
4
|
292 void connectAndAuthenticate() throws Exception {
|
|
293 showConnectionStatus("Initializing...");
|
|
294 if (inSeparateFrame) {
|
|
295 vncFrame.pack();
|
15
|
296 vncFrame.setVisible(true);
|
4
|
297 } else {
|
|
298 validate();
|
|
299 }
|
|
300
|
|
301 showConnectionStatus("Connecting to " + host + ", port " + port + "...");
|
0
|
302
|
15
|
303 rfb = new MyRfbProto(host, port, this);
|
4
|
304 showConnectionStatus("Connected to server");
|
0
|
305
|
4
|
306 rfb.readVersionMsg();
|
|
307 showConnectionStatus("RFB server supports protocol version "
|
|
308 + rfb.serverMajor + "." + rfb.serverMinor);
|
0
|
309
|
4
|
310 rfb.writeVersionMsg();
|
|
311 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
|
|
312 + "." + rfb.clientMinor);
|
0
|
313
|
4
|
314 int secType = rfb.negotiateSecurity();
|
|
315 int authType;
|
|
316 if (secType == RfbProto.SecTypeTight) {
|
|
317 showConnectionStatus("Enabling TightVNC protocol extensions");
|
|
318 rfb.setupTunneling();
|
|
319 authType = rfb.negotiateAuthenticationTight();
|
|
320 } else {
|
|
321 authType = secType;
|
|
322 }
|
0
|
323
|
4
|
324 switch (authType) {
|
|
325 case RfbProto.AuthNone:
|
|
326 showConnectionStatus("No authentication needed");
|
|
327 rfb.authenticateNone();
|
|
328 break;
|
|
329 case RfbProto.AuthVNC:
|
|
330 showConnectionStatus("Performing standard VNC authentication");
|
|
331 if (passwordParam != null) {
|
|
332 rfb.authenticateVNC(passwordParam);
|
|
333 } else {
|
|
334 String pw = askPassword();
|
|
335 rfb.authenticateVNC(pw);
|
|
336 }
|
|
337 break;
|
|
338 default:
|
|
339 throw new Exception("Unknown authentication scheme " + authType);
|
|
340 }
|
|
341 }
|
0
|
342
|
4
|
343 //
|
|
344 // Show a message describing the connection status.
|
|
345 // To hide the connection status label, use (msg == null).
|
|
346 //
|
0
|
347
|
4
|
348 void showConnectionStatus(String msg) {
|
|
349 if (msg == null) {
|
|
350 if (vncContainer.isAncestorOf(connStatusLabel)) {
|
|
351 vncContainer.remove(connStatusLabel);
|
|
352 }
|
|
353 return;
|
|
354 }
|
0
|
355
|
4
|
356 System.out.println(msg);
|
0
|
357
|
4
|
358 if (connStatusLabel == null) {
|
|
359 connStatusLabel = new Label("Status: " + msg);
|
|
360 connStatusLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
|
|
361 } else {
|
|
362 connStatusLabel.setText("Status: " + msg);
|
|
363 }
|
0
|
364
|
4
|
365 if (!vncContainer.isAncestorOf(connStatusLabel)) {
|
|
366 GridBagConstraints gbc = new GridBagConstraints();
|
|
367 gbc.gridwidth = GridBagConstraints.REMAINDER;
|
|
368 gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
369 gbc.anchor = GridBagConstraints.NORTHWEST;
|
|
370 gbc.weightx = 1.0;
|
|
371 gbc.weighty = 1.0;
|
|
372 gbc.insets = new Insets(20, 30, 20, 30);
|
|
373 gridbag.setConstraints(connStatusLabel, gbc);
|
|
374 vncContainer.add(connStatusLabel);
|
|
375 }
|
0
|
376
|
4
|
377 if (inSeparateFrame) {
|
|
378 vncFrame.pack();
|
|
379 } else {
|
|
380 validate();
|
|
381 }
|
|
382 }
|
0
|
383
|
4
|
384 //
|
|
385 // Show an authentication panel.
|
|
386 //
|
0
|
387
|
4
|
388 String askPassword() throws Exception {
|
|
389 showConnectionStatus(null);
|
|
390
|
|
391 AuthPanel authPanel = new AuthPanel(this);
|
0
|
392
|
4
|
393 GridBagConstraints gbc = new GridBagConstraints();
|
|
394 gbc.gridwidth = GridBagConstraints.REMAINDER;
|
|
395 gbc.anchor = GridBagConstraints.NORTHWEST;
|
|
396 gbc.weightx = 1.0;
|
|
397 gbc.weighty = 1.0;
|
|
398 gbc.ipadx = 100;
|
|
399 gbc.ipady = 50;
|
|
400 gridbag.setConstraints(authPanel, gbc);
|
|
401 vncContainer.add(authPanel);
|
0
|
402
|
4
|
403 if (inSeparateFrame) {
|
|
404 vncFrame.pack();
|
|
405 } else {
|
|
406 validate();
|
|
407 }
|
0
|
408
|
4
|
409 authPanel.moveFocusToDefaultField();
|
|
410 String pw = authPanel.getPassword();
|
|
411 vncContainer.remove(authPanel);
|
0
|
412
|
4
|
413 return pw;
|
|
414 }
|
0
|
415
|
4
|
416 //
|
|
417 // Do the rest of the protocol initialisation.
|
|
418 //
|
0
|
419
|
4
|
420 void doProtocolInitialisation() throws IOException {
|
|
421 rfb.writeClientInit();
|
|
422 rfb.readServerInit();
|
0
|
423
|
4
|
424 System.out.println("Desktop name is " + rfb.desktopName);
|
|
425 System.out.println("Desktop size is " + rfb.framebufferWidth + " x "
|
|
426 + rfb.framebufferHeight);
|
|
427
|
|
428 setEncodings();
|
0
|
429
|
4
|
430 showConnectionStatus(null);
|
|
431 }
|
0
|
432
|
4
|
433 //
|
|
434 // Send current encoding list to the RFB server.
|
|
435 //
|
0
|
436
|
4
|
437 int[] encodingsSaved;
|
|
438 int nEncodingsSaved;
|
0
|
439
|
4
|
440 void setEncodings() {
|
|
441 setEncodings(false);
|
|
442 }
|
0
|
443
|
4
|
444 void autoSelectEncodings() {
|
|
445 setEncodings(true);
|
|
446 }
|
0
|
447
|
4
|
448 void setEncodings(boolean autoSelectOnly) {
|
|
449 if (options == null || rfb == null || !rfb.inNormalProtocol)
|
|
450 return;
|
0
|
451
|
4
|
452 int preferredEncoding = options.preferredEncoding;
|
|
453 if (preferredEncoding == -1) {
|
|
454 long kbitsPerSecond = rfb.kbitsPerSecond();
|
|
455 if (nEncodingsSaved < 1) {
|
|
456 // Choose Tight or ZRLE encoding for the very first update.
|
|
457 System.out.println("Using Tight/ZRLE encodings");
|
|
458 preferredEncoding = RfbProto.EncodingTight;
|
|
459 } else if (kbitsPerSecond > 2000
|
|
460 && encodingsSaved[0] != RfbProto.EncodingHextile) {
|
|
461 // Switch to Hextile if the connection speed is above 2Mbps.
|
|
462 System.out.println("Throughput " + kbitsPerSecond
|
|
463 + " kbit/s - changing to Hextile encoding");
|
|
464 preferredEncoding = RfbProto.EncodingHextile;
|
|
465 } else if (kbitsPerSecond < 1000
|
|
466 && encodingsSaved[0] != RfbProto.EncodingTight) {
|
|
467 // Switch to Tight/ZRLE if the connection speed is below 1Mbps.
|
|
468 System.out.println("Throughput " + kbitsPerSecond
|
|
469 + " kbit/s - changing to Tight/ZRLE encodings");
|
|
470 preferredEncoding = RfbProto.EncodingTight;
|
|
471 } else {
|
|
472 // Don't change the encoder.
|
|
473 if (autoSelectOnly)
|
|
474 return;
|
|
475 preferredEncoding = encodingsSaved[0];
|
|
476 }
|
|
477 } else {
|
|
478 // Auto encoder selection is not enabled.
|
|
479 if (autoSelectOnly)
|
|
480 return;
|
|
481 }
|
0
|
482
|
4
|
483 int[] encodings = new int[20];
|
|
484 int nEncodings = 0;
|
0
|
485
|
4
|
486 encodings[nEncodings++] = preferredEncoding;
|
|
487 if (options.useCopyRect) {
|
|
488 encodings[nEncodings++] = RfbProto.EncodingCopyRect;
|
|
489 }
|
0
|
490
|
4
|
491 if (preferredEncoding != RfbProto.EncodingTight) {
|
|
492 encodings[nEncodings++] = RfbProto.EncodingTight;
|
|
493 }
|
|
494 if (preferredEncoding != RfbProto.EncodingZRLE) {
|
|
495 encodings[nEncodings++] = RfbProto.EncodingZRLE;
|
|
496 }
|
|
497 if (preferredEncoding != RfbProto.EncodingHextile) {
|
|
498 encodings[nEncodings++] = RfbProto.EncodingHextile;
|
|
499 }
|
|
500 if (preferredEncoding != RfbProto.EncodingZlib) {
|
|
501 encodings[nEncodings++] = RfbProto.EncodingZlib;
|
|
502 }
|
|
503 if (preferredEncoding != RfbProto.EncodingCoRRE) {
|
|
504 encodings[nEncodings++] = RfbProto.EncodingCoRRE;
|
|
505 }
|
|
506 if (preferredEncoding != RfbProto.EncodingRRE) {
|
|
507 encodings[nEncodings++] = RfbProto.EncodingRRE;
|
|
508 }
|
|
509
|
|
510 if (options.compressLevel >= 0 && options.compressLevel <= 9) {
|
|
511 encodings[nEncodings++] = RfbProto.EncodingCompressLevel0
|
|
512 + options.compressLevel;
|
|
513 }
|
|
514 if (options.jpegQuality >= 0 && options.jpegQuality <= 9) {
|
|
515 encodings[nEncodings++] = RfbProto.EncodingQualityLevel0
|
|
516 + options.jpegQuality;
|
|
517 }
|
0
|
518
|
4
|
519 if (options.requestCursorUpdates) {
|
|
520 encodings[nEncodings++] = RfbProto.EncodingXCursor;
|
|
521 encodings[nEncodings++] = RfbProto.EncodingRichCursor;
|
|
522 if (!options.ignoreCursorUpdates)
|
|
523 encodings[nEncodings++] = RfbProto.EncodingPointerPos;
|
|
524 }
|
|
525
|
|
526 encodings[nEncodings++] = RfbProto.EncodingLastRect;
|
|
527 encodings[nEncodings++] = RfbProto.EncodingNewFBSize;
|
0
|
528
|
4
|
529 boolean encodingsWereChanged = false;
|
|
530 if (nEncodings != nEncodingsSaved) {
|
|
531 encodingsWereChanged = true;
|
|
532 } else {
|
|
533 for (int i = 0; i < nEncodings; i++) {
|
|
534 if (encodings[i] != encodingsSaved[i]) {
|
|
535 encodingsWereChanged = true;
|
|
536 break;
|
|
537 }
|
|
538 }
|
|
539 }
|
0
|
540
|
4
|
541 if (encodingsWereChanged) {
|
|
542 try {
|
|
543 rfb.writeSetEncodings(encodings, nEncodings);
|
|
544 if (vc != null) {
|
|
545 vc.softCursorFree();
|
|
546 }
|
|
547 } catch (Exception e) {
|
|
548 e.printStackTrace();
|
|
549 }
|
|
550 encodingsSaved = encodings;
|
|
551 nEncodingsSaved = nEncodings;
|
|
552 }
|
|
553 }
|
0
|
554
|
4
|
555 //
|
|
556 // setCutText() - send the given cut text to the RFB server.
|
|
557 //
|
0
|
558
|
4
|
559 void setCutText(String text) {
|
|
560 try {
|
|
561 if (rfb != null && rfb.inNormalProtocol) {
|
|
562 rfb.writeClientCutText(text);
|
|
563 }
|
|
564 } catch (Exception e) {
|
|
565 e.printStackTrace();
|
|
566 }
|
|
567 }
|
0
|
568
|
4
|
569 //
|
|
570 // Order change in session recording status. To stop recording, pass
|
|
571 // null in place of the fname argument.
|
|
572 //
|
0
|
573
|
4
|
574 void setRecordingStatus(String fname) {
|
|
575 synchronized (recordingSync) {
|
|
576 sessionFileName = fname;
|
|
577 recordingStatusChanged = true;
|
|
578 }
|
|
579 }
|
|
580
|
|
581 //
|
|
582 // Start or stop session recording. Returns true if this method call
|
|
583 // causes recording of a new session.
|
|
584 //
|
0
|
585
|
4
|
586 boolean checkRecordingStatus() throws IOException {
|
|
587 synchronized (recordingSync) {
|
|
588 if (recordingStatusChanged) {
|
|
589 recordingStatusChanged = false;
|
|
590 if (sessionFileName != null) {
|
|
591 startRecording();
|
|
592 return true;
|
|
593 } else {
|
|
594 stopRecording();
|
|
595 }
|
|
596 }
|
|
597 }
|
|
598 return false;
|
|
599 }
|
0
|
600
|
4
|
601 //
|
|
602 // Start session recording.
|
|
603 //
|
0
|
604
|
4
|
605 protected void startRecording() throws IOException {
|
|
606 synchronized (recordingSync) {
|
|
607 if (!recordingActive) {
|
|
608 // Save settings to restore them after recording the session.
|
|
609 cursorUpdatesDef = options.choices[options.cursorUpdatesIndex]
|
|
610 .getSelectedItem();
|
|
611 eightBitColorsDef = options.choices[options.eightBitColorsIndex]
|
|
612 .getSelectedItem();
|
|
613 // Set options to values suitable for recording.
|
|
614 options.choices[options.cursorUpdatesIndex].select("Disable");
|
|
615 options.choices[options.cursorUpdatesIndex].setEnabled(false);
|
|
616 options.setEncodings();
|
|
617 options.choices[options.eightBitColorsIndex].select("No");
|
|
618 options.choices[options.eightBitColorsIndex].setEnabled(false);
|
|
619 options.setColorFormat();
|
|
620 } else {
|
|
621 rfb.closeSession();
|
|
622 }
|
|
623
|
|
624 System.out.println("Recording the session in " + sessionFileName);
|
|
625 rfb.startSession(sessionFileName);
|
|
626 recordingActive = true;
|
|
627 }
|
|
628 }
|
0
|
629
|
4
|
630 //
|
|
631 // Stop session recording.
|
|
632 //
|
0
|
633
|
4
|
634 protected void stopRecording() throws IOException {
|
|
635 synchronized (recordingSync) {
|
|
636 if (recordingActive) {
|
|
637 // Restore options.
|
|
638 options.choices[options.cursorUpdatesIndex]
|
|
639 .select(cursorUpdatesDef);
|
|
640 options.choices[options.cursorUpdatesIndex].setEnabled(true);
|
|
641 options.setEncodings();
|
|
642 options.choices[options.eightBitColorsIndex]
|
|
643 .select(eightBitColorsDef);
|
|
644 options.choices[options.eightBitColorsIndex].setEnabled(true);
|
|
645 options.setColorFormat();
|
|
646
|
|
647 rfb.closeSession();
|
|
648 System.out.println("Session recording stopped.");
|
|
649 }
|
|
650 sessionFileName = null;
|
|
651 recordingActive = false;
|
|
652 }
|
|
653 }
|
0
|
654
|
4
|
655 //
|
|
656 // readParameters() - read parameters from the html source or from the
|
|
657 // command line. On the command line, the arguments are just a sequence of
|
|
658 // param_name/param_value pairs where the names and values correspond to
|
|
659 // those expected in the html applet tag source.
|
|
660 //
|
0
|
661
|
4
|
662 void readParameters() {
|
18
|
663 host = readParameter("HOST", !inAnApplet);
|
0
|
664
|
18
|
665 if (host == null) { host = getCodeBase().getHost(); if
|
|
666 (host.equals("")) { fatalError("HOST parameter not specified"); } }
|
|
667
|
|
668 port = readIntParameter("PORT", 5550);
|
|
669
|
4
|
670 // Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
|
|
671 readPasswordParameters();
|
|
672
|
|
673 String str;
|
|
674 if (inAnApplet) {
|
|
675 str = readParameter("Open New Window", false);
|
|
676 if (str != null && str.equalsIgnoreCase("Yes"))
|
|
677 inSeparateFrame = true;
|
|
678 }
|
0
|
679
|
4
|
680 // "Show Controls" set to "No" disables button panel.
|
|
681 showControls = true;
|
|
682 str = readParameter("Show Controls", false);
|
|
683 if (str != null && str.equalsIgnoreCase("No"))
|
|
684 showControls = false;
|
0
|
685
|
4
|
686 // "Offer Relogin" set to "No" disables "Login again" and "Close
|
|
687 // window" buttons under error messages in applet mode.
|
|
688 offerRelogin = true;
|
|
689 str = readParameter("Offer Relogin", false);
|
|
690 if (str != null && str.equalsIgnoreCase("No"))
|
|
691 offerRelogin = false;
|
|
692
|
|
693 // Do we continue showing desktop on remote disconnect?
|
|
694 showOfflineDesktop = false;
|
|
695 str = readParameter("Show Offline Desktop", false);
|
|
696 if (str != null && str.equalsIgnoreCase("Yes"))
|
|
697 showOfflineDesktop = true;
|
0
|
698
|
4
|
699 // Fine tuning options.
|
|
700 deferScreenUpdates = readIntParameter("Defer screen updates", 20);
|
|
701 deferCursorUpdates = readIntParameter("Defer cursor updates", 10);
|
|
702 deferUpdateRequests = readIntParameter("Defer update requests", 0);
|
0
|
703
|
4
|
704 // Debugging options.
|
|
705 debugStatsExcludeUpdates = readIntParameter("DEBUG_XU", 0);
|
|
706 debugStatsMeasureUpdates = readIntParameter("DEBUG_CU", 0);
|
0
|
707
|
4
|
708 // SocketFactory.
|
|
709 socketFactory = readParameter("SocketFactory", false);
|
|
710 }
|
0
|
711
|
4
|
712 //
|
|
713 // Read password parameters. If an "ENCPASSWORD" parameter is set,
|
|
714 // then decrypt the password into the passwordParam string. Otherwise,
|
|
715 // try to read the "PASSWORD" parameter directly to passwordParam.
|
|
716 //
|
0
|
717
|
4
|
718 private void readPasswordParameters() {
|
|
719 String encPasswordParam = readParameter("ENCPASSWORD", false);
|
|
720 if (encPasswordParam == null) {
|
|
721 passwordParam = readParameter("PASSWORD", false);
|
0
|
722
|
4
|
723 } else {
|
|
724 // ENCPASSWORD is hexascii-encoded. Decode.
|
|
725 byte[] pw = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
726 int len = encPasswordParam.length() / 2;
|
|
727 if (len > 8)
|
|
728 len = 8;
|
|
729 for (int i = 0; i < len; i++) {
|
|
730 String hex = encPasswordParam.substring(i * 2, i * 2 + 2);
|
|
731 Integer x = new Integer(Integer.parseInt(hex, 16));
|
|
732 pw[i] = x.byteValue();
|
|
733 }
|
|
734 // Decrypt the password.
|
|
735 byte[] key = { 23, 82, 107, 6, 35, 78, 88, 7 };
|
|
736 DesCipher des = new DesCipher(key);
|
|
737 des.decrypt(pw, 0, pw, 0);
|
|
738 passwordParam = new String(pw);
|
|
739
|
|
740 }
|
|
741 }
|
0
|
742
|
4
|
743 public String readParameter(String name, boolean required) {
|
|
744 if (inAnApplet) {
|
|
745 String s = getParameter(name);
|
|
746 if ((s == null) && required) {
|
|
747 fatalError(name + " parameter not specified");
|
|
748 }
|
|
749 return s;
|
|
750 }
|
0
|
751
|
4
|
752 for (int i = 0; i < mainArgs.length; i += 2) {
|
|
753 if (mainArgs[i].equalsIgnoreCase(name)) {
|
|
754 try {
|
|
755 return mainArgs[i + 1];
|
|
756 } catch (Exception e) {
|
|
757 if (required) {
|
|
758 fatalError(name + " parameter not specified");
|
|
759 }
|
|
760 return null;
|
|
761 }
|
|
762 }
|
|
763 }
|
|
764 if (required) {
|
|
765 fatalError(name + " parameter not specified");
|
|
766 }
|
|
767 return null;
|
0
|
768 }
|
4
|
769
|
|
770 int readIntParameter(String name, int defaultValue) {
|
|
771 String str = readParameter(name, false);
|
|
772 int result = defaultValue;
|
|
773 if (str != null) {
|
|
774 try {
|
|
775 result = Integer.parseInt(str);
|
|
776 } catch (NumberFormatException e) {
|
|
777 }
|
|
778 }
|
|
779 return result;
|
|
780 }
|
0
|
781
|
4
|
782 //
|
|
783 // moveFocusToDesktop() - move keyboard focus either to VncCanvas.
|
|
784 //
|
0
|
785
|
4
|
786 void moveFocusToDesktop() {
|
|
787 if (vncContainer != null) {
|
|
788 if (vc != null && vncContainer.isAncestorOf(vc))
|
|
789 vc.requestFocus();
|
|
790 }
|
|
791 }
|
0
|
792
|
4
|
793 //
|
|
794 // disconnect() - close connection to server.
|
|
795 //
|
0
|
796
|
4
|
797 synchronized public void disconnect() {
|
|
798 System.out.println("Disconnecting");
|
0
|
799
|
4
|
800 if (vc != null) {
|
|
801 double sec = (System.currentTimeMillis() - vc.statStartTime) / 1000.0;
|
|
802 double rate = Math.round(vc.statNumUpdates / sec * 100) / 100.0;
|
|
803 int nRealRects = vc.statNumPixelRects;
|
|
804 int nPseudoRects = vc.statNumTotalRects - vc.statNumPixelRects;
|
|
805 System.out.println("Updates received: " + vc.statNumUpdates + " ("
|
|
806 + nRealRects + " rectangles + " + nPseudoRects
|
|
807 + " pseudo), " + rate + " updates/sec");
|
|
808 int numRectsOther = nRealRects - vc.statNumRectsTight
|
|
809 - vc.statNumRectsZRLE - vc.statNumRectsHextile
|
|
810 - vc.statNumRectsRaw - vc.statNumRectsCopy;
|
|
811 System.out.println("Rectangles:" + " Tight=" + vc.statNumRectsTight
|
|
812 + "(JPEG=" + vc.statNumRectsTightJPEG + ") ZRLE="
|
|
813 + vc.statNumRectsZRLE + " Hextile="
|
|
814 + vc.statNumRectsHextile + " Raw=" + vc.statNumRectsRaw
|
|
815 + " CopyRect=" + vc.statNumRectsCopy + " other="
|
|
816 + numRectsOther);
|
0
|
817
|
4
|
818 int raw = vc.statNumBytesDecoded;
|
|
819 int compressed = vc.statNumBytesEncoded;
|
|
820 if (compressed > 0) {
|
|
821 double ratio = Math.round((double) raw / compressed * 1000) / 1000.0;
|
|
822 System.out.println("Pixel data: " + vc.statNumBytesDecoded
|
|
823 + " bytes, " + vc.statNumBytesEncoded
|
|
824 + " compressed, ratio " + ratio);
|
|
825 }
|
|
826 }
|
0
|
827
|
4
|
828 if (rfb != null && !rfb.closed())
|
|
829 rfb.close();
|
|
830 options.dispose();
|
|
831 clipboard.dispose();
|
|
832 if (rec != null)
|
|
833 rec.dispose();
|
0
|
834
|
4
|
835 if (inAnApplet) {
|
|
836 showMessage("Disconnected");
|
|
837 } else {
|
|
838 System.exit(0);
|
|
839 }
|
|
840 }
|
|
841
|
|
842 //
|
|
843 // fatalError() - print out a fatal error message.
|
|
844 // FIXME: Do we really need two versions of the fatalError() method?
|
|
845 //
|
|
846
|
|
847 synchronized public void fatalError(String str) {
|
|
848 System.out.println(str);
|
0
|
849
|
4
|
850 if (inAnApplet) {
|
|
851 // vncContainer null, applet not inited,
|
|
852 // can not present the error to the user.
|
|
853 Thread.currentThread().stop();
|
|
854 } else {
|
|
855 System.exit(1);
|
|
856 }
|
|
857 }
|
0
|
858
|
4
|
859 synchronized public void fatalError(String str, Exception e) {
|
0
|
860
|
4
|
861 if (rfb != null && rfb.closed()) {
|
|
862 // Not necessary to show error message if the error was caused
|
|
863 // by I/O problems after the rfb.close() method call.
|
|
864 System.out.println("RFB thread finished");
|
|
865 return;
|
|
866 }
|
|
867
|
|
868 System.out.println(str);
|
|
869 e.printStackTrace();
|
0
|
870
|
4
|
871 if (rfb != null)
|
|
872 rfb.close();
|
0
|
873
|
4
|
874 if (inAnApplet) {
|
|
875 showMessage(str);
|
|
876 } else {
|
|
877 System.exit(1);
|
|
878 }
|
|
879 }
|
0
|
880
|
4
|
881 //
|
|
882 // Show message text and optionally "Relogin" and "Close" buttons.
|
|
883 //
|
0
|
884
|
4
|
885 void showMessage(String msg) {
|
|
886 vncContainer.removeAll();
|
0
|
887
|
4
|
888 Label errLabel = new Label(msg, Label.CENTER);
|
|
889 errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
|
0
|
890
|
4
|
891 if (offerRelogin) {
|
0
|
892
|
4
|
893 Panel gridPanel = new Panel(new GridLayout(0, 1));
|
|
894 Panel outerPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
|
|
895 outerPanel.add(gridPanel);
|
|
896 vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 16));
|
|
897 vncContainer.add(outerPanel);
|
|
898 Panel textPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
|
|
899 textPanel.add(errLabel);
|
|
900 gridPanel.add(textPanel);
|
|
901 gridPanel.add(new ReloginPanel(this));
|
|
902
|
|
903 } else {
|
0
|
904
|
4
|
905 vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
|
|
906 vncContainer.add(errLabel);
|
|
907
|
|
908 }
|
0
|
909
|
4
|
910 if (inSeparateFrame) {
|
|
911 vncFrame.pack();
|
|
912 } else {
|
|
913 validate();
|
|
914 }
|
|
915 }
|
0
|
916
|
4
|
917 //
|
|
918 // Stop the applet.
|
|
919 // Main applet thread will terminate on first exception
|
|
920 // after seeing that rfbThread has been set to null.
|
|
921 //
|
0
|
922
|
4
|
923 public void stop() {
|
|
924 System.out.println("Stopping applet");
|
|
925 rfbThread = null;
|
|
926 }
|
0
|
927
|
4
|
928 //
|
|
929 // This method is called before the applet is destroyed.
|
|
930 //
|
|
931
|
|
932 public void destroy() {
|
|
933 System.out.println("Destroying applet");
|
0
|
934
|
4
|
935 vncContainer.removeAll();
|
|
936 options.dispose();
|
|
937 clipboard.dispose();
|
|
938 if (rec != null)
|
|
939 rec.dispose();
|
|
940 if (rfb != null && !rfb.closed())
|
|
941 rfb.close();
|
|
942 if (inSeparateFrame)
|
|
943 vncFrame.dispose();
|
|
944 }
|
0
|
945
|
4
|
946 //
|
|
947 // Start/stop receiving mouse events.
|
|
948 //
|
|
949
|
|
950 public void enableInput(boolean enable) {
|
|
951 vc.enableInput(enable);
|
|
952 }
|
0
|
953
|
4
|
954 //
|
|
955 // Close application properly on window close event.
|
|
956 //
|
0
|
957
|
4
|
958 public void windowClosing(WindowEvent evt) {
|
|
959 System.out.println("Closing window");
|
|
960 if (rfb != null)
|
|
961 disconnect();
|
0
|
962
|
4
|
963 vncContainer.hide();
|
|
964
|
|
965 if (!inAnApplet) {
|
|
966 System.exit(0);
|
|
967 }
|
|
968 }
|
0
|
969
|
4
|
970 //
|
|
971 // Ignore window events we're not interested in.
|
|
972 //
|
0
|
973
|
4
|
974 public void windowActivated(WindowEvent evt) {
|
|
975 }
|
|
976
|
|
977 public void windowDeactivated(WindowEvent evt) {
|
|
978 }
|
0
|
979
|
4
|
980 public void windowOpened(WindowEvent evt) {
|
|
981 }
|
|
982
|
|
983 public void windowClosed(WindowEvent evt) {
|
|
984 }
|
0
|
985
|
4
|
986 public void windowIconified(WindowEvent evt) {
|
|
987 }
|
|
988
|
|
989 public void windowDeiconified(WindowEvent evt) {
|
|
990 }
|
0
|
991 }
|