comparison src/org/tn5250j/framework/tn5250/tnvt.java @ 3:e8d2a24e85c6 tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 12:11:10 -0700
parents
children 1b9e27659ef7
comparison
equal deleted inserted replaced
2:a01665cb683d 3:e8d2a24e85c6
1 /**
2 * Title: tnvt.java
3 * Copyright: Copyright (c) 2001 Company:
4 *
5 * @author Kenneth J. Pouncey
6 * @version 0.5
7 *
8 * Description:
9 *
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free Software
12 * Foundation; either version 2, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this software; see the file COPYING. If not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24 package org.tn5250j.framework.tn5250;
25
26 import static org.tn5250j.TN5250jConstants.AID_HELP;
27 import static org.tn5250j.TN5250jConstants.AID_PRINT;
28 import static org.tn5250j.TN5250jConstants.CMD_CLEAR_FORMAT_TABLE;
29 import static org.tn5250j.TN5250jConstants.CMD_CLEAR_UNIT;
30 import static org.tn5250j.TN5250jConstants.CMD_CLEAR_UNIT_ALTERNATE;
31 import static org.tn5250j.TN5250jConstants.CMD_READ_INPUT_FIELDS;
32 import static org.tn5250j.TN5250jConstants.CMD_READ_MDT_FIELDS;
33 import static org.tn5250j.TN5250jConstants.CMD_READ_MDT_IMMEDIATE_ALT;
34 import static org.tn5250j.TN5250jConstants.CMD_READ_SCREEN_IMMEDIATE;
35 import static org.tn5250j.TN5250jConstants.CMD_READ_SCREEN_TO_PRINT;
36 import static org.tn5250j.TN5250jConstants.CMD_RESTORE_SCREEN;
37 import static org.tn5250j.TN5250jConstants.CMD_ROLL;
38 import static org.tn5250j.TN5250jConstants.CMD_SAVE_SCREEN;
39 import static org.tn5250j.TN5250jConstants.CMD_WRITE_ERROR_CODE;
40 import static org.tn5250j.TN5250jConstants.CMD_WRITE_ERROR_CODE_TO_WINDOW;
41 import static org.tn5250j.TN5250jConstants.CMD_WRITE_STRUCTURED_FIELD;
42 import static org.tn5250j.TN5250jConstants.CMD_WRITE_TO_DISPLAY;
43 import static org.tn5250j.TN5250jConstants.NR_REQUEST_ERROR;
44 import static org.tn5250j.TN5250jConstants.PF1;
45 import static org.tn5250j.TN5250jConstants.PF10;
46 import static org.tn5250j.TN5250jConstants.PF11;
47 import static org.tn5250j.TN5250jConstants.PF12;
48 import static org.tn5250j.TN5250jConstants.PF13;
49 import static org.tn5250j.TN5250jConstants.PF14;
50 import static org.tn5250j.TN5250jConstants.PF15;
51 import static org.tn5250j.TN5250jConstants.PF16;
52 import static org.tn5250j.TN5250jConstants.PF17;
53 import static org.tn5250j.TN5250jConstants.PF18;
54 import static org.tn5250j.TN5250jConstants.PF19;
55 import static org.tn5250j.TN5250jConstants.PF2;
56 import static org.tn5250j.TN5250jConstants.PF20;
57 import static org.tn5250j.TN5250jConstants.PF21;
58 import static org.tn5250j.TN5250jConstants.PF22;
59 import static org.tn5250j.TN5250jConstants.PF23;
60 import static org.tn5250j.TN5250jConstants.PF24;
61 import static org.tn5250j.TN5250jConstants.PF3;
62 import static org.tn5250j.TN5250jConstants.PF4;
63 import static org.tn5250j.TN5250jConstants.PF5;
64 import static org.tn5250j.TN5250jConstants.PF6;
65 import static org.tn5250j.TN5250jConstants.PF7;
66 import static org.tn5250j.TN5250jConstants.PF8;
67 import static org.tn5250j.TN5250jConstants.PF9;
68
69 import java.io.BufferedInputStream;
70 import java.io.BufferedOutputStream;
71 import java.io.ByteArrayOutputStream;
72 import java.io.IOException;
73 import java.io.InputStream;
74 import java.io.OutputStream;
75 import java.net.Socket;
76 import java.util.Arrays;
77 import java.util.Properties;
78 import java.util.concurrent.ArrayBlockingQueue;
79 import java.util.concurrent.BlockingQueue;
80
81 import javax.net.ssl.SSLSocket;
82 import javax.swing.SwingUtilities;
83
84 import org.tn5250j.Session5250;
85 import org.tn5250j.TN5250jConstants;
86 import org.tn5250j.encoding.CharMappings;
87 import org.tn5250j.encoding.ICodePage;
88 import org.tn5250j.framework.transport.SocketConnector;
89 import org.tn5250j.tools.logging.TN5250jLogFactory;
90 import org.tn5250j.tools.logging.TN5250jLogger;
91
92 public final class tnvt implements Runnable {
93
94
95 // negotiating commands
96 private static final byte IAC = (byte) -1; // 255 FF
97 private static final byte DONT = (byte) -2; //254 FE
98 private static final byte DO = (byte) -3; //253 FD
99 private static final byte WONT = (byte) -4; //252 FC
100 private static final byte WILL = (byte) -5; //251 FB
101 private static final byte SB = (byte) -6; //250 Sub Begin FA
102 private static final byte SE = (byte) -16; //240 Sub End F0
103 private static final byte EOR = (byte) -17; //239 End of Record EF
104 private static final byte TERMINAL_TYPE = (byte) 24; // 18
105 private static final byte OPT_END_OF_RECORD = (byte) 25; // 19
106 private static final byte TRANSMIT_BINARY = (byte) 0; // 0
107 private static final byte QUAL_IS = (byte) 0; // 0
108 private static final byte TIMING_MARK = (byte) 6; // 6
109 private static final byte NEW_ENVIRONMENT = (byte) 39; // 27
110 private static final byte IS = (byte) 0; // 0
111 private static final byte SEND = (byte) 1; // 1
112 private static final byte INFO = (byte) 2; // 2
113 private static final byte VAR = (byte) 0; // 0
114 private static final byte VALUE = (byte) 1; // 1
115 private static final byte NEGOTIATE_ESC = (byte) 2; // 2
116 private static final byte USERVAR = (byte) 3; // 3
117
118 // miscellaneous
119 private static final byte ESC = 0x04; // 04
120
121 private Socket sock;
122 private BufferedInputStream bin;
123 private BufferedOutputStream bout;
124 private final BlockingQueue<Object> dsq = new ArrayBlockingQueue<Object>(25);
125 private Stream5250 bk;
126 private DataStreamProducer producer;
127 protected Screen5250 screen52;
128 private boolean waitingForInput;
129 private boolean invited;
130 private boolean negotiated = false;
131 private Thread me;
132 private Thread pthread;
133 private int readType;
134 private boolean enhanced = true;
135 private Session5250 controller;
136 private boolean cursorOn = false;
137 private String session = "";
138 private int port = 23;
139 private boolean connected = false;
140 private boolean support132 = true;
141 private ByteArrayOutputStream baosp = null;
142 private ByteArrayOutputStream baosrsp = null;
143 private int devSeq = -1;
144 private String devName;
145 private String devNameUsed;
146 private KbdTypesCodePages kbdTypesCodePage;
147 // WVL - LDC : TR.000300 : Callback scenario from 5250
148 private boolean scan; // = false;
149 private static int STRSCAN = 1;
150 // WVL - LDC : 05/08/2005 : TFX.006253 - support STRPCCMD
151 private boolean strpccmd; // = false;
152 private String user;
153 private String password;
154 private String library;
155 private String initialMenu;
156 private String program;
157 private boolean keepTrucking = true;
158 private boolean pendingUnlock = false;
159 private boolean[] dataIncluded;
160 protected ICodePage codePage;
161 private boolean firstScreen;
162 private String sslType;
163 private WTDSFParser sfParser;
164
165 private final TN5250jLogger log = TN5250jLogFactory.getLogger(this.getClass());
166
167 /**
168 * @param session
169 * @param screen52
170 * @param type
171 * @param support132
172 */
173 public tnvt(Session5250 session, Screen5250 screen52, boolean type, boolean support132) {
174
175 controller = session;
176 if (log.isInfoEnabled()) {
177 log.info(" new session -> " + controller.getSessionName());
178 }
179
180 enhanced = type;
181 this.support132 = support132;
182 setCodePage("37");
183 this.screen52 = screen52;
184 dataIncluded = new boolean[24];
185
186 if (System.getProperties().containsKey("SESSION_CONNECT_USER")) {
187 user = System.getProperties().getProperty("SESSION_CONNECT_USER");
188 if (System.getProperties().containsKey("SESSION_CONNECT_PASSWORD"))
189 password = System.getProperties().getProperty(
190 "SESSION_CONNECT_PASSWORD");
191 if (System.getProperties().containsKey("SESSION_CONNECT_LIBRARY"))
192 library = System.getProperties().getProperty(
193 "SESSION_CONNECT_LIBRARY");
194 if (System.getProperties().containsKey("SESSION_CONNECT_MENU"))
195 initialMenu = System.getProperties().getProperty(
196 "SESSION_CONNECT_MENU");
197 if (System.getProperties().containsKey("SESSION_CONNECT_PROGRAM"))
198 program = System.getProperties().getProperty(
199 "SESSION_CONNECT_PROGRAM");
200 }
201
202 baosp = new ByteArrayOutputStream();
203 baosrsp = new ByteArrayOutputStream();
204 }
205
206 public String getHostName() {
207
208 return session;
209 }
210
211
212 public void setSSLType(String type) {
213 sslType = type;
214 }
215
216 public void setDeviceName(String name) {
217
218 devName = name;
219
220 }
221
222 public String getDeviceName() {
223 return devName;
224 }
225
226 public String getAllocatedDeviceName() {
227 return devNameUsed;
228 }
229
230 public boolean isConnected() {
231
232 return connected;
233 }
234
235 /**
236 * @return true when SSL is used and socket is connected.
237 * @see {@link #isConnected()}
238 */
239 public boolean isSslSocket() {
240 if (this.connected && this.sock != null && this.sock instanceof SSLSocket) {
241 return true;
242 } else {
243 return false;
244 }
245 }
246
247 public final void setProxy(String proxyHost, String proxyPort) {
248
249 Properties systemProperties = System.getProperties();
250 systemProperties.put("socksProxySet", "true");
251 systemProperties.put("socksProxyHost", proxyHost);
252 systemProperties.put("socksProxyPort", proxyPort);
253
254 System.setProperties(systemProperties);
255 log.info(" socks set ");
256 }
257
258 public final boolean connect() {
259
260 return connect(session, port);
261
262 }
263
264
265 public final boolean connect(String s, int port) {
266
267 // We will now see if there are any bypass signon parameters to be
268 // processed. The system properties override these parameters so
269 // have precidence if specified.
270 Properties props = controller.getConnectionProperties();
271 if (user == null && props.containsKey("SESSION_CONNECT_USER")) {
272 user = props.getProperty("SESSION_CONNECT_USER");
273 log.info(" user -> " + user + " " + controller.getSessionName());
274 if (props.containsKey("SESSION_CONNECT_PASSWORD"))
275 password = props.getProperty("SESSION_CONNECT_PASSWORD");
276 if (props.containsKey("SESSION_CONNECT_LIBRARY"))
277 library = props.getProperty("SESSION_CONNECT_LIBRARY");
278 if (props.containsKey("SESSION_CONNECT_MENU"))
279 initialMenu = props.getProperty("SESSION_CONNECT_MENU");
280 if (props.containsKey("SESSION_CONNECT_PROGRAM"))
281 program = props.getProperty("SESSION_CONNECT_PROGRAM");
282 }
283
284
285 try {
286 session = s;
287 this.port = port;
288
289 try {
290 SwingUtilities.invokeAndWait(new Runnable() {
291 public void run() {
292 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_SYSTEM_WAIT,
293 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED,"X - Connecting");
294 }
295 });
296
297 } catch (Exception exc) {
298 log.warn("setStatus(ON) " + exc.getMessage());
299
300 }
301
302 // sock = new Socket(s, port);
303 //smk - For SSL compability
304 SocketConnector sc = new SocketConnector();
305 if (sslType != null)
306 sc.setSSLType(sslType);
307 sock = sc.createSocket(s, port);
308
309 if (sock == null) {
310 log.warn("I did not get a socket");
311 disconnect();
312 return false;
313 }
314
315 connected = true;
316 // used for JDK1.3
317 sock.setKeepAlive(true);
318 sock.setTcpNoDelay(true);
319 sock.setSoLinger(false, 0);
320 InputStream in = sock.getInputStream();
321 OutputStream out = sock.getOutputStream();
322
323 bin = new BufferedInputStream(in, 8192);
324 bout = new BufferedOutputStream(out);
325
326 byte abyte0[];
327 while (negotiate(abyte0 = readNegotiations()));
328 negotiated = true;
329 try {
330 screen52.setCursorActive(false);
331 } catch (Exception excc) {
332 log.warn("setCursorOff " + excc.getMessage());
333
334 }
335
336 producer = new DataStreamProducer(this, bin, dsq, abyte0);
337 pthread = new Thread(producer);
338 // pthread.setPriority(pthread.MIN_PRIORITY);
339 pthread.setPriority(Thread.NORM_PRIORITY);
340 // pthread.setPriority(Thread.NORM_PRIORITY / 2);
341 pthread.start();
342
343 try {
344 SwingUtilities.invokeAndWait(new Runnable() {
345 public void run() {
346 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_NOTINHIBITED,
347 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED);
348 }
349 });
350
351 } catch (Exception exc) {
352 log.warn("setStatus(OFF) " + exc.getMessage());
353 }
354
355 keepTrucking = true;
356 me = new Thread(this);
357 me.start();
358
359 } catch (Exception exception) {
360 if (exception.getMessage() == null)
361 exception.printStackTrace();
362 log.warn("connect() " + exception.getMessage());
363
364 if (sock == null)
365 log.warn("I did not get a socket");
366
367 disconnect();
368 return false;
369 }
370 return true;
371
372 }
373
374 public final boolean disconnect() {
375
376 // Added by LUC - LDC to fix a null pointer exception.
377 if (!connected) {
378 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_SYSTEM_WAIT,
379 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED,"X - Disconnected");
380 return false;
381 }
382
383 if (me != null && me.isAlive()) {
384 me.interrupt();
385 keepTrucking = false;
386 pthread.interrupt();
387 }
388
389 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_SYSTEM_WAIT,
390 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED,"X - Disconnected");
391 screen52.getOIA().setKeyBoardLocked(false);
392 pendingUnlock = false;
393
394 try {
395 if (sock != null) {
396 log.info("Closing socket");
397 sock.close();
398 }
399 if (bin != null)
400 bin.close();
401 if (bout != null)
402 bout.close();
403 connected = false;
404 firstScreen = false;
405
406 // WVL - LDC : TR.000345 : properly disconnect and clear screen
407 // Is this the right place to set screen realestate on disconnect?
408 //controller.getScreen().clearAll();
409 screen52.goto_XY(0);
410 screen52.setCursorActive(false);
411 screen52.clearAll();
412 screen52.restoreScreen();
413
414 controller.fireSessionChanged(TN5250jConstants.STATE_DISCONNECTED);
415
416 } catch (Exception exception) {
417 log.warn(exception.getMessage());
418 connected = false;
419 devSeq = -1;
420 return false;
421
422 }
423 devSeq = -1;
424 return true;
425 }
426
427 private final ByteArrayOutputStream appendByteStream(byte abyte0[]) {
428 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
429 for (int i = 0; i < abyte0.length; i++) {
430 bytearrayoutputstream.write(abyte0[i]);
431 if (abyte0[i] == -1)
432 bytearrayoutputstream.write(-1);
433 }
434
435 return bytearrayoutputstream;
436 }
437
438 private final byte[] readNegotiations() throws IOException {
439 int i = bin.read();
440 if (i < 0) {
441 throw new IOException("Connection closed.");
442 } else {
443 int j = bin.available();
444 byte abyte0[] = new byte[j + 1];
445 abyte0[0] = (byte) i;
446 bin.read(abyte0, 1, j);
447 return abyte0;
448 }
449 }
450
451 private final void writeByte(byte abyte0[]) throws IOException {
452
453 bout.write(abyte0);
454 bout.flush();
455 }
456
457 // private final void writeByte(byte byte0) throws IOException {
458 //
459 // bout.write(byte0);
460 // bout.flush();
461 // }
462
463 public final void sendHeartBeat() throws IOException {
464
465 byte[] b = { (byte) 0xff, (byte) 0xf1 };
466 bout.write(b);
467 bout.flush();
468 }
469
470 private final void readImmediate(int readType) {
471
472 if (screen52.isStatusErrorCode()) {
473 screen52.restoreErrorLine();
474 screen52.setStatus(Screen5250.STATUS_ERROR_CODE,
475 Screen5250.STATUS_VALUE_OFF, null);
476 }
477
478 if (!enhanced) {
479 screen52.setCursorActive(false);
480 }
481 // screen52.setStatus(Screen5250.STATUS_SYSTEM,
482 // Screen5250.STATUS_VALUE_ON, null);
483 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_SYSTEM_WAIT,
484 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED);
485
486 screen52.getOIA().setKeyBoardLocked(true);
487 pendingUnlock = false;
488 invited = false;
489
490 screen52.getScreenFields().readFormatTable(baosp, readType, codePage);
491
492 try {
493
494 writeGDS(0, 3, baosp.toByteArray());
495 } catch (IOException ioe) {
496
497 log.warn(ioe.getMessage());
498 baosp.reset();
499 }
500 baosp.reset();
501
502 }
503
504 public final boolean sendAidKey(int aid) {
505
506 if (screen52.isStatusErrorCode()) {
507 screen52.restoreErrorLine();
508 screen52.setStatus(Screen5250.STATUS_ERROR_CODE,
509 Screen5250.STATUS_VALUE_OFF, null);
510 }
511
512 if (!enhanced) {
513 screen52.setCursorActive(false);
514 }
515 // screen52.setStatus(Screen5250.STATUS_SYSTEM,
516 // Screen5250.STATUS_VALUE_ON, null);
517 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_SYSTEM_WAIT,
518 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED);
519
520 screen52.getOIA().setKeyBoardLocked(true);
521 pendingUnlock = false;
522 invited = false;
523 baosp.write(screen52.getCurrentRow());
524 baosp.write(screen52.getCurrentCol());
525 baosp.write(aid);
526
527 if (dataIncluded(aid))
528
529 screen52.getScreenFields().readFormatTable(baosp, readType,
530 codePage);
531
532 try {
533
534 writeGDS(0, 3, baosp.toByteArray());
535 } catch (IOException ioe) {
536
537 log.warn(ioe.getMessage());
538 baosp.reset();
539 return false;
540 }
541 baosp.reset();
542 return true;
543
544 }
545
546 private boolean dataIncluded(int aid) {
547
548 switch (aid) {
549
550 case PF1:
551 return !dataIncluded[0];
552 case PF2:
553 return !dataIncluded[1];
554 case PF3:
555 return !dataIncluded[2];
556 case PF4:
557 return !dataIncluded[3];
558 case PF5:
559 return !dataIncluded[4];
560 case PF6:
561 return !dataIncluded[5];
562 case PF7:
563 return !dataIncluded[6];
564 case PF8:
565 return !dataIncluded[7];
566 case PF9:
567 return !dataIncluded[8];
568 case PF10:
569 return !dataIncluded[9];
570 case PF11:
571 return !dataIncluded[10];
572 case PF12:
573 return !dataIncluded[11];
574 case PF13:
575 return !dataIncluded[12];
576 case PF14:
577 return !dataIncluded[13];
578 case PF15:
579 return !dataIncluded[14];
580 case PF16:
581 return !dataIncluded[15];
582 case PF17:
583 return !dataIncluded[16];
584 case PF18:
585 return !dataIncluded[17];
586 case PF19:
587 return !dataIncluded[18];
588 case PF20:
589 return !dataIncluded[19];
590 case PF21:
591 return !dataIncluded[20];
592 case PF22:
593 return !dataIncluded[21];
594 case PF23:
595 return !dataIncluded[22];
596 case PF24:
597 return !dataIncluded[23];
598
599 default:
600 return true;
601
602 }
603
604 }
605
606 /**
607 * Help request -
608 *
609 *
610 * See notes inside method
611 */
612 public final void sendHelpRequest() {
613
614 // Client sends header 000D12A0000004000003####F3FFEF
615 // operation code 3
616 // row - first ##
617 // column - second ##
618 // F3 - Help Aid Key
619 // System.out.println("Help request sent");
620 baosp.write(screen52.getCurrentRow());
621 baosp.write(screen52.getCurrentCol());
622 baosp.write(AID_HELP);
623
624 try {
625 writeGDS(0, 3, baosp.toByteArray());
626 } catch (IOException ioe) {
627
628 log.warn(ioe.getMessage());
629 }
630 baosp.reset();
631 }
632
633 /**
634 * Attention Key -
635 *
636 *
637 * See notes inside method
638 */
639 public final void sendAttentionKey() {
640
641 // Client sends header 000A12A000004400000FFEF
642 // 0x40 -> 01000000
643 //
644 // flags
645 // bit 0 - ERR
646 // bit 1 - ATN Attention
647 // bits 2-4 - reserved
648 // bit 5 - SRQ system request
649 // bit 6 - TRQ Test request key
650 // bit 7 - HLP
651
652 // System.out.println("Attention key sent");
653
654 try {
655 writeGDS(0x40, 0, null);
656 } catch (IOException ioe) {
657
658 log.warn(ioe.getMessage());
659 }
660 }
661
662 /**
663 * Opens a dialog and asks the user before sending a request
664 *
665 * @see {@link #systemRequest(String)}
666 */
667 public final void systemRequest() {
668 final String sysreq = this.controller.showSystemRequest();
669 systemRequest(sysreq);
670 }
671
672 /**
673 * @param sr - system request option
674 * @see {@link #systemRequest(String)}
675 */
676 public final void systemRequest(char sr) {
677 systemRequest(Character.toString(sr));
678 }
679
680 /**
681 * System request, taken from the rfc1205, 5250 Telnet interface section 4.3
682 *
683 * @param sr system request option (allowed to be null, but than nothing happens)
684 */
685 public final void systemRequest(String sr) {
686 byte[] bytes = null;
687
688 if ( (sr != null) && (sr.length() > 0)) {
689 // XXX: Not sure, if this is a sufficient check for 'clear dataq'
690 if (sr.charAt(0) == '2') {
691 dsq.clear();
692 }
693 for (int i = 0, l = sr.length(); i < l; i++) {
694 baosp.write(codePage.uni2ebcdic(sr.charAt(i)));
695 }
696 bytes = baosp.toByteArray();
697 }
698
699 try {
700 writeGDS(4, 0, bytes);
701 } catch (IOException ioe) {
702 log.info(ioe.getMessage());
703 }
704 baosp.reset();
705 }
706
707 /**
708 * Cancel Invite - taken from the rfc1205 - 5250 Telnet interface section
709 * 4.3
710 *
711 * See notes inside method
712 */
713 public final void cancelInvite() {
714
715 // screen52.setStatus(Screen5250.STATUS_SYSTEM,
716 // Screen5250.STATUS_VALUE_ON, null);
717 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_SYSTEM_WAIT,
718 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED);
719
720 // from rfc1205 section 4.3
721 // Server: Sends header with the 000A12A0 00000400 000AFFEF
722 // Opcode = Cancel Invite.
723
724 // Client: sends header with the 000A12A0 00000400 000AFFEF
725 // Opcode = Cancel Invite to
726 // indicate that the work station is
727 // no longer invited.
728 try {
729 writeGDS(0, 10, null);
730 } catch (IOException ioe) {
731
732 log.warn(ioe.getMessage());
733 }
734
735 }
736
737 public final void hostPrint(int aid) {
738
739 if (screen52.isStatusErrorCode()) {
740 screen52.restoreErrorLine();
741 screen52.setStatus(Screen5250.STATUS_ERROR_CODE,
742 Screen5250.STATUS_VALUE_OFF, null);
743 }
744
745 screen52.setCursorActive(false);
746 // screen52.setStatus(Screen5250.STATUS_SYSTEM,
747 // Screen5250.STATUS_VALUE_ON, null);
748 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_SYSTEM_WAIT,
749 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED);
750
751 // From client access ip capture
752 // it seems to use an operation code of 3 and 4
753 // also note that the flag field that says reserved is being sent as
754 // well
755 // with a value of 0x80
756 //
757 // I have tried with not setting these flags and sending with 3 or 1
758 // there is no effect and I still get a host print screen. Go figure
759 //0000: 000D 12A0 0000 0400 8003 1407 F6FFEF
760 //0000: 000D 12A0 0000 0400 8001 110E F6FFEF
761 //
762 // Client sends header 000D12A0000004000003####F6FFEF
763 // operation code 3
764 // row - first ##
765 // column - second ##
766 // F6 - Print Aid Key
767
768 baosp.write(screen52.getCurrentRow());
769 baosp.write(screen52.getCurrentCol());
770 baosp.write(AID_PRINT); // aid key
771
772 try {
773 writeGDS(0, 3, baosp.toByteArray());
774 } catch (IOException ioe) {
775
776 log.warn(ioe.getMessage());
777 }
778 baosp.reset();
779 }
780
781 public final void toggleDebug() {
782 producer.toggleDebug(codePage);
783 }
784
785 // write gerneral data stream
786 private final void writeGDS(int flags, int opcode, byte abyte0[])
787 throws IOException {
788
789 // Added to fix for JDK 1.4 this was null coming from another method.
790 // There was a weird keyRelease event coming from another panel when
791 // using a key instead of the mouse to select button.
792 // The other method was fixed as well but this check should be here
793 // anyway.
794 if (bout == null)
795 return;
796
797 int length;
798 if (abyte0 != null)
799 length = abyte0.length + 10;
800 else
801 length = 10;
802
803 // refer to rfc1205 - 5250 Telnet interface
804 // Section 3. Data Stream Format
805
806 // Logical Record Length - 16 bits
807 baosrsp.write(length >> 8); // Length LL
808 baosrsp.write(length & 0xff); // LL
809
810 // Record Type - 16 bits
811 // It should always be set to '12A0'X to indicate the
812 // General Data Stream (GDS) record type.
813 baosrsp.write(18); // 0x12
814 baosrsp.write(160); // 0xA0
815
816 // the next 16 bits are not used
817 baosrsp.write(0); // 0x00
818 baosrsp.write(0); // 0x00
819
820 // The second part is meant to be variable in length
821 // currently this portion is 4 octets long (1 byte or 8 bits for us ;-O)
822 baosrsp.write(4); // 0x04
823
824 baosrsp.write(flags); // flags
825 // bit 0 - ERR
826 // bit 1 - ATN Attention
827 // bits 2-4 - reserved
828 // bit 5 - SRQ system request
829 // bit 6 - TRQ Test request key
830 // bit 7 - HLP
831 baosrsp.write(0); // reserved - set to 0x00
832 baosrsp.write(opcode); // opcode
833
834 if (abyte0 != null)
835 baosrsp.write(abyte0, 0, abyte0.length);
836
837 baosrsp = appendByteStream(baosrsp.toByteArray());
838
839 // make sure we indicate no more to be sent
840 baosrsp.write(IAC);
841 baosrsp.write(EOR);
842
843 baosrsp.writeTo(bout);
844
845 // byte[] b = new byte[baosrsp.size()];
846 // b = baosrsp.toByteArray();
847 // dump(b);
848 bout.flush();
849 // baos = null;
850 baosrsp.reset();
851 }
852
853 protected final int getOpCode() {
854
855 return bk.getOpCode();
856 }
857
858 // private final void sendNotify() throws IOException {
859 //
860 // writeGDS(0, 0, null);
861 // }
862
863 protected boolean[] getActiveAidKeys() {
864 boolean aids[] = new boolean[dataIncluded.length];
865 System.arraycopy(dataIncluded,0,aids,0,dataIncluded.length);
866 return aids;
867 }
868
869 private final void setInvited() {
870
871 log.debug("invited");
872 if (!screen52.isStatusErrorCode())
873 screen52.getOIA().setInputInhibited(ScreenOIA.INPUTINHIBITED_NOTINHIBITED,
874 ScreenOIA.OIA_LEVEL_INPUT_INHIBITED);
875
876 invited = true;
877 }
878
879 // WVL - LDC : 05/08/2005 : TFX.006253 - Support STRPCCMD
880 private void strpccmd()
881 {
882 try
883 {
884 int str = 11;
885 char c;
886 ScreenPlanes planes = screen52.getPlanes();
887 c = planes.getChar(str);
888 boolean waitFor = !(c == 'a');
889
890 StringBuffer command = new StringBuffer();
891 for (int i = str+1; i < 132; i++)
892 {
893 c = planes.getChar(i);
894 if (Character.isISOControl(c))
895 c = ' ';
896 command.append(c);
897 }
898
899 String cmd = command.toString().trim();
900
901 run(cmd, waitFor);
902 }
903 finally
904 {
905 strpccmd = false;
906 screen52.sendKeys(TN5250jConstants.MNEMONIC_ENTER);
907 }
908 }
909
910 // WVL - LDC : 05/08/2005 : TFX.006253 - Support STRPCCMD
911 private void run(String cmd, boolean waitFor)
912 {
913 try
914 {
915 log.debug("RUN cmd = " + cmd);
916 log.debug("RUN wait = " + waitFor);
917
918 Runtime r = Runtime.getRuntime();
919 Process p = r.exec(cmd);
920 if (waitFor)
921 {
922 int result = p.waitFor();
923 log.debug("RUN result = " + result);
924 }
925 }
926 catch (Throwable t)
927 {
928 log.error(t);
929 }
930 }
931
932
933 // WVL - LDC : TR.000300 : Callback scenario from 5250
934 /**
935 * Activate or deactivate the command scanning behaviour.
936 *
937 * @param scan
938 * if true, scanning is enabled; disabled otherwise.
939 *
940 * @see scan4Cmd()
941 */
942 public void setScanningEnabled(boolean scan) {
943 this.scan = scan;
944 }
945
946 // WVL - LDC : TR.000300 : Callback scenario from 5250
947 /**
948 * Checks whether command scanning is enabled.
949 *
950 * @return true is command scanning is enabled; false otherwise.
951 */
952 public boolean isScanningEnabled() {
953 return this.scan;
954 }
955
956 // WVL - LDC : TR.000300 : Callback scenario from 5250
957 /**
958 * When command scanning is activated, the terminal reads the first and
959 * second character in the datastream (the zero position allows to
960 * devisualize the scan stream). If the sequence <code>#!</code> is
961 * encountered and if this sequence is <strong>not </strong> followed by a
962 * blank character, the {@link parseCommand(ScreenChar[])}is called.
963 */
964 private void scan() {
965 // System.out.println("Checking command : " +
966 // screen52.screen[1].getChar() + screen52.screen[2].getChar());
967
968 // ScreenChar[] screen = screen52.screen;
969 ScreenPlanes planes = screen52.getPlanes();
970
971 if ((planes.getChar(STRSCAN) == '#')
972 && (planes.getChar(STRSCAN + 1) == '!')
973 && (planes.getChar(STRSCAN + 2) != ' ')) {
974 try {
975 parseCommand();
976 } catch (Throwable t) {
977 log.info("Exec cmd: " + t.getMessage());
978 t.printStackTrace();
979 }
980 }
981 }
982
983 // WVL - LDC : TR.000300 : Callback scenario from 5250
984 /**
985 * The screen is parsed starting from second position until a white space is
986 * encountered. When found the Session#execCommand(String, int) is
987 * called with the parsed string. The position immediately following the
988 * encountered white space, separating the command from the rest of the
989 * screen, is passed as starting index.
990 *
991 * Note that the character at the starting position can potentially be a
992 * white space itself. The starting position in <code>execCommand</code>
993 * provided to make the scanning sequence more flexible. We'd like for
994 * example to embed also a <code>+</code> or <code>-</code> sign to
995 * indicate whether the tnvt should trigger a repaint or not. This would
996 * allow the flashing of command sequences without them becoming visible.
997 *
998 * <ul>
999 * <li><strong>PRE </strong> The screen character at position
1000 * <code>STRSCAN + 2</code> is not a white space.</li>
1001 * </ul>
1002 */
1003 private void parseCommand() {
1004 // Search for the command i.e. the first token in the stream
1005 // after the #! sequence separated by a space from the rest
1006 // of the screen.
1007 char[] screen = screen52.getScreenAsAllChars();
1008 for (int s = STRSCAN + 2, i = s; i < screen.length; i++) {
1009 if (screen[i] == ' ') {
1010 String command = new String(screen, s, i - s);
1011
1012 // Skip all white spaces between the command and the rest of
1013 // the screen.
1014 //for (; (i < screen.length) && (screen[i] == ' '); i++);
1015
1016 String remainder = new String(screen, i + 1, screen.length
1017 - (i + 1));
1018 // System.out.println("Sensing action command in the input! = "
1019 // + command);
1020 controller.fireScanned(command, remainder);
1021 break;
1022 }
1023 }
1024 }
1025
1026 public void run() {
1027
1028 if (enhanced)
1029 sfParser = new WTDSFParser(this);
1030
1031 bk = new Stream5250();
1032
1033 while (keepTrucking) {
1034
1035 try {
1036 bk.initialize((byte[]) dsq.take());
1037 } catch (InterruptedException ie) {
1038 log.warn(" vt thread interrupted and stopping ");
1039 keepTrucking = false;
1040 continue;
1041 }
1042
1043 // lets play nicely with the others on the playground
1044 // me.yield();
1045
1046 Thread.yield();
1047
1048 invited = false;
1049
1050 screen52.setCursorActive(false);
1051
1052 // System.out.println("operation code: " + bk.getOpCode());
1053 if (bk == null)
1054 continue;
1055
1056 switch (bk.getOpCode()) {
1057 case 00:
1058 log.debug("No operation");
1059 break;
1060 case 1:
1061 log.debug("Invite Operation");
1062 parseIncoming();
1063 // screen52.setKeyboardLocked(false);
1064 pendingUnlock = true;
1065 cursorOn = true;
1066 setInvited();
1067 break;
1068 case 2:
1069 log.debug("Output Only");
1070 parseIncoming();
1071 // System.out.println(screen52.dirty);
1072 screen52.updateDirty();
1073
1074 // invited = true;
1075
1076 break;
1077 case 3:
1078 log.debug("Put/Get Operation");
1079 parseIncoming();
1080 // inviteIt =true;
1081 setInvited();
1082 if (!firstScreen) {
1083 firstScreen = true;
1084 controller.fireSessionChanged(TN5250jConstants.STATE_CONNECTED);
1085 }
1086 break;
1087 case 4:
1088 log.debug("Save Screen Operation");
1089 parseIncoming();
1090 break;
1091
1092 case 5:
1093 log.debug("Restore Screen Operation");
1094 parseIncoming();
1095 break;
1096 case 6:
1097 log.debug("Read Immediate");
1098 sendAidKey(0);
1099 break;
1100 case 7:
1101 log.debug("Reserved");
1102 break;
1103 case 8:
1104 log.debug("Read Screen Operation");
1105 try {
1106 readScreen();
1107 } catch (IOException ex) {
1108 log.warn(ex.getMessage());
1109 }
1110 break;
1111
1112 case 9:
1113 log.debug("Reserved");
1114 break;
1115
1116 case 10:
1117 log.debug("Cancel Invite Operation");
1118 cancelInvite();
1119 break;
1120
1121 case 11:
1122 log.debug("Turn on message light");
1123 screen52.getOIA().setMessageLightOn();
1124 screen52.setCursorActive(true);
1125
1126 break;
1127 case 12:
1128 log.debug("Turn off Message light");
1129 screen52.getOIA().setMessageLightOff();
1130 screen52.setCursorActive(true);
1131
1132 break;
1133 default:
1134 break;
1135 }
1136
1137 if (screen52.isUsingGuiInterface())
1138 screen52.drawFields();
1139
1140 // if (screen52.screen[0][1].getChar() == '#' &&
1141 // screen52.screen[0][2].getChar() == '!')
1142 // execCmd();
1143 // else {
1144
1145 // if (screen52.isHotSpots()) {
1146 // screen52.checkHotSpots();
1147 // }
1148
1149 try {
1150 if (!strpccmd) {
1151 // SwingUtilities.invokeAndWait(
1152 // new Runnable () {
1153 // public void run() {
1154 // screen52.updateDirty();
1155 // }
1156 // }
1157 // );
1158 screen52.updateDirty();
1159 // controller.validate();
1160 // log.debug("update dirty");
1161 } else {
1162 strpccmd();
1163 }
1164 } catch (Exception exd) {
1165 log.warn(" tnvt.run: " + exd.getMessage());
1166 exd.printStackTrace();
1167 }
1168
1169 if (pendingUnlock && !screen52.isStatusErrorCode()) {
1170 screen52.getOIA().setKeyBoardLocked(false);
1171 pendingUnlock = false;
1172 }
1173
1174 if (cursorOn && !screen52.getOIA().isKeyBoardLocked()) {
1175 screen52.setCursorActive(true);
1176 cursorOn = false;
1177 }
1178
1179 // lets play nicely with the others on the playground
1180 //me.yield();
1181 Thread.yield();
1182
1183 }
1184 }
1185
1186 public void dumpStuff() {
1187
1188 if (log.isDebugEnabled()) {
1189 log.debug(" Pending unlock " + pendingUnlock);
1190 log.debug(" Status Error " + screen52.isStatusErrorCode());
1191 log.debug(" Keyboard Locked " + screen52.getOIA().isKeyBoardLocked());
1192 log.debug(" Cursor On " + cursorOn);
1193 log.debug(" Cursor Active " + screen52.cursorActive);
1194 }
1195
1196 }
1197
1198 // private final void execCmd() {
1199 // String name = "";
1200 // String argString = "";
1201 //
1202 // StringBuffer sb = new StringBuffer();
1203 // sb.append(screen52.screen[0][3].getChar());
1204 // sb.append(screen52.screen[0][4].getChar());
1205 // sb.append(screen52.screen[0][5].getChar());
1206 // sb.append(screen52.screen[0][6].getChar());
1207 //
1208 // System.out.println("command = " + sb);
1209 // int x = 8;
1210 // sb.setLength(0);
1211 // while (screen52.screen[0][x].getChar() > ' ') {
1212 // sb.append(screen52.screen[0][x].getChar());
1213 // x++;
1214 // }
1215 // name = sb.toString();
1216 // System.out.println("name = " + name);
1217 //
1218 // sb.setLength(0);
1219 // x++;
1220 // while (screen52.screen[0][x].getChar() >= ' ') {
1221 // sb.append(screen52.screen[0][x].getChar());
1222 // x++;
1223 // }
1224 // argString = sb.toString();
1225 // System.out.println("args = " + argString);
1226 //
1227 // sendAidKey(AID_ENTER);
1228 //
1229 // try {
1230 //
1231 // Class c = Class.forName(name);
1232 // String args1[] = {argString};
1233 // String args2[] = {};
1234 //
1235 // Method m = c.getMethod("main",
1236 // new Class[] { args1.getClass() });
1237 // m.setAccessible(true);
1238 // int mods = m.getModifiers();
1239 // if (m.getReturnType() !=
1240 // void.class || !Modifier.isStatic(mods) ||
1241 // !Modifier.isPublic(mods)) {
1242 //
1243 // throw new NoSuchMethodException("main");
1244 // }
1245 // try {
1246 // if (argString.length() > 0)
1247 // m.invoke(null, new Object[] { args1 });
1248 // else
1249 // m.invoke(null, new Object[] { args2 });
1250 // }
1251 // catch (IllegalAccessException e) {
1252 // // This should not happen, as we have
1253 // // disabled access checks
1254 // System.out.println("iae " + e.getMessage());
1255 //
1256 // }
1257 // }
1258 // catch (ClassNotFoundException cnfe) {
1259 // System.out.println("cnfe " + cnfe.getMessage());
1260 // }
1261 // catch (NoSuchMethodException nsmf) {
1262 // System.out.println("nsmf " + nsmf.getMessage());
1263 // }
1264 // catch (InvocationTargetException ite) {
1265 // System.out.println("ite " + ite.getMessage());
1266 // }
1267 // // catch (IllegalAccessException iae) {
1268 // // System.out.println("iae " + iae.getMessage());
1269 // // }
1270 // // catch (InstantiationException ie) {
1271 // // System.out.println("ie " + ie.getMessage());
1272 // // }
1273 // // try {
1274 // //
1275 // // Runtime rt = Runtime.getRuntime();
1276 // // Process proc = rt.exec("notepad");
1277 // // int exitVal = proc.exitValue();
1278 // // }
1279 // // catch (Throwable t) {
1280 // //
1281 // // t.printStackTrace();
1282 // // }
1283 // }
1284
1285 private final void readScreen() throws IOException {
1286
1287 int rows = screen52.getRows();
1288 int cols = screen52.getColumns();
1289 byte abyte0[] = new byte[rows * cols];
1290 fillScreenArray(abyte0, rows, cols);
1291 writeGDS(0, 0, abyte0);
1292 abyte0 = null;
1293 }
1294
1295 private final void fillScreenArray(byte[] sa, int rows, int cols) {
1296
1297 int la = 32;
1298 int sac = 0;
1299 int len = rows * cols;
1300
1301 ScreenPlanes planes = screen52.planes;
1302
1303 for (int y = 0; y < len; y++) { // save the screen data
1304
1305 if (planes.isAttributePlace(y)) {
1306 la = planes.getCharAttr(y);
1307 sa[sac++] = (byte) la;
1308 } else {
1309 if (planes.getCharAttr(y) != la) {
1310 la = planes.getCharAttr(y);
1311 sac--;
1312 sa[sac++] = (byte) la;
1313 }
1314 //LDC: Check to see if it is an displayable character. If not,
1315 // do not convert the character.
1316 // The characters on screen are in unicode
1317 //sa[sac++] =
1318 // (byte)codePage.uni2ebcdic(screen52.screen[y].getChar());
1319 char ch = planes.getChar(y);
1320 byte byteCh = (byte) ch;
1321 if (isDataUnicode(ch))
1322 byteCh = codePage.uni2ebcdic(ch);
1323 sa[sac++] = byteCh;
1324 }
1325 }
1326 }
1327
1328 private final void fillRegenerationBuffer(ByteArrayOutputStream sc, int rows, int cols)
1329 throws IOException {
1330
1331 int la = 32;
1332 int sac = 0;
1333 int len = rows * cols;
1334
1335 ScreenPlanes planes = screen52.planes;
1336 byte[] sa = new byte[len];
1337
1338 try {
1339 boolean guiExists = sfParser != null && sfParser.isGuisExists();
1340
1341
1342 for (int y = 0; y < len; y++) { // save the screen data
1343
1344 if (guiExists) {
1345
1346 byte[] guiSeg = sfParser.getSegmentAtPos(y);
1347 if (guiSeg != null) {
1348 //log.info(" gui saved at " + y + " - " + screen52.getRow(y) + "," +
1349 // screen52.getCol(y));
1350
1351 byte[] gsa = new byte[sa.length + guiSeg.length + 2];
1352 System.arraycopy(sa,0,gsa,0,sa.length);
1353 System.arraycopy(guiSeg,0,gsa,sac+2,guiSeg.length);
1354 sa = new byte[gsa.length];
1355 System.arraycopy(gsa,0,sa,0,gsa.length);
1356 sa[sac++] = (byte)0x04;
1357 sa[sac++] = (byte)0x11;
1358 sac += guiSeg.length;
1359 //y--;
1360 // continue;
1361 }
1362 }
1363 if (planes.isAttributePlace(y)) {
1364 la = planes.getCharAttr(y);
1365 sa[sac++] = (byte) la;
1366 } else {
1367 if (planes.getCharAttr(y) != la) {
1368 la = planes.getCharAttr(y);
1369 sac--;
1370 sa[sac++] = (byte) la;
1371 }
1372 //LDC: Check to see if it is an displayable character. If not,
1373 // do not convert the character.
1374 // The characters on screen are in unicode
1375 //sa[sac++] =
1376 // (byte)codePage.uni2ebcdic(screen52.screen[y].getChar());
1377 char ch = planes.getChar(y);
1378 byte byteCh = (byte) ch;
1379 if (isDataUnicode(ch))
1380 byteCh = codePage.uni2ebcdic(ch);
1381 sa[sac++] = byteCh;
1382 }
1383 }
1384 }
1385 catch(Exception exc) {
1386
1387 log.info(exc.getMessage());
1388 exc.printStackTrace();
1389 }
1390 sc.write(sa);
1391 }
1392
1393 public final void saveScreen() throws IOException {
1394
1395 ByteArrayOutputStream sc = new ByteArrayOutputStream();
1396 sc.write(4);
1397 sc.write(0x12); // 18
1398 sc.write(0); // 18
1399 sc.write(0); // 18
1400
1401 sc.write((byte) screen52.getRows()); // store the current size
1402 sc.write((byte) screen52.getColumns()); // ""
1403
1404 int cp = screen52.getCurrentPos(); // save off current position
1405 // fix below submitted by Mitch Blevins
1406 //int cp = screen52.getScreenFields().getCurrentFieldPos();
1407 // save off current position
1408 sc.write((byte) (cp >> 8 & 0xff)); // ""
1409 sc.write((byte) (cp & 0xff)); // ""
1410
1411 sc.write((byte) (screen52.homePos >> 8 & 0xff)); // save home pos
1412 sc.write((byte) (screen52.homePos & 0xff)); // ""
1413
1414 int rows = screen52.getRows(); // store the current size
1415 int cols = screen52.getColumns(); // ""
1416
1417 // byte[] sa = new byte[rows * cols];
1418 fillRegenerationBuffer(sc,rows,cols);
1419 // fillScreenArray(sa, rows, cols);
1420 //
1421 // sc.write(sa);
1422 // sa = null;
1423 int sizeFields = screen52.getScreenFields().getSize();
1424 sc.write((byte) (sizeFields >> 8 & 0xff)); // ""
1425 sc.write((byte) (sizeFields & 0xff)); // ""
1426
1427 if (sizeFields > 0) {
1428 int x = 0;
1429 int s = screen52.getScreenFields().getSize();
1430 ScreenField sf = null;
1431 while (x < s) {
1432 sf = screen52.getScreenFields().getField(x);
1433 sc.write((byte) sf.getAttr()); // attribute
1434 int sp = sf.startPos();
1435 sc.write((byte) (sp >> 8 & 0xff)); // ""
1436 sc.write((byte) (sp & 0xff)); // ""
1437 if (sf.mdt)
1438 sc.write((byte) 1);
1439 else
1440 sc.write((byte) 0);
1441 sc.write((byte) (sf.getLength() >> 8 & 0xff)); // ""
1442 sc.write((byte) (sf.getLength() & 0xff)); // ""
1443 sc.write((byte) sf.getFFW1() & 0xff);
1444 sc.write((byte) sf.getFFW2() & 0xff);
1445 sc.write((byte) sf.getFCW1() & 0xff);
1446 sc.write((byte) sf.getFCW2() & 0xff);
1447 log.debug("Saved ");
1448 log.debug(sf.toString());
1449
1450 x++;
1451 }
1452 sf = null;
1453 }
1454
1455 // The following two lines of code looks to have caused all sorts of
1456 // problems so for now we have commented them out.
1457 // screen52.getScreenFields().setCurrentField(null); // set it to null
1458 // for GC ?
1459 // screen52.clearTable();
1460
1461 try {
1462 writeGDS(0, 3, sc.toByteArray());
1463 } catch (IOException ioe) {
1464
1465 log.warn(ioe.getMessage());
1466 }
1467
1468 sc = null;
1469 log.debug("Save Screen end ");
1470 }
1471
1472 /**
1473 *
1474 * @throws IOException
1475 */
1476 public final void restoreScreen() throws IOException {
1477 int which = 0;
1478
1479 ScreenPlanes planes = screen52.planes;
1480
1481 try {
1482 log.debug("Restore ");
1483
1484 bk.getNextByte();
1485 bk.getNextByte();
1486
1487 int rows = bk.getNextByte() & 0xff;
1488 int cols = bk.getNextByte() & 0xff;
1489 int pos = bk.getNextByte() << 8 & 0xff00; // current position
1490 pos |= bk.getNextByte() & 0xff;
1491 int hPos = bk.getNextByte() << 8 & 0xff00; // home position
1492 hPos |= bk.getNextByte() & 0xff;
1493 if (rows != screen52.getRows())
1494 screen52.setRowsCols(rows, cols);
1495 screen52.clearAll(); // initialize what we currenty have
1496 if (sfParser != null && sfParser.isGuisExists())
1497 sfParser.clearGuiStructs();
1498
1499 int b = 32;
1500 int la = 32;
1501 int len = rows * cols;
1502 for (int y = 0; y < len; y++) {
1503
1504 b = bk.getNextByte();
1505 if (b == 0x04) {
1506
1507 log.info(" gui restored at " + y + " - " + screen52.getRow(y) + "," +
1508 screen52.getCol(y));
1509 int command = bk.getNextByte();
1510 byte[] seg = bk.getSegment();
1511
1512 if (seg.length > 0) {
1513 screen52.goto_XY(y);
1514 sfParser.parseWriteToDisplayStructuredField(seg);
1515 }
1516 y--;
1517 // screen52.goto_XY(y);
1518 }
1519 else {
1520 // b = bk.getNextByte();
1521 if (planes.isUseGui(y))
1522 continue;
1523 if (isAttribute(b)) {
1524 planes.setScreenCharAndAttr(y, planes.getChar(y), b, true);
1525 la = b;
1526
1527 }
1528 else {
1529 //LDC - 12/02/2003 - Check to see if it is an displayable
1530 // character. If not,
1531 // do not convert the character.
1532 // The characters on screen are in unicode
1533 char ch = (char) b;
1534 if (isDataEBCDIC(b))
1535 ch = codePage.ebcdic2uni(b);
1536
1537 planes.setScreenCharAndAttr(y, ch, la, false);
1538 }
1539 }
1540 }
1541
1542 int numFields = bk.getNextByte() << 8 & 0xff00;
1543 numFields |= bk.getNextByte() & 0xff;
1544 log.debug("number of fields " + numFields);
1545
1546 if (numFields > 0) {
1547 int x = 0;
1548 int attr = 0;
1549 int fPos = 0;
1550 int fLen = 0;
1551 int ffw1 = 0;
1552 int ffw2 = 0;
1553 int fcw1 = 0;
1554 int fcw2 = 0;
1555 boolean mdt = false;
1556
1557 ScreenField sf = null;
1558 while (x < numFields) {
1559
1560 attr = bk.getNextByte();
1561 fPos = bk.getNextByte() << 8 & 0xff00;
1562 fPos |= bk.getNextByte() & 0xff;
1563 if (bk.getNextByte() == 1)
1564 mdt = true;
1565 else
1566 mdt = false;
1567 fLen = bk.getNextByte() << 8 & 0xff00;
1568 fLen |= bk.getNextByte() & 0xff;
1569 ffw1 = bk.getNextByte();
1570 ffw2 = bk.getNextByte();
1571 fcw1 = bk.getNextByte();
1572 fcw2 = bk.getNextByte();
1573
1574 sf = screen52.getScreenFields().setField(attr,
1575 screen52.getRow(fPos), screen52.getCol(fPos), fLen,
1576 ffw1, ffw2, fcw1, fcw2);
1577
1578 while (fLen-- > 0) {
1579
1580 // now we set the field plane attributes
1581 planes.setScreenFieldAttr(fPos++,ffw1);
1582
1583 }
1584
1585 if (mdt) {
1586 sf.setMDT();
1587 screen52.getScreenFields().setMasterMDT();
1588 }
1589 if (log.isDebugEnabled()) {
1590 log.debug("/nRestored ");
1591 log.debug(sf.toString());
1592 }
1593 x++;
1594 }
1595 }
1596
1597 // Redraw the gui fields if we are in gui mode
1598 if (screen52.isUsingGuiInterface())
1599 screen52.drawFields();
1600
1601 screen52.restoreScreen(); // display the screen
1602
1603 // The position was saved with currentPos which 1,1 offset of the
1604 // screen position.
1605 // The setPendingInsert is the where the cursor position will be
1606 // displayed after the restore.
1607 screen52.setPendingInsert(true, screen52.getRow(pos + cols), screen52
1608 .getCol(pos + cols));
1609 // We need to offset the pos by -1 since the position is 1,1 based
1610 // and the goto_XY is 0,0 based.
1611 screen52.goto_XY(pos - 1);
1612 screen52.isInField();
1613 // // Redraw the gui fields if we are in gui mode
1614 // if (screen52.isUsingGuiInterface())
1615 // screen52.drawFields();
1616 } catch (Exception e) {
1617 log.warn("error restoring screen " + which + " with "
1618 + e.getMessage());
1619 }
1620 }
1621
1622 public final boolean waitingForInput() {
1623
1624 return waitingForInput;
1625 }
1626
1627 private void parseIncoming() {
1628
1629 boolean done = false;
1630 boolean error = false;
1631
1632 try {
1633 while (bk.hasNext() && !done) {
1634 byte b = bk.getNextByte();
1635
1636 switch (b) {
1637 case 0:
1638 case 1:
1639 break;
1640 case CMD_SAVE_SCREEN: // 0x02 2 Save Screen
1641 case 3: // 0x03 3 Save Partial Screen
1642 log.debug("save screen partial");
1643 saveScreen();
1644 break;
1645
1646 case ESC: // ESCAPE
1647 break;
1648 case 7: // audible bell
1649 controller.signalBell();
1650 bk.getNextByte();
1651 bk.getNextByte();
1652 break;
1653 case CMD_WRITE_TO_DISPLAY: // 0x11 17 write to display
1654 error = writeToDisplay(true);
1655 // WVL - LDC : TR.000300 : Callback scenario from 5250
1656 // Only scan when WRITE_TO_DISPLAY operation (i.e. refill
1657 // screen buffer)
1658 // has been issued!
1659 if (scan)
1660 scan();
1661
1662 break;
1663 case CMD_RESTORE_SCREEN: // 0x12 18 Restore Screen
1664 case 13: // 0x13 19 Restore Partial Screen
1665 log.debug("restore screen partial");
1666 restoreScreen();
1667 break;
1668
1669 case CMD_CLEAR_UNIT_ALTERNATE: // 0x20 32 clear unit alternate
1670 int param = bk.getNextByte();
1671 if (param != 0) {
1672 log.debug(" clear unit alternate error "
1673 + Integer.toHexString(param));
1674 sendNegResponse(NR_REQUEST_ERROR, 03, 01, 05,
1675 " clear unit alternate not supported");
1676 done = true;
1677 } else {
1678 if (screen52.getRows() != 27)
1679 screen52.setRowsCols(27, 132);
1680
1681 screen52.clearAll();
1682 if (sfParser != null && sfParser.isGuisExists())
1683 sfParser.clearGuiStructs();
1684
1685
1686 }
1687 break;
1688
1689 case CMD_WRITE_ERROR_CODE: // 0x21 33 Write Error Code
1690 writeErrorCode();
1691 error = writeToDisplay(false);
1692 break;
1693 case CMD_WRITE_ERROR_CODE_TO_WINDOW: // 0x22 34
1694 // Write Error Code to window
1695 writeErrorCodeToWindow();
1696 error = writeToDisplay(false);
1697 break;
1698
1699 case CMD_READ_SCREEN_IMMEDIATE: // 0x62 98
1700 case CMD_READ_SCREEN_TO_PRINT: // 0x66 102 read screen to print
1701 readScreen();
1702 break;
1703
1704 case CMD_CLEAR_UNIT: // 64 0x40 clear unit
1705 if (screen52.getRows() != 24)
1706 screen52.setRowsCols(24, 80);
1707 screen52.clearAll();
1708 if (sfParser != null && sfParser.isGuisExists())
1709 sfParser.clearGuiStructs();
1710
1711 break;
1712
1713 case CMD_CLEAR_FORMAT_TABLE: // 80 0x50 Clear format table
1714 screen52.clearTable();
1715 break;
1716
1717 case CMD_READ_INPUT_FIELDS: //0x42 66 read input fields
1718 case CMD_READ_MDT_FIELDS: // 0x52 82 read MDT Fields
1719 bk.getNextByte();
1720 bk.getNextByte();
1721 readType = b;
1722 screen52.goHome();
1723 // do nothing with the cursor here it is taken care of
1724 // in the main loop.
1725 //////////////// screen52.setCursorOn();
1726 waitingForInput = true;
1727 pendingUnlock = true;
1728 // screen52.setKeyboardLocked(false);
1729 break;
1730 case CMD_READ_MDT_IMMEDIATE_ALT: // 0x53 83
1731 readType = b;
1732 // screen52.goHome();
1733 // waitingForInput = true;
1734 // screen52.setKeyboardLocked(false);
1735 readImmediate(readType);
1736 break;
1737 case CMD_WRITE_STRUCTURED_FIELD: // 243 0xF3 -13 Write
1738 // structured field
1739 writeStructuredField();
1740 break;
1741 case CMD_ROLL: // 0x23 35 Roll Not sure what it does right now
1742 int updown = bk.getNextByte();
1743 int topline = bk.getNextByte();
1744 int bottomline = bk.getNextByte();
1745 screen52.rollScreen(updown, topline, bottomline);
1746 break;
1747
1748 default:
1749 done = true;
1750 sendNegResponse(NR_REQUEST_ERROR, 03, 01, 01,
1751 "parseIncoming");
1752 break;
1753 }
1754
1755 if (error)
1756 done = true;
1757 }
1758 // BEGIN FRAMEWORK
1759 // I took this out for debugging a problem
1760 // ScreenField[] a = this.screen52.getScreenFields().getFields();
1761 // if (log.isDebugEnabled()) {
1762 // for (int x = 0; x < a.length; x++) {
1763 // log.debug(a[x].toString());
1764 // }
1765 // }
1766 //
1767 // String strokes = this.screen52.getKeys();
1768 // if (!strokes.equals("")) {
1769 // Tn5250jKeyEvents e = new Tn5250jKeyEvents(this.screen52,
1770 // strokes);
1771 // //from the previous screen.
1772 // Tn5250jController.getCurrent().handleEvent(e);
1773 // }
1774 //
1775 // Tn5250jEvent event = new Tn5250jEvent(screen52);
1776 // Tn5250jController.getCurrent().handleEvent(event);
1777 //
1778 // //END FRAMEWORK
1779 } catch (Exception exc) {
1780 log.warn("incoming " + exc.getMessage());
1781 }
1782 ;
1783 }
1784
1785 /**
1786 * This routine handles sending negative responses back to the host.
1787 *
1788 * You can find a description of the types of responses to be sent back by
1789 * looking at section 12.4 of the 5250 Functions Reference manual
1790 *
1791 *
1792 * @param cat
1793 * @param modifier
1794 * @param uByte1
1795 * @param uByte2
1796 * @param from
1797 *
1798 */
1799 protected void sendNegResponse(int cat, int modifier, int uByte1,
1800 int uByte2, String from) {
1801
1802 try {
1803
1804 int os = bk.getByteOffset(-1) & 0xf0;
1805 int cp = (bk.getCurrentPos() - 1);
1806 log.info("invalid " + from + " command " + os
1807 + " at pos " + cp);
1808 } catch (Exception e) {
1809
1810 log.warn("Send Negative Response error " + e.getMessage());
1811 }
1812
1813 baosp.write(cat);
1814 baosp.write(modifier);
1815 baosp.write(uByte1);
1816 baosp.write(uByte2);
1817
1818 try {
1819 writeGDS(128, 0, baosp.toByteArray());
1820 } catch (IOException ioe) {
1821
1822 log.warn(ioe.getMessage());
1823 }
1824 baosp.reset();
1825
1826 }
1827
1828 public void sendNegResponse2(int ec) {
1829
1830 screen52.setPrehelpState(true, true, false);
1831 baosp.write(0x00);
1832 baosp.write(ec);
1833
1834 try {
1835 writeGDS(1, 0, baosp.toByteArray());
1836 } catch (IOException ioe) {
1837
1838 log.warn(ioe.getMessage());
1839 }
1840
1841 baosp.reset();
1842 }
1843
1844 private boolean writeToDisplay(boolean controlsExist) {
1845
1846 boolean error = false;
1847 boolean done = false;
1848 int attr;
1849 byte control0 = 0;
1850 byte control1 = 0;
1851 int saRows = screen52.getRows();
1852 int saCols = screen52.getColumns();
1853
1854 try {
1855 if (controlsExist) {
1856 control0 = bk.getNextByte();
1857 control1 = bk.getNextByte();
1858 processCC0(control0);
1859 }
1860 while (bk.hasNext() && !done) {
1861 // pos = bk.getCurrentPos();
1862
1863 // int rowc = screen52.getCurrentRow();
1864 // int colc = screen52.getCurrentCol();
1865
1866 byte bytebk = bk.getNextByte();
1867
1868 switch (bytebk) {
1869
1870 case 1: // SOH - Start of Header Order
1871 log.debug("SOH - Start of Header Order");
1872 error = processSOH();
1873
1874 break;
1875 case 02: // RA - Repeat to address
1876 log.debug("RA - Repeat to address");
1877 int row = screen52.getCurrentRow();
1878 int col = screen52.getCurrentCol();
1879
1880 int toRow = bk.getNextByte();
1881 int toCol = bk.getNextByte() & 0xff;
1882 if (toRow >= row) {
1883 int repeat = bk.getNextByte();
1884
1885 // a little intelligence here I hope
1886 if (row == 1 && col == 2 && toRow == screen52.getRows()
1887 && toCol == screen52.getColumns())
1888
1889 screen52.clearScreen();
1890 else {
1891 if (repeat != 0) {
1892 //LDC - 13/02/2003 - convert it to unicode
1893 repeat = codePage.ebcdic2uni(repeat);
1894 //repeat = getASCIIChar(repeat);
1895 }
1896
1897 int times = ((toRow * screen52.getColumns()) + toCol)
1898 - ((row * screen52.getColumns()) + col);
1899 while (times-- >= 0) {
1900 screen52.setChar(repeat);
1901 }
1902
1903 }
1904 } else {
1905 sendNegResponse(NR_REQUEST_ERROR, 0x05, 0x01, 0x23,
1906 " RA invalid");
1907 error = true;
1908 }
1909 break;
1910
1911 case 03: // EA - Erase to address
1912 log.debug("EA - Erase to address");
1913 int EArow = screen52.getCurrentRow();
1914 int EAcol = screen52.getCurrentCol();
1915
1916 int toEARow = bk.getNextByte();
1917 int toEACol = bk.getNextByte() & 0xff;
1918 int EALength = bk.getNextByte() & 0xff;
1919 while (--EALength > 0) {
1920
1921 bk.getNextByte();
1922
1923 }
1924 char EAAttr = (char) 0;
1925
1926 // a little intelligence here I hope
1927 if (EArow == 1 && EAcol == 2
1928 && toEARow == screen52.getRows()
1929 && toEACol == screen52.getColumns())
1930
1931 screen52.clearScreen();
1932 else {
1933 int times = ((toEARow * screen52.getColumns()) + toEACol)
1934 - ((EArow * screen52.getColumns()) + EAcol);
1935 while (times-- >= 0) {
1936 screen52.setChar(EAAttr);
1937 }
1938 }
1939 break;
1940 case 04: // Command - Escape
1941 log.debug("Command - Escape");
1942 done = true;
1943 break;
1944
1945 case 16: // TD - Transparent Data
1946 log.debug("TD - Transparent Data");
1947 int j = (bk.getNextByte() & 0xff) << 8 | bk.getNextByte()
1948 & 0xff; // length
1949 break;
1950
1951 case 17: // SBA - set buffer address order (row column)
1952 log.debug("SBA - set buffer address order (row column)");
1953 int saRow = bk.getNextByte();
1954 int saCol = bk.getNextByte() & 0xff;
1955 // make sure it is in bounds
1956 if (saRow >= 0 && saRow <= screen52.getRows() && saCol >= 0
1957 && saCol <= screen52.getColumns()) {
1958 screen52.setCursor(saRow, saCol); // now set screen
1959 // position for output
1960
1961 } else {
1962
1963 sendNegResponse(NR_REQUEST_ERROR, 0x05, 0x01, 0x22,
1964 "invalid row/col order" + " saRow = " + saRow
1965 + " saRows = " + screen52.getRows()
1966 + " saCol = " + saCol);
1967
1968 error = true;
1969
1970 }
1971 break;
1972
1973 case 18: // WEA - Extended Attribute
1974 log.debug("WEA - Extended Attribute");
1975 bk.getNextByte();
1976 bk.getNextByte();
1977 break;
1978
1979 case 19: // IC - Insert Cursor
1980 log.debug("IC - Insert Cursor");
1981 int icX = bk.getNextByte();
1982 int icY = bk.getNextByte() & 0xff;
1983 if (icX >= 0 && icX <= saRows && icY >= 0 && icY <= saCols) {
1984
1985 log.debug(" IC " + icX + " " + icY);
1986 screen52.setPendingInsert(true, icX, icY);
1987 } else {
1988 sendNegResponse(NR_REQUEST_ERROR, 0x05, 0x01, 0x22,
1989 " IC/IM position invalid ");
1990 error = true;
1991 }
1992
1993 break;
1994
1995 case 20: // MC - Move Cursor
1996 log.debug("MC - Move Cursor");
1997 int imcX = bk.getNextByte();
1998 int imcY = bk.getNextByte() & 0xff;
1999 if (imcX >= 0 && imcX <= saRows && imcY >= 0
2000 && imcY <= saCols) {
2001
2002 log.debug(" MC " + imcX + " " + imcY);
2003 screen52.setPendingInsert(false, imcX, imcY);
2004 } else {
2005 sendNegResponse(NR_REQUEST_ERROR, 0x05, 0x01, 0x22,
2006 " IC/IM position invalid ");
2007 error = true;
2008 }
2009
2010 break;
2011
2012 case 21: // WTDSF - Write To Display Structured Field order
2013 log
2014 .debug("WTDSF - Write To Display Structured Field order");
2015 byte[] seg = bk.getSegment();
2016 error = sfParser.parseWriteToDisplayStructuredField(seg);
2017
2018 // error = writeToDisplayStructuredField();
2019 break;
2020
2021 case 29: // SF - Start of Field
2022 log.debug("SF - Start of Field");
2023 int fcw1 = 0;
2024 int fcw2 = 0;
2025 int ffw1 = 0;
2026 int ffw0 = bk.getNextByte() & 0xff; // FFW
2027
2028 if ((ffw0 & 0x40) == 0x40) {
2029 ffw1 = bk.getNextByte() & 0xff; // FFW 1
2030
2031 fcw1 = bk.getNextByte() & 0xff; // check for field
2032 // control word
2033
2034 // check if the first fcw1 is an 0x81 if it is then get
2035 // the
2036 // next pair for checking
2037 if (fcw1 == 0x81) {
2038 bk.getNextByte();
2039 fcw1 = bk.getNextByte() & 0xff; // check for field
2040 // control word
2041 }
2042
2043 if (!isAttribute(fcw1)) {
2044
2045 fcw2 = bk.getNextByte() & 0xff; // FCW 2
2046 attr = bk.getNextByte() & 0xff; // attribute field
2047
2048 while (!isAttribute(attr)) {
2049 log.info(Integer.toHexString(fcw1) + " "
2050 + Integer.toHexString(fcw2)
2051 + " ");
2052 log.info(Integer.toHexString(attr)
2053 + " "
2054 + Integer.toHexString(bk
2055 .getNextByte() & 0xff));
2056 // bk.getNextByte();
2057 attr = bk.getNextByte() & 0xff; // attribute
2058 // field
2059 }
2060 } else {
2061 attr = fcw1; // attribute of field
2062 fcw1 = 0;
2063 }
2064 } else {
2065 attr = ffw0;
2066 }
2067
2068 int fLength = (bk.getNextByte() & 0xff) << 8
2069 | bk.getNextByte() & 0xff;
2070 screen52.addField(attr, fLength, ffw0, ffw1, fcw1, fcw2);
2071
2072 break;
2073 // WVL - LDC : 05/08/2005 : TFX.006253 - Support STRPCCMD
2074 case -128: //STRPCCMD
2075 // if (screen52.getCurrentPos() == 82) {
2076 log.debug("STRPCCMD got a -128 command at " + screen52.getCurrentPos());
2077 StringBuffer value = new StringBuffer();
2078 int b;
2079 char c;
2080 int[] pco = new int[9];
2081 int[] pcoOk = {0xfc, 0xd7, 0xc3, 0xd6, 0x40, 0x83, 0x80, 0xa1, 0x80};
2082
2083 for (int i = 0; i < 9; i++)
2084 {
2085 b = bk.getNextByte();
2086 pco[i] = ((b & 0xff));
2087 c = codePage.ebcdic2uni(b);
2088 value.append(c);
2089 }
2090
2091 // Check "PCO-String"
2092 if (Arrays.equals(pco, pcoOk)) {
2093 strpccmd = true;
2094 }
2095 // we return in the stream to have all chars
2096 // arrive at the screen for later processing
2097 for (int i = 0; i < 9; i++)
2098 bk.setPrevByte();
2099 //}
2100 // no break: so every chars arrives
2101 // on the screen for later parsing
2102 //break;
2103
2104 default: // all others must be output to screen
2105 log.debug("all others must be output to screen");
2106 byte byte0 = bk.getByteOffset(-1);
2107 if (isAttribute(byte0)) {
2108 screen52.setAttr(byte0);
2109 } else {
2110 if (!screen52.isStatusErrorCode()) {
2111 if (!isDataEBCDIC(byte0)) {
2112 // if (byte0 == 255) {
2113 // sendNegResponse(NR_REQUEST_ERROR,0x05,0x01,0x42,
2114 // " Attempt to send FF to screen");
2115 // }
2116 // else
2117
2118 screen52.setChar(byte0);
2119 } else
2120 //LDC - 13/02/2003 - Convert it to unicode
2121 //screen52.setChar(getASCIIChar(byte0));
2122 screen52.setChar(codePage.ebcdic2uni(byte0));
2123 } else {
2124 if (byte0 == 0)
2125 screen52.setChar(byte0);
2126 else
2127 //LDC - 13/02/2003 - Convert it to unicode
2128 //screen52.setChar(getASCIIChar(byte0));
2129 screen52.setChar(codePage.ebcdic2uni(byte0));
2130 }
2131 }
2132
2133 break;
2134 }
2135
2136 if (error)
2137 done = true;
2138 }
2139 }
2140
2141 catch (Exception e) {
2142 log.warn("write to display " + e.getMessage());
2143 e.printStackTrace();
2144 }
2145 ;
2146
2147 processCC1(control1);
2148
2149 return error;
2150
2151 }
2152
2153 private boolean processSOH() throws Exception {
2154
2155 int l = bk.getNextByte(); // length
2156 log.debug(" byte 0 " + l);
2157
2158 if (l > 0 && l <= 7) {
2159 bk.getNextByte(); // flag byte 2
2160 bk.getNextByte(); // reserved
2161 bk.getNextByte(); // resequence fields
2162
2163 screen52.clearTable();
2164
2165 // well that is the first time I have seen this. This fixes a
2166 // problem
2167 // with S/36 command line. Finally got it.
2168 if (l <= 3)
2169 return false;
2170
2171 screen52.setErrorLine(bk.getNextByte()); // error row
2172
2173 int byte1 = 0;
2174 if (l >= 5) {
2175 byte1 = bk.getNextByte();
2176 dataIncluded[23] = (byte1 & 0x80) == 0x80;
2177 dataIncluded[22] = (byte1 & 0x40) == 0x40;
2178 dataIncluded[21] = (byte1 & 0x20) == 0x20;
2179 dataIncluded[20] = (byte1 & 0x10) == 0x10;
2180 dataIncluded[19] = (byte1 & 0x8) == 0x8;
2181 dataIncluded[18] = (byte1 & 0x4) == 0x4;
2182 dataIncluded[17] = (byte1 & 0x2) == 0x2;
2183 dataIncluded[16] = (byte1 & 0x1) == 0x1;
2184 }
2185
2186 if (l >= 6) {
2187 byte1 = bk.getNextByte();
2188 dataIncluded[15] = (byte1 & 0x80) == 0x80;
2189 dataIncluded[14] = (byte1 & 0x40) == 0x40;
2190 dataIncluded[13] = (byte1 & 0x20) == 0x20;
2191 dataIncluded[12] = (byte1 & 0x10) == 0x10;
2192 dataIncluded[11] = (byte1 & 0x8) == 0x8;
2193 dataIncluded[10] = (byte1 & 0x4) == 0x4;
2194 dataIncluded[9] = (byte1 & 0x2) == 0x2;
2195 dataIncluded[8] = (byte1 & 0x1) == 0x1;
2196 }
2197
2198 if (l >= 7) {
2199 byte1 = bk.getNextByte();
2200 dataIncluded[7] = (byte1 & 0x80) == 0x80;
2201 dataIncluded[6] = (byte1 & 0x40) == 0x40;
2202 dataIncluded[5] = (byte1 & 0x20) == 0x20;
2203 dataIncluded[4] = (byte1 & 0x10) == 0x10;
2204 dataIncluded[3] = (byte1 & 0x8) == 0x8;
2205 dataIncluded[2] = (byte1 & 0x4) == 0x4;
2206 dataIncluded[1] = (byte1 & 0x2) == 0x2;
2207 dataIncluded[0] = (byte1 & 0x1) == 0x1;
2208 }
2209 return false;
2210 } else {
2211 sendNegResponse(NR_REQUEST_ERROR, 0x05, 0x01, 0x2B,
2212 "invalid SOH length");
2213 return true;
2214 }
2215
2216 }
2217
2218 private void processCC0(byte byte0) {
2219 log.debug(" Control byte0 " + Integer.toBinaryString(byte0 & 0xff));
2220 boolean lockKeyboard = true;
2221 boolean resetMDT = false;
2222 boolean resetMDTAll = false;
2223 boolean nullMDT = false;
2224 boolean nullAll = false;
2225
2226 // Bits 3 to 6 are reserved and should be set to '0000'
2227 // 0xE0 = '11100000' - only the first 3 bits are tested
2228 if ((byte0 & 0xE0) == 0x00) {
2229 lockKeyboard = false;
2230 }
2231
2232 // '00100000' = 0x20 /32 -- just lock keyboard
2233 // '01000000' = 0x40 /64
2234 // '01100000' = 0x60 /96
2235 // '10000000' = 0x80 /128
2236 // '10100000' = 0xA0 /160
2237 // '11000000' = 0xC0 /192
2238 // '11100000' = 0xE0 /224
2239
2240 switch (byte0 & 0xE0) {
2241
2242 case 0x40:
2243 resetMDT = true;
2244 break;
2245 case 0x60:
2246 resetMDTAll = true;
2247 break;
2248 case 0x80:
2249 nullMDT = true;
2250 break;
2251 case 0xA0:
2252 resetMDT = true;
2253 nullAll = true;
2254 break;
2255 case 0xC0:
2256 resetMDT = true;
2257 nullMDT = true;
2258 break;
2259
2260 case 0xE0:
2261 resetMDTAll = true;
2262 nullAll = true;
2263 break;
2264
2265 }
2266
2267 if (lockKeyboard) {
2268 screen52.getOIA().setKeyBoardLocked(true);
2269 pendingUnlock = false;
2270 } else
2271 pendingUnlock = false;
2272
2273 if (resetMDT || resetMDTAll || nullMDT || nullAll) {
2274 ScreenField sf;
2275
2276 int f = screen52.getScreenFields().getSize();
2277 for (int x = 0; x < f; x++) {
2278 sf = screen52.getScreenFields().getField(x);
2279
2280 if (!sf.isBypassField()) {
2281 if ((nullMDT && sf.mdt) || nullAll) {
2282 sf.setFieldChar((char) 0x0);
2283 screen52.drawField(sf);
2284 }
2285 }
2286 if (resetMDTAll || (resetMDT && !sf.isBypassField()))
2287 sf.resetMDT();
2288
2289 }
2290 sf = null;
2291 }
2292
2293 }
2294
2295 private void processCC1(byte byte1) {
2296 log.debug(" Control byte1 " + Integer.toBinaryString(byte1 & 0xff));
2297
2298 if ((byte1 & 0x04) == 0x04) {
2299 controller.signalBell();
2300 }
2301 if ((byte1 & 0x02) == 0x02) {
2302 screen52.getOIA().setMessageLightOff();
2303 }
2304 if ((byte1 & 0x01) == 0x01) {
2305 screen52.getOIA().setMessageLightOn();
2306 }
2307
2308 if ((byte1 & 0x01) == 0x01 && (byte1 & 0x02) == 0x02) {
2309 screen52.getOIA().setMessageLightOn();
2310 }
2311
2312 // reset blinking cursor seems to control whether to set or not set the
2313 // the cursor position. No documentation for this just testing and
2314 // looking at the bit settings of this field. This was a pain in the
2315 // ass!
2316 //
2317 // if it is off '0' then keep existing cursor positioning information
2318 // if it is on '1' then reset the cursor positioning information
2319 // *** Note *** unless we receive bit 4 on at the same time
2320 // this seems to work so far
2321 if ((byte1 & 0x20) == 0x20 && (byte1 & 0x08) == 0x00) {
2322 screen52.setPendingInsert(false);
2323 log.debug(" WTD position no move");
2324 } else {
2325
2326 screen52.setPendingInsert(true);
2327 log.debug(" WTD position move to home" + screen52.homePos + " row "
2328 + screen52.getRow(screen52.homePos) + " col "
2329 + screen52.getCol(screen52.homePos));
2330
2331 }
2332 // in enhanced mode we sometimes only receive bit 6 turned on which
2333 // is reset blinking cursor
2334 if ((byte1 & 0x20) == 0x20 && enhanced) {
2335 cursorOn = true;
2336 }
2337
2338 if (!screen52.isStatusErrorCode() && (byte1 & 0x08) == 0x08) {
2339
2340 // screen52.setStatus(screen52.STATUS_SYSTEM,screen52.STATUS_VALUE_OFF,null);
2341 cursorOn = true;
2342 }
2343
2344 if ((byte1 & 0x20) == 0x20 && (byte1 & 0x08) == 0x00) {
2345 screen52.setPendingInsert(false, 1, 1);
2346 }
2347
2348 }
2349
2350 private boolean isAttribute(int byte0) {
2351 int byte1 = byte0 & 0xff;
2352 return (byte1 & 0xe0) == 0x20;
2353 }
2354
2355 //LDC - 12/02/2003 - Function name changed from isData to isDataEBCDIC
2356 private boolean isDataEBCDIC(int byte0) {
2357 int byte1 = byte0 & 0xff;
2358 // here it should always be less than 255
2359 if (byte1 >= 64 && byte1 < 255)
2360
2361 return true;
2362 else
2363 return false;
2364
2365 }
2366
2367 //LDC - 12/02/2003 - Test if the unicode character is a displayable
2368 // character.
2369 // The first 32 characters are non displayable characters
2370 // This is normally the inverse of isDataEBCDIC (That's why there is a
2371 // check on 255 -> 0xFFFF
2372 private boolean isDataUnicode(int byte0) {
2373 return (((byte0 < 0) || (byte0 >= 32)) && (byte0 != 0xFFFF));
2374 }
2375
2376 private void writeStructuredField() {
2377
2378 boolean done = false;
2379 try {
2380 int length = ((bk.getNextByte() & 0xff) << 8 | (bk.getNextByte() & 0xff));
2381 while (bk.hasNext() && !done) {
2382 switch (bk.getNextByte()) {
2383
2384 case -39: // SOH - Start of Header Order
2385
2386 switch (bk.getNextByte()) {
2387 case 112: // 5250 Query
2388 bk.getNextByte(); // get null required field
2389 sendQueryResponse();
2390 break;
2391 default:
2392 log.debug("invalid structured field sub command "
2393 + bk.getByteOffset(-1));
2394 break;
2395 }
2396 break;
2397 default:
2398 log.debug("invalid structured field command "
2399 + bk.getByteOffset(-1));
2400 break;
2401 }
2402 }
2403 } catch (Exception e) {
2404 }
2405 ;
2406
2407 }
2408
2409 private final void writeErrorCode() throws Exception {
2410 screen52.setCursor(screen52.getErrorLine(), 1); // Skip the control byte
2411 screen52.setStatus(Screen5250.STATUS_ERROR_CODE,
2412 Screen5250.STATUS_VALUE_ON, null);
2413 screen52.saveErrorLine();
2414 cursorOn = true;
2415
2416 }
2417
2418 private final void writeErrorCodeToWindow() throws Exception {
2419 int fromCol = bk.getNextByte() & 0xff; // from column
2420 int toCol = bk.getNextByte() & 0xff; // to column
2421 screen52.setCursor(screen52.getErrorLine(), fromCol); // Skip the control
2422 // byte
2423 screen52.setStatus(Screen5250.STATUS_ERROR_CODE,
2424 Screen5250.STATUS_VALUE_ON, null);
2425 screen52.saveErrorLine();
2426 cursorOn = true;
2427
2428 }
2429
2430 /**
2431 * Method sendQueryResponse
2432 *
2433 * The query command is used to obtain information about the capabilities of
2434 * the 5250 display.
2435 *
2436 * The Query command must follow an Escape (0x04) and Write Structured Field
2437 * command (0xF3).
2438 *
2439 * This section is modeled after the rfc1205 - 5250 Telnet Interface section
2440 * 5.3
2441 *
2442 * @throws IOException
2443 */
2444 private final void sendQueryResponse() throws IOException {
2445
2446 log.info("sending query response");
2447 byte abyte0[] = new byte[64];
2448 abyte0[0] = 0; // Cursor Row/column (set to zero)
2449 abyte0[1] = 0; // ""
2450 abyte0[2] = -120; // X'88' inbound write structure Field aid
2451 if (enhanced == true) {
2452 abyte0[3] = 0; // 0x003D (61) length of query response
2453 abyte0[4] = 64; // "" see note below ?????????
2454 } else {
2455 abyte0[3] = 0; // 0x003A (58) length of query response
2456 abyte0[4] = 58; // ""
2457 // the length between 58 and 64 seems to cause
2458 // different formatting codes to be sent from
2459 // the host ???????????????? why ???????
2460 // Well the why can be found in the manual if
2461 // read a little more ;-)
2462 }
2463 abyte0[5] = -39; // command class 0xD9
2464 abyte0[6] = 112; // Command type query 0x70
2465 abyte0[7] = -128; // 0x80 Flag byte
2466 abyte0[8] = 6; // Controller Hardware Class
2467 abyte0[9] = 0; // 0x0600 - Other WSF or another 5250 Emulator
2468 abyte0[10] = 1; // Controller Code Level
2469 abyte0[11] = 1; // Version 1 Rel 1.0
2470 abyte0[12] = 0; // ""
2471
2472 abyte0[13] = 0; // 13 - 28 are reserved so set to 0x00
2473 abyte0[14] = 0; // ""
2474 abyte0[15] = 0; // ""
2475 abyte0[16] = 0; // ""
2476 abyte0[17] = 0; // ""
2477 abyte0[18] = 0; // ""
2478 abyte0[19] = 0; // ""
2479 abyte0[20] = 0; // ""
2480 abyte0[21] = 0; // ""
2481 abyte0[22] = 0; // ""
2482 abyte0[23] = 0; // ""
2483 abyte0[24] = 0; // ""
2484 abyte0[25] = 0; // ""
2485 abyte0[26] = 0; // ""
2486 abyte0[27] = 0; // ""
2487 abyte0[28] = 0; // ""
2488 abyte0[29] = 1; // Device type - 0x01 5250 Emulator
2489 abyte0[30] = codePage.uni2ebcdic('5'); // Device type character
2490 abyte0[31] = codePage.uni2ebcdic('2'); // ""
2491 abyte0[32] = codePage.uni2ebcdic('5'); // ""
2492 abyte0[33] = codePage.uni2ebcdic('1'); // ""
2493 abyte0[34] = codePage.uni2ebcdic('0'); // ""
2494 abyte0[35] = codePage.uni2ebcdic('1'); // ""
2495 abyte0[36] = codePage.uni2ebcdic('1'); // ""
2496
2497 abyte0[37] = 2; // Keyboard Id - 0x02 Standard Keyboard
2498 abyte0[38] = 0; // extended keyboard id
2499 abyte0[39] = 0; // reserved
2500
2501 abyte0[40] = 0; // 40 - 43 Display Serial Number
2502 abyte0[41] = 36; //
2503 abyte0[42] = 36; //
2504 abyte0[43] = 0; //
2505
2506 abyte0[44] = 1; // Maximum number of display fields - 256
2507 abyte0[45] = 0; // 0x0100
2508 abyte0[46] = 0; // 46 -48 Reserved set to 0x00
2509 abyte0[47] = 0;
2510 abyte0[48] = 0;
2511 abyte0[49] = 1; // 49 - 53 Controller Display Capability
2512 abyte0[50] = 17; // see rfc - tired of typing :-)
2513 abyte0[51] = 0; // ""
2514 abyte0[52] = 0; // ""
2515
2516 // 53
2517 // Bit 0-2: B'000' - no graphics capability
2518 // B'001' - 5292-2 style graphics
2519 // Bit 3-7: B '00000' = reserved (it seems for Client access)
2520
2521 if (enhanced == true) {
2522 // abyte0[53] = 0x5E; // 0x5E turns on ehnhanced mode
2523 // abyte0[53] = 0x27; // 0x5E turns on ehnhanced mode
2524 abyte0[53] = 0x7; // 0x5E turns on ehnhanced mode
2525 log.info("enhanced options");
2526 } else
2527 abyte0[53] = 0x0; // 0x0 is normal emulation
2528
2529 abyte0[54] = 24; // 54 - 60 Reserved set to 0x00
2530 // 54 - I found out is used for enhanced user
2531 // interface level 3. Bit 4 allows headers
2532 // and footers for windows
2533 abyte0[54] = 8; // 54 - 60 Reserved set to 0x00
2534 // 54 - I found out is used for enhanced user
2535 // interface level 3. Bit 4 allows headers
2536 // and footers for windows
2537 abyte0[55] = 0;
2538 abyte0[56] = 0;
2539 abyte0[57] = 0;
2540 abyte0[58] = 0;
2541 abyte0[59] = 0;
2542 abyte0[60] = 0;
2543 abyte0[61] = 0; // gridlines are not supported
2544 abyte0[62] = 0; // gridlines are not supported
2545 abyte0[63] = 0;
2546 writeGDS(0, 0, abyte0); // now tell them about us
2547 abyte0 = null;
2548
2549 }
2550
2551 protected final boolean negotiate(byte abyte0[]) throws IOException {
2552 int i = 0;
2553
2554
2555 // from server negotiations
2556 if(abyte0[i] == IAC) { // -1
2557
2558 while(i < abyte0.length && abyte0[i++] == -1)
2559 // while(i < abyte0.length && (abyte0[i] == -1 || abyte0[i++] == 0x20))
2560 switch(abyte0[i++]) {
2561
2562 // we will not worry about what it WONT do
2563 case WONT: // -4
2564 default:
2565 break;
2566
2567 case DO: //-3
2568
2569 // not sure why but since moving to V5R2 we are receiving a
2570 // DO with no option when connecting a second session with
2571 // device name. Can not find the cause at all. If anybody
2572 // is interested please debug this until then this works.
2573 if (i < abyte0.length) {
2574 switch(abyte0[i]) {
2575 case TERMINAL_TYPE: // 24
2576 baosp.write(IAC);
2577 baosp.write(WILL);
2578 baosp.write(TERMINAL_TYPE);
2579 writeByte(baosp.toByteArray());
2580 baosp.reset();
2581
2582 break;
2583
2584 case OPT_END_OF_RECORD: // 25
2585
2586 baosp.write(IAC);
2587 baosp.write(WILL);
2588 baosp.write(OPT_END_OF_RECORD);
2589 writeByte(baosp.toByteArray());
2590 baosp.reset();
2591 break;
2592
2593 case TRANSMIT_BINARY: // 0
2594
2595 baosp.write(IAC);
2596 baosp.write(WILL);
2597 baosp.write(TRANSMIT_BINARY);
2598 writeByte(baosp.toByteArray());
2599 baosp.reset();
2600
2601 break;
2602
2603 case TIMING_MARK: // 6 rfc860
2604 // System.out.println("Timing Mark Received and notifying " +
2605 // "the server that we will not do it");
2606 baosp.write(IAC);
2607 baosp.write(WONT);
2608 baosp.write(TIMING_MARK);
2609 writeByte(baosp.toByteArray());
2610 baosp.reset();
2611
2612 break;
2613
2614 case NEW_ENVIRONMENT: // 39 rfc1572, rfc4777
2615 // allways send new environment vars ...
2616 baosp.write(IAC);
2617 baosp.write(WILL);
2618 baosp.write(NEW_ENVIRONMENT);
2619 writeByte(baosp.toByteArray());
2620 baosp.reset();
2621 break;
2622
2623 default: // every thing else we will not do at this time
2624 baosp.write(IAC);
2625 baosp.write(WONT);
2626 baosp.write(abyte0[i]); // either
2627 writeByte(baosp.toByteArray());
2628 baosp.reset();
2629
2630 break;
2631 }
2632 }
2633
2634 i++;
2635 break;
2636
2637 case WILL:
2638
2639 switch(abyte0[i]) {
2640 case OPT_END_OF_RECORD: // 25
2641 baosp.write(IAC);
2642 baosp.write(DO);
2643 baosp.write(OPT_END_OF_RECORD);
2644 writeByte(baosp.toByteArray());
2645 baosp.reset();
2646
2647 break;
2648
2649 case TRANSMIT_BINARY: // '\0'
2650 baosp.write(IAC);
2651 baosp.write(DO);
2652 baosp.write(TRANSMIT_BINARY);
2653 writeByte(baosp.toByteArray());
2654 baosp.reset();
2655
2656 break;
2657 }
2658 i++;
2659 break;
2660
2661 case SB: // -6
2662
2663 if(abyte0[i] == NEW_ENVIRONMENT && abyte0[i + 1] == 1) {
2664 negNewEnvironment();
2665
2666 while (++i < abyte0.length && abyte0[i + 1] != IAC);
2667 }
2668
2669 if(abyte0[i] == TERMINAL_TYPE && abyte0[i + 1] == 1) {
2670 baosp.write(IAC);
2671 baosp.write(SB);
2672 baosp.write(TERMINAL_TYPE);
2673 baosp.write(QUAL_IS);
2674 if(!support132)
2675 baosp.write("IBM-3179-2".getBytes());
2676 else
2677 baosp.write("IBM-3477-FC".getBytes());
2678 baosp.write(IAC);
2679 baosp.write(SE);
2680 writeByte(baosp.toByteArray());
2681 baosp.reset();
2682
2683 i++;
2684 }
2685 i++;
2686 break;
2687 }
2688 return true;
2689 }
2690 else {
2691 return false;
2692 }
2693 }
2694
2695 /**
2696 * Negotiate new environment string for device name
2697 *
2698 * @throws IOException
2699 */
2700 private void negNewEnvironment() throws IOException {
2701
2702 baosp.write(IAC);
2703 baosp.write(SB);
2704 baosp.write(NEW_ENVIRONMENT);
2705 baosp.write(IS);
2706
2707 // http://tools.ietf.org/html/rfc4777
2708
2709 if (kbdTypesCodePage != null) {
2710 baosp.write(USERVAR);
2711 baosp.write("KBDTYPE".getBytes());
2712 baosp.write(VALUE);
2713 baosp.write(kbdTypesCodePage.kbdType.getBytes());
2714
2715 baosp.write(USERVAR);
2716 baosp.write("CODEPAGE".getBytes());
2717 baosp.write(VALUE);
2718 baosp.write(kbdTypesCodePage.codepage.getBytes());
2719
2720 baosp.write(USERVAR);
2721 baosp.write("CHARSET".getBytes());
2722 baosp.write(VALUE);
2723 baosp.write(kbdTypesCodePage.charset.getBytes());
2724 }
2725
2726 if (devName != null) {
2727 baosp.write(USERVAR);
2728
2729 baosp.write("DEVNAME".getBytes());
2730
2731 baosp.write(VALUE);
2732
2733 baosp.write(negDeviceName().getBytes());
2734 }
2735
2736 if (user != null) {
2737
2738 baosp.write(VAR);
2739 baosp.write("USER".getBytes());
2740 baosp.write(VALUE);
2741 baosp.write(user.getBytes());
2742
2743 if (password != null) {
2744 baosp.write(USERVAR);
2745 baosp.write("IBMRSEED".getBytes());
2746 baosp.write(VALUE);
2747 baosp.write(NEGOTIATE_ESC);
2748 baosp.write(0x0);
2749 baosp.write(0x0);
2750 baosp.write(0x0);
2751 baosp.write(0x0);
2752 baosp.write(0x0);
2753 baosp.write(0x0);
2754 baosp.write(0x0);
2755 baosp.write(0x0);
2756 baosp.write(USERVAR);
2757 baosp.write("IBMSUBSPW".getBytes());
2758 baosp.write(VALUE);
2759 baosp.write(password.getBytes());
2760 }
2761
2762 if (library != null) {
2763 baosp.write(USERVAR);
2764 baosp.write("IBMCURLIB".getBytes());
2765 baosp.write(VALUE);
2766 baosp.write(library.getBytes());
2767 }
2768
2769 if (initialMenu != null) {
2770 baosp.write(USERVAR);
2771 baosp.write("IBMIMENU".getBytes());
2772 baosp.write(VALUE);
2773 baosp.write(initialMenu.getBytes());
2774 }
2775
2776 if (program != null) {
2777 baosp.write(USERVAR);
2778 baosp.write("IBMPROGRAM".getBytes());
2779 baosp.write(VALUE);
2780 baosp.write(program.getBytes());
2781 }
2782 }
2783 baosp.write(IAC);
2784 baosp.write(SE);
2785
2786 writeByte(baosp.toByteArray());
2787 baosp.reset();
2788
2789 }
2790
2791 /**
2792 * This will negotiate a device name with controller. if the sequence is
2793 * less than zero then it will send the device name as specified. On each
2794 * unsuccessful attempt a sequential number is appended until we find one or
2795 * the controller says no way.
2796 *
2797 * @return String
2798 */
2799 private String negDeviceName() {
2800
2801 if (devSeq++ == -1) {
2802 devNameUsed = devName;
2803 return devName;
2804 } else {
2805 StringBuffer sb = new StringBuffer(devName + devSeq);
2806 int ei = 1;
2807 while (sb.length() > 10) {
2808
2809 sb.setLength(0);
2810 sb.append(devName.substring(0, devName.length() - ei++));
2811 sb.append(devSeq);
2812
2813 }
2814 devNameUsed = sb.toString();
2815 return devNameUsed;
2816 }
2817 }
2818
2819 public final void setCodePage(String cp) {
2820 codePage = CharMappings.getCodePage(cp);
2821 cp = cp.toLowerCase();
2822 for (KbdTypesCodePages kbdtyp : KbdTypesCodePages.values()) {
2823 if (("cp"+kbdtyp.codepage).equals(cp) || kbdtyp.ccsid.equals(cp)) {
2824 kbdTypesCodePage = kbdtyp;
2825 break;
2826 }
2827 }
2828 if (log.isInfoEnabled()) {
2829 log.info("Choosed keyboard mapping " + kbdTypesCodePage.toString() + " for code page " + cp);
2830 }
2831 }
2832
2833 public final ICodePage getCodePage() {
2834 return codePage;
2835 }
2836
2837 /**
2838 * @see org.tn5250j.Session5250#signalBell()
2839 */
2840 public void signalBell() {
2841 controller.signalBell();
2842 }
2843
2844 }