Skip to content

Commit f823a0c

Browse files
committed
start working on an asap management protocol
1 parent bdbf920 commit f823a0c

File tree

3 files changed

+159
-7
lines changed

3 files changed

+159
-7
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
276276
try {
277277
// read URI
278278
String uri = asapAssimiliationPDU.getChannel();
279+
if(!senderStorage.existsChunk(uri, eraSender) && asapAssimiliationPDU.recipientPeerSet()) {
280+
//<<<<<<<<<<<<<<<<<<debug
281+
b = new StringBuilder();
282+
b.append(this.getLogStart());
283+
b.append("recipient ( ");
284+
b.append(asapAssimiliationPDU.getRecipientPeer());
285+
b.append(") but no chunk on my side - should run asap management before (TODO):");
286+
System.out.println(b.toString());
287+
//>>>>>>>>>>>>>>>>>>>debug
288+
}
289+
279290
ASAPChunk chunk = senderStorage.getChunk(uri, eraSender);
280291

281292
if(chunk != null) {
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
11
package net.sharksystem.asap.protocol;
22

33
public interface ASAP_PDU_1_0 {
4+
/**
5+
* @return ASAP command. Version 1 know three alternatives: interest, offer, assimilate
6+
*/
7+
byte getCommand();
8+
9+
/**
10+
* @return ID of sender of this message
11+
*/
412
String getPeer();
13+
14+
/**
15+
* @return format A URI style is strongly recommended.
16+
* In most cases a format will match with an application that deals with one (or more
17+
* formats)
18+
*
19+
*/
520
String getFormat();
21+
22+
/**
23+
* @return A URI representing content (semantics) of this message.
24+
* It is assumed that multiple messages with same uri are floating around the
25+
* ASAP network
26+
*/
627
String getChannel();
28+
29+
/**
30+
* @return Sender can send era in which this message was created. Can be helpful
31+
* in multihop networks to reduce communication costs.
32+
*/
733
int getEra();
834

35+
/**
36+
* @return a flag that indicates whether the optional peer parameter was transmitted
37+
*/
938
boolean peerSet();
39+
/**
40+
* @return a flag that indicates whether the optional channel parameter was transmitted
41+
*/
1042
boolean channelSet();
43+
/**
44+
* @return a flag that indicates whether the optional era parameter was transmitted
45+
*/
1146
boolean eraSet();
12-
13-
byte getCommand();
1447
}

test/net/sharksystem/asap/MultihopTests.java

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.Test;
66

77
import java.io.IOException;
8+
import java.util.List;
89

910
public class MultihopTests {
1011
/**
@@ -92,7 +93,83 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
9293
Assert.assertTrue(same);
9394
}
9495

95-
@Test
96+
//@Test
97+
public void createNonOpenStorage() throws IOException, ASAPException, InterruptedException {
98+
CmdLineUI ui = new CmdLineUI(System.out);
99+
100+
ui.doResetASAPStorages();
101+
102+
// Alice creates storage with three recipients
103+
ui.doCreateASAPStorage("Alice nonOpen");
104+
ui.doCreateASAPStorage("Bob nonOpen");
105+
ui.doCreateASAPStorage("Clara nonOpen");
106+
107+
ui.doAddRecipient("Alice:nonOpen Bob");
108+
ui.doAddRecipient("Alice:nonOpen Clara");
109+
ui.doAddRecipient("Alice:nonOpen David");
110+
111+
// message shall reach Bob and Clara but not David
112+
ui.doAddRecipient("Bob:nonOpen Clara");
113+
114+
ui.doSetSendReceivedMessage("Alice:nonOpen on");
115+
ui.doSetSendReceivedMessage("Bob:nonOpen on");
116+
117+
// add message to alice storage
118+
String messageAlice2Clara = "HiClara";
119+
String parameters = "Alice nonOpen abcChat " + messageAlice2Clara;
120+
ui.doCreateASAPMessage(parameters);
121+
122+
// message is no in Alice storage
123+
124+
System.out.println("**************************************************************************");
125+
System.out.println("** connect Alice with Bob **");
126+
System.out.println("**************************************************************************");
127+
// connect alice with bob
128+
ui.doCreateASAPMultiEngine("Alice");
129+
ui.doOpen("7070 Alice");
130+
// wait a moment to give server socket time to be created
131+
Thread.sleep(10);
132+
ui.doCreateASAPMultiEngine("Bob");
133+
134+
ui.doConnect("7070 Bob");
135+
136+
// alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
137+
138+
// wait a moment
139+
Thread.sleep(1000);
140+
141+
// kill connections
142+
ui.doKill("all");
143+
144+
// alice should stay in era 1 (no content change), bob should be in era 1 received something
145+
146+
// wait a moment
147+
Thread.sleep(1000);
148+
149+
// Bob should now have created an closed asap storage with three recipients
150+
ASAPStorage bobStorage = this.getFreshStorageByName(ui, "Bob:nonOpen");
151+
152+
CharSequence uri = bobStorage.getChannelURIs().get(0);
153+
List<CharSequence> recipientsList = bobStorage.getRecipients("nonOpen");
154+
boolean aliceFound = false;
155+
boolean bobFound = false;
156+
boolean claraFound = false;
157+
boolean davidFound = false;
158+
for(CharSequence recipient : recipientsList) {
159+
String recipientString = recipient.toString();
160+
switch(recipient.toString()) {
161+
case "Alice": aliceFound = true; break;
162+
case "Bob": bobFound = true; break;
163+
case "Clara": claraFound = true; break;
164+
case "David": davidFound = true; break;
165+
default: Assert.fail("found unexpected recipient: " + recipient);
166+
}
167+
}
168+
169+
Assert.assertTrue(aliceFound && bobFound && claraFound && davidFound);
170+
}
171+
172+
//@Test
96173
public void twoHopsNonOpenStorage() throws IOException, ASAPException, InterruptedException {
97174
CmdLineUI ui = new CmdLineUI(System.out);
98175

@@ -102,15 +179,17 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
102179
ui.doCreateASAPStorage("Alice twoHops");
103180
ui.doCreateASAPStorage("Bob twoHops");
104181
ui.doCreateASAPStorage("Clara twoHops");
182+
ui.doCreateASAPStorage("David twoHops");
105183

106184
ui.doAddRecipient("Alice:twoHops Bob");
107185
ui.doAddRecipient("Alice:twoHops Clara");
186+
ui.doAddRecipient("Alice:twoHops David");
108187

109-
Assert.fail("test case implementation not yet finished");
188+
// message shall reach Bob and Clara but not David
189+
ui.doAddRecipient("Bob:twoHops Clara");
110190

111191
ui.doSetSendReceivedMessage("Alice:twoHops on");
112192
ui.doSetSendReceivedMessage("Bob:twoHops on");
113-
ui.doSetSendReceivedMessage("Clara:twoHops on");
114193

115194
// add message to alice storage
116195
String messageAlice2Clara = "HiClara";
@@ -142,6 +221,31 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
142221
// wait a moment
143222
Thread.sleep(1000);
144223

224+
System.out.println("**************************************************************************");
225+
System.out.println("** connect Bob with David **");
226+
System.out.println("**************************************************************************");
227+
// connect alice with bob
228+
ui.doCreateASAPMultiEngine("Bob");
229+
ui.doOpen("7070 Bob");
230+
// wait a moment to give server socket time to be created
231+
Thread.sleep(10);
232+
ui.doCreateASAPMultiEngine("David");
233+
234+
ui.doConnect("7070 David");
235+
236+
// alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
237+
238+
// wait a moment
239+
Thread.sleep(1000);
240+
241+
// kill connections
242+
ui.doKill("all");
243+
244+
// alice should stay in era 1 (no content change), bob should be in era 1 received something
245+
246+
// wait a moment
247+
Thread.sleep(1000);
248+
145249
System.out.println("**************************************************************************");
146250
System.out.println("** connect Bob with Clara **");
147251
System.out.println("**************************************************************************");
@@ -159,8 +263,7 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
159263
ui.doKill("all");
160264

161265
// get Clara storage
162-
String rootFolder = ui.getEngineRootFolderByStorageName("Clara:twoHops");
163-
ASAPStorage clara = ASAPEngineFS.getExistingASAPEngineFS(rootFolder);
266+
ASAPStorage clara = this.getFreshStorageByName(ui, "Clara:twoHops");
164267

165268
/* message was actually from Bob but originated from Alice. It is put
166269
into a incoming folder as it would have been directly received from Alice.
@@ -175,4 +278,9 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
175278
boolean same = messageAlice2Clara.equalsIgnoreCase(message.toString());
176279
Assert.assertTrue(same);
177280
}
281+
282+
private ASAPStorage getFreshStorageByName(CmdLineUI ui, String storageName) throws ASAPException, IOException {
283+
String rootFolder = ui.getEngineRootFolderByStorageName(storageName);
284+
return ASAPEngineFS.getExistingASAPEngineFS(rootFolder);
285+
}
178286
}

0 commit comments

Comments
 (0)