Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/VncProxyService.java @ 127:97bb1436e34c
change encode to Hextile from ZRLE
author | e085711 |
---|---|
date | Tue, 02 Aug 2011 13:17:23 +0900 |
parents | 762d2b7f1db2 |
children | fa2122e5c807 |
rev | line source |
---|---|
24 | 1 package myVncProxy; |
13 | 2 import java.awt.*; |
3 import java.awt.event.*; | |
4 import java.io.*; | |
5 import java.net.*; | |
6 | |
7 public class VncProxyService implements java.lang.Runnable { | |
8 | |
9 public static void main(String[] argv) { | |
10 VncProxyService v = new VncProxyService(); | |
30 | 11 |
12 v.checkArgs(argv); | |
13 | |
13 | 14 v.mainArgs = argv; |
15 | |
16 v.init(); | |
50 | 17 v.start_threads(); |
13 | 18 } |
15 | 19 |
13 | 20 String[] mainArgs; |
31 | 21 String username; |
22 | |
23 CreateHtmlFile htmlFile; | |
13 | 24 |
25 // RfbProto rfb; | |
26 MyRfbProto rfb; | |
27 Thread rfbThread; | |
27 | 28 Thread accThread; |
63
4864a7d1df00
add CreateThread.java modify acceptClient.java and VncProxyService.java
Yu Taninari <e085734@ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
29 Thread clientThread; |
4864a7d1df00
add CreateThread.java modify acceptClient.java and VncProxyService.java
Yu Taninari <e085734@ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
30 CreateThread geth; |
73 | 31 AcceptClient acc; |
13 | 32 |
33 Frame vncFrame; | |
34 Container vncContainer; | |
35 ScrollPane desktopScrollPane; | |
36 GridBagLayout gridbag; | |
37 ButtonPanel buttonPanel; | |
38 Label connStatusLabel; | |
39 ProxyVncCanvas vc; | |
67 | 40 // OptionsFrame options; |
41 OptionsNoFrame options; | |
13 | 42 ClipboardFrame clipboard; |
43 RecordingFrame rec; | |
44 | |
45 // Control session recording. | |
46 Object recordingSync; | |
47 String sessionFileName; | |
48 boolean recordingActive; | |
49 boolean recordingStatusChanged; | |
50 String cursorUpdatesDef; | |
51 String eightBitColorsDef; | |
52 | |
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; | |
66 | |
30 | 67 |
68 void checkArgs(String[] argv){ | |
31 | 69 if(argv.length > 3){ |
70 username = argv[3]; | |
71 }else if(argv.length < 2){ | |
30 | 72 System.out.println("Please enter argv"); |
73 System.out.println("hostname(IPaddress) port password"); | |
74 System.exit(0); | |
31 | 75 }else{ |
76 username = argv[0]; | |
30 | 77 } |
78 } | |
79 | |
13 | 80 // |
81 // init() | |
82 // | |
83 | |
84 public void init() { | |
85 | |
86 readParameters(); | |
87 | |
67 | 88 options = new OptionsNoFrame(this); |
13 | 89 recordingSync = new Object(); |
90 | |
91 sessionFileName = null; | |
92 recordingActive = false; | |
93 recordingStatusChanged = false; | |
94 cursorUpdatesDef = null; | |
95 eightBitColorsDef = null; | |
96 | |
25 | 97 try{ |
98 connectAndAuthenticate(); | |
99 }catch (NoRouteToHostException e) { | |
100 fatalError("Network error: no route to server: " + host, e); | |
101 } catch (UnknownHostException e) { | |
102 fatalError("Network error: server name unknown: " + host, e); | |
103 } catch (ConnectException e) { | |
104 fatalError("Network error: could not connect to server: " + host | |
105 + ":" + port, e); | |
106 }catch(Exception e){} | |
51 | 107 |
13 | 108 rfbThread = new Thread(this); |
71 | 109 accThread = new Thread(new AcceptThread(rfb)); |
50 | 110 |
111 } | |
112 | |
113 public void start_threads(){ | |
13 | 114 rfbThread.start(); |
27 | 115 accThread.start(); |
13 | 116 } |
117 | |
118 // | |
119 // run() - executed by the rfbThread to deal with the RFB socket. | |
120 // | |
121 | |
122 public void run() { | |
123 | |
124 try { | |
25 | 125 // connectAndAuthenticate(); |
13 | 126 doProtocolInitialisation(); |
45 | 127 /* |
31 | 128 htmlFile = new CreateHtmlFile(rfb, host, username); |
129 htmlFile.createHtml(); | |
45 | 130 */ |
13 | 131 vc = new ProxyVncCanvas(this, 0, 0); |
25 | 132 vc.updateFramebufferSize(); |
133 | |
13 | 134 processNormalProtocol();// main loop |
135 | |
136 } catch (NoRouteToHostException e) { | |
137 fatalError("Network error: no route to server: " + host, e); | |
138 } catch (UnknownHostException e) { | |
139 fatalError("Network error: server name unknown: " + host, e); | |
140 } catch (ConnectException e) { | |
141 fatalError("Network error: could not connect to server: " + host | |
142 + ":" + port, e); | |
143 } catch (EOFException e) { | |
144 if (showOfflineDesktop) { | |
145 e.printStackTrace(); | |
146 System.out | |
147 .println("Network error: remote side closed connection"); | |
148 if (vc != null) { | |
149 vc.enableInput(false); | |
150 } | |
151 if (rfb != null && !rfb.closed()) | |
152 rfb.close(); | |
153 if (showControls && buttonPanel != null) { | |
154 buttonPanel.disableButtonsOnDisconnect(); | |
155 } | |
156 } else { | |
157 fatalError("Network error: remote side closed connection", e); | |
158 } | |
159 } catch (IOException e) { | |
160 String str = e.getMessage(); | |
161 if (str != null && str.length() != 0) { | |
162 fatalError("Network Error: " + str, e); | |
163 } else { | |
164 fatalError(e.toString(), e); | |
165 } | |
26 | 166 } catch (Exception e) { |
13 | 167 String str = e.getMessage(); |
168 if (str != null && str.length() != 0) { | |
169 fatalError("Error: " + str, e); | |
170 } else { | |
171 fatalError(e.toString(), e); | |
172 } | |
173 } | |
174 | |
175 } | |
176 | |
177 // | |
178 // Process RFB socket messages. | |
179 // If the rfbThread is being stopped, ignore any exceptions, | |
180 // otherwise rethrow the exception so it can be handled. | |
181 // | |
182 | |
183 void processNormalProtocol() throws Exception { | |
184 try { | |
185 vc.processNormalProtocol();// main loop | |
186 } catch (Exception e) { | |
187 if (rfbThread == null) { | |
188 System.out.println("Ignoring RFB socket exceptions" | |
189 + " because applet is stopping"); | |
190 } else { | |
191 throw e; | |
192 } | |
193 } | |
194 } | |
195 | |
196 // | |
197 // Connect to the RFB server and authenticate the user. | |
198 // | |
199 | |
200 void connectAndAuthenticate() throws Exception { | |
73 | 201 acc = new AcceptClient(mainArgs[0]); |
63
4864a7d1df00
add CreateThread.java modify acceptClient.java and VncProxyService.java
Yu Taninari <e085734@ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
202 geth = new CreateThread(acc); |
64 | 203 Thread thread = new Thread(geth); |
204 thread.start(); | |
48 | 205 |
206 | |
13 | 207 showConnectionStatus("Initializing..."); |
208 | |
209 showConnectionStatus("Connecting to " + host + ", port " + port + "..."); | |
210 | |
211 // rfb = new RfbProto(host, port, this); | |
212 rfb = new MyRfbProto(host, port); | |
213 showConnectionStatus("Connected to server"); | |
214 | |
215 rfb.readVersionMsg(); | |
216 showConnectionStatus("RFB server supports protocol version " | |
217 + rfb.serverMajor + "." + rfb.serverMinor); | |
218 | |
219 rfb.writeVersionMsg(); | |
220 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor | |
221 + "." + rfb.clientMinor); | |
222 | |
223 int secType = rfb.negotiateSecurity(); | |
224 int authType; | |
225 if (secType == RfbProto.SecTypeTight) { | |
226 showConnectionStatus("Enabling TightVNC protocol extensions"); | |
227 rfb.setupTunneling(); | |
228 authType = rfb.negotiateAuthenticationTight(); | |
229 } else { | |
230 authType = secType; | |
231 } | |
232 | |
233 switch (authType) { | |
234 case RfbProto.AuthNone: | |
235 showConnectionStatus("No authentication needed"); | |
236 rfb.authenticateNone(); | |
237 break; | |
238 case RfbProto.AuthVNC: | |
239 showConnectionStatus("Performing standard VNC authentication"); | |
240 if (passwordParam != null) { | |
241 rfb.authenticateVNC(passwordParam); | |
242 } else { | |
243 String pw = askPassword(); | |
244 rfb.authenticateVNC(pw); | |
245 } | |
246 break; | |
247 default: | |
248 throw new Exception("Unknown authentication scheme " + authType); | |
249 } | |
250 } | |
251 | |
252 // | |
253 // Show a message describing the connection status. | |
254 // To hide the connection status label, use (msg == null). | |
255 // | |
256 | |
257 void showConnectionStatus(String msg) { | |
258 System.out.println(msg); | |
259 } | |
260 | |
261 // | |
262 // Show an authentication panel. | |
263 // | |
264 | |
265 String askPassword() throws Exception { | |
266 /* | |
30 | 267 * showConnectionStatus(null); |
13 | 268 * AuthPanel authPanel = new AuthPanel(this); |
269 * | |
270 * GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = | |
271 * GridBagConstraints.REMAINDER; gbc.anchor = | |
272 * GridBagConstraints.NORTHWEST; gbc.weightx = 1.0; gbc.weighty = 1.0; | |
273 * gbc.ipadx = 100; gbc.ipady = 50; gridbag.setConstraints(authPanel, | |
274 * gbc); vncContainer.add(authPanel); | |
275 | |
276 | |
277 authPanel.moveFocusToDefaultField(); | |
278 vncContainer.remove(authPanel); | |
279 */ | |
30 | 280 showConnectionStatus("ask password..."); |
31 | 281 String pw = mainArgs[2]; |
13 | 282 return pw; |
283 } | |
284 | |
285 // | |
286 // Do the rest of the protocol initialisation. | |
287 // | |
288 | |
289 void doProtocolInitialisation() throws IOException { | |
290 rfb.writeClientInit(); | |
291 rfb.readServerInit(); | |
292 | |
293 | |
294 System.out.println("Desktop name is " + rfb.desktopName); | |
295 System.out.println("Desktop size is " + rfb.framebufferWidth + " x " | |
296 + rfb.framebufferHeight); | |
297 | |
298 setEncodings(); | |
299 | |
30 | 300 //showConnectionStatus(null); |
13 | 301 } |
302 | |
303 // | |
304 // Send current encoding list to the RFB server. | |
305 // | |
306 | |
307 int[] encodingsSaved; | |
308 int nEncodingsSaved; | |
309 | |
310 void setEncodings() { | |
311 setEncodings(false); | |
312 } | |
313 | |
314 void autoSelectEncodings() { | |
315 setEncodings(true); | |
316 } | |
317 | |
318 void setEncodings(boolean autoSelectOnly) { | |
319 if (options == null || rfb == null || !rfb.inNormalProtocol) | |
320 return; | |
321 | |
322 int preferredEncoding = options.preferredEncoding; | |
323 if (preferredEncoding == -1) { | |
324 long kbitsPerSecond = rfb.kbitsPerSecond(); | |
126 | 325 |
13 | 326 if (nEncodingsSaved < 1) { |
327 // Choose Tight or ZRLE encoding for the very first update. | |
328 System.out.println("Using Tight/ZRLE encodings"); | |
127 | 329 // preferredEncoding = RfbProto.EncodingTight; |
13 | 330 } else if (kbitsPerSecond > 2000 |
331 && encodingsSaved[0] != RfbProto.EncodingHextile) { | |
332 // Switch to Hextile if the connection speed is above 2Mbps. | |
333 System.out.println("Throughput " + kbitsPerSecond | |
334 + " kbit/s - changing to Hextile encoding"); | |
335 preferredEncoding = RfbProto.EncodingHextile; | |
336 } else if (kbitsPerSecond < 1000 | |
337 && encodingsSaved[0] != RfbProto.EncodingTight) { | |
338 // Switch to Tight/ZRLE if the connection speed is below 1Mbps. | |
339 System.out.println("Throughput " + kbitsPerSecond | |
340 + " kbit/s - changing to Tight/ZRLE encodings"); | |
127 | 341 // preferredEncoding = RfbProto.EncodingTight; |
13 | 342 } else { |
343 // Don't change the encoder. | |
344 if (autoSelectOnly) | |
345 return; | |
346 preferredEncoding = encodingsSaved[0]; | |
347 } | |
348 } else { | |
349 // Auto encoder selection is not enabled. | |
350 if (autoSelectOnly) | |
351 return; | |
352 } | |
353 | |
354 int[] encodings = new int[20]; | |
355 int nEncodings = 0; | |
356 | |
126 | 357 |
13 | 358 encodings[nEncodings++] = preferredEncoding; |
359 if (options.useCopyRect) { | |
360 encodings[nEncodings++] = RfbProto.EncodingCopyRect; | |
361 } | |
127 | 362 /* |
13 | 363 if (preferredEncoding != RfbProto.EncodingTight) { |
364 encodings[nEncodings++] = RfbProto.EncodingTight; | |
365 } | |
366 if (preferredEncoding != RfbProto.EncodingZRLE) { | |
367 encodings[nEncodings++] = RfbProto.EncodingZRLE; | |
368 } | |
127 | 369 */ |
13 | 370 if (preferredEncoding != RfbProto.EncodingHextile) { |
371 encodings[nEncodings++] = RfbProto.EncodingHextile; | |
372 } | |
127 | 373 /* |
13 | 374 if (preferredEncoding != RfbProto.EncodingZlib) { |
375 encodings[nEncodings++] = RfbProto.EncodingZlib; | |
376 } | |
127 | 377 |
13 | 378 if (preferredEncoding != RfbProto.EncodingCoRRE) { |
379 encodings[nEncodings++] = RfbProto.EncodingCoRRE; | |
380 } | |
381 if (preferredEncoding != RfbProto.EncodingRRE) { | |
382 encodings[nEncodings++] = RfbProto.EncodingRRE; | |
383 } | |
127 | 384 /* |
13 | 385 if (options.compressLevel >= 0 && options.compressLevel <= 9) { |
386 encodings[nEncodings++] = RfbProto.EncodingCompressLevel0 | |
387 + options.compressLevel; | |
388 } | |
389 if (options.jpegQuality >= 0 && options.jpegQuality <= 9) { | |
390 encodings[nEncodings++] = RfbProto.EncodingQualityLevel0 | |
391 + options.jpegQuality; | |
392 } | |
393 if (options.requestCursorUpdates) { | |
394 encodings[nEncodings++] = RfbProto.EncodingXCursor; | |
395 encodings[nEncodings++] = RfbProto.EncodingRichCursor; | |
396 if (!options.ignoreCursorUpdates) | |
397 encodings[nEncodings++] = RfbProto.EncodingPointerPos; | |
398 } | |
78 | 399 */ |
400 | |
13 | 401 encodings[nEncodings++] = RfbProto.EncodingLastRect; |
402 encodings[nEncodings++] = RfbProto.EncodingNewFBSize; | |
403 | |
404 boolean encodingsWereChanged = false; | |
405 if (nEncodings != nEncodingsSaved) { | |
406 encodingsWereChanged = true; | |
407 } else { | |
408 for (int i = 0; i < nEncodings; i++) { | |
409 if (encodings[i] != encodingsSaved[i]) { | |
410 encodingsWereChanged = true; | |
411 break; | |
412 } | |
413 } | |
414 } | |
415 | |
416 if (encodingsWereChanged) { | |
417 try { | |
418 rfb.writeSetEncodings(encodings, nEncodings); | |
419 if (vc != null) { | |
420 vc.softCursorFree(); | |
421 } | |
422 } catch (Exception e) { | |
423 e.printStackTrace(); | |
424 } | |
425 encodingsSaved = encodings; | |
426 nEncodingsSaved = nEncodings; | |
427 } | |
428 } | |
429 | |
430 // | |
431 // setCutText() - send the given cut text to the RFB server. | |
432 // | |
433 | |
434 void setCutText(String text) { | |
435 try { | |
436 if (rfb != null && rfb.inNormalProtocol) { | |
437 rfb.writeClientCutText(text); | |
438 } | |
439 } catch (Exception e) { | |
440 e.printStackTrace(); | |
441 } | |
442 } | |
443 | |
444 // | |
445 // Order change in session recording status. To stop recording, pass | |
446 // null in place of the fname argument. | |
447 // | |
448 | |
449 void setRecordingStatus(String fname) { | |
450 synchronized (recordingSync) { | |
451 sessionFileName = fname; | |
452 recordingStatusChanged = true; | |
453 } | |
454 } | |
455 | |
456 // | |
457 // Start or stop session recording. Returns true if this method call | |
458 // causes recording of a new session. | |
459 // | |
460 | |
461 boolean checkRecordingStatus() throws IOException { | |
462 synchronized (recordingSync) { | |
463 if (recordingStatusChanged) { | |
464 recordingStatusChanged = false; | |
465 if (sessionFileName != null) { | |
466 startRecording(); | |
467 return true; | |
468 } else { | |
469 stopRecording(); | |
470 } | |
471 } | |
472 } | |
473 return false; | |
474 } | |
475 | |
476 // | |
477 // Start session recording. | |
478 // | |
479 | |
480 protected void startRecording() throws IOException { | |
481 synchronized (recordingSync) { | |
482 if (!recordingActive) { | |
483 // Save settings to restore them after recording the session. | |
484 cursorUpdatesDef = options.choices[options.cursorUpdatesIndex] | |
485 .getSelectedItem(); | |
486 eightBitColorsDef = options.choices[options.eightBitColorsIndex] | |
487 .getSelectedItem(); | |
488 // Set options to values suitable for recording. | |
489 options.choices[options.cursorUpdatesIndex].select("Disable"); | |
490 options.choices[options.cursorUpdatesIndex].setEnabled(false); | |
491 options.setEncodings(); | |
492 options.choices[options.eightBitColorsIndex].select("No"); | |
493 options.choices[options.eightBitColorsIndex].setEnabled(false); | |
494 options.setColorFormat(); | |
495 } else { | |
496 rfb.closeSession(); | |
497 } | |
498 | |
499 System.out.println("Recording the session in " + sessionFileName); | |
500 rfb.startSession(sessionFileName); | |
501 recordingActive = true; | |
502 } | |
503 } | |
504 | |
505 // | |
506 // Stop session recording. | |
507 // | |
508 | |
509 protected void stopRecording() throws IOException { | |
510 synchronized (recordingSync) { | |
511 if (recordingActive) { | |
512 // Restore options. | |
513 options.choices[options.cursorUpdatesIndex] | |
514 .select(cursorUpdatesDef); | |
515 options.choices[options.cursorUpdatesIndex].setEnabled(true); | |
516 options.setEncodings(); | |
517 options.choices[options.eightBitColorsIndex] | |
518 .select(eightBitColorsDef); | |
519 options.choices[options.eightBitColorsIndex].setEnabled(true); | |
520 options.setColorFormat(); | |
521 | |
522 rfb.closeSession(); | |
523 System.out.println("Session recording stopped."); | |
524 } | |
525 sessionFileName = null; | |
526 recordingActive = false; | |
527 } | |
528 } | |
529 | |
530 // | |
531 // readParameters() - read parameters from the html source or from the | |
532 // command line. On the command line, the arguments are just a sequence of | |
533 // param_name/param_value pairs where the names and values correspond to | |
534 // those expected in the html applet tag source. | |
535 // | |
536 | |
537 void readParameters() { | |
538 | |
30 | 539 host = mainArgs[0]; |
31 | 540 if(mainArgs.length < 2){ |
30 | 541 port = 5900; |
542 }else{ | |
31 | 543 port = Integer.parseInt(mainArgs[1]); |
30 | 544 } |
545 | |
13 | 546 |
547 // Read "ENCPASSWORD" or "PASSWORD" parameter if specified. | |
548 // readPasswordParameters(); | |
549 | |
550 String str; | |
551 | |
552 // "Show Controls" set to "No" disables button panel. | |
553 showControls = true; | |
554 str = readParameter("Show Controls", false); | |
555 if (str != null && str.equalsIgnoreCase("No")) | |
556 showControls = false; | |
557 | |
558 // "Offer Relogin" set to "No" disables "Login again" and "Close | |
559 // window" buttons under error messages in applet mode. | |
560 offerRelogin = true; | |
561 str = readParameter("Offer Relogin", false); | |
562 if (str != null && str.equalsIgnoreCase("No")) | |
563 offerRelogin = false; | |
564 | |
565 // Do we continue showing desktop on remote disconnect? | |
566 showOfflineDesktop = false; | |
567 str = readParameter("Show Offline Desktop", false); | |
568 if (str != null && str.equalsIgnoreCase("Yes")) | |
569 showOfflineDesktop = true; | |
570 | |
571 // Fine tuning options. | |
572 deferScreenUpdates = readIntParameter("Defer screen updates", 20); | |
573 deferCursorUpdates = readIntParameter("Defer cursor updates", 10); | |
574 deferUpdateRequests = readIntParameter("Defer update requests", 0); | |
575 | |
576 // Debugging options. | |
577 debugStatsExcludeUpdates = readIntParameter("DEBUG_XU", 0); | |
578 debugStatsMeasureUpdates = readIntParameter("DEBUG_CU", 0); | |
579 | |
580 // SocketFactory. | |
581 socketFactory = readParameter("SocketFactory", false); | |
582 } | |
583 | |
584 // | |
585 // Read password parameters. If an "ENCPASSWORD" parameter is set, | |
586 // then decrypt the password into the passwordParam string. Otherwise, | |
587 // try to read the "PASSWORD" parameter directly to passwordParam. | |
588 // | |
589 | |
590 private void readPasswordParameters() { | |
591 // String encPasswordParam = readParameter("ENCPASSWORD", false); | |
31 | 592 String encPasswordParam = mainArgs[2]; |
13 | 593 |
594 if (encPasswordParam == null) { | |
595 // passwordParam = readParameter("PASSWORD", false); | |
596 | |
597 } else { | |
598 // ENCPASSWORD is hexascii-encoded. Decode. | |
599 byte[] pw = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
600 int len = encPasswordParam.length() / 2; | |
601 if (len > 8) | |
602 len = 8; | |
603 for (int i = 0; i < len; i++) { | |
604 String hex = encPasswordParam.substring(i * 2, i * 2 + 2); | |
605 Integer x = new Integer(Integer.parseInt(hex, 16)); | |
606 pw[i] = x.byteValue(); | |
607 } | |
608 // Decrypt the password. | |
609 byte[] key = { 23, 82, 107, 6, 35, 78, 88, 7 }; | |
610 DesCipher des = new DesCipher(key); | |
611 des.decrypt(pw, 0, pw, 0); | |
612 passwordParam = new String(pw); | |
613 | |
614 } | |
615 } | |
616 | |
617 public String readParameter(String name, boolean required) { | |
618 for (int i = 0; i < mainArgs.length; i += 2) { | |
619 if (mainArgs[i].equalsIgnoreCase(name)) { | |
620 try { | |
621 return mainArgs[i + 1]; | |
622 } catch (Exception e) { | |
623 if (required) { | |
624 fatalError(name + " parameter not specified"); | |
625 } | |
626 return null; | |
627 } | |
628 } | |
629 } | |
630 if (required) { | |
631 fatalError(name + " parameter not specified"); | |
632 } | |
633 return null; | |
634 } | |
635 | |
636 int readIntParameter(String name, int defaultValue) { | |
637 String str = readParameter(name, false); | |
638 int result = defaultValue; | |
639 if (str != null) { | |
640 try { | |
641 result = Integer.parseInt(str); | |
642 } catch (NumberFormatException e) { | |
643 } | |
644 } | |
645 return result; | |
646 } | |
647 | |
648 // | |
649 // disconnect() - close connection to server. | |
650 // | |
651 | |
652 synchronized public void disconnect() { | |
653 System.out.println("Disconnecting"); | |
654 | |
655 if (vc != null) { | |
656 double sec = (System.currentTimeMillis() - vc.statStartTime) / 1000.0; | |
657 double rate = Math.round(vc.statNumUpdates / sec * 100) / 100.0; | |
658 int nRealRects = vc.statNumPixelRects; | |
659 int nPseudoRects = vc.statNumTotalRects - vc.statNumPixelRects; | |
660 System.out.println("Updates received: " + vc.statNumUpdates + " (" | |
661 + nRealRects + " rectangles + " + nPseudoRects | |
662 + " pseudo), " + rate + " updates/sec"); | |
663 int numRectsOther = nRealRects - vc.statNumRectsTight | |
664 - vc.statNumRectsZRLE - vc.statNumRectsHextile | |
665 - vc.statNumRectsRaw - vc.statNumRectsCopy; | |
666 System.out.println("Rectangles:" + " Tight=" + vc.statNumRectsTight | |
667 + "(JPEG=" + vc.statNumRectsTightJPEG + ") ZRLE=" | |
668 + vc.statNumRectsZRLE + " Hextile=" | |
669 + vc.statNumRectsHextile + " Raw=" + vc.statNumRectsRaw | |
670 + " CopyRect=" + vc.statNumRectsCopy + " other=" | |
671 + numRectsOther); | |
672 | |
673 int raw = vc.statNumBytesDecoded; | |
674 int compressed = vc.statNumBytesEncoded; | |
675 if (compressed > 0) { | |
676 double ratio = Math.round((double) raw / compressed * 1000) / 1000.0; | |
677 System.out.println("Pixel data: " + vc.statNumBytesDecoded | |
678 + " bytes, " + vc.statNumBytesEncoded | |
679 + " compressed, ratio " + ratio); | |
680 } | |
681 } | |
682 | |
683 if (rfb != null && !rfb.closed()) | |
684 rfb.close(); | |
67 | 685 // options.dispose(); |
13 | 686 clipboard.dispose(); |
687 if (rec != null) | |
688 rec.dispose(); | |
689 | |
690 System.exit(0); | |
691 | |
692 } | |
693 | |
694 // | |
695 // fatalError() - print out a fatal error message. | |
696 // FIXME: Do we really need two versions of the fatalError() method? | |
697 // | |
698 | |
699 synchronized public void fatalError(String str) { | |
700 System.out.println(str); | |
701 System.exit(1); | |
702 } | |
703 | |
704 synchronized public void fatalError(String str, Exception e) { | |
705 | |
706 if (rfb != null && rfb.closed()) { | |
707 // Not necessary to show error message if the error was caused | |
708 // by I/O problems after the rfb.close() method call. | |
709 System.out.println("RFB thread finished"); | |
710 return; | |
711 } | |
712 | |
713 System.out.println(str); | |
714 e.printStackTrace(); | |
715 | |
716 if (rfb != null) | |
717 rfb.close(); | |
718 | |
719 System.exit(1); | |
720 | |
721 } | |
722 | |
723 // | |
724 // Show message text and optionally "Relogin" and "Close" buttons. | |
725 // | |
726 | |
727 void showMessage(String msg) { | |
728 vncContainer.removeAll(); | |
729 | |
730 Label errLabel = new Label(msg, Label.CENTER); | |
731 errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12)); | |
732 | |
733 if (offerRelogin) { | |
734 /* | |
735 * Panel gridPanel = new Panel(new GridLayout(0, 1)); Panel | |
736 * outerPanel = new Panel(new FlowLayout(FlowLayout.LEFT)); | |
737 * outerPanel.add(gridPanel); vncContainer.setLayout(new | |
738 * FlowLayout(FlowLayout.LEFT, 30, 16)); | |
739 * vncContainer.add(outerPanel); Panel textPanel = new Panel(new | |
740 * FlowLayout(FlowLayout.CENTER)); textPanel.add(errLabel); | |
741 * gridPanel.add(textPanel); gridPanel.add(new ReloginPanel(this)); | |
742 */ | |
743 } else { | |
744 /* | |
745 * vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30)); | |
746 * vncContainer.add(errLabel); | |
747 */ | |
748 } | |
749 | |
750 } | |
751 | |
752 // | |
753 // Stop the applet. | |
754 // Main applet thread will terminate on first exception | |
755 // after seeing that rfbThread has been set to null. | |
756 // | |
757 | |
758 public void stop() { | |
759 System.out.println("Stopping applet"); | |
760 rfbThread = null; | |
761 } | |
762 | |
763 // | |
764 // This method is called before the applet is destroyed. | |
765 // | |
766 | |
767 public void destroy() { | |
768 System.out.println("Destroying applet"); | |
769 | |
770 vncContainer.removeAll(); | |
67 | 771 // options.dispose(); |
13 | 772 clipboard.dispose(); |
773 if (rec != null) | |
774 rec.dispose(); | |
775 if (rfb != null && !rfb.closed()) | |
776 rfb.close(); | |
777 } | |
778 | |
779 // | |
780 // Start/stop receiving mouse events. | |
781 // | |
782 | |
783 public void enableInput(boolean enable) { | |
784 vc.enableInput(enable); | |
785 } | |
786 | |
787 // | |
788 // Close application properly on window close event. | |
789 // | |
790 | |
791 public void windowClosing(WindowEvent evt) { | |
792 System.out.println("Closing window"); | |
793 if (rfb != null) | |
794 disconnect(); | |
795 | |
796 vncContainer.hide(); | |
797 | |
798 } | |
799 | |
800 // | |
801 // Ignore window events we're not interested in. | |
802 // | |
803 | |
804 public void windowActivated(WindowEvent evt) { | |
805 } | |
806 | |
807 public void windowDeactivated(WindowEvent evt) { | |
808 } | |
809 | |
810 public void windowOpened(WindowEvent evt) { | |
811 } | |
812 | |
813 public void windowClosed(WindowEvent evt) { | |
814 } | |
815 | |
816 public void windowIconified(WindowEvent evt) { | |
817 } | |
818 | |
819 public void windowDeiconified(WindowEvent evt) { | |
820 } | |
821 } |