Class IntermediateEnvelope<T extends com.google.protobuf.MessageLite>

  • All Implemented Interfaces:
    io.netty.util.ReferenceCounted

    public class IntermediateEnvelope<T extends com.google.protobuf.MessageLite>
    extends Object
    implements io.netty.util.ReferenceCounted
    This class allows to read a given ByteBuf encoded protobuf message in parts with only decoding the requested parts of the given ByteBuf. If a part was request it will be translated into a Java object.
    • Method Detail

      • of

        public static <T extends com.google.protobuf.MessageLite> IntermediateEnvelope<T> of​(io.netty.buffer.ByteBuf message)
                                                                                      throws IOException
        Wraps the given ByteBuf into an IntermediateEnvelope.

        Note: The given message ByteBuf will not be modified. This object uses a duplicate of the given ByteBuf.

        ReferenceCounted.release() ownership of message is transferred to this IntermediateEnvelope.

        Parameters:
        message - the message that should be wrapped. ReferenceCounted.release() ownership is transferred to this IntermediateEnvelope.
        Returns:
        an IntermediateEnvelope
        Throws:
        IOException - if the given ByteBuf is not readable
      • of

        public static <T extends com.google.protobuf.MessageLite> IntermediateEnvelope<T> of​(Protocol.PublicHeader publicHeader,
                                                                                             Protocol.PrivateHeader privateHeader,
                                                                                             T body)
        Creates a message envelope from publicHeader, privateHeader, and body.
        Parameters:
        publicHeader - message's public header
        privateHeader - message's private header
        body - the message
        Returns:
        an IntermediateEnvelope
      • of

        public static <T extends com.google.protobuf.MessageLite> IntermediateEnvelope<T> of​(Protocol.PublicHeader publicHeader,
                                                                                             byte[] bytes)
                                                                                      throws IOException
        Creates a message envelope from publicHeader and bytes
        Parameters:
        publicHeader - message's public header
        bytes - message's remainder as bytes (may be encrypted)
        Returns:
        an IntermediateEnvelope
        Throws:
        IOException - if publicHeader and bytes can not be serialized
      • of

        public static <T extends com.google.protobuf.MessageLite> IntermediateEnvelope<T> of​(Protocol.PublicHeader publicHeader,
                                                                                             io.netty.buffer.ByteBuf bytes)
                                                                                      throws IOException
        Creates a message envelope from publicHeader and bytes.

        ReferenceCounted.release() ownership of bytes is transferred to this IntermediateEnvelope.

        Parameters:
        publicHeader - message's public header
        bytes - message's remainder as bytes (may be encrypted). ReferenceCounted.release() ownership is transferred to this IntermediateEnvelope.
        Returns:
        an IntermediateEnvelope
        Throws:
        IOException - if publicHeader and bytes can not be serialized
      • getPublicHeader

        public Protocol.PublicHeader getPublicHeader()
                                              throws IOException
        Reads only the public header of the given message and retains the underlying byte representation of the full message.
        Returns:
        header of the message
        Throws:
        IOException - if the public header cannot be read
      • getPrivateHeader

        public Protocol.PrivateHeader getPrivateHeader()
                                                throws IOException
        Reads only the (private) header of the given message and retains the underlying byte representation of the full message.
        Returns:
        the private header
        Throws:
        IOException - if the private header cannot be read
      • getBody

        public T getBody()
                  throws IOException
        Reads only the body of the given message and retains the underlying byte representation of the full message.
        Returns:
        the body
        Throws:
        IOException - if the body cannot be read
      • getBodyAndRelease

        public T getBodyAndRelease()
                            throws IOException
        Reads only the body of the given message and releases the underlying byte representation of the full message.
        Returns:
        the body
        Throws:
        IOException - if the body cannot be read
      • copy

        public io.netty.buffer.ByteBuf copy()
        This method returns a copy of the IntermediateEnvelope and resets the reader index to 0.
        Returns:
        the wrapped ByteBuf of this envelope
      • getInternalByteBuf

        public io.netty.buffer.ByteBuf getInternalByteBuf()
        This method returns the internal wrapped ByteBuf of this IntermediateEnvelope.
        Returns:
        the internal wrapped ByteBuf
      • getOrBuildByteBuf

        public io.netty.buffer.ByteBuf getOrBuildByteBuf()
                                                  throws IOException
        The external ByteBuf or if null builds it first.
        Returns:
        the envelope as ByteBuf
        Throws:
        IOException - if the envelope can't be build
      • refCnt

        public int refCnt()
        Specified by:
        refCnt in interface io.netty.util.ReferenceCounted
      • retain

        public io.netty.util.ReferenceCounted retain()
        Specified by:
        retain in interface io.netty.util.ReferenceCounted
      • retain

        public io.netty.util.ReferenceCounted retain​(int increment)
        Specified by:
        retain in interface io.netty.util.ReferenceCounted
      • touch

        public io.netty.util.ReferenceCounted touch()
        Specified by:
        touch in interface io.netty.util.ReferenceCounted
      • touch

        public io.netty.util.ReferenceCounted touch​(Object hint)
        Specified by:
        touch in interface io.netty.util.ReferenceCounted
      • release

        public boolean release()
        Specified by:
        release in interface io.netty.util.ReferenceCounted
      • release

        public boolean release​(int decrement)
        Specified by:
        release in interface io.netty.util.ReferenceCounted
      • releaseAll

        public void releaseAll()
        This method does release all used ByteBufs. The corresponding elements where also set to null.
      • getNetworkId

        public int getNetworkId()
                         throws IOException
        Throws:
        IOException - if the public header cannot be read
      • getHopCount

        public byte getHopCount()
                         throws IOException
        Throws:
        IOException - if the public header cannot be read
      • incrementHopCount

        public void incrementHopCount()
                               throws IOException
        Throws:
        IOException - if the public header cannot be read or be updated
      • getSignature

        public byte[] getSignature()
                            throws IOException
        Returns:
        signature as byte array
        Throws:
        IOException - if the public header cannot be read
      • arm

        public IntermediateEnvelope<T> arm​(CompressedPrivateKey privateKey)
                                    throws IOException
        Returns an armed version of this envelope for sending it through untrustworthy channels.

        privateKey must match message's sender.

        Arming includes the following steps:

        Note: Do not forget to release the original message

        Parameters:
        privateKey - message is signed with this key
        Returns:
        the armed version of this envelope
        Throws:
        IOException - if arming was not possible
      • armAndRelease

        public IntermediateEnvelope<T> armAndRelease​(CompressedPrivateKey privateKey)
                                              throws IOException
        Returns an armed version of this envelope for sending it through untrustworthy channels.

        privateKey must match message's sender.

        Arming includes the following steps:

        This method will release all resources even in case of an exception.

        Parameters:
        privateKey - message is signed with this key
        Returns:
        the armed version of this envelope
        Throws:
        IOException - if arming was not possible
      • disarm

        public IntermediateEnvelope<T> disarm​(CompressedPrivateKey privateKey)
                                       throws IOException
        Returns a disarmed version of this envelope.

        privateKey must match message's recipient (not implemented yet!).

        Disarming includes the following steps:

        • the encrypted privateHeader and encrypted body will be decrypted with privateKey (not implemented yet!)
        • the signed portions of the message (privateHeader and body) are verified against sender's public key.

        Note: Do not forget to release the original message

        Returns:
        the disarmed version of this envelope
        Throws:
        IOException - if disarming was not possible
      • disarmAndRelease

        public IntermediateEnvelope<T> disarmAndRelease​(CompressedPrivateKey privateKey)
                                                 throws IOException
        Returns a disarmed version of this envelope.

        privateKey must match message's recipient (not implemented yet!).

        Disarming includes the following steps:

        • the encrypted privateHeader and encrypted body will be decrypted with privateKey (not implemented yet!)
        • the signed portions of the message (privateHeader and body) are verified against sender's public key.

        This method will release all resources even in case of an exception.

        Returns:
        the disarmed version of this envelope
        Throws:
        IOException - if disarming was not possible
      • acknowledgement

        public static IntermediateEnvelope<Protocol.Acknowledgement> acknowledgement​(int networkId,
                                                                                     CompressedPublicKey sender,
                                                                                     ProofOfWork proofOfWork,
                                                                                     CompressedPublicKey recipient,
                                                                                     MessageId correspondingId)
        Creates new acknowledgement message.
        Parameters:
        networkId - the network id of the node server
        sender - the public key of the node server
        proofOfWork - the proof of work of the node server
        recipient - the public key of the recipient
        correspondingId - the corresponding id of the previous join message
      • discovery

        public static IntermediateEnvelope<Protocol.Discovery> discovery​(int networkId,
                                                                         CompressedPublicKey sender,
                                                                         ProofOfWork proofOfWork,
                                                                         CompressedPublicKey recipient,
                                                                         long joinTime)
        Creates a new join message.
        Parameters:
        networkId - the network of the joining node
        sender - the public key of the joining node
        proofOfWork - the proof of work
        recipient - the public key of the node to join
        joinTime - if 0 greater then 0, node will join a children.
      • isChunk

        public boolean isChunk()
                        throws IOException
        Returns true if this message is a chunk. Otherwise false is returned.
        Returns:
        true if this message is a chunk. Otherwise false.
        Throws:
        IOException - if the public header cannot be read
      • getChunkNo

        public UnsignedShort getChunkNo()
                                 throws IOException
        Returns the number of the chunk. If the message is not a chunk, 0 is returned.
        Returns:
        number of the chunk or 0 if message is not a chunk
        Throws:
        IOException - if the public header cannot be read
      • getTotalChunks

        public UnsignedShort getTotalChunks()
                                     throws IOException
        Returns the total chunks number. If the message is not a chunk, 0 is returned.
        Returns:
        total chunks number or 0 if message is not a chunk
        Throws:
        IOException - if the public header cannot be read
      • magicNumber

        public static byte[] magicNumber()