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