The MLLPDriver

The MLLPDriver handles the MLLP protocol. For example, given a connected Socket (or any other pair of Input/OutputStreams (e.g. System.in/.out) the MLLPDriver is used as follows:

MLLPDriver driver = new MLLPDriver(socket.getInputStream(),
                                   socket.get.OutputStream()
                                   true);

the MLLPDriver then provides a pair of MLLP driven Input- and OutputStreams? (sort of proxy-streams)

driver.getInputStream();
driver.getOutputStream();

The driver manages the MLLP half-duplex state but does so pretty transparently and without much annoying exceptions.

The boolean argument to the constructor specifies whether this driver starts out as a sender or as a receiver. This argument is called the "clearToSend". Once a line is clear to send, as soon as the first write to the output stream, the MLLP start- of-transmission control is sent.

When the driver is in sending state the first attempt to read on the input stream terminates the sending state (end-of- transmission sequence is sent) and skips forward to immediately behind the next start-of-transmission control character received from the peer.

When in receiving mode, the driver transparently buffers any output sent wile the MLLP state does not allow for writing because the peer is not done writing. Then, when the peer has finished writing, the buffered output is sent. Once the driver is in sending state, all further output is directly sent to the real output stream, without further buffering.

In order to terminate sending without immediately attempting to receive data, one can explicitly call

driver.turn();

to turn over the connection to the peer.

In order to find out whether a new message is available, one can call

driver.hasMoreInput() 

which blocks if no more input is available or if the stream is closed. If the client closes the input, this results in driver.hasMoreInput() to return false. A closed input channel can also be detected with the predicate

driver.inputClosed()

that returns true is the input is closed.

The fact that a receiver is done sending a message is apparent from the input stream that keeps returning -1 (the EOF mark). Only after the driver sent something (or alternatively when driver.turn() has been called without anything sent, resulting in an empty transmission frame), only then will a subsequent read return actual data.

Before abandoning a client driver, do close it so that it will close and free the server.

driver.close();