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