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