Skip to content

Commit 2db07ca

Browse files
committed
going to store asap hop list with chunks and transmit with assimilate pdu.
1 parent cdb4578 commit 2db07ca

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

src/net/sharksystem/asap/engine/ASAPInternalChunkFS.java

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

33
import net.sharksystem.asap.ASAPChannel;
4+
import net.sharksystem.asap.ASAPHop;
45
import net.sharksystem.asap.utils.Helper;
56

67
import java.io.*;
@@ -22,6 +23,8 @@ public class ASAPInternalChunkFS implements ASAPInternalChunk {
2223
private List<Long> messageStartOffsets = new ArrayList<>();
2324
private File metaFile;
2425
private File messageFile;
26+
27+
private List<ASAPHop> hopList;
2528

2629
private int era;
2730

@@ -105,6 +108,7 @@ private void initFiles(String trunkName) throws IOException {
105108
this.recipients = new HashSet<>();
106109
this.deliveredTo = new ArrayList<>();
107110
this.messageStartOffsets = new ArrayList<>();
111+
this.hopList = new ArrayList<>();
108112
}
109113
}
110114

src/net/sharksystem/asap/utils/ASAPSerialization.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import net.sharksystem.utils.Log;
99

1010
import java.io.*;
11+
import java.util.ArrayList;
1112
import java.util.HashSet;
13+
import java.util.List;
1214
import java.util.Set;
1315

1416
public class ASAPSerialization {
@@ -254,29 +256,63 @@ public static void writeEncounterConnectionType(EncounterConnectionType connecti
254256

255257
public static void writeBooleanParameter(boolean value, OutputStream os) throws IOException {
256258
if(value) ASAPSerialization.writeByteParameter((byte) 1, os);
257-
ASAPSerialization.writeByteParameter((byte) 0, os);
259+
else ASAPSerialization.writeByteParameter((byte) 0, os);
258260
}
259261

260262
public static boolean readBooleanParameter(InputStream is) throws IOException, ASAPException {
261263
return ASAPSerialization.readByte(is) == 1;
262264
}
263265

266+
public static void writeASAPHop(ASAPHop asapHop, OutputStream os) throws IOException {
267+
ASAPSerialization.writeCharSequenceParameter(asapHop.sender(), os);
268+
ASAPSerialization.writeBooleanParameter(asapHop.verified(), os);
269+
ASAPSerialization.writeBooleanParameter(asapHop.encrypted(), os);
270+
ASAPSerialization.writeEncounterConnectionType(asapHop.getConnectionType(), os);
271+
}
272+
264273
public static byte[] asapHop2ByteArray(ASAPHop asapHop) throws IOException {
265274
ByteArrayOutputStream baos = new ByteArrayOutputStream();
266-
ASAPSerialization.writeCharSequenceParameter(asapHop.sender(), baos);
267-
ASAPSerialization.writeBooleanParameter(asapHop.verified(), baos);
268-
ASAPSerialization.writeBooleanParameter(asapHop.encrypted(), baos);
269-
ASAPSerialization.writeEncounterConnectionType(asapHop.getConnectionType(), baos);
275+
writeASAPHop(asapHop, baos);
270276
return baos.toByteArray();
271277
}
272278

279+
public static ASAPHop readASAPHop(InputStream is) throws IOException, ASAPException {
280+
CharSequence sender = ASAPSerialization.readCharSequenceParameter(is);
281+
boolean verified = ASAPSerialization.readBooleanParameter(is);
282+
boolean encrypted = ASAPSerialization.readBooleanParameter(is);
283+
EncounterConnectionType connectionType = ASAPSerialization.readEncounterConnectionType(is);
284+
285+
return new ASAPHopImpl(sender, verified, encrypted, connectionType);
286+
}
287+
273288
public static ASAPHop byteArray2ASAPHop(byte[] bytes) throws IOException, ASAPException {
274289
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
275-
return new ASAPHopImpl(
276-
ASAPSerialization.readCharSequenceParameter(bais),
277-
ASAPSerialization.readBooleanParameter(bais),
278-
ASAPSerialization.readBooleanParameter(bais),
279-
ASAPSerialization.readEncounterConnectionType(bais)
280-
);
290+
return readASAPHop(bais);
291+
}
292+
293+
public static void writeASAPHopList(List<ASAPHop> asapHopList, OutputStream os) throws IOException {
294+
if(asapHopList == null || asapHopList.isEmpty()) {
295+
// no hops
296+
ASAPSerialization.writeIntegerParameter(0, os);
297+
return;
298+
}
299+
300+
// write number of hops
301+
ASAPSerialization.writeIntegerParameter(asapHopList.size(), os);
302+
for(ASAPHop asapHop : asapHopList) {
303+
ASAPSerialization.writeASAPHop(asapHop, os);
304+
}
305+
}
306+
307+
public static List<ASAPHop> readASAPHopList(InputStream is) throws IOException, ASAPException {
308+
List<ASAPHop> asapHopList = new ArrayList<>();
309+
310+
int number = ASAPSerialization.readIntegerParameter(is);
311+
while(number-- > 0) {
312+
asapHopList.add(ASAPSerialization.readASAPHop(is));
313+
}
314+
315+
return asapHopList;
316+
281317
}
282318
}

test/net/sharksystem/asap/serialization/SerializationTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package net.sharksystem.asap.serialization;
22

3+
import net.sharksystem.ASAPHopImpl;
34
import net.sharksystem.asap.ASAPException;
5+
import net.sharksystem.asap.ASAPHop;
6+
import net.sharksystem.asap.EncounterConnectionType;
47
import net.sharksystem.asap.utils.ASAPSerialization;
8+
import net.sharksystem.asap.utils.PeerIDHelper;
59
import org.junit.Assert;
610
import org.junit.Test;
711

812
import java.io.ByteArrayInputStream;
913
import java.io.ByteArrayOutputStream;
1014
import java.io.IOException;
15+
import java.util.ArrayList;
16+
import java.util.List;
1117

1218
public class SerializationTests {
1319
@Test
@@ -21,4 +27,56 @@ public void test1() throws IOException, ASAPException {
2127

2228
Assert.assertTrue(messageOut.equals(messageIn));
2329
}
30+
31+
@Test
32+
public void serializeASAPHopListLen1() throws IOException, ASAPException {
33+
List<ASAPHop> exampleList = new ArrayList<>();
34+
35+
exampleList.add(new ASAPHopImpl("Alice", true, true, EncounterConnectionType.ASAP_HUB));
36+
37+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
38+
ASAPSerialization.writeASAPHopList(exampleList, baos);
39+
40+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
41+
List<ASAPHop> receivedList = ASAPSerialization.readASAPHopList(bais);
42+
43+
Assert.assertEquals(exampleList.size(), receivedList.size());
44+
for(int i = 0; i < exampleList.size(); i++) {
45+
ASAPHop origHop = exampleList.get(i);
46+
ASAPHop receivedHop = receivedList.get(i);
47+
48+
Assert.assertTrue(PeerIDHelper.sameID(origHop.sender(), receivedHop.sender()));
49+
Assert.assertTrue(origHop.verified() == receivedHop.verified());
50+
Assert.assertTrue(origHop.encrypted() == receivedHop.encrypted());
51+
Assert.assertTrue(origHop.getConnectionType() == receivedHop.getConnectionType());
52+
}
53+
}
54+
55+
@Test
56+
public void serializeASAPHopListLen5() throws IOException, ASAPException {
57+
List<ASAPHop> exampleList = new ArrayList<>();
58+
59+
exampleList.add(new ASAPHopImpl("Alice", true, true, EncounterConnectionType.ASAP_HUB));
60+
exampleList.add(new ASAPHopImpl("Bob", false, true, EncounterConnectionType.INTERNET));
61+
exampleList.add(new ASAPHopImpl("Clara", false, false, EncounterConnectionType.AD_HOC_LAYER_2_NETWORK));
62+
exampleList.add(new ASAPHopImpl("David", true, false, EncounterConnectionType.ONION_NETWORK));
63+
exampleList.add(new ASAPHopImpl("Eveline", true, false, EncounterConnectionType.UNKNOWN));
64+
65+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
66+
ASAPSerialization.writeASAPHopList(exampleList, baos);
67+
68+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
69+
List<ASAPHop> receivedList = ASAPSerialization.readASAPHopList(bais);
70+
71+
Assert.assertEquals(exampleList.size(), receivedList.size());
72+
for(int i = 0; i < exampleList.size(); i++) {
73+
ASAPHop origHop = exampleList.get(i);
74+
ASAPHop receivedHop = receivedList.get(i);
75+
76+
Assert.assertTrue(PeerIDHelper.sameID(origHop.sender(), receivedHop.sender()));
77+
Assert.assertTrue(origHop.verified() == receivedHop.verified());
78+
Assert.assertTrue(origHop.encrypted() == receivedHop.encrypted());
79+
Assert.assertTrue(origHop.getConnectionType() == receivedHop.getConnectionType());
80+
}
81+
}
2482
}

0 commit comments

Comments
 (0)