11package net .sharksystem .asap .sharknet ;
22
33import net .sharksystem .asap .*;
4+ import net .sharksystem .asap .protocol .ASAPConnection ;
45import net .sharksystem .asap .util .Helper ;
5- import net .sharksystem .crypto .ASAPCryptoAlgorithms ;
66import net .sharksystem .crypto .BasicCryptoParameters ;
7- import net .sharksystem .utils .Serialization ;
87
9- import java .io .ByteArrayInputStream ;
10- import java .io .ByteArrayOutputStream ;
11- import java .io .IOException ;
8+ import java .io .*;
129import java .util .HashSet ;
1310import java .util .Iterator ;
1411import java .util .Set ;
@@ -40,45 +37,14 @@ public CharSequence getOwnerID() {
4037 return this .asapPeer .getOwner ();
4138 }
4239
43- private static final int SIGNED_MASK = 0x1 ;
44- private static final int ENCRYPTED_MASK = 0x2 ;
45-
4640 @ Override
4741 public void sendSharkNetMessage (byte [] message , CharSequence topic , CharSequence recipient ,
4842 boolean sign , boolean encrypt ) throws IOException , ASAPException {
4943
5044 // serialize sn message
51-
52- // merge content, sender and recipient
53- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
54- Serialization .writeByteArray (message , baos );
55- Serialization .writeCharSequenceParameter (this .getOwnerID (), baos );
56- Serialization .writeCharSequenceParameter (recipient , baos );
57- message = baos .toByteArray ();
58-
59- byte flags = 0 ;
60- if (sign ) {
61- byte [] signature = ASAPCryptoAlgorithms .sign (message , this .basicCryptoParameters );
62- baos = new ByteArrayOutputStream ();
63- Serialization .writeByteArray (message , baos );
64- Serialization .writeByteArray (signature , baos );
65- // attach signature to message
66- message = baos .toByteArray ();
67- flags += SIGNED_MASK ;
68- }
69-
70- if (encrypt ) {
71- message = ASAPCryptoAlgorithms .produceEncryptedMessagePackage (
72- message , recipient , this .basicCryptoParameters );
73- flags += ENCRYPTED_MASK ;
74- }
75-
76- // serialize SN message
77- baos = new ByteArrayOutputStream ();
78- Serialization .writeByteParameter (flags , baos );
79- Serialization .writeByteArray (message , baos );
80-
81- this .sharkNetEngine .add (topic , baos .toByteArray ());
45+ this .sharkNetEngine .add (topic ,
46+ SharkNetMessage .serializeMessage (message , topic , recipient , sign , encrypt ,
47+ this .getOwnerID (), this .basicCryptoParameters ));
8248 }
8349
8450 @ Override
@@ -104,54 +70,18 @@ public void chunkReceived(String format, String sender, String uri, int era) thr
10470 byte [] message = msgIter .next ();
10571
10672 // deserialize SNMessage
107- ByteArrayInputStream bais = new ByteArrayInputStream (message );
10873 try {
109- byte flags = Serialization .readByte (bais );
110- byte [] snMessage = Serialization .readByteArray (bais );
111-
112- boolean signed = (flags & SIGNED_MASK ) != 0 ;
113- boolean encrypted = (flags & ENCRYPTED_MASK ) != 0 ;
114-
115- if (encrypted ) {
116- // decrypt
117- bais = new ByteArrayInputStream (snMessage );
118- ASAPCryptoAlgorithms .EncryptedMessagePackage
119- encryptedMessagePackage = ASAPCryptoAlgorithms .parseEncryptedMessagePackage (bais );
120-
121- // for me?
122- if (!encryptedMessagePackage .getRecipient ().equals (this .getOwnerID ())) {
123- System .out .println (this .getLogStart () + "message not for me" );
124- continue ; // still in asapStorage and will be redistributed
125- }
126-
127- // replace message with decrypted message
128- snMessage = ASAPCryptoAlgorithms .decryptPackage (
129- encryptedMessagePackage , this .basicCryptoParameters );
130- }
131-
132- byte [] signature = null ;
133- if (signed ) {
134- // split message from signature
135- bais = new ByteArrayInputStream (snMessage );
136- snMessage = Serialization .readByteArray (bais );
137- signature = Serialization .readByteArray (bais );
138- }
139- bais = new ByteArrayInputStream (snMessage );
140- String snSender = Serialization .readCharSequenceParameter (bais );
141- String snReceiver = Serialization .readCharSequenceParameter (bais );
142-
143- boolean verified = false ; // initialize
144- if (signature != null ) {
145- verified = ASAPCryptoAlgorithms .verify (
146- snMessage , signature , snSender , this .basicCryptoParameters );
147- }
74+ SharkNetMessage snMessage =
75+ SharkNetMessage .parseMessage (message , sender , uri , this .getOwnerID (), this .basicCryptoParameters );
14876
14977 // we have anything - tell listeners
15078 for (SharkNetMessageListener l : this .snListenerSet ) {
151- l .messageReceived (snMessage , uri , snSender , verified , encrypted );
79+ l .messageReceived (snMessage .getContent (), uri , snMessage .getSender (), snMessage .verified (),
80+ snMessage .encrypted ());
15281 }
15382 } catch (ASAPException e ) {
15483 System .out .println (this .getLogStart () + "problems when deserializing SharkNet message" );
84+ e .printStackTrace ();
15585 continue ; // try next
15686 }
15787 }
@@ -161,4 +91,9 @@ public void chunkReceived(String format, String sender, String uri, int era) thr
16191 private String getLogStart () {
16292 return this .getClass ().getSimpleName () + ": " ;
16393 }
94+
95+ @ Override
96+ public ASAPConnection handleConnection (InputStream is , OutputStream os ) throws IOException , ASAPException {
97+ return this .asapPeer .handleConnection (is , os );
98+ }
16499}
0 commit comments