Skip to content

Commit cdb4578

Browse files
committed
introduced ASAPHop - makes SharkMessenger API easier to understand. Makes difference between E2E and Point2Point more obvious. At least I hope so.
1 parent a9c2748 commit cdb4578

2 files changed

Lines changed: 82 additions & 3 deletions

File tree

src/net/sharksystem/ASAPHopImpl.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,27 @@ public boolean verified() {
3535
public boolean encrypted() {
3636
return this.encrypted;
3737
}
38+
39+
public String toString() {
40+
StringBuilder sb = new StringBuilder();
41+
sb.append("sender: ");
42+
sb.append(this.sender);
43+
sb.append(" | ");
44+
sb.append("verified: ");
45+
sb.append(this.verified);
46+
sb.append(" | ");
47+
sb.append("encrypted: ");
48+
sb.append(this.encrypted);
49+
sb.append(" | ");
50+
sb.append("connectionType: ");
51+
switch (this.connectionType) {
52+
case ONION_NETWORK: sb.append("onionNetwork"); break;
53+
case ASAP_HUB: sb.append("ASAP Hub"); break;
54+
case AD_HOC_LAYER_2_NETWORK: sb.append("Ad-hoc protocol"); break;
55+
case INTERNET: sb.append("Internet"); break;
56+
default: sb.append("unknown"); break;
57+
}
58+
59+
return sb.toString();
60+
}
3861
}

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package net.sharksystem.asap.utils;
22

3+
import net.sharksystem.ASAPHopImpl;
34
import net.sharksystem.asap.ASAPException;
5+
import net.sharksystem.asap.ASAPHop;
6+
import net.sharksystem.asap.ASAPPeer;
7+
import net.sharksystem.asap.EncounterConnectionType;
8+
import net.sharksystem.utils.Log;
49

5-
import java.io.IOException;
6-
import java.io.InputStream;
7-
import java.io.OutputStream;
10+
import java.io.*;
811
import java.util.HashSet;
912
import java.util.Set;
1013

@@ -223,4 +226,57 @@ public static Set<CharSequence> readCharSequenceSetParameter(InputStream is) thr
223226

224227
return charSet;
225228
}
229+
230+
public static EncounterConnectionType readEncounterConnectionType(InputStream is) throws IOException, ASAPException {
231+
byte readByte = ASAPSerialization.readByte(is);
232+
switch(readByte) {
233+
case 1: return EncounterConnectionType.ASAP_HUB;
234+
case 2: return EncounterConnectionType.AD_HOC_LAYER_2_NETWORK;
235+
case 3: return EncounterConnectionType.ONION_NETWORK;
236+
case 4: return EncounterConnectionType.INTERNET;
237+
default:
238+
Log.writeLogErr(ASAPSerialization.class, "unknown encounter connection type: " + readByte);
239+
}
240+
// default
241+
return EncounterConnectionType.UNKNOWN;
242+
}
243+
244+
public static void writeEncounterConnectionType(EncounterConnectionType connectionType, OutputStream os) throws IOException {
245+
switch(connectionType) {
246+
case ASAP_HUB: ASAPSerialization.writeByteParameter((byte) 1, os); break;
247+
case AD_HOC_LAYER_2_NETWORK: ASAPSerialization.writeByteParameter((byte) 2, os); break;
248+
case ONION_NETWORK: ASAPSerialization.writeByteParameter((byte) 3, os); break;
249+
case INTERNET: ASAPSerialization.writeByteParameter((byte) 4, os); break;
250+
case UNKNOWN:
251+
default: ASAPSerialization.writeByteParameter((byte) 0, os);
252+
}
253+
}
254+
255+
public static void writeBooleanParameter(boolean value, OutputStream os) throws IOException {
256+
if(value) ASAPSerialization.writeByteParameter((byte) 1, os);
257+
ASAPSerialization.writeByteParameter((byte) 0, os);
258+
}
259+
260+
public static boolean readBooleanParameter(InputStream is) throws IOException, ASAPException {
261+
return ASAPSerialization.readByte(is) == 1;
262+
}
263+
264+
public static byte[] asapHop2ByteArray(ASAPHop asapHop) throws IOException {
265+
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);
270+
return baos.toByteArray();
271+
}
272+
273+
public static ASAPHop byteArray2ASAPHop(byte[] bytes) throws IOException, ASAPException {
274+
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+
);
281+
}
226282
}

0 commit comments

Comments
 (0)