Package io.permazen.kv.raft.msg
Class Message
java.lang.Object
io.permazen.kv.raft.msg.Message
- Direct Known Subclasses:
AppendRequest
,AppendResponse
,CommitRequest
,CommitResponse
,GrantVote
,InstallSnapshot
,PingRequest
,PingResponse
,RequestVote
Support superclass for Raft messages.
-
Constructor Summary
ModifierConstructorDescriptionprotected
protected
Message
(byte type, ByteBuffer buf, int version) -
Method Summary
Modifier and TypeMethodDescriptionprotected int
calculateSize
(int version) Calculate the number of bytes required bywriteTo()
.protected static int
calculateSize
(Timestamp timestamp, int version) protected static int
calculateSize
(String string) protected static int
calculateSize
(ByteBuffer buf) static Message
decode
(ByteBuffer buf, int version) Decode a message from the given input.static int
Read the protocol version number header from the message encoded in the given input.encode
(int version) Serialize this instance.protected static boolean
getBoolean
(ByteBuffer buf) Deserialize a boolean value previously serialized byputBoolean()
from the buffer.protected static ByteBuffer
getByteBuffer
(ByteBuffer buf) Deserialize aByteBuffer
previously serialized byputByteBuffer()
from the buffer.int
Get the cluster ID of the sender.static int
Get the current (i.e., maximum known) protocol version number.Get the identity of the recipient.Get the identity of the sender.protected static String
getString
(ByteBuffer buf) Deserialize aString
previously serialized byputString()
from the buffer.long
getTerm()
Get the term of the sender of this message.protected static Timestamp
getTimestamp
(ByteBuffer buf, int version) Deserialize aTimestamp
value previously serialized byputTimestamp()
from the buffer.boolean
Determine whether this message is only sent by leaders.protected static void
putBoolean
(ByteBuffer dest, boolean value) Serialize a boolean value into the buffer.protected static void
putByteBuffer
(ByteBuffer dest, ByteBuffer buf) Serialize aByteBuffer
into the buffer.protected static void
putString
(ByteBuffer dest, String string) Serialize aString
into the buffer.protected static void
putTimestamp
(ByteBuffer dest, Timestamp timestamp, int version) Serialize aTimestamp
value into the buffer.abstract String
toString()
abstract void
visit
(MessageSwitch handler) Apply the visitor pattern based on this instance's type.void
writeTo
(ByteBuffer buf, int version) Serialize this instance into the given buffer.
-
Constructor Details
-
Message
-
Message
-
-
Method Details
-
getClusterId
public int getClusterId()Get the cluster ID of the sender.- Returns:
- sender's cluster ID
-
getSenderId
Get the identity of the sender.- Returns:
- sender's unique identity string
-
getRecipientId
Get the identity of the recipient.- Returns:
- recipient's unique identity string
-
getTerm
public long getTerm()Get the term of the sender of this message.- Returns:
- requester's unique ID
-
isLeaderMessage
public boolean isLeaderMessage()Determine whether this message is only sent by leaders.- Returns:
- true if receipt of this message implies sender is a leader
-
visit
Apply the visitor pattern based on this instance's type.- Parameters:
handler
- target for visit
-
getCurrentProtocolVersion
public static int getCurrentProtocolVersion()Get the current (i.e., maximum known) protocol version number.- Returns:
- latest message protocol version
-
decodeProtocolVersion
Read the protocol version number header from the message encoded in the given input.- Parameters:
buf
- source for encoded message- Returns:
- decoded message protocol version
-
decode
Decode a message from the given input. The protocol version number header should already have been read.Note that data is not necessarily copied out of
buf
, so the returned instance may become invalid if the data inbuf
gets overwritten.- Parameters:
buf
- source for encoded messageversion
- message version- Returns:
- decoded message
- Throws:
BufferUnderflowException
- if there is not enough dataIllegalArgumentException
- ifversion
is bogusIllegalArgumentException
- if encoded message is bogusIllegalArgumentException
- if there is trailing garbage
-
encode
Serialize this instance.- Parameters:
version
- protocol encoding version number- Returns:
- encoded message
- Throws:
IllegalArgumentException
- ifversion
is bogus
-
writeTo
Serialize this instance into the given buffer.- Parameters:
buf
- destination for encoded dataversion
- protocol encoding version number- Throws:
BufferOverflowException
- if data overflowsbuf
IllegalArgumentException
- ifversion
is bogus
-
calculateSize
protected int calculateSize(int version) Calculate the number of bytes required bywriteTo()
.- Parameters:
version
- protocol encoding version- Returns:
- an upper bound on the number of encoded bytes
-
toString
-
putByteBuffer
Serialize aByteBuffer
into the buffer.- Parameters:
dest
- destination for encoded databuf
- data to encode- Throws:
ReadOnlyBufferException
- ifdest
is read onlyBufferOverflowException
- ifdest
overflowsIllegalArgumentException
- ifbuf
has more than 2^31 bytes remainingIllegalArgumentException
- if either parameter is null
-
getByteBuffer
Deserialize aByteBuffer
previously serialized byputByteBuffer()
from the buffer.- Parameters:
buf
- source for encoded data- Returns:
- decoded data
- Throws:
BufferUnderflowException
- ifbuf
underflowsIllegalArgumentException
- if input is bogusIllegalArgumentException
- ifbuf
is null
-
calculateSize
-
putString
Serialize aString
into the buffer.- Parameters:
dest
- destination for encoded datastring
- string to encode- Throws:
ReadOnlyBufferException
- ifdest
is read onlyBufferOverflowException
- ifdest
overflowsIllegalArgumentException
- if either parameter is null
-
getString
Deserialize aString
previously serialized byputString()
from the buffer.- Parameters:
buf
- source for encoded data- Returns:
- decoded string, never null
- Throws:
BufferUnderflowException
- ifbuf
underflowsIllegalArgumentException
- if input is bogus
-
calculateSize
-
putBoolean
Serialize a boolean value into the buffer.- Parameters:
dest
- destination for encoded datavalue
- value to encode- Throws:
ReadOnlyBufferException
- ifdest
is read onlyBufferOverflowException
- ifdest
overflowsIllegalArgumentException
- ifdest
is null
-
getBoolean
Deserialize a boolean value previously serialized byputBoolean()
from the buffer.- Parameters:
buf
- source for encoded data- Returns:
- decoded value
- Throws:
BufferUnderflowException
- ifbuf
underflowsIllegalArgumentException
- if input is bogus
-
putTimestamp
Serialize aTimestamp
value into the buffer.- Parameters:
dest
- destination for encoded datatimestamp
- value to encodeversion
- protocol encoding version- Throws:
ReadOnlyBufferException
- ifdest
is read onlyBufferOverflowException
- ifdest
overflowsIllegalArgumentException
- ifdest
ortimestamp
is null
-
getTimestamp
Deserialize aTimestamp
value previously serialized byputTimestamp()
from the buffer.- Parameters:
buf
- source for encoded dataversion
- protocol encoding version- Returns:
- decoded value
- Throws:
BufferUnderflowException
- ifbuf
underflowsIllegalArgumentException
- if input is bogus
-
calculateSize
-