public abstract class WireMessage
Generic representation for a wire message, generally executed by calling send.
Static Methods
getParameters(DataInputStream input)
protected static byte[][] getParameters(DataInputStream input)
Extracts parameters/arguments from the user input.
Name | Description |
input | DataInputStream The data stream containing the user request. |
Type | Description |
byte[][] | A byte array of user-defined parameters to be bound. |
Type | Description |
Exception | If reading fails in any way. |
Constructors
WireMessage(ConnectionHandler connection, int length)
public WireMessage(ConnectionHandler connection, int length)
Name | Description |
connection | ConnectionHandler |
length | int |
Fields
connection
protected ConnectionHandler connection
Type | Description |
ConnectionHandler |
inputStream
protected DataInputStream inputStream
Type | Description |
DataInputStream |
length
protected int length
Type | Description |
int |
outputStream
protected DataOutputStream outputStream
Type | Description |
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.
Type | Description |
int | The remaining length to be processed once "header" information is processed. |
getIdentifier()
protected abstract String getIdentifier()
Used for logging.
Type | Description |
String | Message Identifier (int for Bootstrap, char otherwise). |
getMessageName()
protected abstract String getMessageName()
Used for logging.
Type | Description |
String | The official name of the wire message. |
getPayloadString()
protected abstract String getPayloadString()
Used for logging.
Type | Description |
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.
Type | Description |
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.
Name | Description |
length | int The number of bytes to read. |
Type | Description |
String | the string. |
Type | Description |
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.
Type | Description |
String | The remainder of the message in String format. |
Type | Description |
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.
Type | Description |
String | the string. |
Type | Description |
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.
Type | Description |
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.
Type | Description |
Exception | If any step in output message fails. |
toString()
public String toString()
Type | Description |
String |