Mercurial > 510Connectbot
annotate src/org/tn5250j/framework/tn5250/DataStreamProducer.java @ 78:044b1a951925 tn5250
convert 5250 to vt320 attributes
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sun, 15 Jun 2014 11:05:06 -0700 |
parents | 8181cb01c64d |
children | 16e023784917 |
rev | line source |
---|---|
3 | 1 package org.tn5250j.framework.tn5250; |
2 | |
3 import java.io.BufferedInputStream; | |
4 import java.io.BufferedOutputStream; | |
5 import java.io.ByteArrayOutputStream; | |
6 import java.io.EOFException; | |
7 import java.io.FileNotFoundException; | |
8 import java.io.FileOutputStream; | |
9 import java.io.IOException; | |
10 import java.net.SocketException; | |
11 import java.util.concurrent.BlockingQueue; | |
12 | |
13 import org.tn5250j.encoding.ICodePage; | |
72
8181cb01c64d
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
69
diff
changeset
|
14 |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
15 import android.util.Log; |
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
16 |
72
8181cb01c64d
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
69
diff
changeset
|
17 import de.mud.terminal.vt320; |
8181cb01c64d
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
69
diff
changeset
|
18 |
3 | 19 |
20 public class DataStreamProducer implements Runnable { | |
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"; |
69
294435151b0c
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
22 private tnvt vt; |
3 | 23 private BufferedInputStream bin; |
69
294435151b0c
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
24 private vt320 buffer; |
3 | 25 private ByteArrayOutputStream baosin; |
26 private Thread me; | |
27 private byte[] saveStream; | |
28 private final BlockingQueue<Object> dsq; | |
29 private byte[] abyte2; | |
30 private FileOutputStream fw; | |
31 private BufferedOutputStream dw; | |
32 private boolean dumpBytes = false; | |
33 private ICodePage codePage; | |
34 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
35 |
3 | 36 |
69
294435151b0c
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
37 public DataStreamProducer(tnvt vt, BufferedInputStream bin, vt320 buffer, BlockingQueue<Object> queue, byte[] init) { |
294435151b0c
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
38 this.bin = bin; |
294435151b0c
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
39 this.vt = vt; |
294435151b0c
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
40 this.buffer = buffer; |
3 | 41 baosin = new ByteArrayOutputStream(); |
42 dsq = queue; | |
43 abyte2 = init; | |
44 } | |
45 | |
46 public void setInputStream(ByteArrayOutputStream is) { | |
47 | |
48 baosin = is; | |
49 | |
50 } | |
51 | |
52 public final void run() { | |
53 | |
54 boolean done = false; | |
55 | |
56 me = Thread.currentThread(); | |
57 | |
58 // load the first response screen | |
59 loadStream(abyte2, 0); | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
60 |
3 | 61 while (!done) { |
62 try { | |
63 | |
64 byte[] abyte0 = readIncoming(); | |
65 | |
66 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK | |
67 // Restructured to the readIncoming() method to return null | |
68 // on TIMING MARK. Don't process in that case (abyte0 == null)! | |
69 if (abyte0 != null) | |
70 { | |
71 // WVL - LDC : 16/07/2003 : TR.000345 | |
72 // When the socket has been closed, the reading returns | |
73 // no bytes (an empty byte arrray). | |
74 // But the loadStream fails on this, so we check it here! | |
75 if (abyte0.length > 0) | |
76 { | |
77 loadStream(abyte0, 0); | |
78 } | |
79 // WVL - LDC : 16/07/2003 : TR.000345 | |
80 // Returning no bytes means the input buffer has | |
81 // reached end-of-stream, so we do a disconnect! | |
82 else | |
83 { | |
84 done = true; | |
85 vt.disconnect(); | |
86 } | |
87 } | |
88 | |
89 } | |
90 | |
91 catch (SocketException se) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
92 Log.w(TAG," DataStreamProducer thread interrupted and stopping " + se.getMessage()); |
3 | 93 done = true; |
94 } | |
95 | |
96 catch (IOException ioe) { | |
97 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
98 Log.w(TAG,ioe.getMessage()); |
3 | 99 if (me.isInterrupted()) |
100 done = true; | |
101 | |
102 } | |
103 catch (Exception ex) { | |
104 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
105 Log.w(TAG,ex.getMessage()); |
3 | 106 if (me.isInterrupted()) |
107 done = true; | |
108 | |
109 } | |
110 } | |
111 } | |
112 | |
113 private final void loadStream(byte abyte0[], int i) { | |
114 | |
115 int j = 0; | |
116 int size = 0; | |
117 if (saveStream == null) { | |
118 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; | |
119 size = abyte0.length; | |
120 } | |
121 else { | |
122 size = saveStream.length + abyte0.length; | |
123 byte[] inter = new byte[size]; | |
124 System.arraycopy(saveStream, 0, inter, 0, saveStream.length); | |
125 System.arraycopy(abyte0, 0, inter, saveStream.length, abyte0.length); | |
126 abyte0 = new byte[size]; | |
127 System.arraycopy(inter, 0, abyte0, 0, size); | |
128 saveStream = null; | |
129 inter = null; | |
130 j = (abyte0[i] & 0xff) << 8 | abyte0[i + 1] & 0xff; | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
131 Log.d(TAG,"partial stream found"); |
3 | 132 } |
133 | |
134 if (j > size) { | |
135 saveStream = new byte[abyte0.length]; | |
136 System.arraycopy(abyte0, 0, saveStream, 0, abyte0.length); | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
137 Log.d(TAG,"partial stream saved"); |
3 | 138 } |
139 else { | |
140 byte abyte1[]; | |
141 try { | |
142 abyte1 = new byte[j + 2]; | |
143 | |
144 System.arraycopy(abyte0, i, abyte1, 0, j + 2); | |
145 dsq.put(abyte1); | |
146 if(abyte0.length > abyte1.length + i) | |
147 loadStream(abyte0, i + j + 2); | |
148 } | |
149 catch (Exception ex) { | |
150 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
151 Log.w(TAG,"load stream error " + ex.getMessage()); |
3 | 152 // ex.printStackTrace(); |
153 // dump(abyte0); | |
154 | |
155 } | |
156 } | |
157 } | |
158 | |
159 public final byte[] readIncoming() | |
160 throws IOException { | |
161 | |
162 boolean done = false; | |
163 boolean negotiate = false; | |
164 | |
165 baosin.reset(); | |
166 int j = -1; | |
167 int i = 0; | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
168 |
3 | 169 while(!done) { |
69
294435151b0c
use 5250 encryption config entry
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
170 if (bin.available() == 0) buffer.testChanged(); |
3 | 171 i = bin.read(); |
172 | |
173 // WVL - LDC : 16/07/2003 : TR.000345 | |
174 // The inStream return -1 when end-of-stream is reached. This | |
175 // happens e.g. when the connection is closed from the AS/400. | |
176 // So we stop in this case! | |
177 // ==> an empty byte array is returned from this method. | |
178 if (i == -1) // nothing read! | |
179 { | |
180 done = true; | |
181 vt.disconnect(); | |
182 continue; | |
183 } | |
184 | |
185 // We use the values instead of the static values IAC and EOR | |
186 // because they are defined as bytes. | |
187 // | |
188 // The > if(i != 255 || j != 255) < is a hack for the double FF FF's | |
189 // that are being returned. I do not know why this is like this and | |
190 // can not find any documentation for it. It is also being returned | |
191 // on my Client Access tcp dump as well so they are handling it. | |
192 // | |
193 // my5250 | |
194 // 0000: 00 50 DA 44 C8 45 42 00 00 00 00 24 08 00 45 00 .P.D.EB....$..E. | |
195 // 0010: 04 2A BC F9 00 00 40 06 D0 27 C1 A8 33 04 C1 A8 .*....@..'..3... | |
196 // 0020: 33 58 00 17 04 18 6F A2 83 CB 00 1E D1 BA 50 18 3X....o.......P. | |
197 // 0030: 20 00 8A 9A 00 00 03 FF FF 12 A0 00 00 04 00 00 ............... | |
198 // --------------------------- || || ------------------------------------- | |
199 // 0040: 03 04 40 04 11 00 20 01 07 00 00 00 18 00 00 10 ..@... ......... | |
200 | |
201 if(j == 255 && i == 255) { | |
202 j = -1; | |
203 continue; | |
204 } | |
205 baosin.write(i); | |
206 // check for end of record EOR and IAC - FFEF | |
207 if(j == 255 && i == 239) | |
208 done = true; | |
209 | |
210 // This is to check for the TELNET TIMING MARK OPTION | |
211 // rfc860 explains this in more detail. When we receive it | |
212 // we will negotiate with the server by sending a WONT'T TIMING-MARK | |
213 // This will let the server know that we processed the information | |
214 // and are just waiting for the user to enter some data so keep the | |
215 // socket alive. This is more or less a AYT (ARE YOU THERE) or not. | |
216 if(i == 253 && j == 255) { | |
217 done = true; | |
218 negotiate = true; | |
219 } | |
220 j = i; | |
221 } | |
222 | |
223 // after the initial negotiation we might get other options such as | |
224 // timing marks ?????????????? do we ???????????? look at telnet spec | |
225 // yes we do. rfc860 explains about timing marks. | |
226 | |
227 // WVL - LDC : 17/05/2004 : Device name negotiations send TIMING MARK | |
228 // to existing device! | |
229 // Handled incorrectly: we cannot continue processing the TIMING MARK DO | |
230 // after we have handled it in the vt.negotiate() | |
231 // We should not return the bytes; | |
232 // ==> restructured to return null after negotiation! | |
233 // Impacts the run method! Added the null check. | |
234 byte[] rBytes = baosin.toByteArray(); | |
235 | |
236 if (dumpBytes) { | |
237 dump(rBytes); | |
238 } | |
239 | |
240 if (negotiate) { | |
241 // get the negotiation option | |
242 baosin.write(bin.read()); | |
243 vt.negotiate(rBytes); | |
244 | |
245 return null; | |
246 } | |
247 return rBytes; | |
248 } | |
249 | |
250 protected final void toggleDebug (ICodePage cp) { | |
251 | |
252 if (codePage == null) | |
253 codePage = cp; | |
254 | |
255 dumpBytes = !dumpBytes; | |
256 if (dumpBytes) { | |
257 | |
258 try { | |
259 if (fw == null) { | |
260 fw = new FileOutputStream("log.txt"); | |
261 dw = new BufferedOutputStream(fw); | |
262 } | |
263 } | |
264 catch (FileNotFoundException fnfe) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
265 Log.w(TAG,fnfe.getMessage()); |
3 | 266 } |
267 | |
268 } | |
269 else { | |
270 | |
271 try { | |
272 | |
273 if (dw != null) | |
274 dw.close(); | |
275 if (fw != null) | |
276 fw.close(); | |
277 dw = null; | |
278 fw = null; | |
279 codePage = null; | |
280 } | |
281 catch(IOException ioe) { | |
282 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
283 Log.w(TAG,ioe.getMessage()); |
3 | 284 } |
285 } | |
286 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
287 Log.i(TAG,"Data Stream output is now " + dumpBytes); |
3 | 288 } |
289 | |
290 public void dump (byte[] abyte0) { | |
291 try { | |
292 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
293 Log.i(TAG,"\n Buffer Dump of data from AS400: "); |
3 | 294 dw.write("\r\n Buffer Dump of data from AS400: ".getBytes()); |
295 | |
296 StringBuffer h = new StringBuffer(); | |
297 for (int x = 0; x < abyte0.length; x++) { | |
298 if (x % 16 == 0) { | |
299 System.out.println(" " + h.toString()); | |
300 dw.write((" " + h.toString() + "\r\n").getBytes()); | |
301 | |
302 h.setLength(0); | |
303 h.append("+0000"); | |
304 h.setLength(5 - Integer.toHexString(x).length()); | |
305 h.append(Integer.toHexString(x).toUpperCase()); | |
306 | |
307 System.out.print(h.toString()); | |
308 dw.write(h.toString().getBytes()); | |
309 | |
310 h.setLength(0); | |
311 } | |
312 char ac = codePage.ebcdic2uni(abyte0[x]); | |
313 if (ac < ' ') | |
314 h.append('.'); | |
315 else | |
316 h.append(ac); | |
317 if (x % 4 == 0) { | |
318 System.out.print(" "); | |
319 dw.write((" ").getBytes()); | |
320 | |
321 } | |
322 | |
323 if (Integer.toHexString(abyte0[x] & 0xff).length() == 1){ | |
324 System.out.print("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()); | |
325 dw.write(("0" + Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes()); | |
326 | |
327 } | |
328 else { | |
329 System.out.print(Integer.toHexString(abyte0[x] & 0xff).toUpperCase()); | |
330 dw.write((Integer.toHexString(abyte0[x] & 0xff).toUpperCase()).getBytes()); | |
331 } | |
332 | |
333 } | |
334 System.out.println(); | |
335 dw.write("\r\n".getBytes()); | |
336 | |
337 dw.flush(); | |
338 } | |
339 catch(EOFException _ex) { } | |
340 catch(Exception _ex) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
3
diff
changeset
|
341 Log.w(TAG,"Cannot dump from host\n\r"); |
3 | 342 } |
343 | |
344 } | |
345 | |
346 // public void dumpBytes() { | |
347 // byte shit[] = bk.buffer; | |
348 // for (int i = 0;i < shit.length;i++) | |
349 // System.out.println(i + ">" + shit[i] + "< - ascii - >" + getASCIIChar(shit[i]) + "<"); | |
350 // } | |
351 // | |
352 // public void dumpHexBytes(byte[] abyte) { | |
353 // byte shit[] = abyte; | |
354 // for (int i = 0;i < shit.length;i++) | |
355 // System.out.println(i + ">" + shit[i] + "< hex >" + Integer.toHexString((shit[i] & 0xff))); | |
356 // } | |
357 | |
358 } |