Skip to content

Commit 354c84c

Browse files
committed
listener is now informed about newly read chunks
1 parent 2c37e69 commit 354c84c

File tree

7 files changed

+88
-50
lines changed

7 files changed

+88
-50
lines changed

src/net/sharksystem/asp3/ASP3ChunkReader.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
import java.io.DataInputStream;
44
import java.io.IOException;
5-
import java.util.logging.Level;
6-
import java.util.logging.Logger;
75

86
class ASP3ChunkReader implements Runnable {
97
private final DataInputStream dis;
108
private final String peer;
119
private final String owner;
1210
private final ASP3ChunkStorage storage;
11+
private final ASP3ReceivedChunkListener listener;
1312

1413
ASP3ChunkReader(DataInputStream dis, String owner,
15-
String peer, ASP3ChunkStorage storage) {
14+
String peer, ASP3ChunkStorage storage,
15+
ASP3ReceivedChunkListener listener) {
1616
this.dis = dis;
1717
this.peer = peer;
1818
this.owner = owner;
1919
this.storage = storage;
20+
this.listener = listener;
2021
}
2122

2223
private String getLogStart() {
@@ -51,7 +52,8 @@ public void run() {
5152
//>>>>>>>>>>>>>>>>>>>debug
5253

5354
try {
54-
ASP3ChunkSerialization.readChunk(this.storage, peerStorage, dis);
55+
ASP3ChunkSerialization.readChunks(peer, this.storage,
56+
peerStorage, dis, listener);
5557
} catch (IOException ex) {
5658
try {
5759
// give up

src/net/sharksystem/asp3/ASP3ChunkSerialization.java

Lines changed: 73 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,60 +60,90 @@ static void sendChunk(ASP3Chunk chunk, DataOutputStream dos)
6060
System.out.println(b.toString());
6161
}
6262

63-
static void readChunk(ASP3ChunkStorage chunkStorage, ASP3Storage storage,
64-
DataInputStream dis) throws IOException {
63+
/**
64+
* This methods reads until an IOException notifies closed connection.
65+
* It expects serialized chunks (plural!) which are written to received
66+
* message storage.
67+
*
68+
* @param chunkStorage where the received chunks are stored
69+
* @param storage that looks like a design glitch TODO
70+
* @param dis input stream to read serialized chunks
71+
* @param listener to be notified about a successfully deserialized chunk
72+
* @throws IOException
73+
*/
74+
static void readChunks(String sender, ASP3ChunkStorage chunkStorage,
75+
ASP3Storage storage, DataInputStream dis,
76+
ASP3ReceivedChunkListener listener) throws IOException {
6577

6678
try {
67-
// read URI
68-
String uri = dis.readUTF();
69-
70-
// read number of messages
71-
int number = dis.readInt();
72-
73-
//<<<<<<<<<<<<<<<<<<debug
74-
StringBuilder b = new StringBuilder();
75-
b.append("read chunkURI / #messages ");
76-
b.append(uri);
77-
b.append(" / ");
78-
b.append(number);
79-
System.out.println(b.toString());
80-
//>>>>>>>>>>>>>>>>>>>debug
81-
ASP3Chunk chunk =
82-
storage.getChunk(uri, chunkStorage.getEra());
83-
84-
if(chunk != null) {
79+
while(true) { // until IOException informs end of communication
80+
// read URI
81+
String uri = dis.readUTF();
82+
83+
// read number of messages
84+
int number = dis.readInt();
85+
8586
//<<<<<<<<<<<<<<<<<<debug
86-
b = new StringBuilder();
87-
b.append("got chunk: ");
87+
StringBuilder b = new StringBuilder();
88+
b.append("ASPChunkDeserialization: ");
89+
b.append("read chunkURI / #messages / sender");
8890
b.append(uri);
89-
System.out.println(b.toString());
90-
//>>>>>>>>>>>>>>>>>>>debug
91-
} else {
92-
//<<<<<<<<<<<<<<<<<<debug
93-
b = new StringBuilder();
94-
b.append("ERROR: no chunk found for sender/uri: ");
9591
b.append(" / ");
96-
b.append(uri);
97-
System.err.println(b.toString());
98-
//>>>>>>>>>>>>>>>>>>>debug
99-
throw new IOException("couldn't create local chunk storage - give up");
100-
}
101-
102-
for(;number > 0; number--) {
103-
// escapes with IOException
104-
String message = dis.readUTF();
105-
//<<<<<<<<<<<<<<<<<<debug
106-
b = new StringBuilder();
107-
b.append("chunk deserialisation read message: ");
108-
b.append(message);
92+
b.append(number);
93+
b.append(" / ");
94+
b.append(sender);
10995
System.out.println(b.toString());
11096
//>>>>>>>>>>>>>>>>>>>debug
97+
ASP3Chunk chunk =
98+
storage.getChunk(uri, chunkStorage.getEra());
99+
100+
if(chunk != null) {
101+
//<<<<<<<<<<<<<<<<<<debug
102+
b = new StringBuilder();
103+
b.append("ASPChunkDeserialization: ");
104+
b.append("got chunk: ");
105+
b.append(uri);
106+
System.out.println(b.toString());
107+
//>>>>>>>>>>>>>>>>>>>debug
108+
} else {
109+
//<<<<<<<<<<<<<<<<<<debug
110+
b = new StringBuilder();
111+
b.append("ASPChunkDeserialization: ");
112+
b.append("ERROR: no chunk found for sender/uri: ");
113+
b.append(" / ");
114+
b.append(uri);
115+
System.err.println(b.toString());
116+
//>>>>>>>>>>>>>>>>>>>debug
117+
throw new IOException("couldn't create local chunk storage - give up");
118+
}
111119

112-
chunk.add(message);
120+
for(;number > 0; number--) {
121+
// escapes with IOException
122+
String message = dis.readUTF();
123+
//<<<<<<<<<<<<<<<<<<debug
124+
b = new StringBuilder();
125+
b.append("ASPChunkDeserialization: ");
126+
b.append("read message: ");
127+
b.append(message);
128+
System.out.println(b.toString());
129+
//>>>>>>>>>>>>>>>>>>>debug
130+
131+
chunk.add(message);
132+
}
133+
134+
// read all messages
135+
listener.chunkReceived(sender, uri, chunkStorage.getEra());
113136
}
114137

115138
} catch (IOException ex) {
116-
// done
139+
// done - connection closed
140+
141+
//<<<<<<<<<<<<<<<<<<debug
142+
StringBuilder b = new StringBuilder();
143+
b.append("ASPChunkDeserialization: ");
144+
b.append("connection close - done");
145+
System.out.println(b.toString());
146+
//>>>>>>>>>>>>>>>>>>>debug
117147
}
118148
}
119149
}

src/net/sharksystem/asp3/ASP3Engine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void handleConnection(InputStream is, OutputStream os,
121121

122122
// start reading from remote peer
123123
Thread readerThread = new Thread(
124-
new ASP3ChunkReader(dis, this.owner, peer, this));
124+
new ASP3ChunkReader(dis, this.owner, peer, this, listener));
125125

126126
readerThread.start();
127127
//<<<<<<<<<<<<<<<<<<debug

test/ASP3ChunkReceiverTester.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ public class ASP3ChunkReceiverTester implements ASP3ReceivedChunkListener {
1111
private int era;
1212

1313
@Override
14-
public void chunkReceived(String sender, String Uri, int era) {
14+
public void chunkReceived(String sender, String uri, int era) {
15+
System.out.println("ChunkReceiverTester.chunkReceived called" +
16+
sender +
17+
" / " +
18+
uri +
19+
" / " +
20+
era);
1521
this.sender = sender;
16-
this.uri = Uri;
22+
this.uri = uri;
1723
this.era = era;
1824
}
1925

34 Bytes
Binary file not shown.
Binary file not shown.
-34 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)