|
1 | 1 | package net.sharksystem.asap.utils; |
2 | 2 |
|
| 3 | +import net.sharksystem.ASAPHopImpl; |
3 | 4 | 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; |
4 | 9 |
|
5 | | -import java.io.IOException; |
6 | | -import java.io.InputStream; |
7 | | -import java.io.OutputStream; |
| 10 | +import java.io.*; |
8 | 11 | import java.util.HashSet; |
9 | 12 | import java.util.Set; |
10 | 13 |
|
@@ -223,4 +226,57 @@ public static Set<CharSequence> readCharSequenceSetParameter(InputStream is) thr |
223 | 226 |
|
224 | 227 | return charSet; |
225 | 228 | } |
| 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 | + } |
226 | 282 | } |
0 commit comments