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
ConstructorsModifierConstructorDescriptionprotectedprotectedMessage(byte type, ByteBuffer buf, int version) -
Method Summary
Modifier and TypeMethodDescriptionprotected intcalculateSize(int version) Calculate the number of bytes required bywriteTo().protected static intcalculateSize(Timestamp timestamp, int version) protected static intcalculateSize(String string) protected static intcalculateSize(ByteBuffer buf) static Messagedecode(ByteBuffer buf, int version) Decode a message from the given input.static intRead the protocol version number header from the message encoded in the given input.encode(int version) Serialize this instance.protected static booleangetBoolean(ByteBuffer buf) Deserialize a boolean value previously serialized byputBoolean()from the buffer.protected static ByteBuffergetByteBuffer(ByteBuffer buf) Deserialize aByteBufferpreviously serialized byputByteBuffer()from the buffer.intGet the cluster ID of the sender.static intGet the current (i.e., maximum known) protocol version number.Get the identity of the recipient.Get the identity of the sender.protected static StringgetString(ByteBuffer buf) Deserialize aStringpreviously serialized byputString()from the buffer.longgetTerm()Get the term of the sender of this message.protected static TimestampgetTimestamp(ByteBuffer buf, int version) Deserialize aTimestampvalue previously serialized byputTimestamp()from the buffer.booleanDetermine whether this message is only sent by leaders.protected static voidputBoolean(ByteBuffer dest, boolean value) Serialize a boolean value into the buffer.protected static voidputByteBuffer(ByteBuffer dest, ByteBuffer buf) Serialize aByteBufferinto the buffer.protected static voidputString(ByteBuffer dest, String string) Serialize aStringinto the buffer.protected static voidputTimestamp(ByteBuffer dest, Timestamp timestamp, int version) Serialize aTimestampvalue into the buffer.abstract StringtoString()abstract voidvisit(MessageSwitch handler) Apply the visitor pattern based on this instance's type.voidwriteTo(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 inbufgets overwritten.- Parameters:
buf- source for encoded messageversion- message version- Returns:
- decoded message
- Throws:
BufferUnderflowException- if there is not enough dataIllegalArgumentException- ifversionis 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- ifversionis bogus
-
writeTo
Serialize this instance into the given buffer.- Parameters:
buf- destination for encoded dataversion- protocol encoding version number- Throws:
BufferOverflowException- if data overflowsbufIllegalArgumentException- ifversionis 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 aByteBufferinto the buffer.- Parameters:
dest- destination for encoded databuf- data to encode- Throws:
ReadOnlyBufferException- ifdestis read onlyBufferOverflowException- ifdestoverflowsIllegalArgumentException- ifbufhas more than 2^31 bytes remainingIllegalArgumentException- if either parameter is null
-
getByteBuffer
Deserialize aByteBufferpreviously serialized byputByteBuffer()from the buffer.- Parameters:
buf- source for encoded data- Returns:
- decoded data
- Throws:
BufferUnderflowException- ifbufunderflowsIllegalArgumentException- if input is bogusIllegalArgumentException- ifbufis null
-
calculateSize
-
putString
Serialize aStringinto the buffer.- Parameters:
dest- destination for encoded datastring- string to encode- Throws:
ReadOnlyBufferException- ifdestis read onlyBufferOverflowException- ifdestoverflowsIllegalArgumentException- if either parameter is null
-
getString
Deserialize aStringpreviously serialized byputString()from the buffer.- Parameters:
buf- source for encoded data- Returns:
- decoded string, never null
- Throws:
BufferUnderflowException- ifbufunderflowsIllegalArgumentException- if input is bogus
-
calculateSize
-
putBoolean
Serialize a boolean value into the buffer.- Parameters:
dest- destination for encoded datavalue- value to encode- Throws:
ReadOnlyBufferException- ifdestis read onlyBufferOverflowException- ifdestoverflowsIllegalArgumentException- ifdestis null
-
getBoolean
Deserialize a boolean value previously serialized byputBoolean()from the buffer.- Parameters:
buf- source for encoded data- Returns:
- decoded value
- Throws:
BufferUnderflowException- ifbufunderflowsIllegalArgumentException- if input is bogus
-
putTimestamp
Serialize aTimestampvalue into the buffer.- Parameters:
dest- destination for encoded datatimestamp- value to encodeversion- protocol encoding version- Throws:
ReadOnlyBufferException- ifdestis read onlyBufferOverflowException- ifdestoverflowsIllegalArgumentException- ifdestortimestampis null
-
getTimestamp
Deserialize aTimestampvalue previously serialized byputTimestamp()from the buffer.- Parameters:
buf- source for encoded dataversion- protocol encoding version- Returns:
- decoded value
- Throws:
BufferUnderflowException- ifbufunderflowsIllegalArgumentException- if input is bogus
-
calculateSize
-