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