Skip to content

Commit 7feba7b

Browse files
committed
added idle channel closer
1 parent b7b396a commit 7feba7b

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

src/net/sharksystem/asap/utils/ASAPSerialization.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public static void writeCharSequenceParameter(CharSequence parameter, OutputStre
6767
byte[] bytes = parameter.toString().getBytes();
6868
writeNonNegativeIntegerParameter(bytes.length, os);
6969
os.write(bytes);
70+
71+
/*
72+
String lenString = printBitsToString(bytes.length);
73+
String readBytesString = printByteArrayToString(bytes);
74+
String log = "writeCharSequenceParameter\nlen: " + lenString + "\nbytes: " + readBytesString;
75+
System.out.println(log);
76+
*/
7077
}
7178

7279
public static void writeByteParameter(byte parameter, OutputStream os) throws IOException {
@@ -143,6 +150,37 @@ public static void printBits(long l, int bits) {
143150
System.out.print(" ");
144151
}
145152

153+
public static String printBitsToString(long l, int bits) {
154+
StringBuilder sb = new StringBuilder();
155+
156+
long mask = 1;
157+
mask = mask << bits-1;
158+
short byteBitCounter = 4;
159+
while(mask != 0) {
160+
if((l & mask) != 0) sb.append("1");
161+
else sb.append("0");
162+
if(--byteBitCounter == 0) {
163+
byteBitCounter = 4;
164+
sb.append(" ");
165+
}
166+
mask = mask >> 1;
167+
}
168+
sb.append(" ");
169+
170+
return sb.toString();
171+
}
172+
173+
public static String printByteArrayToString(byte[] byteArray) {
174+
StringBuilder sb = new StringBuilder();
175+
for(int index = byteArray.length - 1; index >= 0; index--) {
176+
sb.append(printByteToString(byteArray[index]));
177+
}
178+
return sb.toString();
179+
}
180+
181+
public static String printByteToString(short s) { return printBitsToString(s, 8); }
182+
public static String printBitsToString(int i) { return printBitsToString(i, 16); }
183+
146184
public static void printByteArray(byte[] byteArray) {
147185
for(int index = byteArray.length - 1; index >= 0; index--) {
148186
printByte(byteArray[index]);
@@ -186,8 +224,10 @@ public static short readShortParameter(InputStream is) throws IOException, ASAPE
186224
}
187225

188226
public static int readIntegerParameter(InputStream is) throws IOException, ASAPException {
189-
int value = readShortParameter(is);
227+
int value = 0;
228+
value = readShortParameter(is);
190229
value = value << 16;
230+
191231
value = value & BLANK_RIGHT_INTEGER;
192232

193233
int right = readShortParameter(is);
@@ -218,11 +258,33 @@ public static long readLongParameter(InputStream is) throws IOException, ASAPExc
218258
}
219259

220260
public static String readCharSequenceParameter(InputStream is) throws IOException, ASAPException {
221-
int length = readIntegerParameter(is);
222-
byte[] parameterBytes = new byte[length];
261+
int length = 0;
262+
byte[] parameterBytes = null;
263+
String lenString = null;
264+
// try {
265+
length = readIntegerParameter(is);
266+
/*
267+
lenString = printBitsToString(length);
268+
System.out.println("readCharSequenceParameter length = " + lenString);
269+
270+
*/
271+
parameterBytes = new byte[length];
272+
/*
273+
}
274+
catch(OutOfMemoryError e) {
275+
int i = 42;
276+
throw new ASAPException(e);
277+
}
278+
*/
223279

224280
is.read(parameterBytes);
225281

282+
/*
283+
String readBytesString = printByteArrayToString(parameterBytes);
284+
String log = "readCharSequenceParameter\nlen: " + lenString + "\nbytes: " + readBytesString;
285+
System.out.println(log);
286+
*/
287+
226288
return new String(parameterBytes);
227289
}
228290

src/net/sharksystem/streams/IdleStreamPairCloser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ void setStreamPairWrapper(StreamPairWrapper streamPairWrapper) {
3232
}
3333

3434
public void start() {
35-
this.alarmClock = new AlarmClock(this.timeout, this);
35+
// give it more time in the first round - there will be a connection establishment process on its way...
36+
this.alarmClock = new AlarmClock(this.timeout * 2, this);
3637
}
3738

3839
@Override

src/net/sharksystem/streams/StreamLink.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sharksystem.streams;
22

3+
import net.sharksystem.asap.ASAP;
4+
import net.sharksystem.asap.utils.ASAPSerialization;
35
import net.sharksystem.utils.Log;
46

57
import java.io.IOException;
@@ -30,38 +32,53 @@ public void close() {
3032
private boolean again = true;
3133

3234
public void run() {
33-
Log.writeLog(this, "start read/write loop");
35+
Log.writeLog(this, this.toString(), "start read/write loop");
3436
try {
37+
int singleReadCounter = 0;
3538
int read = -1;
3639
do {
3740
int available = sourceIS.available();
41+
//Log.writeLog(this, this.toString(), "available: " + available);
3842
if (available > 0) {
43+
singleReadCounter = 0;
3944
byte[] buffer = new byte[available];
4045
sourceIS.read(buffer);
4146
targetOS.write(buffer);
47+
//Log.writeLog(this, this.toString(), ASAPSerialization.printByteArrayToString(buffer)
48+
// + " end buffer:\n");
4249
} else {
4350
// block
44-
//Log.writeLog(this, "going to block in read(): " + id);
51+
//Log.writeLog(this, this.toString(), "going to block in read(): ");
4552
read = sourceIS.read();
4653
if(read != -1) {
54+
/*
55+
Log.writeLog(this, this.toString(), "read != -1 (" + ++singleReadCounter + ")");
56+
Log.writeLog(this, this.toString(), ASAPSerialization.printByteToString((short) read)
57+
+ " end byte");
58+
*/
4759
targetOS.write(read);
4860
} else {
61+
//Log.writeLog(this, this.toString(), "read -1 - end");
4962
again = false;
5063
}
5164
}
5265
} while (again);
5366
} catch (IOException e) {
54-
Log.writeLog(this, "ioException - most probably connection closed: " + id);
67+
Log.writeLog(this, this.toString(), "ioException - most probably connection closed: " + id);
5568
} finally {
5669
if(this.closeStreams) {
57-
Log.writeLog(this, "try closing linked streams: " + id);
70+
Log.writeLog(this, this.toString(), "try closing linked streams: " + id);
5871
try {this.targetOS.close();}
59-
catch (IOException ioException) { Log.writeLog(this, "failed close input stream: " + id); }
72+
catch (IOException ioException) { Log.writeLog(this, this.toString(), "failed close input stream: " + id); }
6073
try {this.sourceIS.close();}
61-
catch (IOException ioException) { Log.writeLog(this, "failed close output stream: " + id); }
74+
catch (IOException ioException) { Log.writeLog(this, this.toString(), "failed close output stream: " + id); }
6275
}
6376

64-
Log.writeLog(this, "end linked streams connection: " + id);
77+
Log.writeLog(this, this.toString(), "end linked streams connection: " + id);
6578
}
6679
}
80+
81+
public String toString() {
82+
return this.id;
83+
}
6784
}

src/net/sharksystem/streams/StreamPairLink.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class StreamPairLink implements StreamPairListener {
99
private StreamLink streamLinkB2A;
1010

1111
public StreamPairLink(StreamPair pairA, CharSequence idA, StreamPair pairB, CharSequence idB) throws IOException {
12-
String tagA2B = idA + " -> " + idB;
12+
String tagA2B = idB + " ==> " + idA;
1313
this.streamLinkA2B = new StreamLink(pairA.getInputStream(), pairB.getOutputStream(), true, tagA2B);
14-
String tagB2A = idA + " <- " + idB;
14+
String tagB2A = idA + " ==> " + idB;
1515
this.streamLinkB2A = new StreamLink(pairB.getInputStream(), pairA.getOutputStream(), true, tagB2A);
1616

1717
// listen to close

0 commit comments

Comments
 (0)