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.
Parameter | |
---|---|
Name | Description |
input | DataInputStream The data stream containing the user request. |
Returns | |
---|---|
Type | Description |
byte[][] | A byte array of user-defined parameters to be bound. |
Exceptions | |
---|---|
Type | Description |
Exception | If reading fails in any way. |
Constructors
WireMessage(ConnectionHandler connection, int length)
public WireMessage(ConnectionHandler connection, int length)
Parameters | |
---|---|
Name | Description |
connection | ConnectionHandler |
length | int |
Fields
connection
protected ConnectionHandler connection
Field Value | |
---|---|
Type | Description |
ConnectionHandler |
inputStream
protected DataInputStream inputStream
Field Value | |
---|---|
Type | Description |
DataInputStream |
length
protected int length
Field Value | |
---|---|
Type | Description |
int |
outputStream
protected DataOutputStream outputStream
Field Value | |
---|---|
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.
Returns | |
---|---|
Type | Description |
int | The remaining length to be processed once "header" information is processed. |
getIdentifier()
public abstract String getIdentifier()
Used for logging.
Returns | |
---|---|
Type | Description |
String | Message Identifier (int for Bootstrap, char otherwise). |
getMessageName()
protected abstract String getMessageName()
Used for logging.
Returns | |
---|---|
Type | Description |
String | The official name of the wire message. |
getPayloadString()
protected abstract String getPayloadString()
Used for logging.
Returns | |
---|---|
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.
Exceptions | |
---|---|
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.
Parameter | |
---|---|
Name | Description |
length | int The number of bytes to read. |
Returns | |
---|---|
Type | Description |
String | the string. |
Exceptions | |
---|---|
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.
Returns | |
---|---|
Type | Description |
String | The remainder of the message in String format. |
Exceptions | |
---|---|
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.
Returns | |
---|---|
Type | Description |
String | the string. |
Exceptions | |
---|---|
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.
Exceptions | |
---|---|
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.
Exceptions | |
---|---|
Type | Description |
Exception | If any step in output message fails. |
toString()
public String toString()
Returns | |
---|---|
Type | Description |
String |