Skip to content

Commit 3e0e0a0

Browse files
committed
transient message sending introduced.
1 parent 7d145fd commit 3e0e0a0

File tree

15 files changed

+193
-117
lines changed

15 files changed

+193
-117
lines changed

src/main/java/net/sharksystem/asap/ASAP.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import java.util.Random;
44

55
public class ASAP {
6-
public static final int TRANSIENT_ERA = -1;
6+
public static final int TRANSIENT_ERA = Integer.MAX_VALUE;
77
public static final int INITIAL_ERA = 0;
88
public static final int MIN_ERA = INITIAL_ERA;
9-
public static final int MAX_ERA = Integer.MAX_VALUE;
9+
public static final int MAX_ERA = Integer.MAX_VALUE-1;
1010

1111
public static int nextEra(int workingEra) {
1212
if(workingEra == ASAP.MAX_ERA) {

src/main/java/net/sharksystem/asap/ASAPMessageSender.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public interface ASAPMessageSender {
1313
void sendASAPMessage(CharSequence appName, CharSequence uri,
1414
byte[] message) throws ASAPException;
1515

16+
/**
17+
* When calling this methode, this asap message is sent over any existing connection.
18+
* It is not stored on sender or receiver side. Message listeners are called as usual. Nothing happens (no
19+
* exception is thrown) if there is not a single peer encounter running.
20+
* @param appName
21+
* @param uri
22+
* @param message
23+
* @throws ASAPException
24+
* @throws IOException
25+
*/
1626
void sendTransientASAPMessage(CharSequence appName, CharSequence uri, byte[] message)
1727
throws ASAPException, IOException;
1828
}

src/main/java/net/sharksystem/asap/ASAPPeerFS.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public void chunkReceived(String format, String senderE2E, String uri, int era,
4646

4747
sb = new StringBuilder();
4848
sb.append("\n++++++++++++++++++++++++++++++++++++++++++ chunkReceived +++++++++++++++++++++++++++++++++++++++++++\n");
49-
sb.append("E2E|P2P: " + senderE2E + " | " + asapHopList.get(asapHopList.size()-1).sender() + " | uri: " + uri + " | era: " + era + " | appFormat: " + format);
49+
sb.append("E2E|P2P: " + senderE2E + " | " + asapHopList.get(asapHopList.size()-1).sender() + " | uri: " + uri);
50+
sb.append(" | era: ");
51+
if(era == ASAP.TRANSIENT_ERA) sb.append("transient");
52+
else sb.append(era);
53+
sb.append(" | appFormat: " + format);
5054
sb.append("\n");
5155
sb.append(hopListString);
5256
sb.append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

src/main/java/net/sharksystem/asap/engine/ASAPEngine.java

Lines changed: 101 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public void setSecurityAdministrator(DefaultSecurityAdministrator securityAdmini
3737

3838
protected String owner = ANONYMOUS_OWNER;
3939

40-
protected int era = 0;
41-
protected int oldestEra = 0;
40+
protected int era = ASAP.INITIAL_ERA;
41+
protected int oldestEra = ASAP.INITIAL_ERA;
4242
protected HashMap<String, Integer> lastSeen = new HashMap<>();
4343
protected ASAPMemento memento = null;
4444
public long lastMementoWritten;
@@ -403,135 +403,138 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimilationPDU, A
403403
b.append("handle assimilate pdu received from ");
404404
b.append(senderE2E);
405405
b.append(" | era: ");
406-
b.append(eraSender);
406+
if(eraSender == ASAP.TRANSIENT_ERA) b.append("transient");
407+
else b.append(eraSender);
407408
Log.writeLog(this, this.toString(), b.toString());
408409
//>>>>>>>>>>>>>>>>>>>debug
409410

410411
// get received storage
411412
ASAPInternalStorage incomingStorage = (ASAPInternalStorage) this.getIncomingStorage(senderE2E, true);
412413
ASAPChunkStorage incomingChunkStorage = incomingStorage.getChunkStorage();
413-
//ASAPChunkStorage incomingChunkStorage = this.getReceivedChunksStorage(senderE2E);
414414
Log.writeLog(this, this.toString(), "got incoming chunk storage for senderE2E: " + senderE2E);
415415

416416
boolean changed = false;
417417
boolean allowedAssimilation = true;
418418

419-
try {
420-
// read URI
421-
String uri = asapAssimilationPDU.getChannelUri();
419+
// add entry to hop list
420+
List<ASAPHop> asapHopList = asapAssimilationPDU.getASAPHopList();
421+
Log.writeLog(this, this.toString(), "got hop list: " + asapHopList);
422422

423-
// get local target for data to come
424-
ASAPInternalChunk localChunk = null;
423+
// add this new hop
424+
ASAPHop lastHop = new ASAPHopImpl(encounteredPeer, asapAssimilationPDU.verified(),
425+
asapAssimilationPDU.encrypted(), connectionType);
425426

426-
if(!incomingChunkStorage.existsChunk(uri, eraSender)) {
427-
// is there a local chunk - to clone recipients from?
428-
if(this.channelExists(uri)) {
429-
localChunk = this.getStorage().getChunk(uri, this.getEra());
430-
} else {
431-
Log.writeLog(this, this.toString(), "asked to set up new channel: (uri/senderE2E): "
432-
+ uri + " | " + senderE2E);
433-
// this channel is new to local peer - am I allowed to create it?
434-
if(!this.securityAdministrator.allowedToCreateChannel(asapAssimilationPDU)) {
435-
Log.writeLog(this, this.toString(),
436-
".. not allowed .. TODO not yet implemented .. always set up");
427+
asapHopList.add(lastHop);
428+
429+
// get URI
430+
String uri = asapAssimilationPDU.getChannelUri();
431+
432+
if(eraSender != ASAP.TRANSIENT_ERA) {
433+
try {
434+
// get local target for data to come
435+
ASAPInternalChunk localChunk = null;
437436

438-
allowedAssimilation = false; // TODO
437+
if (!incomingChunkStorage.existsChunk(uri, eraSender)) {
438+
// is there a local chunk - to clone recipients from?
439+
if (this.channelExists(uri)) {
440+
localChunk = this.getStorage().getChunk(uri, this.getEra());
439441
} else {
440-
Log.writeLog(this, this.toString(), "allowed. Set it up.");
441-
this.createChannel(uri);
442+
Log.writeLog(this, this.toString(), "asked to set up new channel: (uri/senderE2E): "
443+
+ uri + " | " + senderE2E);
444+
// this channel is new to local peer - am I allowed to create it?
445+
if (!this.securityAdministrator.allowedToCreateChannel(asapAssimilationPDU)) {
446+
Log.writeLog(this, this.toString(),
447+
".. not allowed .. TODO not yet implemented .. always set up");
448+
449+
allowedAssimilation = false; // TODO
450+
} else {
451+
Log.writeLog(this, this.toString(), "allowed. Set it up.");
452+
this.createChannel(uri);
453+
}
442454
}
443-
}
444-
} else {
445-
Log.writeLog(this, this.toString(), "received chunk that already exists - did nothing: "
446-
+ senderE2E + " | " + eraSender + " | " + uri);
455+
} else {
456+
Log.writeLog(this, this.toString(), "received chunk that already exists - did nothing: "
457+
+ senderE2E + " | " + eraSender + " | " + uri);
447458

448-
// read assimilation message payload to oblivion!
449-
asapAssimilationPDU.takeDataFromStream();
450-
return;
451-
}
459+
// read assimilation message payload to oblivion!
460+
asapAssimilationPDU.takeDataFromStream();
461+
return;
462+
}
452463

453-
ASAPInternalChunk incomingChunk = incomingStorage.createNewChunk(uri, eraSender);
454-
//ASAPInternalChunk incomingChunk = incomingChunkStorage.getChunk(uri, eraSender);
464+
ASAPInternalChunk incomingChunk = incomingStorage.createNewChunk(uri, eraSender);
465+
//ASAPInternalChunk incomingChunk = incomingChunkStorage.getChunk(uri, eraSender);
455466

456-
if(localChunk != null) {
457-
Log.writeLog(this, this.toString(), "copy local meta data into newly created incoming chunk");
458-
incomingChunk.copyMetaData(this.getChannel(uri));
459-
}
467+
if (localChunk != null) {
468+
Log.writeLog(this, this.toString(), "copy local meta data into newly created incoming chunk");
469+
incomingChunk.copyMetaData(this.getChannel(uri));
470+
}
460471

461-
List<Integer> messageOffsets = asapAssimilationPDU.getMessageOffsets();
472+
List<Integer> messageOffsets = asapAssimilationPDU.getMessageOffsets();
473+
474+
// iterate messages and stream into chunk
475+
InputStream protocolInputStream = asapAssimilationPDU.getInputStream();
476+
long offset = 0;
477+
for (long nextOffset : messageOffsets) {
478+
//<<<<<<<<<<<<<<<<<<debug
479+
b = new StringBuilder();
480+
b.append("going to read message: [");
481+
b.append(offset);
482+
b.append(", ");
483+
b.append(nextOffset);
484+
b.append(")");
485+
Log.writeLog(this, this.toString(), b.toString());
486+
//>>>>>>>>>>>>>>>>>>>debug
487+
incomingChunk.addMessage(protocolInputStream, nextOffset - offset);
488+
//if(!changed) { changed = true; this.contentChanged();}
489+
offset = nextOffset;
490+
}
462491

463-
// iterate messages and stream into chunk
464-
InputStream protocolInputStream = asapAssimilationPDU.getInputStream();
465-
long offset = 0;
466-
for(long nextOffset : messageOffsets) {
492+
// last round
467493
//<<<<<<<<<<<<<<<<<<debug
468494
b = new StringBuilder();
469-
b.append("going to read message: [");
495+
b.append("going to read last message: from offset ");
470496
b.append(offset);
471-
b.append(", ");
472-
b.append(nextOffset);
473-
b.append(")");
497+
b.append(" to end of file - total length: ");
498+
b.append(asapAssimilationPDU.getLength());
474499
Log.writeLog(this, this.toString(), b.toString());
475500
//>>>>>>>>>>>>>>>>>>>debug
476-
incomingChunk.addMessage(protocolInputStream, nextOffset - offset);
477-
//if(!changed) { changed = true; this.contentChanged();}
478-
offset = nextOffset;
501+
502+
incomingChunk.addMessage(protocolInputStream, asapAssimilationPDU.getLength() - offset);
503+
504+
// add hop list to newly create chunk
505+
incomingChunk.setASAPHopList(asapHopList);
506+
} catch (IOException | ASAPException e) {
507+
Log.writeLogErr(this, this.toString(),
508+
"exception (give up, keep streams untouched): " + e.getLocalizedMessage());
509+
throw e;
479510
}
511+
}
512+
513+
/////// notify listeners - if any
480514

481-
// last round
515+
// read all messages
516+
if(listener != null) {
482517
//<<<<<<<<<<<<<<<<<<debug
483518
b = new StringBuilder();
484-
b.append("going to read last message: from offset ");
485-
b.append(offset);
486-
b.append(" to end of file - total length: ");
487-
b.append(asapAssimilationPDU.getLength());
519+
b.append("call ");
520+
b.append(listener.getClass().getSimpleName());
521+
b.append(".chunkReceived(senderE2E: ");
522+
b.append(senderE2E);
523+
b.append(", uri: ");
524+
b.append(uri);
525+
b.append(", eraSender: ");
526+
if(eraSender != ASAP.TRANSIENT_ERA) b.append(eraSender);
527+
else b.append("transient");
528+
b.append(")");
488529
Log.writeLog(this, this.toString(), b.toString());
489530
//>>>>>>>>>>>>>>>>>>>debug
490531

491-
incomingChunk.addMessage(protocolInputStream, asapAssimilationPDU.getLength() - offset);
492-
// receiving has no effect on era.
493-
//if(!changed) { changed = true; this.contentChanged();}
494-
495-
// add hop list.
496-
List<ASAPHop> asapHopList = asapAssimilationPDU.getASAPHopList();
497-
Log.writeLog(this, this.toString(), "got hop list: " + asapHopList);
498-
499-
// add this new hop
500-
ASAPHop lastHop = new ASAPHopImpl(encounteredPeer, asapAssimilationPDU.verified(),
501-
asapAssimilationPDU.encrypted(), connectionType);
502-
503-
asapHopList.add(lastHop);
504-
505-
incomingChunk.setASAPHopList(asapHopList);
506-
507-
// read all messages
508-
if(listener != null) {
509-
//<<<<<<<<<<<<<<<<<<debug
510-
b = new StringBuilder();
511-
b.append("call ");
512-
b.append(listener.getClass().getSimpleName());
513-
b.append(".chunkReceived(senderE2E: ");
514-
b.append(senderE2E);
515-
b.append(", uri: ");
516-
b.append(uri);
517-
b.append(", eraSender: ");
518-
b.append(eraSender);
519-
b.append(")");
520-
Log.writeLog(this, this.toString(), b.toString());
521-
//>>>>>>>>>>>>>>>>>>>debug
522-
523-
listener.chunkReceived(this.format,
524-
senderE2E, uri, eraSender,
525-
asapHopList
526-
);
527-
} else {
528-
Log.writeLog(this, this.toString(), "no chunk received listener found");
529-
}
530-
}
531-
catch (IOException | ASAPException e) {
532-
Log.writeLogErr(this, this.toString(),
533-
"exception (give up, keep streams untouched): " + e.getLocalizedMessage());
534-
throw e;
532+
listener.chunkReceived(this.format,
533+
senderE2E, uri, eraSender,
534+
asapHopList
535+
);
536+
} else {
537+
Log.writeLog(this, this.toString(), "no chunk received listener found");
535538
}
536539
}
537540

src/main/java/net/sharksystem/asap/engine/ASAPOnlineMessageSenderEngineSide.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sharksystem.asap.engine;
22

3+
import net.sharksystem.asap.ASAP;
34
import net.sharksystem.asap.ASAPException;
45
import net.sharksystem.asap.protocol.ASAPConnection;
56
import net.sharksystem.asap.protocol.ASAPOnlineMessageSource;
@@ -66,7 +67,8 @@ public void sendASAPAssimilateMessage(CharSequence format, CharSequence uri, Set
6667
sb.append("| uri: ");
6768
sb.append(uri);
6869
sb.append("| era: ");
69-
sb.append(era);
70+
if(era != ASAP.TRANSIENT_ERA) sb.append(era);
71+
else sb.append("transient");
7072
sb.append("| #receiver: ");
7173
if(receiver != null) sb.append(receiver.size());
7274
else sb.append("null");

src/main/java/net/sharksystem/asap/protocol/ASAP_Modem_Impl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sharksystem.asap.protocol;
22

3+
import net.sharksystem.asap.ASAP;
34
import net.sharksystem.asap.ASAPException;
45
import net.sharksystem.asap.ASAPHop;
56
import net.sharksystem.asap.ASAPSecurityException;
@@ -197,7 +198,7 @@ public void assimilate(CharSequence sender, CharSequence recipient, CharSequence
197198
OutputStream os, boolean signed, boolean encrypted) throws IOException, ASAPException {
198199

199200
if(data == null || data.length == 0) throw new ASAPException("data must not be null");
200-
if(era < 0) throw new ASAPException("era must be a non-negative value: " + era);
201+
if(era < -1) throw new ASAPException("era must be a non-negative value: " + era);
201202

202203
this.assimilate(sender, recipient, format, channel, era, data.length, offsets, asapHops,
203204
new ByteArrayInputStream(data), os, signed, encrypted);

src/test/java/junit5Tests/release_1/MultipleEncounterTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import net.sharksystem.TestHelper;
55
import net.sharksystem.asap.ASAPException;
66
import net.sharksystem.asap.ASAPPeer;
7-
import net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS;
7+
import net.sharksystem.testsupport.ASAPTestPeerFS;
88
import net.sharksystem.asap.engine.ASAPEngineFS;
99
import net.sharksystem.asap.mockAndTemplates.ASAPMessageReceivedListenerExample;
1010
import net.sharksystem.asap.mockAndTemplates.TestUtils;

src/test/java/net/sharksystem/TestHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sharksystem;
22

3+
import net.sharksystem.asap.engine.ASAPEngineFS;
34
import net.sharksystem.utils.Utils;
45

56
public class TestHelper {
@@ -42,4 +43,8 @@ public static byte[] produceTestMessage() {
4243
public static boolean sameMessages(byte[] msgA, byte[] msgB) {
4344
return Utils.compareArrays(msgA, msgB);
4445
}
46+
47+
public static void removeFolder(String foldername) {
48+
ASAPEngineFS.removeFolder(foldername);
49+
}
4550
}

src/test/java/net/sharksystem/V1TestSuite.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import junit5Tests.release_1.MultipleEncounterTests;
55
import net.sharksystem.asap.engine.*;
66
import net.sharksystem.asap.helper.HelperTester;
7+
import net.sharksystem.asap.peer.TransientMessages;
78
import net.sharksystem.asap.protocol.PDUTests;
89
import net.sharksystem.asap.serialization.SerializationTests;
910
import net.sharksystem.asap.storage.StorageTests;
@@ -23,7 +24,8 @@
2324
CryptoUsage.class,
2425
HelperTester.class,
2526
MultihopTests.class,
26-
CommandlineTests.class//,
27+
CommandlineTests.class,
28+
TransientMessages.class//,
2729
//MultipleEncounterTests.class
2830
//E2EStreamPairLinkTestVersion2.class TODO
2931
})

src/test/java/net/sharksystem/asap/appTests/Reenactment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.sharksystem.asap.appTests;
22

3-
import net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS;
3+
import net.sharksystem.testsupport.ASAPTestPeerFS;
44
import net.sharksystem.asap.ASAPException;
55
import org.junit.Test;
66

0 commit comments

Comments
 (0)