Class WireMessage (0.7.0)

public abstract class WireMessage

Generic representation for a wire message, generally executed by calling send.

Inheritance

java.lang.Object > WireMessage

Static Methods

getParameters(DataInputStream input)

protected static byte[][] getParameters(DataInputStream input)

Extracts parameters/arguments from the user input.

Parameter
NameDescription
inputDataInputStream

The data stream containing the user request.

Returns
TypeDescription
byte[][]

A byte array of user-defined parameters to be bound.

Exceptions
TypeDescription
Exception

If reading fails in any way.

Constructors

WireMessage(ConnectionHandler connection, int length)

public WireMessage(ConnectionHandler connection, int length)
Parameters
NameDescription
connectionConnectionHandler
lengthint

Fields

connection

protected ConnectionHandler connection
Field Value
TypeDescription
ConnectionHandler

inputStream

protected DataInputStream inputStream
Field Value
TypeDescription
DataInputStream

length

protected int length
Field Value
TypeDescription
int

outputStream

protected DataOutputStream outputStream
Field Value
TypeDescription
DataOutputStream

Methods

getHeaderLength()

protected int getHeaderLength()

How many bytes is taken by the payload header. Header is defined here as protocol definition + length. Most common value here is four bytes, so we keep that as default. Effectively, this is how much of the message you "don't" want to read from the message's total length with readAll.

Returns
TypeDescription
int

The remaining length to be processed once "header" information is processed.

getIdentifier()

protected abstract String getIdentifier()

Used for logging.

Returns
TypeDescription
String

Message Identifier (int for Bootstrap, char otherwise).

getMessageName()

protected abstract String getMessageName()

Used for logging.

Returns
TypeDescription
String

The official name of the wire message.

getPayloadString()

protected abstract String getPayloadString()

Used for logging.

Returns
TypeDescription
String

Payload metadata.

nextHandler()

public void nextHandler()

Some messages may have some more context and require some order. This handles state machine setting for ConnectionHandler.

Exceptions
TypeDescription
Exception

read(int length)

public String read(int length)

Reads a fixed-length string from the saved stream. The string still needs to be null-terminated, but the read is more efficient, as we can read the entire string in one go instead of continuously checking for a null-terminator.

Parameter
NameDescription
lengthint

The number of bytes to read.

Returns
TypeDescription
String

the string.

Exceptions
TypeDescription
IOException

if an error occurs while reading from the stream, or if no null-terminator is found at the end of the string.

readAll()

protected String readAll()

Read the remainder of the received message. To be called once metadata for the message is read. Metadata is designated as type, length, etc.

Returns
TypeDescription
String

The remainder of the message in String format.

Exceptions
TypeDescription
Exception

If reading fails in any way.

readString()

public String readString()

Reads a null-terminated string from a DataInputStream. Note that though existing solutions for this exist, they are either not keyed exactly for our use case, or would lead to a more combersome addition to this codebase. Also note the 128 byte length is chosen from profiling and determining that it exceeds the 90th percentile size for inbound messages.

Returns
TypeDescription
String

the string.

Exceptions
TypeDescription
IOException

if an error occurs while reading from the stream, or if no null-terminator is found before the end of the stream.

send()

public void send()

Once the message is ready, call this to send it across the wire. Effectively a template pattern.

Exceptions
TypeDescription
Exception

If the sending fails.

sendPayload()

protected abstract void sendPayload()

Override this method to include post-processing and metadata in the sending process. Template method for send.

Exceptions
TypeDescription
Exception

If any step in output message fails.

toString()

public String toString()
Returns
TypeDescription
String
Overrides