Skip to content

Commit 83f7e55

Browse files
thscthsc
authored andcommitted
before removing chunkReceived and chunkSend interfaces.
1 parent 73cc3a3 commit 83f7e55

14 files changed

+139
-296
lines changed

src/net/sharksystem/asp3/ASP3ChunkReader.java

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
import java.io.IOException;
55

66
class ASP3ChunkReader implements Runnable {
7-
ASP3Reader reader;
87
private final DataInputStream dis;
98
private final String peer;
109
private final String owner;
10+
private final ASP3ChunkStorage storage;
1111

12-
ASP3ChunkReader(ASP3Reader reader, DataInputStream dis, String owner,
13-
String peer) {
14-
this.reader = reader;
12+
ASP3ChunkReader(DataInputStream dis, String owner,
13+
String peer, ASP3ChunkStorage storage) {
1514
this.dis = dis;
1615
this.peer = peer;
1716
this.owner = owner;
17+
this.storage = storage;
1818
}
1919

2020
private String getLogStart() {
@@ -37,16 +37,51 @@ public void run() {
3737
System.out.println(b.toString());
3838
//>>>>>>>>>>>>>>>>>>>debug
3939

40-
this.reader.newSender(peer);
40+
ASP3Storage peerStorage = null;
41+
42+
// get received storage
43+
peerStorage = this.storage.getReceivedChunkStorage(peer);
44+
//<<<<<<<<<<<<<<<<<<debug
45+
b = new StringBuilder();
46+
b.append(this.getLogStart());
47+
b.append("got received chunk storage ");
48+
System.out.println(b.toString());
49+
//>>>>>>>>>>>>>>>>>>>debug
50+
4151
try {
42-
String chunkUrl = dis.readUTF();
52+
String uri = dis.readUTF();
4353
//<<<<<<<<<<<<<<<<<<debug
4454
b = new StringBuilder();
4555
b.append(this.getLogStart());
4656
b.append("read chunkURI: ");
47-
b.append(chunkUrl);
57+
b.append(uri);
4858
System.out.println(b.toString());
4959
//>>>>>>>>>>>>>>>>>>>debug
60+
ASP3Chunk2Send chunk =
61+
peerStorage.getChunk(uri, storage.getEra());
62+
63+
if(chunk != null) {
64+
//<<<<<<<<<<<<<<<<<<debug
65+
b = new StringBuilder();
66+
b.append(this.getLogStart());
67+
b.append("got chunk: ");
68+
b.append(uri);
69+
System.out.println(b.toString());
70+
//>>>>>>>>>>>>>>>>>>>debug
71+
} else {
72+
//<<<<<<<<<<<<<<<<<<debug
73+
b = new StringBuilder();
74+
b.append(this.getLogStart());
75+
b.append("ERROR: no chunk found for sender/uri: ");
76+
b.append(peer);
77+
b.append(" / ");
78+
b.append(uri);
79+
System.err.println(b.toString());
80+
//>>>>>>>>>>>>>>>>>>>debug
81+
}
82+
83+
ASP3ChunkSerialization.readChunk(chunk, dis);
84+
5085
for(;;) {
5186
// escapes with IOException
5287
String message = dis.readUTF();
@@ -58,9 +93,17 @@ public void run() {
5893
System.out.println(b.toString());
5994
//>>>>>>>>>>>>>>>>>>>debug
6095

61-
if(this.reader != null) {
62-
this.reader.read(chunkUrl, message);
63-
}
96+
//<<<<<<<<<<<<<<<<<<debug
97+
b = new StringBuilder();
98+
b.append(this.getLogStart());
99+
b.append("going to write to received chunk: ");
100+
b.append(uri);
101+
b.append(" / era: ");
102+
b.append(storage.getEra());
103+
System.out.println(b.toString());
104+
//>>>>>>>>>>>>>>>>>>>debug
105+
106+
chunk.add(message);
64107
}
65108
} catch (IOException ex) {
66109
// done
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package net.sharksystem.asp3;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.util.Iterator;
7+
8+
/**
9+
*
10+
* @author thsc
11+
*/
12+
abstract class ASP3ChunkSerialization {
13+
static void sendChunk(ASP3Chunk2Send chunk, DataOutputStream dos)
14+
throws IOException {
15+
16+
//<<<<<<<<<<<<<<<<<<debug
17+
StringBuilder b = new StringBuilder();
18+
b.append("send chunk url: ");
19+
b.append(chunk.getUri());
20+
System.out.println(b.toString());
21+
//>>>>>>>>>>>>>>>>>>>debug
22+
23+
// send url
24+
dos.writeUTF(chunk.getUri());
25+
26+
Iterator<CharSequence> messages = chunk.getMessages();
27+
//<<<<<<<<<<<<<<<<<<debug
28+
b = new StringBuilder();
29+
b.append("iterate messages ");
30+
System.out.println(b.toString());
31+
//>>>>>>>>>>>>>>>>>>>debug
32+
while(messages.hasNext()) {
33+
CharSequence message = messages.next();
34+
//<<<<<<<<<<<<<<<<<<debug
35+
b = new StringBuilder();
36+
b.append("read message from chunk: ");
37+
b.append(message);
38+
System.out.println(b.toString());
39+
//>>>>>>>>>>>>>>>>>>>debug
40+
41+
dos.writeUTF((String) message);
42+
43+
//<<<<<<<<<<<<<<<<<<debug
44+
b = new StringBuilder();
45+
b.append("wrote message to stream: ");
46+
b.append(message);
47+
System.out.println(b.toString());
48+
//>>>>>>>>>>>>>>>>>>>debug
49+
50+
}
51+
//<<<<<<<<<<<<<<<<<<debug
52+
b = new StringBuilder();
53+
b.append("stop iterating messages ");
54+
System.out.println(b.toString());
55+
}
56+
57+
static void readChunk(ASP3ChunkReceived chunk, DataInputStream dis)
58+
throws IOException {
59+
60+
for(;;) {
61+
// escapes with IOException
62+
String message = dis.readUTF();
63+
//<<<<<<<<<<<<<<<<<<debug
64+
StringBuilder b = new StringBuilder();
65+
b.append("chunk deserialisation read message: ");
66+
b.append(message);
67+
System.out.println(b.toString());
68+
//>>>>>>>>>>>>>>>>>>>debug
69+
70+
chunk.addReceivedMessage(message);
71+
}
72+
}
73+
}

src/net/sharksystem/asp3/ASP3DefaultReader.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/net/sharksystem/asp3/ASP3Engine.java

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,12 @@ public abstract class ASP3Engine implements ASP3ChunkStorage, ASP3ProtocolEngine
3030
protected HashMap<String, Integer> lastSeen = new HashMap<>();
3131
protected ASP3Memento memento = null;
3232

33-
private final ASP3Reader reader;
3433
/* private */ final private ASP3Storage chunkStorage;
3534

36-
protected ASP3Engine(ASP3Storage chunkStorage, ASP3Reader reader)
35+
protected ASP3Engine(ASP3Storage chunkStorage)
3736
throws ASP3Exception, IOException {
3837

3938
this.chunkStorage = chunkStorage;
40-
this.reader = reader;
41-
42-
/*
43-
if(reader == null) {
44-
throw new ASP3Exception("reader must not be null");
45-
}
46-
*/
4739
}
4840

4941
ASP3Storage getStorage() {
@@ -129,7 +121,7 @@ public void handleConnection(InputStream is, OutputStream os,
129121

130122
// start reading from remote peer
131123
Thread readerThread = new Thread(
132-
new ASP3ChunkReader(this.reader, dis, this.owner, peer));
124+
new ASP3ChunkReader(dis, this.owner, peer, this));
133125

134126
readerThread.start();
135127
//<<<<<<<<<<<<<<<<<<debug
@@ -219,7 +211,7 @@ public void handleConnection(InputStream is, OutputStream os,
219211
b.append("send chunk");
220212
System.out.println(b.toString());
221213
//>>>>>>>>>>>>>>>>>>>debug
222-
this.sendChunk(chunk, dos);
214+
ASP3ChunkSerialization.sendChunk(chunk, dos);
223215

224216
// remember sent
225217
chunk.removeRecipient(peer);
@@ -355,65 +347,6 @@ private synchronized void incrementEra() throws IOException {
355347
this.chunkStorage.dropChunks(this.era);
356348
}
357349

358-
private void sendChunk(ASP3Chunk2Send chunk, DataOutputStream dos)
359-
throws IOException {
360-
361-
//<<<<<<<<<<<<<<<<<<debug
362-
StringBuilder b = new StringBuilder();
363-
b.append("ASP3Engine (");
364-
b.append(this.owner);
365-
b.append(")");
366-
b.append("send chunk url: ");
367-
b.append(chunk.getUri());
368-
System.out.println(b.toString());
369-
//>>>>>>>>>>>>>>>>>>>debug
370-
371-
// send url
372-
dos.writeUTF(chunk.getUri());
373-
374-
Iterator<CharSequence> messages = chunk.getMessages();
375-
//<<<<<<<<<<<<<<<<<<debug
376-
b = new StringBuilder();
377-
b.append("ASP3Engine (");
378-
b.append(this.owner);
379-
b.append(")");
380-
b.append("iterate messages ");
381-
System.out.println(b.toString());
382-
//>>>>>>>>>>>>>>>>>>>debug
383-
while(messages.hasNext()) {
384-
CharSequence message = messages.next();
385-
//<<<<<<<<<<<<<<<<<<debug
386-
b = new StringBuilder();
387-
b.append("ASP3Engine (");
388-
b.append(this.owner);
389-
b.append(")");
390-
b.append("read message: ");
391-
b.append(message);
392-
System.out.println(b.toString());
393-
//>>>>>>>>>>>>>>>>>>>debug
394-
395-
dos.writeUTF((String) message);
396-
397-
//<<<<<<<<<<<<<<<<<<debug
398-
b = new StringBuilder();
399-
b.append("ASP3Engine (");
400-
b.append(this.owner);
401-
b.append(")");
402-
b.append("wrote message: ");
403-
b.append(message);
404-
System.out.println(b.toString());
405-
//>>>>>>>>>>>>>>>>>>>debug
406-
407-
}
408-
//<<<<<<<<<<<<<<<<<<debug
409-
b = new StringBuilder();
410-
b.append("ASP3Engine (");
411-
b.append(this.owner);
412-
b.append(")");
413-
b.append("stop iterating messages ");
414-
System.out.println(b.toString());
415-
}
416-
417350
/**
418351
* We interpret an existing chunk with *no* recipients as
419352
* public chunk

src/net/sharksystem/asp3/ASP3EngineFS.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public class ASP3EngineFS extends ASP3Engine {
1818
private ASP3StorageFS chunkStorageFS;
1919

2020
private ASP3EngineFS(String rootDirectory,
21-
ASP3StorageFS chunkStorage, ASP3Reader reader)
21+
ASP3StorageFS chunkStorage)
2222
throws ASP3Exception, IOException {
2323

24-
super(new ASP3StorageFS(rootDirectory), reader);
24+
super(new ASP3StorageFS(rootDirectory));
2525

2626
this.rootDirectory = rootDirectory;
2727
}
@@ -35,7 +35,7 @@ public static ASP3ChunkStorage getASP3ChunkStorage(String owner, String rootDire
3535
root.mkdirs();
3636
}
3737

38-
return ASP3EngineFS.getASP3Engine(owner, rootDirectory, null);
38+
return ASP3EngineFS.getASP3Engine(owner, rootDirectory);
3939

4040
}
4141

@@ -49,18 +49,6 @@ public static ASP3ChunkStorage getASP3ChunkStorage(String rootDirectory)
4949
public static ASP3Engine getASP3Engine(String owner, String rootDirectory)
5050
throws IOException, ASP3Exception {
5151

52-
ASP3DefaultReader reader = new ASP3DefaultReader();
53-
ASP3Engine engine = ASP3EngineFS.getASP3Engine(owner, rootDirectory, reader);
54-
55-
reader.setEngine(engine);
56-
57-
return engine;
58-
59-
}
60-
61-
public static ASP3Engine getASP3Engine(String owner, String rootDirectory,
62-
ASP3Reader reader) throws IOException, ASP3Exception {
63-
6452
// root directory must exist when setting up an engine
6553
File root = new File(rootDirectory);
6654
if(!root.exists() || !root.isDirectory()) {
@@ -69,8 +57,7 @@ public static ASP3Engine getASP3Engine(String owner, String rootDirectory,
6957

7058
ASP3EngineFS engine = new ASP3EngineFS(
7159
rootDirectory,
72-
new ASP3StorageFS(rootDirectory),
73-
reader);
60+
new ASP3StorageFS(rootDirectory));
7461

7562

7663
ASP3EngineFS.ASP3MementoFS mementoFS = engine.getMemento(rootDirectory);
@@ -96,10 +83,10 @@ public static ASP3Engine getASP3Engine(String owner, String rootDirectory,
9683
return engine;
9784
}
9885

99-
public static ASP3Engine getASP3Engine(String rootDirectory, ASP3Reader reader)
86+
public static ASP3Engine getASP3Engine(String rootDirectory)
10087
throws IOException, ASP3Exception {
10188

102-
return ASP3EngineFS.getASP3Engine(null, rootDirectory, reader);
89+
return ASP3EngineFS.getASP3Engine(null, rootDirectory);
10390

10491
}
10592

@@ -111,7 +98,8 @@ private ASP3EngineFS.ASP3MementoFS getMemento(String rootDirectory) {
11198

11299
@Override
113100
public ASP3Storage getReceivedChunkStorage(CharSequence sender) {
114-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
101+
String dir = this.rootDirectory + "/" + sender;
102+
return new ASP3StorageFS(dir);
115103
}
116104

117105
class ASP3MementoFS implements ASP3Memento {

0 commit comments

Comments
 (0)