Skip to content

Commit f2c23d7

Browse files
committed
working on asap management message hopping
1 parent 0995b7a commit f2c23d7

File tree

9 files changed

+90
-26
lines changed

9 files changed

+90
-26
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,15 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
376376

377377
try {
378378
// read URI
379-
String uri = asapAssimiliationPDU.getChannel();
379+
String uri = asapAssimiliationPDU.getChannelUri();
380380
if(!senderStorage.existsChunk(uri, eraSender) && asapAssimiliationPDU.recipientPeerSet()) {
381381
//<<<<<<<<<<<<<<<<<<debug
382382
b = new StringBuilder();
383383
b.append(this.getLogStart());
384-
b.append("recipient ( ");
384+
b.append("recipient (");
385385
b.append(asapAssimiliationPDU.getRecipientPeer());
386-
b.append(") but no chunk on my side - should run asap management before (TODO):");
386+
b.append(") but no chunk on my side - should run asap management before?:");
387+
b.append(asapAssimiliationPDU.toString());
387388
System.out.println(b.toString());
388389
//>>>>>>>>>>>>>>>>>>>debug
389390
}

src/net/sharksystem/asap/ASAPEngineFS.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ public static void removeFolder(String eraPathName) {
221221
ASAPEngineFS.removeFolder(fileInDir.getAbsolutePath());
222222
} else {
223223
try {
224-
fileInDir.delete();
224+
if(!fileInDir.delete()) {
225+
System.out.println("ASAPEngineFS: cannot delete file (try deleteOnExit):" + fileInDir);
226+
}
225227
} catch (RuntimeException e) {
226228
System.err.println("ASAPEngineFS: cannot file:" + e.getLocalizedMessage());
227229
// try next

src/net/sharksystem/asap/MultiASAPEngineFS_Impl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,19 @@ public void pushInterests(OutputStream os) throws IOException, ASAPException {
391391
} else {
392392
System.out.println(this.getLogStart() + "no more apps/formats on that engine - no interests to be sent");
393393
}
394+
395+
// management messages must be sent first - if any
396+
try {
397+
ASAPEngine managementEngine = this.getEngineByFormat(ASAP_1_0.ASAP_MANAGEMENT_FORMAT);
398+
System.out.println(this.getLogStart() + "send interest for app/format: " + ASAP_1_0.ASAP_MANAGEMENT_FORMAT);
399+
protocol.interest(this.owner, null, ASAP_1_0.ASAP_MANAGEMENT_FORMAT,null, -1, -1, os, false);
400+
}
401+
catch(Exception e) {
402+
// ignore - engine does not exist
403+
}
394404
// issue an interest for each owner / format combination
395405
for(CharSequence format : this.folderMap.keySet()) {
396-
// if(format.toString().equalsIgnoreCase(ASAP_1_0.ASAP_MANAGEMENT_FORMAT)) continue; // already send
406+
if(format.toString().equalsIgnoreCase(ASAP_1_0.ASAP_MANAGEMENT_FORMAT)) continue; // already sent
397407
System.out.println(this.getLogStart() + "send interest for app/format: " + format);
398408
protocol.interest(this.owner, null, format,null, -1, -1, os, false);
399409
}

src/net/sharksystem/asap/management/ASAPManagementMessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,6 @@ private void handleASAPManagementMessage(byte[] message) throws ASAPException, I
119119
}
120120

121121
private String getLogStart() {
122-
return this.getClass().getSimpleName() + /*"(" + this.getOwner() + ")*/ ": ";
122+
return this.getClass().getSimpleName() + "(" + this.multiASAPEngine.getOwner() + "): ";
123123
}
124124
}

src/net/sharksystem/asap/protocol/ASAP_PDU_1_0.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface ASAP_PDU_1_0 {
2424
* It is assumed that multiple messages with same uri are floating around the
2525
* ASAP network
2626
*/
27-
String getChannel();
27+
String getChannelUri();
2828

2929
/**
3030
* @return Sender can send era in which this message was created. Can be helpful

src/net/sharksystem/asap/protocol/PDU_Impl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected void evaluateFlags(int flag) {
136136
public String getFormat() { return this.format; }
137137

138138
@Override
139-
public String getChannel() { return this.channel; }
139+
public String getChannelUri() { return this.channel; }
140140

141141
@Override
142142
public int getEra() { return this.era;}

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,17 +515,16 @@ public void doPrintChannelInformation(String parameterString) {
515515
return;
516516
}
517517

518-
Set<CharSequence> recipients = asapStorage.getRecipients(uri);
518+
ASAPChannel channel = asapStorage.getChannel(uri);
519+
Set<CharSequence> recipients = channel.getRecipients();
519520

520-
System.out.println("Owner:App:Channel == " + owner + ":" + appName + ":" + uri);
521+
System.out.println("Owner:App:Channel == " + channel.getOwner() + ":" + appName + ":" + channel.getUri());
521522
System.out.println("#Recipients == " + recipients.size());
522523
for(CharSequence recipient : recipients) {
523524
System.out.println(recipient);
524525
}
525526
}
526-
catch(RuntimeException e) {
527-
this.printUsage(CREATE_ASAP_MESSAGE, e.getLocalizedMessage());
528-
} catch (IOException e) {
527+
catch(RuntimeException | ASAPException | IOException e) {
529528
this.printUsage(CREATE_ASAP_MESSAGE, e.getLocalizedMessage());
530529
}
531530
}

test/net/sharksystem/asap/MultihopTests.java

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
3434
String parameters = "Alice twoHops sn2://abc " + messageAlice2Clara;
3535
ui.doCreateASAPMessage(parameters);
3636

37+
// remember Alice' era
38+
ASAPStorage aliceStorage = this.getFreshStorageByName(ui, "Alice:twoHops");
39+
int aliceEraWhenIssuedMessage = aliceStorage.getEra();
40+
3741
System.out.println("**************************************************************************");
3842
System.out.println("** connect Alice with Bob **");
3943
System.out.println("**************************************************************************");
@@ -74,21 +78,22 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
7478
Thread.sleep(1000);
7579
// kill connections
7680
ui.doKill("all");
81+
// wait a moment
82+
Thread.sleep(1000);
7783

7884
// get Clara storage
79-
String rootFolder = ui.getEngineRootFolderByStorageName("Clara:twoHops");
80-
ASAPStorage clara = ASAPEngineFS.getExistingASAPEngineFS(rootFolder);
85+
ASAPStorage clara = this.getFreshStorageByName(ui, "Clara:twoHops");
8186

8287
/* message was actually from Bob but originated from Alice. It is put
8388
into a incoming folder as it would have been directly received from Alice.
8489
Signatures would allow ensuring if origin was really who mediator claims to be.
8590
*/
86-
ASAPChunkStorage claraBob = clara.getIncomingChunkStorage("Alice");
91+
ASAPChunkStorage claraAlice = clara.getIncomingChunkStorage("Alice");
8792

8893
// clara era was increased after connection terminated - message from bob is in era before current one
8994
// int eraToLook = ASAPEngine.previousEra(clara.getEra());
9095
int eraToLook = clara.getEra();
91-
ASAPChunk claraABCChat = claraBob.getChunk("sn2://abc", eraToLook);
96+
ASAPChunk claraABCChat = claraAlice.getChunk("sn2://abc", aliceEraWhenIssuedMessage);
9297
CharSequence message = claraABCChat.getMessages().next();
9398
boolean same = messageAlice2Clara.equalsIgnoreCase(message.toString());
9499
Assert.assertTrue(same);
@@ -153,21 +158,68 @@ public void createNonOpenStorage() throws IOException, ASAPException, Interrupte
153158
}
154159
}
155160

161+
// closed channel created with all recipients and owner?
156162
Assert.assertTrue(aliceFound && bobFound && claraFound);
157163
Assert.assertTrue(bobClosedChannel.getOwner().toString().equalsIgnoreCase("Alice"));
158164

159-
// check for a message
160-
/* message was actually from Bob but originated from Alice. It is put
161-
into a incoming folder as it would have been directly received from Alice.
162-
Signatures would allow ensuring if origin was really who mediator claims to be.
163-
*/
165+
// message received?
164166
ASAPChunkStorage bobAlice = bobStorage.getIncomingChunkStorage("Alice");
165-
166167
// clara era was increased after connection terminated - message from bob is in era before current one
167168
int eraToLook = ASAPEngine.previousEra(bobStorage.getEra());
168169
ASAPChunk bobABCChat = bobAlice.getChunk("sn2://closedChannel", eraToLook);
169170
CharSequence message = bobABCChat.getMessages().next();
170171
Assert.assertTrue(messageAlice2Clara.equalsIgnoreCase(message.toString()));
172+
173+
System.out.println("**************************************************************************");
174+
System.out.println("** connect Bob with Clara **");
175+
System.out.println("**************************************************************************");
176+
// connect alice with bob
177+
ui.doCreateASAPMultiEngine("Bob");
178+
ui.doOpen("7071 Bob");
179+
// wait a moment to give server socket time to be created
180+
Thread.sleep(10);
181+
ui.doCreateASAPMultiEngine("Clara");
182+
ui.doConnect("7071 Clara");
183+
184+
// alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
185+
// wait a moment
186+
Thread.sleep(1000);
187+
// kill connections
188+
ui.doKill("all");
189+
// alice should stay in era 1 (no content change), bob should be in era 1 received something
190+
// wait a moment
191+
Thread.sleep(1000);
192+
// Bob should now have created an closed asap storage with three recipients
193+
ASAPStorage claraStorage = this.getFreshStorageByName(ui, "Clara:chat");
194+
195+
ui.doPrintChannelInformation("Clara chat sn2://closedChannel");
196+
197+
ASAPChannel claraClosedChannel = bobStorage.getChannel("sn2://closedChannel");
198+
recipientsList = bobClosedChannel.getRecipients();
199+
aliceFound = false;
200+
bobFound = false;
201+
claraFound = false;
202+
for(CharSequence recipient : recipientsList) {
203+
String recipientString = recipient.toString();
204+
switch(recipient.toString()) {
205+
case "Alice": aliceFound = true; break;
206+
case "Bob": bobFound = true; break;
207+
case "Clara": claraFound = true; break;
208+
default: Assert.fail("found unexpected recipient: " + recipient);
209+
}
210+
}
211+
212+
// closed channel created with all recipients and owner?
213+
Assert.assertTrue(aliceFound && bobFound && claraFound);
214+
Assert.assertTrue(bobClosedChannel.getOwner().toString().equalsIgnoreCase("Alice"));
215+
216+
// message received?
217+
ASAPChunkStorage claraAlice = claraStorage.getIncomingChunkStorage("Alice");
218+
// clara era was increased after connection terminated - message from bob is in era before current one
219+
eraToLook = ASAPEngine.previousEra(bobStorage.getEra());
220+
ASAPChunk claraABCChat = claraAlice.getChunk("sn2://closedChannel", eraToLook);
221+
message = claraABCChat.getMessages().next();
222+
Assert.assertTrue(messageAlice2Clara.equalsIgnoreCase(message.toString()));
171223
}
172224

173225
private ASAPStorage getFreshStorageByName(CmdLineUI ui, String storageName) throws ASAPException, IOException {

test/net/sharksystem/asap/protocol/PDUTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void offer(CharSequence peer, CharSequence format, CharSequence channel, int era
3535

3636
ASAP_OfferPDU_1_0 offerPDU = (ASAP_OfferPDU_1_0) asap_pdu_1_0;
3737

38-
Assert.assertTrue(offerPDU.getChannel().equalsIgnoreCase(channel));
38+
Assert.assertTrue(offerPDU.getChannelUri().equalsIgnoreCase(channel));
3939
Assert.assertTrue(offerPDU.getFormat().equalsIgnoreCase(format));
4040
Assert.assertTrue(offerPDU.getPeer().equalsIgnoreCase(peer));
4141
Assert.assertEquals(offerPDU.getEra(), era);
@@ -69,7 +69,7 @@ void interest(CharSequence peer, CharSequence sourcePeer, CharSequence format,
6969

7070
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
7171

72-
Assert.assertTrue(interestPDU.getChannel().equalsIgnoreCase(channel));
72+
Assert.assertTrue(interestPDU.getChannelUri().equalsIgnoreCase(channel));
7373
Assert.assertTrue(interestPDU.getFormat().equalsIgnoreCase(format));
7474
Assert.assertTrue(interestPDU.getPeer().equalsIgnoreCase(peer));
7575
Assert.assertTrue(interestPDU.getSourcePeer().equalsIgnoreCase(sourcePeer));
@@ -114,7 +114,7 @@ public void sendAndReceiveAssimilate() throws IOException, ASAPException {
114114

115115
ASAP_AssimilationPDU_1_0 assimilationPDU = (ASAP_AssimilationPDU_1_0) asap_pdu_1_0;
116116

117-
Assert.assertTrue(assimilationPDU.getChannel().equalsIgnoreCase(channel));
117+
Assert.assertTrue(assimilationPDU.getChannelUri().equalsIgnoreCase(channel));
118118
Assert.assertTrue(assimilationPDU.getFormat().equalsIgnoreCase(format));
119119
Assert.assertTrue(assimilationPDU.getPeer().equalsIgnoreCase(peer));
120120
Assert.assertTrue(assimilationPDU.getRecipientPeer().equalsIgnoreCase(recipientPeer));

0 commit comments

Comments
 (0)