Skip to content

Commit ca66cc4

Browse files
committed
two hop scenario works
1 parent 36c2be5 commit ca66cc4

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ public void newEra() {
683683

684684
if(this.contentChanged) {
685685
sb.append("content changed - increment era...");
686+
System.out.println(sb.toString());
686687
try {
687688
int oldEra = this.era;
688689
int nextEra = this.getNextEra(this.era);
@@ -692,11 +693,10 @@ public void newEra() {
692693

693694
// we are done here - we are in a new era.
694695
this.era = nextEra;
696+
695697
// persistent values
696698
if(this.memento != null) this.memento.save(this);
697699

698-
// do some - probably time consuming stuff
699-
700700
// drop very very old chunks - if available
701701
this.chunkStorage.dropChunks(nextEra);
702702

@@ -706,8 +706,7 @@ public void newEra() {
706706
copyChunk.clone(chunk);
707707
}
708708

709-
sb.append(" done");
710-
System.out.println(sb.toString());
709+
System.out.println(this.getLogStart() + "era incremented");
711710
} catch (IOException ex) {
712711
sb.append("IOException while incrementing era: ");
713712
sb.append(ex.getLocalizedMessage());

src/net/sharksystem/asap/ASAPEngineFS.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ private ASAPEngineFS(String owner,
2828
this.rootDirectory = rootDirectory;
2929
}
3030

31+
public String getRootFolder() {
32+
return this.rootDirectory;
33+
}
34+
3135
public String getOwner() {
3236
return this.owner;
3337
}
@@ -58,6 +62,7 @@ public static ASAPEngine getASAPEngine(String owner, String rootDirectory, CharS
5862
}
5963

6064
public ASAPStorage refresh() throws IOException, ASAPException {
65+
this.memento.save(this);
6166
return ASAPEngineFS.getExistingASAPEngineFS(this.rootDirectory);
6267
}
6368

src/net/sharksystem/asap/ASAPMementoFS.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import net.sharksystem.asap.protocol.ASAP_1_0;
44

5-
import java.io.DataInputStream;
6-
import java.io.DataOutputStream;
7-
import java.io.File;
8-
import java.io.FileInputStream;
9-
import java.io.FileOutputStream;
10-
import java.io.IOException;
5+
import java.io.*;
116
import java.util.HashMap;
127

138
/**
@@ -82,10 +77,18 @@ public void restore(ASAPEngine engine) throws IOException {
8277
engine.owner = dis.readUTF();
8378
engine.format = dis.readUTF();
8479
engine.era = dis.readInt();
85-
engine.oldestEra = dis.readInt();
86-
engine.contentChanged = dis.readBoolean();
87-
engine.dropDeliveredChunks = dis.readBoolean();
88-
engine.sendReceivedChunks = dis.readBoolean();
80+
81+
// from hereon optional values - could work with defaults
82+
try {
83+
engine.oldestEra = dis.readInt();
84+
engine.contentChanged = dis.readBoolean();
85+
engine.dropDeliveredChunks = dis.readBoolean();
86+
engine.sendReceivedChunks = dis.readBoolean();
87+
}
88+
catch(EOFException e) {
89+
// ignore and work with set defaults
90+
return; // reached end of file - nothing to do here
91+
}
8992

9093
// try to read lastSeen list
9194
boolean first = true;

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,6 @@ public void doResetASAPStorages() {
428428
ASAPEngineFS.removeFolder("tests");
429429
}
430430

431-
public ASAPStorage getStorage(String storageName) throws ASAPException {
432-
ASAPStorage asapStorage = this.storages.get(storageName);
433-
if(asapStorage == null) throw new ASAPException("no such storage: " + storageName);
434-
435-
return asapStorage;
436-
}
437-
438431
public void doSetSendReceivedMessage(String parameterString) {
439432
StringTokenizer st = new StringTokenizer(parameterString);
440433

@@ -452,11 +445,24 @@ public void doSetSendReceivedMessage(String parameterString) {
452445
}
453446
}
454447

448+
private ASAPStorage getStorage(String storageName) throws ASAPException {
449+
ASAPStorage asapStorage = this.storages.get(storageName);
450+
if(asapStorage == null) throw new ASAPException("no storage with name: " + storageName);
451+
452+
return asapStorage;
453+
}
454+
455455
private boolean parseOnOffValue(String onOff) throws ASAPException {
456456
if(onOff.equalsIgnoreCase("on")) return true;
457457
if(onOff.equalsIgnoreCase("off")) return false;
458458

459459
throw new ASAPException("unexpected value; expected on or off, found: " + onOff);
460460

461461
}
462+
463+
public String getEngineRootFolderByStorageName(String storageName) throws ASAPException {
464+
465+
ASAPEngineFS asapEngineFS = (ASAPEngineFS) this.getStorage(storageName);
466+
return asapEngineFS.getRootFolder();
467+
}
462468
}

test/net/sharksystem/asap/MultihopTests.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
2929
ui.doSetSendReceivedMessage("Clara:twoHops on");
3030

3131
// add message to alice storage
32-
String messageAlice2Clara = "Alice twoHops abcChat HiClara";
33-
ui.doCreateASAPMessage(messageAlice2Clara);
32+
String messageAlice2Clara = "HiClara";
33+
String parameters = "Alice twoHops abcChat " + messageAlice2Clara;
34+
ui.doCreateASAPMessage(parameters);
3435

3536
System.out.println("**************************************************************************");
3637
System.out.println("** connect Alice with Bob **");
@@ -44,12 +45,16 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
4445

4546
ui.doConnect("7070 Bob");
4647

48+
// alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
49+
4750
// wait a moment
4851
Thread.sleep(1000);
4952

5053
// kill connections
5154
ui.doKill("all");
5255

56+
// alice should stay in era 1 (no content change), bob should be in era 1 received something
57+
5358
// wait a moment
5459
Thread.sleep(1000);
5560

@@ -62,16 +67,27 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
6267
Thread.sleep(10);
6368
ui.doConnect("8080 Bob");
6469

70+
// bob should remain in era 1 o changes, clara is era 0
71+
6572
// wait a moment
6673
Thread.sleep(1000);
6774
// kill connections
6875
ui.doKill("all");
6976

7077
// get Clara storage
71-
ASAPStorage clara = ui.getStorage("Clara:twoHops");
78+
String rootFolder = ui.getEngineRootFolderByStorageName("Clara:twoHops");
79+
ASAPStorage clara = ASAPEngineFS.getExistingASAPEngineFS(rootFolder);
80+
81+
/* that asap message is from Bob even if it was created by Alice .. !!
82+
apps on top of asap could and should deal differently with ownership of messages.
83+
*/
7284
ASAPChunkStorage claraBob = clara.getIncomingChunkStorage("Bob");
73-
ASAPChunk claraABCChat = claraBob.getChunk("abcChat", clara.getEra());
85+
86+
// clara era was increased after connection terminated - message from bob is in era before current one
87+
int eraToLook = ASAPEngine.previousEra(clara.getEra());
88+
ASAPChunk claraABCChat = claraBob.getChunk("abcChat", eraToLook);
7489
CharSequence message = claraABCChat.getMessages().next();
75-
Assert.assertTrue(message.toString().equalsIgnoreCase(messageAlice2Clara));
90+
boolean same = messageAlice2Clara.equalsIgnoreCase(message.toString());
91+
Assert.assertTrue(same);
7692
}
7793
}

0 commit comments

Comments
 (0)