Class IntermediateEnvelope<T extends com.google.protobuf.MessageLite>
- java.lang.Object
-
- org.drasyl.remote.protocol.IntermediateEnvelope<T>
-
- 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 givenByteBuf
encoded protobuf message in parts with only decoding the requested parts of the givenByteBuf
. If a part was request it will be translated into a Java object.
-
-
Field Summary
Fields Modifier and Type Field Description static short
MAGIC_NUMBER_LENGTH
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static IntermediateEnvelope<Protocol.Acknowledgement>
acknowledgement(int networkId, CompressedPublicKey sender, ProofOfWork proofOfWork, CompressedPublicKey recipient, MessageId correspondingId)
Creates new acknowledgement message.static IntermediateEnvelope<Protocol.Application>
application(int networkId, CompressedPublicKey sender, ProofOfWork proofOfWork, CompressedPublicKey recipient, String type, byte[] payload)
Creates new application message.IntermediateEnvelope<T>
arm(CompressedPrivateKey privateKey)
Returns an armed version of this envelope for sending it through untrustworthy channels.IntermediateEnvelope<T>
armAndRelease(CompressedPrivateKey privateKey)
Returns an armed version of this envelope for sending it through untrustworthy channels.io.netty.buffer.ByteBuf
copy()
This method returns a copy of theIntermediateEnvelope
and resets the reader index to 0.IntermediateEnvelope<T>
disarm(CompressedPrivateKey privateKey)
Returns a disarmed version of this envelope.IntermediateEnvelope<T>
disarmAndRelease(CompressedPrivateKey privateKey)
Returns a disarmed version of this envelope.static IntermediateEnvelope<Protocol.Discovery>
discovery(int networkId, CompressedPublicKey sender, ProofOfWork proofOfWork, CompressedPublicKey recipient, long joinTime)
Creates a new join message.T
getBody()
Reads only the body of the given message and retains the underlying byte representation of the full message.T
getBodyAndRelease()
Reads only the body of the given message and releases the underlying byte representation of the full message.UnsignedShort
getChunkNo()
Returns the number of the chunk.byte
getHopCount()
MessageId
getId()
io.netty.buffer.ByteBuf
getInternalByteBuf()
This method returns the internal wrappedByteBuf
of thisIntermediateEnvelope
.int
getNetworkId()
io.netty.buffer.ByteBuf
getOrBuildByteBuf()
The externalByteBuf
or ifnull
builds it first.Protocol.PrivateHeader
getPrivateHeader()
Reads only the (private) header of the given message and retains the underlying byte representation of the full message.ProofOfWork
getProofOfWork()
Protocol.PublicHeader
getPublicHeader()
Reads only the public header of the given message and retains the underlying byte representation of the full message.CompressedPublicKey
getRecipient()
CompressedPublicKey
getSender()
byte[]
getSignature()
UnsignedShort
getTotalChunks()
Returns the total chunks number.void
incrementHopCount()
boolean
isChunk()
Returnstrue
if this message is a chunk.static byte[]
magicNumber()
static <T extends com.google.protobuf.MessageLite>
IntermediateEnvelope<T>of(io.netty.buffer.ByteBuf message)
Wraps the givenByteBuf
into anIntermediateEnvelope
.static <T extends com.google.protobuf.MessageLite>
IntermediateEnvelope<T>of(Protocol.PublicHeader publicHeader, byte[] bytes)
Creates a message envelope frompublicHeader
andbytes
static <T extends com.google.protobuf.MessageLite>
IntermediateEnvelope<T>of(Protocol.PublicHeader publicHeader, io.netty.buffer.ByteBuf bytes)
Creates a message envelope frompublicHeader
andbytes
.static <T extends com.google.protobuf.MessageLite>
IntermediateEnvelope<T>of(Protocol.PublicHeader publicHeader, Protocol.PrivateHeader privateHeader, T body)
Creates a message envelope frompublicHeader
,privateHeader
, andbody
.int
refCnt()
boolean
release()
boolean
release(int decrement)
void
releaseAll()
This method does release all usedByteBuf
s.io.netty.util.ReferenceCounted
retain()
io.netty.util.ReferenceCounted
retain(int increment)
String
toString()
io.netty.util.ReferenceCounted
touch()
io.netty.util.ReferenceCounted
touch(Object hint)
static IntermediateEnvelope<Protocol.Unite>
unite(int networkId, CompressedPublicKey sender, ProofOfWork proofOfWork, CompressedPublicKey recipient, CompressedPublicKey publicKey, InetSocketAddress address)
-
-
-
Field Detail
-
MAGIC_NUMBER_LENGTH
public static final short MAGIC_NUMBER_LENGTH
- See Also:
- Constant Field Values
-
-
Method Detail
-
of
public static <T extends com.google.protobuf.MessageLite> IntermediateEnvelope<T> of(io.netty.buffer.ByteBuf message) throws IOException
Wraps the givenByteBuf
into anIntermediateEnvelope
.Note: The given
message
ByteBuf
will not be modified. This object uses a duplicate of the givenByteBuf
.ReferenceCounted.release()
ownership ofmessage
is transferred to thisIntermediateEnvelope
.- Parameters:
message
- the message that should be wrapped.ReferenceCounted.release()
ownership is transferred to thisIntermediateEnvelope
.- Returns:
- an IntermediateEnvelope
- Throws:
IOException
- if the givenByteBuf
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 frompublicHeader
,privateHeader
, andbody
.- Parameters:
publicHeader
- message's public headerprivateHeader
- message's private headerbody
- 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 frompublicHeader
andbytes
- Parameters:
publicHeader
- message's public headerbytes
- message's remainder as bytes (may be encrypted)- Returns:
- an IntermediateEnvelope
- Throws:
IOException
- ifpublicHeader
andbytes
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 frompublicHeader
andbytes
.ReferenceCounted.release()
ownership ofbytes
is transferred to thisIntermediateEnvelope
.- Parameters:
publicHeader
- message's public headerbytes
- message's remainder as bytes (may be encrypted).ReferenceCounted.release()
ownership is transferred to thisIntermediateEnvelope
.- Returns:
- an IntermediateEnvelope
- Throws:
IOException
- ifpublicHeader
andbytes
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 theIntermediateEnvelope
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 wrappedByteBuf
of thisIntermediateEnvelope
.- Returns:
- the internal wrapped
ByteBuf
-
getOrBuildByteBuf
public io.netty.buffer.ByteBuf getOrBuildByteBuf() throws IOException
The externalByteBuf
or ifnull
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 interfaceio.netty.util.ReferenceCounted
-
retain
public io.netty.util.ReferenceCounted retain()
- Specified by:
retain
in interfaceio.netty.util.ReferenceCounted
-
retain
public io.netty.util.ReferenceCounted retain(int increment)
- Specified by:
retain
in interfaceio.netty.util.ReferenceCounted
-
touch
public io.netty.util.ReferenceCounted touch()
- Specified by:
touch
in interfaceio.netty.util.ReferenceCounted
-
touch
public io.netty.util.ReferenceCounted touch(Object hint)
- Specified by:
touch
in interfaceio.netty.util.ReferenceCounted
-
release
public boolean release()
- Specified by:
release
in interfaceio.netty.util.ReferenceCounted
-
release
public boolean release(int decrement)
- Specified by:
release
in interfaceio.netty.util.ReferenceCounted
-
releaseAll
public void releaseAll()
This method does release all usedByteBuf
s. The corresponding elements where also set tonull
.
-
getId
public MessageId getId() throws IOException
- Throws:
IOException
- if the public header cannot be read
-
getNetworkId
public int getNetworkId() throws IOException
- Throws:
IOException
- if the public header cannot be read
-
getSender
public CompressedPublicKey getSender() throws IOException
- Throws:
IOException
- if the public header cannot be read
-
getProofOfWork
public ProofOfWork getProofOfWork() throws IOException
- Throws:
IOException
- if the public header cannot be read
-
getRecipient
public CompressedPublicKey getRecipient() 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:
- encrypted
privateHeader
and encryptedbody
will be signed withprivateKey
privateHeader
andbody
will be encrypted with recipient's public key (not implemented!)
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
- encrypted
-
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:
- encrypted
privateHeader
and encryptedbody
will be signed withprivateKey
privateHeader
andbody
will be encrypted with recipient's public key (not implemented!)
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
- encrypted
-
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 encryptedbody
will be decrypted withprivateKey
(not implemented yet!) - the signed portions of the message (
privateHeader
andbody
) 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
- the encrypted
-
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 encryptedbody
will be decrypted withprivateKey
(not implemented yet!) - the signed portions of the message (
privateHeader
andbody
) 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
- the encrypted
-
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 serversender
- the public key of the node serverproofOfWork
- the proof of work of the node serverrecipient
- the public key of the recipientcorrespondingId
- the corresponding id of the previous join message
-
application
public static IntermediateEnvelope<Protocol.Application> application(int networkId, CompressedPublicKey sender, ProofOfWork proofOfWork, CompressedPublicKey recipient, String type, byte[] payload)
Creates new application message.- Parameters:
networkId
- the network the sender belongs tosender
- the senderproofOfWork
- the sender's proof of workrecipient
- the recipienttype
- the payload typepayload
- the data to be sent
-
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 nodesender
- the public key of the joining nodeproofOfWork
- the proof of workrecipient
- the public key of the node to joinjoinTime
- if0
greater then 0, node will join a children.
-
unite
public static IntermediateEnvelope<Protocol.Unite> unite(int networkId, CompressedPublicKey sender, ProofOfWork proofOfWork, CompressedPublicKey recipient, CompressedPublicKey publicKey, InetSocketAddress address)
-
isChunk
public boolean isChunk() throws IOException
Returnstrue
if this message is a chunk. Otherwisefalse
is returned.- Returns:
true
if this message is a chunk. Otherwisefalse
.- 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()
-
-