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