Skip to content

Commit 1c0605e

Browse files
thscthsc
authored andcommitted
first simple but complete test works. Vey nice :)
1 parent 91c4c57 commit 1c0605e

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

src/net/sharksystem/asp3/ASP3ChunkReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void run() {
4242
//<<<<<<<<<<<<<<<<<<debug
4343
b = new StringBuilder();
4444
b.append(this.getLogStart());
45-
b.append("read chunk URL: ");
45+
b.append("read chunkURI: ");
4646
b.append(chunkUrl);
4747
System.out.println(b.toString());
4848
//>>>>>>>>>>>>>>>>>>>debug

src/net/sharksystem/asp3/ASP3Engine.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ public void handleConnection(InputStream is, OutputStream os) {
149149
System.out.println(b.toString());
150150
//>>>>>>>>>>>>>>>>>>>debug
151151

152+
// era we are about to transmit
152153
int workingEra = this.getEraStartSync(peer);
154+
155+
// newest era (which is not necessarily highest number!!)
153156
int currentEra = this.era;
154157

155-
// we start a conversation - increase era
158+
// we start a conversation - increase era for newly produced messages
156159
this.incrementEra();
157160

158161
//<<<<<<<<<<<<<<<<<<debug
@@ -175,9 +178,22 @@ public void handleConnection(InputStream is, OutputStream os) {
175178
b.append("memento saved");
176179
System.out.println(b.toString());
177180
//>>>>>>>>>>>>>>>>>>>debug
178-
181+
182+
/*
183+
There is a little challenge: era uses a circle of numbers
184+
We cannot say: higher number, later era. That rule does *not*
185+
apply. We can calculate next era, though.
186+
187+
That loop has to be re-entered as long as working era has not
188+
yet reached currentEra. In other words: lastRound is reached whenever
189+
workingEra == currentEra. Processing currentEra is the last round
190+
We at at least one round!
191+
*/
192+
193+
boolean lastRound = false; // assume more than one round
179194
do {
180-
195+
lastRound = workingEra == currentEra;
196+
181197
List<ASP3Chunk> chunks = this.chunkStorage.getChunks(workingEra);
182198
//<<<<<<<<<<<<<<<<<<debug
183199
b = new StringBuilder();
@@ -211,6 +227,7 @@ public void handleConnection(InputStream is, OutputStream os) {
211227
b = new StringBuilder();
212228
b.append(this.getLogStart());
213229
b.append("send chunk");
230+
System.out.println(b.toString());
214231
//>>>>>>>>>>>>>>>>>>>debug
215232
this.sendChunk(chunk, dos);
216233

@@ -219,15 +236,18 @@ public void handleConnection(InputStream is, OutputStream os) {
219236
//<<<<<<<<<<<<<<<<<<debug
220237
b = new StringBuilder();
221238
b.append(this.getLogStart());
222-
b.append("removed recipient");
239+
b.append("removed recipient ");
223240
b.append(peer);
241+
System.out.println(b.toString());
224242
//>>>>>>>>>>>>>>>>>>>debug
225243
// empty?
226244
if(chunk.getRecipients().isEmpty()) {
227245
chunk.drop();
228246
//<<<<<<<<<<<<<<<<<<debug
247+
b = new StringBuilder();
229248
b.append(this.getLogStart());
230249
b.append("chunk dropped");
250+
System.out.println(b.toString());
231251
}
232252
}
233253

@@ -240,8 +260,8 @@ public void handleConnection(InputStream is, OutputStream os) {
240260
// next era which isn't necessarilly workingEra++
241261
workingEra = this.getNextEra(workingEra);
242262

243-
// until served era we came in in the first place
244-
} while(workingEra != currentEra);
263+
// as long as not already performed last round
264+
} while(!lastRound);
245265

246266
//<<<<<<<<<<<<<<<<<<debug
247267
b = new StringBuilder();
@@ -379,6 +399,7 @@ private void sendChunk(ASP3Chunk chunk, DataOutputStream dos)
379399
b.append(")");
380400
b.append("read message: ");
381401
b.append(message);
402+
System.out.println(b.toString());
382403
//>>>>>>>>>>>>>>>>>>>debug
383404

384405
dos.writeUTF((String) message);
@@ -390,6 +411,7 @@ private void sendChunk(ASP3Chunk chunk, DataOutputStream dos)
390411
b.append(")");
391412
b.append("wrote message: ");
392413
b.append(message);
414+
System.out.println(b.toString());
393415
//>>>>>>>>>>>>>>>>>>>debug
394416

395417
}
@@ -399,7 +421,7 @@ private void sendChunk(ASP3Chunk chunk, DataOutputStream dos)
399421
b.append(this.owner);
400422
b.append(")");
401423
b.append("stop iterating messages ");
402-
424+
System.out.println(b.toString());
403425
}
404426

405427
/**

src/net/sharksystem/asp3/ASP3StorageFS.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,26 @@ public List<ASP3Chunk> getChunks(int era) throws IOException {
8585
List<ASP3Chunk> chunkList = new ArrayList<>();
8686

8787
File dir = new File(this.getPath(era));
88+
89+
// can be null!
8890
File[] contentFileList = dir.listFiles(new FilenameFilter() {
8991
@Override
9092
public boolean accept(File dir, String fileName) {
9193
return fileName.endsWith(DATA_EXTENSION);
9294
}
9395
});
9496

95-
for (int i = 0; i < contentFileList.length; i++) {
96-
String name = contentFileList[i].getName();
97-
98-
// cut extension
99-
int index = name.lastIndexOf('.');
100-
if(index != -1) {
101-
String chunkName = name.substring(0, index);
102-
String fName = this.getFullFileNameByChunkName(era, chunkName);
103-
chunkList.add(new ASP3ChunkFS(this, fName));
97+
if(contentFileList != null) {
98+
for (int i = 0; i < contentFileList.length; i++) {
99+
String name = contentFileList[i].getName();
100+
101+
// cut extension
102+
int index = name.lastIndexOf('.');
103+
if(index != -1) {
104+
String chunkName = name.substring(0, index);
105+
String fName = this.getFullFileNameByChunkName(era, chunkName);
106+
chunkList.add(new ASP3ChunkFS(this, fName));
107+
}
104108
}
105109
}
106110

test/BasicTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
88
import java.util.List;
9-
import java.util.logging.Level;
10-
import java.util.logging.Logger;
119
import net.sharksystem.asp3.ASP3Engine;
1210
import net.sharksystem.asp3.ASP3EngineFS;
1311
import net.sharksystem.asp3.ASP3Exception;
@@ -103,7 +101,7 @@ public void usage() throws IOException, ASP3Exception, InterruptedException {
103101

104102
bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream());
105103

106-
Thread.sleep(2000);
104+
Thread.sleep(10000);
107105

108106
// check received message
109107
List<String> messages = aliceReader.getMessages();

0 commit comments

Comments
 (0)