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);
|
80
|
306 rfb = new MyRfbProto(pHost, port);
|
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) {
|
91
|
320 boolean useEchoPortFlag = rfb.readProxyFlag();
|
|
321 if (useEchoPortFlag) {
|
90
|
322 byte[] b = new byte[4];
|
|
323 b = rfb.readEchoPort();
|
|
324 echoPort = castByteInt(b);
|
76
|
325
|
90
|
326 InetAddress addr = InetAddress.getByName(pHost);
|
|
327 String h = new String(addr.getHostAddress());
|
|
328
|
|
329 getParentName();
|
|
330 if (!(h.equals(host))) {
|
|
331 rfb.changeParent(host, port);
|
|
332 rfb.readVersionMsg();
|
|
333 rfb.writeVersionMsg();
|
|
334 }
|
74
|
335 }
|
90
|
336 }
|
74
|
337
|
60
|
338 int secType = rfb.negotiateSecurity();
|
|
339 int authType;
|
|
340 if (secType == RfbProto.SecTypeTight) {
|
|
341 showConnectionStatus("Enabling TightVNC protocol extensions");
|
|
342 rfb.setupTunneling();
|
|
343 authType = rfb.negotiateAuthenticationTight();
|
|
344 } else {
|
|
345 authType = secType;
|
|
346 }
|
|
347
|
|
348 switch (authType) {
|
|
349 case RfbProto.AuthNone:
|
|
350 showConnectionStatus("No authentication needed");
|
|
351 rfb.authenticateNone();
|
|
352 break;
|
|
353 case RfbProto.AuthVNC:
|
|
354 showConnectionStatus("Performing standard VNC authentication");
|
|
355 if (passwordParam != null) {
|
|
356 rfb.authenticateVNC(passwordParam);
|
|
357 } else {
|
|
358 String pw = askPassword();
|
|
359 rfb.authenticateVNC(pw);
|
|
360 }
|
|
361 break;
|
|
362 default:
|
|
363 throw new Exception("Unknown authentication scheme " + authType);
|
|
364 }
|
|
365 }
|
|
366
|
70
|
367
|
|
368
|
69
|
369 void reConnectAndAuthenticate() throws Exception {
|
|
370 showConnectionStatus("Initializing...");
|
|
371
|
|
372 showConnectionStatus("Connecting to " + host + ", port " + port + "...");
|
|
373
|
|
374 rfb.changeParent(host, port);
|
|
375
|
|
376 showConnectionStatus("Connected to server");
|
|
377
|
|
378 rfb.readVersionMsg();
|
|
379 showConnectionStatus("RFB server supports protocol version "
|
|
380 + rfb.serverMajor + "." + rfb.serverMinor);
|
|
381
|
|
382 rfb.writeVersionMsg();
|
|
383 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
|
|
384 + "." + rfb.clientMinor);
|
|
385
|
90
|
386 if (rfb.serverMinor == 855) {
|
91
|
387 boolean useEchoPortFlag = rfb.readProxyFlag();
|
|
388 if (useEchoPortFlag) {
|
90
|
389 byte[] b = new byte[4];
|
|
390 b = rfb.readEchoPort();
|
|
391 echoPort = castByteInt(b);
|
79
|
392
|
90
|
393 InetAddress addr = InetAddress.getByName(pHost);
|
|
394 String h = new String(addr.getHostAddress());
|
|
395
|
|
396 getParentName();
|
|
397 if (!(h.equals(host))) {
|
|
398 rfb.changeParent(host, port);
|
|
399 rfb.readVersionMsg();
|
|
400 rfb.writeVersionMsg();
|
|
401 }
|
79
|
402 }
|
90
|
403 }
|
79
|
404
|
69
|
405 int secType = rfb.negotiateSecurity();
|
|
406 int authType;
|
|
407 if (secType == RfbProto.SecTypeTight) {
|
|
408 showConnectionStatus("Enabling TightVNC protocol extensions");
|
|
409 rfb.setupTunneling();
|
|
410 authType = rfb.negotiateAuthenticationTight();
|
|
411 } else {
|
|
412 authType = secType;
|
|
413 }
|
|
414
|
|
415 switch (authType) {
|
|
416 case RfbProto.AuthNone:
|
|
417 showConnectionStatus("No authentication needed");
|
|
418 rfb.authenticateNone();
|
|
419 break;
|
|
420 case RfbProto.AuthVNC:
|
|
421 showConnectionStatus("Performing standard VNC authentication");
|
|
422 if (passwordParam != null) {
|
|
423 rfb.authenticateVNC(passwordParam);
|
|
424 } else {
|
|
425 String pw = askPassword();
|
|
426 rfb.authenticateVNC(pw);
|
|
427 }
|
|
428 break;
|
|
429 default:
|
|
430 throw new Exception("Unknown authentication scheme " + authType);
|
|
431 }
|
|
432 }
|
|
433
|
60
|
434 //
|
|
435 // Show a message describing the connection status.
|
|
436 // To hide the connection status label, use (msg == null).
|
|
437 //
|
|
438
|
|
439 void showConnectionStatus(String msg) {
|
|
440 System.out.println(msg);
|
|
441 }
|
|
442
|
|
443 //
|
|
444 // Show an authentication panel.
|
|
445 //
|
|
446
|
|
447 String askPassword() throws Exception {
|
|
448 /*
|
65
|
449 * showConnectionStatus(null); AuthPanel authPanel = new
|
|
450 * AuthPanel(this);
|
60
|
451 *
|
|
452 * GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth =
|
|
453 * GridBagConstraints.REMAINDER; gbc.anchor =
|
|
454 * GridBagConstraints.NORTHWEST; gbc.weightx = 1.0; gbc.weighty = 1.0;
|
|
455 * gbc.ipadx = 100; gbc.ipady = 50; gridbag.setConstraints(authPanel,
|
|
456 * gbc); vncContainer.add(authPanel);
|
65
|
457 *
|
|
458 *
|
|
459 * authPanel.moveFocusToDefaultField(); vncContainer.remove(authPanel);
|
60
|
460 */
|
|
461 showConnectionStatus("ask password...");
|
|
462 String pw = mainArgs[2];
|
|
463 return pw;
|
|
464 }
|
|
465
|
|
466 //
|
|
467 // Do the rest of the protocol initialisation.
|
|
468 //
|
|
469
|
|
470 void doProtocolInitialisation() throws IOException {
|
|
471 rfb.writeClientInit();
|
|
472 rfb.readServerInit();
|
|
473
|
|
474 System.out.println("Desktop name is " + rfb.desktopName);
|
|
475 System.out.println("Desktop size is " + rfb.framebufferWidth + " x "
|
|
476 + rfb.framebufferHeight);
|
|
477
|
|
478 setEncodings();
|
|
479
|
65
|
480 // showConnectionStatus(null);
|
60
|
481 }
|
|
482
|
|
483 //
|
|
484 // Send current encoding list to the RFB server.
|
|
485 //
|
|
486
|
|
487 int[] encodingsSaved;
|
|
488 int nEncodingsSaved;
|
|
489
|
|
490 void setEncodings() {
|
|
491 setEncodings(false);
|
|
492 }
|
|
493
|
|
494 void autoSelectEncodings() {
|
|
495 setEncodings(true);
|
|
496 }
|
|
497
|
|
498 void setEncodings(boolean autoSelectOnly) {
|
|
499 if (options == null || rfb == null || !rfb.inNormalProtocol)
|
|
500 return;
|
|
501
|
|
502 int preferredEncoding = options.preferredEncoding;
|
|
503 if (preferredEncoding == -1) {
|
|
504 long kbitsPerSecond = rfb.kbitsPerSecond();
|
|
505 if (nEncodingsSaved < 1) {
|
|
506 // Choose Tight or ZRLE encoding for the very first update.
|
|
507 System.out.println("Using Tight/ZRLE encodings");
|
|
508 preferredEncoding = RfbProto.EncodingTight;
|
|
509 } else if (kbitsPerSecond > 2000
|
|
510 && encodingsSaved[0] != RfbProto.EncodingHextile) {
|
|
511 // Switch to Hextile if the connection speed is above 2Mbps.
|
|
512 System.out.println("Throughput " + kbitsPerSecond
|
|
513 + " kbit/s - changing to Hextile encoding");
|
|
514 preferredEncoding = RfbProto.EncodingHextile;
|
|
515 } else if (kbitsPerSecond < 1000
|
|
516 && encodingsSaved[0] != RfbProto.EncodingTight) {
|
|
517 // Switch to Tight/ZRLE if the connection speed is below 1Mbps.
|
|
518 System.out.println("Throughput " + kbitsPerSecond
|
|
519 + " kbit/s - changing to Tight/ZRLE encodings");
|
|
520 preferredEncoding = RfbProto.EncodingTight;
|
|
521 } else {
|
|
522 // Don't change the encoder.
|
|
523 if (autoSelectOnly)
|
|
524 return;
|
|
525 preferredEncoding = encodingsSaved[0];
|
|
526 }
|
|
527 } else {
|
|
528 // Auto encoder selection is not enabled.
|
|
529 if (autoSelectOnly)
|
|
530 return;
|
|
531 }
|
|
532
|
|
533 int[] encodings = new int[20];
|
|
534 int nEncodings = 0;
|
|
535
|
|
536 encodings[nEncodings++] = preferredEncoding;
|
|
537 if (options.useCopyRect) {
|
|
538 encodings[nEncodings++] = RfbProto.EncodingCopyRect;
|
|
539 }
|
|
540 if (preferredEncoding != RfbProto.EncodingTight) {
|
|
541 encodings[nEncodings++] = RfbProto.EncodingTight;
|
|
542 }
|
|
543 if (preferredEncoding != RfbProto.EncodingZRLE) {
|
|
544 encodings[nEncodings++] = RfbProto.EncodingZRLE;
|
|
545 }
|
|
546 if (preferredEncoding != RfbProto.EncodingHextile) {
|
|
547 encodings[nEncodings++] = RfbProto.EncodingHextile;
|
|
548 }
|
|
549 if (preferredEncoding != RfbProto.EncodingZlib) {
|
|
550 encodings[nEncodings++] = RfbProto.EncodingZlib;
|
|
551 }
|
|
552 /*
|
65
|
553 * if (preferredEncoding != RfbProto.EncodingCoRRE) {
|
|
554 * encodings[nEncodings++] = RfbProto.EncodingCoRRE; } if
|
|
555 * (preferredEncoding != RfbProto.EncodingRRE) { encodings[nEncodings++]
|
|
556 * = RfbProto.EncodingRRE; }
|
|
557 *
|
|
558 * if (options.compressLevel >= 0 && options.compressLevel <= 9) {
|
|
559 * encodings[nEncodings++] = RfbProto.EncodingCompressLevel0 +
|
|
560 * options.compressLevel; } if (options.jpegQuality >= 0 &&
|
|
561 * options.jpegQuality <= 9) { encodings[nEncodings++] =
|
|
562 * RfbProto.EncodingQualityLevel0 + options.jpegQuality; } if
|
|
563 * (options.requestCursorUpdates) { encodings[nEncodings++] =
|
|
564 * RfbProto.EncodingXCursor; encodings[nEncodings++] =
|
|
565 * RfbProto.EncodingRichCursor; if (!options.ignoreCursorUpdates)
|
|
566 * encodings[nEncodings++] = RfbProto.EncodingPointerPos; }
|
|
567 */
|
60
|
568
|
|
569 encodings[nEncodings++] = RfbProto.EncodingLastRect;
|
|
570 encodings[nEncodings++] = RfbProto.EncodingNewFBSize;
|
|
571
|
|
572 boolean encodingsWereChanged = false;
|
|
573 if (nEncodings != nEncodingsSaved) {
|
|
574 encodingsWereChanged = true;
|
|
575 } else {
|
|
576 for (int i = 0; i < nEncodings; i++) {
|
|
577 if (encodings[i] != encodingsSaved[i]) {
|
|
578 encodingsWereChanged = true;
|
|
579 break;
|
|
580 }
|
|
581 }
|
|
582 }
|
|
583
|
|
584 if (encodingsWereChanged) {
|
|
585 try {
|
65
|
586 // rfb.writeSetEncodings(encodings, nEncodings);
|
60
|
587 if (vc != null) {
|
|
588 vc.softCursorFree();
|
|
589 }
|
|
590 } catch (Exception e) {
|
|
591 e.printStackTrace();
|
|
592 }
|
|
593 encodingsSaved = encodings;
|
|
594 nEncodingsSaved = nEncodings;
|
|
595 }
|
|
596 }
|
|
597
|
|
598 //
|
|
599 // setCutText() - send the given cut text to the RFB server.
|
|
600 //
|
|
601
|
|
602 void setCutText(String text) {
|
|
603 try {
|
|
604 if (rfb != null && rfb.inNormalProtocol) {
|
|
605 rfb.writeClientCutText(text);
|
|
606 }
|
|
607 } catch (Exception e) {
|
|
608 e.printStackTrace();
|
|
609 }
|
|
610 }
|
|
611
|
|
612 //
|
|
613 // Order change in session recording status. To stop recording, pass
|
|
614 // null in place of the fname argument.
|
|
615 //
|
|
616
|
|
617 void setRecordingStatus(String fname) {
|
|
618 synchronized (recordingSync) {
|
|
619 sessionFileName = fname;
|
|
620 recordingStatusChanged = true;
|
|
621 }
|
|
622 }
|
|
623
|
|
624 //
|
|
625 // Start or stop session recording. Returns true if this method call
|
|
626 // causes recording of a new session.
|
|
627 //
|
|
628
|
|
629 boolean checkRecordingStatus() throws IOException {
|
|
630 synchronized (recordingSync) {
|
|
631 if (recordingStatusChanged) {
|
|
632 recordingStatusChanged = false;
|
|
633 if (sessionFileName != null) {
|
|
634 startRecording();
|
|
635 return true;
|
|
636 } else {
|
|
637 stopRecording();
|
|
638 }
|
|
639 }
|
|
640 }
|
|
641 return false;
|
|
642 }
|
|
643
|
|
644 //
|
|
645 // Start session recording.
|
|
646 //
|
|
647
|
|
648 protected void startRecording() throws IOException {
|
|
649 synchronized (recordingSync) {
|
|
650 if (!recordingActive) {
|
|
651 // Save settings to restore them after recording the session.
|
|
652 cursorUpdatesDef = options.choices[options.cursorUpdatesIndex]
|
|
653 .getSelectedItem();
|
|
654 eightBitColorsDef = options.choices[options.eightBitColorsIndex]
|
|
655 .getSelectedItem();
|
|
656 // Set options to values suitable for recording.
|
|
657 options.choices[options.cursorUpdatesIndex].select("Disable");
|
|
658 options.choices[options.cursorUpdatesIndex].setEnabled(false);
|
|
659 options.setEncodings();
|
|
660 options.choices[options.eightBitColorsIndex].select("No");
|
|
661 options.choices[options.eightBitColorsIndex].setEnabled(false);
|
|
662 options.setColorFormat();
|
|
663 } else {
|
|
664 rfb.closeSession();
|
|
665 }
|
|
666
|
|
667 System.out.println("Recording the session in " + sessionFileName);
|
|
668 rfb.startSession(sessionFileName);
|
|
669 recordingActive = true;
|
|
670 }
|
|
671 }
|
|
672
|
|
673 //
|
|
674 // Stop session recording.
|
|
675 //
|
|
676
|
|
677 protected void stopRecording() throws IOException {
|
|
678 synchronized (recordingSync) {
|
|
679 if (recordingActive) {
|
|
680 // Restore options.
|
|
681 options.choices[options.cursorUpdatesIndex]
|
|
682 .select(cursorUpdatesDef);
|
|
683 options.choices[options.cursorUpdatesIndex].setEnabled(true);
|
|
684 options.setEncodings();
|
|
685 options.choices[options.eightBitColorsIndex]
|
|
686 .select(eightBitColorsDef);
|
|
687 options.choices[options.eightBitColorsIndex].setEnabled(true);
|
|
688 options.setColorFormat();
|
|
689
|
|
690 rfb.closeSession();
|
|
691 System.out.println("Session recording stopped.");
|
|
692 }
|
|
693 sessionFileName = null;
|
|
694 recordingActive = false;
|
|
695 }
|
|
696 }
|
|
697
|
|
698 //
|
|
699 // readParameters() - read parameters from the html source or from the
|
|
700 // command line. On the command line, the arguments are just a sequence of
|
|
701 // param_name/param_value pairs where the names and values correspond to
|
|
702 // those expected in the html applet tag source.
|
|
703 //
|
|
704
|
74
|
705 void readParameters() {
|
|
706 /*
|
60
|
707 * host = readParameter("HOST", !inAnApplet);
|
|
708 *
|
|
709 * if (host == null) { host = getCodeBase().getHost(); if
|
|
710 * (host.equals("")) { fatalError("HOST parameter not specified"); } }
|
|
711 *
|
|
712 * port = readIntParameter("PORT", 5550);
|
|
713 */
|
|
714
|
|
715 // Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
|
65
|
716 // readPasswordParameters();
|
60
|
717
|
|
718 String str;
|
|
719 if (inAnApplet) {
|
|
720 str = readParameter("Open New Window", false);
|
|
721 if (str != null && str.equalsIgnoreCase("Yes"))
|
|
722 inSeparateFrame = true;
|
|
723 }
|
|
724
|
|
725 // "Show Controls" set to "No" disables button panel.
|
|
726 showControls = true;
|
|
727 str = readParameter("Show Controls", false);
|
|
728 if (str != null && str.equalsIgnoreCase("No"))
|
|
729 showControls = false;
|
|
730
|
|
731 // "Offer Relogin" set to "No" disables "Login again" and "Close
|
|
732 // window" buttons under error messages in applet mode.
|
|
733 offerRelogin = true;
|
|
734 str = readParameter("Offer Relogin", false);
|
|
735 if (str != null && str.equalsIgnoreCase("No"))
|
|
736 offerRelogin = false;
|
|
737
|
|
738 // Do we continue showing desktop on remote disconnect?
|
|
739 showOfflineDesktop = false;
|
|
740 str = readParameter("Show Offline Desktop", false);
|
|
741 if (str != null && str.equalsIgnoreCase("Yes"))
|
|
742 showOfflineDesktop = true;
|
|
743
|
|
744 // Fine tuning options.
|
|
745 deferScreenUpdates = readIntParameter("Defer screen updates", 20);
|
|
746 deferCursorUpdates = readIntParameter("Defer cursor updates", 10);
|
|
747 deferUpdateRequests = readIntParameter("Defer update requests", 0);
|
|
748
|
|
749 // Debugging options.
|
|
750 debugStatsExcludeUpdates = readIntParameter("DEBUG_XU", 0);
|
|
751 debugStatsMeasureUpdates = readIntParameter("DEBUG_CU", 0);
|
|
752
|
|
753 // SocketFactory.
|
|
754 socketFactory = readParameter("SocketFactory", false);
|
|
755 }
|
|
756
|
|
757 //
|
|
758 // Read password parameters. If an "ENCPASSWORD" parameter is set,
|
|
759 // then decrypt the password into the passwordParam string. Otherwise,
|
|
760 // try to read the "PASSWORD" parameter directly to passwordParam.
|
|
761 //
|
|
762
|
|
763 private void readPasswordParameters() {
|
|
764 String encPasswordParam = readParameter("ENCPASSWORD", false);
|
|
765
|
|
766 if (encPasswordParam == null) {
|
65
|
767 passwordParam = readParameter("PASSWORD", false);
|
60
|
768 } else {
|
|
769 // ENCPASSWORD is hexascii-encoded. Decode.
|
|
770 byte[] pw = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
771 int len = encPasswordParam.length() / 2;
|
|
772 if (len > 8)
|
|
773 len = 8;
|
|
774 for (int i = 0; i < len; i++) {
|
|
775 String hex = encPasswordParam.substring(i * 2, i * 2 + 2);
|
|
776 Integer x = new Integer(Integer.parseInt(hex, 16));
|
|
777 pw[i] = x.byteValue();
|
|
778 }
|
|
779 // Decrypt the password.
|
|
780 byte[] key = { 23, 82, 107, 6, 35, 78, 88, 7 };
|
|
781 DesCipher des = new DesCipher(key);
|
|
782 des.decrypt(pw, 0, pw, 0);
|
|
783 passwordParam = new String(pw);
|
|
784
|
|
785 }
|
|
786 }
|
|
787
|
|
788 public String readParameter(String name, boolean required) {
|
|
789 for (int i = 0; i < mainArgs.length; i += 2) {
|
|
790 if (mainArgs[i].equalsIgnoreCase(name)) {
|
|
791 try {
|
|
792 return mainArgs[i + 1];
|
|
793 } catch (Exception e) {
|
|
794 if (required) {
|
|
795 fatalError(name + " parameter not specified");
|
|
796 }
|
|
797 return null;
|
|
798 }
|
|
799 }
|
|
800 }
|
|
801 if (required) {
|
|
802 fatalError(name + " parameter not specified");
|
|
803 }
|
|
804 return null;
|
|
805 }
|
|
806
|
|
807 int readIntParameter(String name, int defaultValue) {
|
|
808 String str = readParameter(name, false);
|
|
809 int result = defaultValue;
|
|
810 if (str != null) {
|
|
811 try {
|
|
812 result = Integer.parseInt(str);
|
|
813 } catch (NumberFormatException e) {
|
|
814 }
|
|
815 }
|
|
816 return result;
|
|
817 }
|
|
818
|
|
819 //
|
|
820 // disconnect() - close connection to server.
|
|
821 //
|
|
822
|
|
823 synchronized public void disconnect() {
|
|
824 System.out.println("Disconnecting");
|
|
825
|
|
826 if (vc != null) {
|
|
827 double sec = (System.currentTimeMillis() - vc.statStartTime) / 1000.0;
|
|
828 double rate = Math.round(vc.statNumUpdates / sec * 100) / 100.0;
|
|
829 int nRealRects = vc.statNumPixelRects;
|
|
830 int nPseudoRects = vc.statNumTotalRects - vc.statNumPixelRects;
|
|
831 System.out.println("Updates received: " + vc.statNumUpdates + " ("
|
|
832 + nRealRects + " rectangles + " + nPseudoRects
|
|
833 + " pseudo), " + rate + " updates/sec");
|
|
834 int numRectsOther = nRealRects - vc.statNumRectsTight
|
|
835 - vc.statNumRectsZRLE - vc.statNumRectsHextile
|
|
836 - vc.statNumRectsRaw - vc.statNumRectsCopy;
|
|
837 System.out.println("Rectangles:" + " Tight=" + vc.statNumRectsTight
|
|
838 + "(JPEG=" + vc.statNumRectsTightJPEG + ") ZRLE="
|
|
839 + vc.statNumRectsZRLE + " Hextile="
|
|
840 + vc.statNumRectsHextile + " Raw=" + vc.statNumRectsRaw
|
|
841 + " CopyRect=" + vc.statNumRectsCopy + " other="
|
|
842 + numRectsOther);
|
|
843
|
|
844 int raw = vc.statNumBytesDecoded;
|
|
845 int compressed = vc.statNumBytesEncoded;
|
|
846 if (compressed > 0) {
|
|
847 double ratio = Math.round((double) raw / compressed * 1000) / 1000.0;
|
|
848 System.out.println("Pixel data: " + vc.statNumBytesDecoded
|
|
849 + " bytes, " + vc.statNumBytesEncoded
|
|
850 + " compressed, ratio " + ratio);
|
|
851 }
|
|
852 }
|
|
853
|
|
854 if (rfb != null && !rfb.closed())
|
|
855 rfb.close();
|
65
|
856 // options.dispose();
|
60
|
857 clipboard.dispose();
|
|
858 if (rec != null)
|
|
859 rec.dispose();
|
|
860
|
|
861 System.exit(0);
|
|
862
|
|
863 }
|
|
864
|
|
865 //
|
|
866 // fatalError() - print out a fatal error message.
|
|
867 // FIXME: Do we really need two versions of the fatalError() method?
|
|
868 //
|
|
869
|
|
870 synchronized public void fatalError(String str) {
|
|
871 System.out.println(str);
|
|
872 System.exit(1);
|
|
873 }
|
|
874
|
|
875 synchronized public void fatalError(String str, Exception e) {
|
|
876
|
|
877 if (rfb != null && rfb.closed()) {
|
|
878 // Not necessary to show error message if the error was caused
|
|
879 // by I/O problems after the rfb.close() method call.
|
|
880 System.out.println("RFB thread finished");
|
|
881 return;
|
|
882 }
|
|
883
|
|
884 System.out.println(str);
|
|
885 e.printStackTrace();
|
|
886
|
|
887 if (rfb != null)
|
|
888 rfb.close();
|
|
889
|
|
890 System.exit(1);
|
|
891
|
|
892 }
|
|
893
|
|
894 //
|
|
895 // Show message text and optionally "Relogin" and "Close" buttons.
|
|
896 //
|
|
897
|
|
898 void showMessage(String msg) {
|
|
899 vncContainer.removeAll();
|
|
900
|
|
901 Label errLabel = new Label(msg, Label.CENTER);
|
|
902 errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
|
|
903
|
|
904 if (offerRelogin) {
|
|
905 /*
|
|
906 * Panel gridPanel = new Panel(new GridLayout(0, 1)); Panel
|
|
907 * outerPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
|
|
908 * outerPanel.add(gridPanel); vncContainer.setLayout(new
|
|
909 * FlowLayout(FlowLayout.LEFT, 30, 16));
|
|
910 * vncContainer.add(outerPanel); Panel textPanel = new Panel(new
|
|
911 * FlowLayout(FlowLayout.CENTER)); textPanel.add(errLabel);
|
|
912 * gridPanel.add(textPanel); gridPanel.add(new ReloginPanel(this));
|
|
913 */
|
|
914 } else {
|
|
915 /*
|
|
916 * vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
|
|
917 * vncContainer.add(errLabel);
|
|
918 */
|
|
919 }
|
|
920
|
|
921 }
|
|
922
|
|
923 //
|
|
924 // Stop the applet.
|
|
925 // Main applet thread will terminate on first exception
|
|
926 // after seeing that rfbThread has been set to null.
|
|
927 //
|
|
928
|
|
929 public void stop() {
|
|
930 System.out.println("Stopping applet");
|
|
931 rfbThread = null;
|
|
932 }
|
|
933
|
|
934 //
|
|
935 // This method is called before the applet is destroyed.
|
|
936 //
|
|
937
|
|
938 public void destroy() {
|
|
939 System.out.println("Destroying applet");
|
|
940
|
|
941 vncContainer.removeAll();
|
65
|
942 // options.dispose();
|
60
|
943 clipboard.dispose();
|
|
944 if (rec != null)
|
|
945 rec.dispose();
|
|
946 if (rfb != null && !rfb.closed())
|
|
947 rfb.close();
|
|
948 }
|
|
949
|
|
950 //
|
|
951 // Start/stop receiving mouse events.
|
|
952 //
|
|
953
|
|
954 public void enableInput(boolean enable) {
|
|
955 vc.enableInput(enable);
|
|
956 }
|
|
957
|
|
958 //
|
|
959 // Close application properly on window close event.
|
|
960 //
|
|
961
|
|
962 public void windowClosing(WindowEvent evt) {
|
|
963 System.out.println("Closing window");
|
|
964 if (rfb != null)
|
|
965 disconnect();
|
|
966
|
|
967 vncContainer.hide();
|
|
968
|
|
969 }
|
|
970
|
|
971 //
|
|
972 // Ignore window events we're not interested in.
|
|
973 //
|
|
974
|
|
975 public void windowActivated(WindowEvent evt) {
|
|
976 }
|
|
977
|
|
978 public void windowDeactivated(WindowEvent evt) {
|
|
979 }
|
|
980
|
|
981 public void windowOpened(WindowEvent evt) {
|
|
982 }
|
|
983
|
|
984 public void windowClosed(WindowEvent evt) {
|
|
985 }
|
|
986
|
|
987 public void windowIconified(WindowEvent evt) {
|
|
988 }
|
|
989
|
|
990 public void windowDeiconified(WindowEvent evt) {
|
|
991 }
|
|
992
|
|
993 public void setClientSocket(Socket sock) {
|
|
994 clientSocket = sock;
|
|
995 }
|
|
996
|
65
|
997 public void start() {
|
|
998
|
61
|
999 }
|
71
|
1000 public void close() {
|
|
1001 rfb.close();
|
|
1002 }
|
79
|
1003 public void getParentName() {
|
|
1004 if (echoValue == null) {
|
65
|
1005
|
74
|
1006 if (clientSocket == null) {
|
|
1007 String pHost;
|
|
1008 if (mainArgs.length > 0)
|
|
1009 pHost = mainArgs[0];
|
|
1010 else
|
|
1011 pHost = "cls080.ie.u-ryukyu.ac.jp";
|
|
1012 // echo = new EchoClient(pHost, this);
|
79
|
1013 echoValue = new EchoClient(pHost, this, echoPort);
|
|
1014 echoValue.openport();
|
74
|
1015
|
88
|
1016 echoValue = echoValue.requestHostName("1");
|
74
|
1017 } else {
|
79
|
1018 echoValue = new EchoClient();
|
|
1019 echoValue = echoValue.Interruption(clientSocket);
|
74
|
1020 }
|
|
1021 }
|
78
|
1022
|
|
1023 // proxyからの返信で接続先を決定する
|
79
|
1024 host = echoValue.responseLine;
|
|
1025 parent = echoValue.parent;
|
|
1026 if (echoValue.treenum != null) {
|
|
1027 treenum = echoValue.treenum;
|
78
|
1028 } else {
|
79
|
1029 treenum = echoValue.treenum;
|
78
|
1030 }
|
79
|
1031 if (echoValue.leaderflag != null) {
|
|
1032 leaderflag = echoValue.leaderflag;
|
78
|
1033 } else {
|
79
|
1034 leaderflag = echoValue.leaderflag;
|
78
|
1035 }
|
|
1036 System.out.println("Parent =" + parent);
|
|
1037 System.out.println("mynumber =" + treenum);
|
|
1038 System.out.println("connect host =" + host);
|
|
1039 System.out.println("leaderflag(boolean) = " + leaderflag);
|
|
1040
|
|
1041 // port = 5999;
|
74
|
1042
|
|
1043 }
|
79
|
1044 public void setEchoValue(EchoClient value) {
|
|
1045 this.echoValue = value;
|
|
1046 }
|
|
1047
|
74
|
1048
|
|
1049 int castByteInt(byte[] b) {
|
|
1050 ByteBuffer bb = ByteBuffer.wrap(b);
|
79
|
1051 int echoValue = bb.getInt();
|
|
1052 return echoValue;
|
74
|
1053 }
|
91
|
1054 public Image getScreenImage() {
|
|
1055 return vc.rawPixelsImage;
|
|
1056 }
|
|
1057
|
|
1058 public void writeScreenData(byte[] b, String imageFormat) {
|
|
1059 /*
|
|
1060 try{
|
|
1061 vc.drawBufferedImage(b, imageFormat);
|
|
1062 }catch(IOException e){
|
|
1063 e.printStackTrace();
|
|
1064 }
|
|
1065 */
|
|
1066 }
|
74
|
1067
|
60
|
1068 }
|