Class RemoteEnvelope<T extends com.google.protobuf.MessageLite>
- java.lang.Object
-
- org.drasyl.remote.protocol.RemoteEnvelope<T>
-
- All Implemented Interfaces:
io.netty.util.ReferenceCounted
,AutoCloseable
public class RemoteEnvelope<T extends com.google.protobuf.MessageLite> extends Object implements io.netty.util.ReferenceCounted, AutoCloseable
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 com.google.protobuf.ByteString
MAGIC_NUMBER
static short
MAGIC_NUMBER_LENGTH
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static RemoteEnvelope<Protocol.Acknowledgement>
acknowledgement(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, Nonce correspondingId)
Creates newProtocol.Acknowledgement
message (reply toProtocol.Discovery
message).static RemoteEnvelope<Protocol.Application>
application(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, String type, byte[] payload)
Creates newProtocol.Application
message.RemoteEnvelope<T>
arm(com.goterl.lazysodium.utils.SessionPair sessionPair)
Returns an armed version of this envelope for sending it through untrustworthy channels.RemoteEnvelope<T>
armAndRelease(com.goterl.lazysodium.utils.SessionPair sessionPair)
Returns an armed version of this envelope for sending it through untrustworthy channels.void
close()
io.netty.buffer.ByteBuf
copy()
This method returns a copy of theRemoteEnvelope
and resets the reader index to 0.RemoteEnvelope<T>
disarm(com.goterl.lazysodium.utils.SessionPair sessionPair)
Returns a disarmed version of this envelope.RemoteEnvelope<T>
disarmAndRelease(com.goterl.lazysodium.utils.SessionPair sessionPair)
Returns a disarmed version of this envelope.static RemoteEnvelope<Protocol.Discovery>
discovery(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork)
Creates a new multicastProtocol.Discovery
message (sent byLocalNetworkDiscovery
}.static RemoteEnvelope<Protocol.Discovery>
discovery(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, long joinTime)
Creates a newProtocol.Discovery
message (sent byInternetDiscovery
).AgreementId
getAgreementId()
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()
io.netty.buffer.ByteBuf
getInternalByteBuf()
This method returns the internal wrappedByteBuf
of thisRemoteEnvelope
.int
getNetworkId()
Nonce
getNonce()
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.Pair<Protocol.PublicHeader,io.netty.buffer.ByteBuf>
getPublicHeaderAndRemainingBytes()
Reads the public header of the given message and returns aByteBuf
with all remaining bytes.IdentityPublicKey
getRecipient()
Returns theIdentityPublicKey
of the message recipient.IdentityPublicKey
getSender()
UnsignedShort
getTotalChunks()
Returns the total chunks number.void
incrementHopCount()
boolean
isChunk()
Returnstrue
if this message is a chunk.static RemoteEnvelope<Protocol.KeyExchange>
keyExchange(int networkId, IdentityPublicKey sender, ProofOfWork identityProofOfWork, IdentityPublicKey recipient, KeyAgreementPublicKey sessionKey)
Creates a newProtocol.KeyExchange
message (sent byArmHandler
; used for perfect forwarded secrecy).static RemoteEnvelope<Protocol.KeyExchangeAcknowledgement>
keyExchangeAcknowledgement(int networkId, IdentityPublicKey sender, ProofOfWork identityProofOfWork, IdentityPublicKey recipient, AgreementId agreementId)
Creates a newProtocol.KeyExchangeAcknowledgement
message (sent byArmHandler
; used for perfect forwarded secrecy).static <T extends com.google.protobuf.MessageLite>
RemoteEnvelope<T>of(io.netty.buffer.ByteBuf message)
Wraps the givenByteBuf
into anRemoteEnvelope
.static <T extends com.google.protobuf.MessageLite>
RemoteEnvelope<T>of(Protocol.PublicHeader publicHeader, byte[] bytes)
Creates a message envelope frompublicHeader
andbytes
static <T extends com.google.protobuf.MessageLite>
RemoteEnvelope<T>of(Protocol.PublicHeader publicHeader, io.netty.buffer.ByteBuf bytes)
Creates a message envelope frompublicHeader
andbytes
.static <T extends com.google.protobuf.MessageLite>
RemoteEnvelope<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)
RemoteEnvelope<T>
setAgreementId(AgreementId agreementId)
String
toString()
io.netty.util.ReferenceCounted
touch()
io.netty.util.ReferenceCounted
touch(Object hint)
static RemoteEnvelope<Protocol.Unite>
unite(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, IdentityPublicKey publicKey, InetSocketAddress address)
Creates a newProtocol.Unite
message (sent byInternetDiscovery
; used for UDP hole punching}.
-
-
-
Method Detail
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
of
public static <T extends com.google.protobuf.MessageLite> RemoteEnvelope<T> of(io.netty.buffer.ByteBuf message) throws InvalidMessageFormatException
Wraps the givenByteBuf
into anRemoteEnvelope
.Note: The given
message
ByteBuf
will not be modified. This object uses a duplicate of the givenByteBuf
.ReferenceCounted.release()
ownership ofmessage
is transferred to thisRemoteEnvelope
.- Parameters:
message
- the message that should be wrapped.ReferenceCounted.release()
ownership is transferred to thisRemoteEnvelope
.- Returns:
- an RemoteEnvelope
- Throws:
InvalidMessageFormatException
- if the givenByteBuf
is not readable
-
of
public static <T extends com.google.protobuf.MessageLite> RemoteEnvelope<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 RemoteEnvelope
-
of
public static <T extends com.google.protobuf.MessageLite> RemoteEnvelope<T> of(Protocol.PublicHeader publicHeader, byte[] bytes) throws InvalidMessageFormatException
Creates a message envelope frompublicHeader
andbytes
- Parameters:
publicHeader
- message's public headerbytes
- message's remainder as bytes (may be encrypted)- Returns:
- an RemoteEnvelope
- Throws:
InvalidMessageFormatException
- ifpublicHeader
andbytes
can not be serialized
-
of
public static <T extends com.google.protobuf.MessageLite> RemoteEnvelope<T> of(Protocol.PublicHeader publicHeader, io.netty.buffer.ByteBuf bytes) throws InvalidMessageFormatException
Creates a message envelope frompublicHeader
andbytes
.ReferenceCounted.release()
ownership ofbytes
is transferred to thisRemoteEnvelope
.- Parameters:
publicHeader
- message's public headerbytes
- message's remainder as bytes (may be encrypted).ReferenceCounted.release()
ownership is transferred to thisRemoteEnvelope
.- Returns:
- an RemoteEnvelope
- Throws:
InvalidMessageFormatException
- ifpublicHeader
andbytes
can not be serialized
-
getPublicHeader
public Protocol.PublicHeader getPublicHeader() throws InvalidMessageFormatException
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:
InvalidMessageFormatException
- if the public header cannot be read
-
getPrivateHeader
public Protocol.PrivateHeader getPrivateHeader() throws InvalidMessageFormatException
Reads only the (private) header of the given message and retains the underlying byte representation of the full message.- Returns:
- the private header
- Throws:
InvalidMessageFormatException
- if the private header cannot be read
-
getBody
public T getBody() throws InvalidMessageFormatException
Reads only the body of the given message and retains the underlying byte representation of the full message.- Returns:
- the body
- Throws:
InvalidMessageFormatException
- if the body cannot be read
-
getBodyAndRelease
public T getBodyAndRelease() throws InvalidMessageFormatException
Reads only the body of the given message and releases the underlying byte representation of the full message.- Returns:
- the body
- Throws:
InvalidMessageFormatException
- if the body cannot be read
-
copy
public io.netty.buffer.ByteBuf copy()
This method returns a copy of theRemoteEnvelope
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 thisRemoteEnvelope
.- Returns:
- the internal wrapped
ByteBuf
-
getOrBuildByteBuf
public io.netty.buffer.ByteBuf getOrBuildByteBuf() throws InvalidMessageFormatException
The externalByteBuf
or ifnull
builds it first.- Returns:
- the envelope as
ByteBuf
- Throws:
InvalidMessageFormatException
- 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
.
-
getNonce
public Nonce getNonce() throws InvalidMessageFormatException
- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
getNetworkId
public int getNetworkId() throws InvalidMessageFormatException
- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
getSender
public IdentityPublicKey getSender() throws InvalidMessageFormatException
- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
getProofOfWork
public ProofOfWork getProofOfWork() throws InvalidMessageFormatException
- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
getRecipient
public IdentityPublicKey getRecipient() throws InvalidMessageFormatException
Returns theIdentityPublicKey
of the message recipient. If the message has no recipient (e.g. because it is a multicast message)null
is returned.- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
getHopCount
public byte getHopCount() throws InvalidMessageFormatException
- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
getAgreementId
public AgreementId getAgreementId() throws InvalidMessageFormatException
- Returns:
AgreementId
ornull
if no key was used- Throws:
InvalidMessageFormatException
- if the public header cannot be read or be updated
-
incrementHopCount
public void incrementHopCount() throws InvalidMessageFormatException
- Throws:
InvalidMessageFormatException
- if the public header cannot be read or be updated
-
getPublicHeaderAndRemainingBytes
public Pair<Protocol.PublicHeader,io.netty.buffer.ByteBuf> getPublicHeaderAndRemainingBytes() throws InvalidMessageFormatException
Reads the public header of the given message and returns aByteBuf
with all remaining bytes.Note: The given
ByteBuf
is inherited fromgetInternalByteBuf()
and will therefore use the same reference counter. Meaning that releasinggetInternalByteBuf()
will invalidate the returnedByteBuf
and vice versa.- Returns:
Pair
with public header as first element andByteBuf
with all remaining bytes as second element- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
setAgreementId
public RemoteEnvelope<T> setAgreementId(AgreementId agreementId) throws InvalidMessageFormatException
- Throws:
InvalidMessageFormatException
- if the public header cannot be read or be updated
-
arm
public RemoteEnvelope<T> arm(com.goterl.lazysodium.utils.SessionPair sessionPair) throws InvalidMessageFormatException
Returns an armed version of this envelope for sending it through untrustworthy channels.Arming includes the following steps:
getPublicHeader()
will be added as authentication tagprivateHeader
andbody
will be encrypted withsessionPair
Note: Do not forget to release the original
message
- Parameters:
sessionPair
- will be used for encryption- Returns:
- the armed version of this envelope
- Throws:
InvalidMessageFormatException
- if arming was not possible
-
armAndRelease
public RemoteEnvelope<T> armAndRelease(com.goterl.lazysodium.utils.SessionPair sessionPair) throws InvalidMessageFormatException
Returns an armed version of this envelope for sending it through untrustworthy channels.Arming includes the following steps:
getPublicHeader()
will be added as authentication tagprivateHeader
andbody
will be encrypted withsessionPair
This method will release all resources even in case of an exception.
- Parameters:
sessionPair
- will be used for encryption- Returns:
- the armed version of this envelope
- Throws:
InvalidMessageFormatException
- if arming was not possible
-
disarm
public RemoteEnvelope<T> disarm(com.goterl.lazysodium.utils.SessionPair sessionPair) throws InvalidMessageFormatException
Returns a disarmed version of this envelope.secretKey
must match message's recipient (not implemented yet!).Disarming includes the following steps:
- the encrypted
privateHeader
and encryptedbody
will be decrypted withsecretKey
(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:
InvalidMessageFormatException
- if disarming was not possible
- the encrypted
-
disarmAndRelease
public RemoteEnvelope<T> disarmAndRelease(com.goterl.lazysodium.utils.SessionPair sessionPair) throws InvalidMessageFormatException
Returns a disarmed version of this envelope.secretKey
must match message's recipient (not implemented yet!).Disarming includes the following steps:
- the encrypted
privateHeader
and encryptedbody
will be decrypted withsecretKey
(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:
InvalidMessageFormatException
- if disarming was not possible
- the encrypted
-
acknowledgement
public static RemoteEnvelope<Protocol.Acknowledgement> acknowledgement(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, Nonce correspondingId)
Creates newProtocol.Acknowledgement
message (reply toProtocol.Discovery
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- Throws:
NullPointerException
- ifsender
,proofOfWork
,recipient
, orcorrespondingId
isnull
-
application
public static RemoteEnvelope<Protocol.Application> application(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, String type, byte[] payload)
Creates newProtocol.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- Throws:
NullPointerException
- ifsender
,proofOfWork
,recipient
, orpayload
isnull
-
discovery
public static RemoteEnvelope<Protocol.Discovery> discovery(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, long joinTime)
Creates a newProtocol.Discovery
message (sent byInternetDiscovery
).- 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.- Throws:
NullPointerException
- ifsender
,proofOfWork
, orrecipient
isnull
-
discovery
public static RemoteEnvelope<Protocol.Discovery> discovery(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork)
Creates a new multicastProtocol.Discovery
message (sent byLocalNetworkDiscovery
}.- Parameters:
networkId
- the network of the joining nodesender
- the public key of the joining nodeproofOfWork
- the proof of work- Throws:
NullPointerException
- ifsender
, orproofOfWork
isnull
-
unite
public static RemoteEnvelope<Protocol.Unite> unite(int networkId, IdentityPublicKey sender, ProofOfWork proofOfWork, IdentityPublicKey recipient, IdentityPublicKey publicKey, InetSocketAddress address)
Creates a newProtocol.Unite
message (sent byInternetDiscovery
; used for UDP hole punching}.- Parameters:
networkId
- the network of the super peer nodesender
- the public key of the super peer nodeproofOfWork
- the super peer's proof of workrecipient
- the recipient of this messagepublicKey
- the public key of the node with which the receiver should uniteaddress
- theInetSocketAddress
of the node with which the receiver should unite- Throws:
NullPointerException
- ifsender
,proofOfWork
,recipient
,publicKey
, oraddress
isnull
IllegalArgumentException
- ifaddress
is unresolved
-
keyExchange
public static RemoteEnvelope<Protocol.KeyExchange> keyExchange(int networkId, IdentityPublicKey sender, ProofOfWork identityProofOfWork, IdentityPublicKey recipient, KeyAgreementPublicKey sessionKey)
Creates a newProtocol.KeyExchange
message (sent byArmHandler
; used for perfect forwarded secrecy).- Parameters:
networkId
- the network of the nodesender
- the public key of the nodeidentityProofOfWork
- the node's proof of workrecipient
- the recipient of this messagesessionKey
- the ephemeral key agreement key- Returns:
Protocol.KeyExchange
message or exception- Throws:
NullPointerException
- ifsender
,identityProofOfWork
,recipient
,sessionKey
, orsessionProofOfWork
isnull
-
keyExchangeAcknowledgement
public static RemoteEnvelope<Protocol.KeyExchangeAcknowledgement> keyExchangeAcknowledgement(int networkId, IdentityPublicKey sender, ProofOfWork identityProofOfWork, IdentityPublicKey recipient, AgreementId agreementId)
Creates a newProtocol.KeyExchangeAcknowledgement
message (sent byArmHandler
; used for perfect forwarded secrecy).- Parameters:
networkId
- the network of the nodesender
- the public key of the nodeidentityProofOfWork
- the node's proof of workrecipient
- the recipient of this messageagreementId
- the id of the used session- Returns:
Protocol.KeyExchangeAcknowledgement
message or exception- Throws:
NullPointerException
- ifsender
,identityProofOfWork
,recipient
, orkeyId
isnull
-
isChunk
public boolean isChunk() throws InvalidMessageFormatException
Returnstrue
if this message is a chunk. Otherwisefalse
is returned.- Returns:
true
if this message is a chunk. Otherwisefalse
.- Throws:
InvalidMessageFormatException
- if the public header cannot be read
-
getChunkNo
public UnsignedShort getChunkNo() throws InvalidMessageFormatException
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:
InvalidMessageFormatException
- if the public header cannot be read
-
getTotalChunks
public UnsignedShort getTotalChunks() throws InvalidMessageFormatException
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:
InvalidMessageFormatException
- if the public header cannot be read
-
-