annotate app/src/main/java/org/tn5250j/framework/tn5250/DataStreamProducer.java @ 471:4f686f5a8b0f

Added tag stable-1.9.3-8 for changeset 2cc170e3fc9b
author Carl Byington <carl@five-ten-sg.com>
date Fri, 04 Oct 2019 17:13:10 -0700
parents 5ce5235adde6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 package org.tn5250j.framework.tn5250;
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2
446
5ce5235adde6 fix compile errors
Carl Byington <carl@five-ten-sg.com>
parents: 445
diff changeset
3 import org.tn5250j.encoding.ICodePage;
5ce5235adde6 fix compile errors
Carl Byington <carl@five-ten-sg.com>
parents: 445
diff changeset
4
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 import java.io.BufferedInputStream;
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 import java.io.ByteArrayOutputStream;
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 import java.io.IOException;
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 import java.net.SocketException;
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 import java.util.concurrent.BlockingQueue;
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
10
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
11 import static org.tn5250j.framework.tn5250.Stream5250.OPCODE_OFFSET;
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
12
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
13 import java.util.Timer;
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
14 import java.util.TimerTask;
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15
25
5949eb469a79 adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents: 3
diff changeset
16 import android.util.Log;
5949eb469a79 adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents: 3
diff changeset
17
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 public class DataStreamProducer implements Runnable {
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
20 private static final int MINIMAL_PARTIAL_STREAM_LEN = 2;
25
5949eb469a79 adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents: 3
diff changeset
21 private static final String TAG = "DataStreamProducer";
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
22 private tnvt vt;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
23 private BufferedInputStream bin;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
24 private ByteArrayOutputStream baosin;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
25 private byte[] saveStream;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
26 private final BlockingQueue<Object> dsq;
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
27 private byte[] dataStream;
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
28 private DataStreamDumper dataStreamDumper = new DataStreamDumper();
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29
25
5949eb469a79 adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents: 3
diff changeset
30
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
32 public DataStreamProducer(tnvt vt, BufferedInputStream bin, BlockingQueue<Object> queue, byte[] init) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
33 this.bin = bin;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
34 this.vt = vt;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
35 baosin = new ByteArrayOutputStream();
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
36 dsq = queue;
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
37 dataStream = init;
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
38 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
40 public final void run() {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
41 boolean done = false;
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
42 Thread me = Thread.currentThread();
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
43 // load the first response screen
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
44 loadStream(dataStream, 0);
25
5949eb469a79 adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents: 3
diff changeset
45
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
46 while (!done) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
47 try {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
48 byte[] abyte0 = readIncoming();
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
50 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
51 // Restructured to the readIncoming() method to return null
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
52 // on TIMING MARK. Don't process in that case (abyte0 == null)!
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
53 if (abyte0 != null) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
54 // WVL - LDC : 16/07/2003 : TR.000345
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
55 // When the socket has been closed, the reading returns
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
56 // no bytes (an empty byte arrray).
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
57 // But the loadStream fails on this, so we check it here!
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
58 if (abyte0.length > 0) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
59 loadStream(abyte0, 0);
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
60 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
61 // WVL - LDC : 16/07/2003 : TR.000345
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
62 // Returning no bytes means the input buffer has
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
63 // reached end-of-stream, so we do a disconnect!
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
64 else {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
65 done = true;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
66 vt.disconnect();
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
67 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
68 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
69 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
70 catch (SocketException se) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
71 Log.w(TAG, " DataStreamProducer thread interrupted and stopping " + se.getMessage());
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
72 done = true;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
73 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
74 catch (IOException ioe) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
75 Log.w(TAG, ioe.getMessage());
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
76 if (me.isInterrupted()) done = true;
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
77 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
78 catch (Exception ex) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
79 Log.w(TAG, ex.getMessage());
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
80 if (me.isInterrupted()) done = true;
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
81 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
82 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 }
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
85 private void loadStream(byte streamBuffer[], int offset) {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
86
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
87 int partialLen = (streamBuffer[offset] & 0xff) << 8 | streamBuffer[offset + 1] & 0xff;
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
88 int bufferLen = streamBuffer.length;
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
89
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
90 Log.d(TAG, "loadStream() offset=" + offset + " partialLen=" + partialLen + " bufferLen=" + bufferLen);
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
92 if (saveStream != null) {
446
5ce5235adde6 fix compile errors
Carl Byington <carl@five-ten-sg.com>
parents: 445
diff changeset
93 Log.d(TAG, "partial stream found");
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
94 bufferLen = saveStream.length + streamBuffer.length;
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
95 byte[] inter = new byte[bufferLen];
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
96 System.arraycopy(saveStream, 0, inter, 0, saveStream.length);
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
97 System.arraycopy(streamBuffer, 0, inter, saveStream.length, streamBuffer.length);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
98 streamBuffer = new byte[bufferLen];
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
99 System.arraycopy(inter, 0, streamBuffer, 0, bufferLen);
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
100 saveStream = null;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
101 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
103 if (partialLen > bufferLen) {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
104 saveStream = new byte[streamBuffer.length];
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
105 Log.d(TAG, "partial stream saved");
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
106 System.arraycopy(streamBuffer, 0, saveStream, 0, streamBuffer.length);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
107 } else {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
108 int buf_len = partialLen + 2;
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
109 byte[] buf = new byte[buf_len];
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
110 if (isBufferShifted(partialLen, bufferLen) && isOpcodeShifted(streamBuffer, offset)) {
446
5ce5235adde6 fix compile errors
Carl Byington <carl@five-ten-sg.com>
parents: 445
diff changeset
111 Log.d(TAG, "Invalid stream buffer detected. Ignoring the inserted byte.");
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
112 System.arraycopy(streamBuffer, offset, buf, 0, MINIMAL_PARTIAL_STREAM_LEN);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
113 System.arraycopy(streamBuffer, offset + MINIMAL_PARTIAL_STREAM_LEN + 1, buf, MINIMAL_PARTIAL_STREAM_LEN, partialLen);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
114 } else {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
115 System.arraycopy(streamBuffer, offset, buf, 0, buf_len);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
116 }
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
117 try {
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
118 dsq.put(buf);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
119 if (streamBuffer.length > buf.length + offset + MINIMAL_PARTIAL_STREAM_LEN)
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
120 loadStream(streamBuffer, offset + buf_len);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
121 } catch (InterruptedException ex) {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
122 Log.w(TAG, "load stream error.", ex);
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
123 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
124 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
125 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
127 private boolean isOpcodeShifted(byte[] streamBuffer, int offset) {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
128 byte code = streamBuffer[offset + 1 + OPCODE_OFFSET];
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
129 return (0 <= code && code <= 12);
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
130 }
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
131
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
132 private boolean isBufferShifted(int partialLen, int bufferLen) {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
133 return partialLen + MINIMAL_PARTIAL_STREAM_LEN + 1 == bufferLen;
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
134 }
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
135
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
136 public final byte[] readIncoming() throws IOException {
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
137 boolean done = false;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
138 boolean negotiate = false;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
139 baosin.reset();
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
140 int j = -1;
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
141 Timer timer = new Timer("data.stream", true);
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
142 TimerTask task = null;
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
144 while (!done) {
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
145 if (bin.available() == 0) {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
146 task = new TimerTask() {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
147 public void run() {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
148 try {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
149 dsq.put(new Integer(0)); // trigger buffer.testChanged()
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
150 }
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
151 catch (Exception ex) {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
152 Log.w(TAG, "readIncoming error " + ex.getMessage());
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
153 }
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
154 }
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
155 };
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
156 timer.schedule(task, 10); // 10 ms delay
392
7ac1a0b02f44 add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 219
diff changeset
157 }
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
158
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
159 int i = bin.read();
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
160
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
161 if (task != null) {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
162 task.cancel();
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
163 task = null;
392
7ac1a0b02f44 add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 219
diff changeset
164 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
165
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
166 // WVL - LDC : 16/07/2003 : TR.000345
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
167 // The inStream return -1 when end-of-stream is reached. This
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
168 // happens e.g. when the connection is closed from the AS/400.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
169 // So we stop in this case!
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
170 // ==> an empty byte array is returned from this method.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
171 if (i == -1) { // nothing read!
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
172 done = true;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
173 vt.disconnect();
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
174 continue;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
175 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
176
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
177 // We use the values instead of the static values IAC and EOR
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
178 // because they are defined as bytes.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
179 //
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
180 // The > if(i != 255 || j != 255) < is a hack for the double FF FF's
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
181 // that are being returned. I do not know why this is like this and
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
182 // can not find any documentation for it. It is also being returned
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
183 // on my Client Access tcp dump as well so they are handling it.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
184 //
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
185 // my5250
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
186 // 0000: 00 50 DA 44 C8 45 42 00 00 00 00 24 08 00 45 00 .P.D.EB....$..E.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
187 // 0010: 04 2A BC F9 00 00 40 06 D0 27 C1 A8 33 04 C1 A8 .*....@..'..3...
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
188 // 0020: 33 58 00 17 04 18 6F A2 83 CB 00 1E D1 BA 50 18 3X....o.......P.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
189 // 0030: 20 00 8A 9A 00 00 03 FF FF 12 A0 00 00 04 00 00 ...............
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
190 // --------------------------- || || -------------------------------------
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
191 // 0040: 03 04 40 04 11 00 20 01 07 00 00 00 18 00 00 10 ..@... .........
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
192
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
193 if (j == 255 && i == 255) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
194 j = -1;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
195 continue;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
196 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
197
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
198 baosin.write(i);
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
199
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
200 // check for end of record EOR and IAC - FFEF
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
201 if (j == 255 && i == 239)
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
202 done = true;
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
203
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
204 // This is to check for the TELNET TIMING MARK OPTION
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
205 // rfc860 explains this in more detail. When we receive it
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
206 // we will negotiate with the server by sending a WONT'T TIMING-MARK
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
207 // This will let the server know that we processed the information
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
208 // and are just waiting for the user to enter some data so keep the
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
209 // socket alive. This is more or less a AYT (ARE YOU THERE) or not.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
210 if (i == 253 && j == 255) {
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
211 done = true;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
212 negotiate = true;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
213 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
214
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
215 j = i;
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
216 }
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
217
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
218 // after the initial negotiation we might get other options such as
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
219 // timing marks ?????????????? do we ???????????? look at telnet spec
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
220 // yes we do. rfc860 explains about timing marks.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
221 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
222 // to existing device!
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
223 // Handled incorrectly: we cannot continue processing the TIMING MARK DO
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
224 // after we have handled it in the vt.negotiate()
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
225 // We should not return the bytes;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
226 // ==> restructured to return null after negotiation!
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
227 // Impacts the run method! Added the null check.
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
228 byte[] rBytes = baosin.toByteArray();
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
229
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
230 dataStreamDumper.dump(rBytes);
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
231
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
232 if (negotiate) {
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
233 if (bin.available() == 0) {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
234 task = new TimerTask() {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
235 public void run() {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
236 try {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
237 dsq.put(new Integer(0)); // trigger buffer.testChanged()
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
238 }
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
239 catch (Exception ex) {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
240 Log.w(TAG, "readIncoming error " + ex.getMessage());
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
241 }
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
242 }
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
243 };
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
244 timer.schedule(task, 10); // 10 ms delay
392
7ac1a0b02f44 add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 219
diff changeset
245 }
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
246
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
247 // get the negotiation option
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
248 baosin.write(bin.read());
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
249
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
250 if (task != null) {
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
251 task.cancel();
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
252 task = null;
392
7ac1a0b02f44 add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 219
diff changeset
253 }
394
24186858750f add test buffer changed when input stream runs dry, similar to async test
Carl Byington <carl@five-ten-sg.com>
parents: 392
diff changeset
254
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
255 vt.negotiate(rBytes);
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
256 return null;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
257 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
258
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
259 return rBytes;
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
260 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
261
445
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
262 protected void toggleDebug(ICodePage codePage) {
8fa8e73e2f5c update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
263 dataStreamDumper.toggleDebug(codePage);
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 99
diff changeset
264 }
3
e8d2a24e85c6 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
265
446
5ce5235adde6 fix compile errors
Carl Byington <carl@five-ten-sg.com>
parents: 445
diff changeset
266 }