public interface Pipe
Ports between Modules.
Each Casing provides eight ports to each installed module, each
pair of modules sharing two pipes, going in both directions. This abstracts
data transfer between modules.
Pipes operate in a synchronized manner, ensuring that when two modules
start reading and writing on a pipe between them in the same update step,
there is no execution-order-dependent behavior: the pipe requires a read
and write operation to be started via beginRead() and
beginWrite(short), respectively, and will then, after the
modules finished their update for the current step switch to a transferring
state, so that the modules can finish the operation in the next update.
A transfer is completed by the receiver calling read(), which will
reset the pipe and call Module.onWriteComplete(Port) on the writing
module.
So in short, a full transfer cycle looks like this:
canTransfer() being true.read().Module.onWriteComplete(Port) method called.
Note that at any time during the transfer cycle, one of the two parties may
cancel their reading or writing operation by calling cancelRead()
or cancelWrite(), respectively. The other part of the transfer will
not be explicitly notified of this change, but this can be checked
using the isReading() and isWriting() methods.
| Modifier and Type | Method and Description |
|---|---|
void |
beginRead()
Begin a reading operation on the pipe.
|
void |
beginWrite(short value)
Begin a writing operation on the pipe.
|
void |
cancelRead()
Cancel an active read operation.
|
void |
cancelWrite()
Cancel an active write operation.
|
boolean |
canTransfer()
Whether the pipe can transfer data, i.e.
|
boolean |
isReading()
Whether the pipe is currently being read from.
|
boolean |
isWriting()
Whether the pipe is currently being written to.
|
short |
read()
Finish a read operation by fetching the data that is written by the
write operation running at the same time.
|
void beginWrite(short value)
throws java.lang.IllegalStateException
Make sure not to call this if the pipe is already being written to, or
an exception will be thrown. Use isWriting() to check for this.
value - the value to write to the pipe.java.lang.IllegalStateException - if the pipe is already being written to.void cancelWrite()
If the pipe is not currently being written to this does nothing.
boolean isWriting()
This is true as soon as beginWrite(short) was called and
until read() or cancelWrite() was called to finish or
cancel the operation.
void beginRead()
throws java.lang.IllegalStateException
Make sure not to call this if the pipe is already being read from, or
an exception will be thrown. Use isReading() to check for this.
java.lang.IllegalStateException - if the pipe is already being read from.void cancelRead()
If the pipe is not currently being read from this does nothing.
boolean isReading()
This is true as soon as beginRead() was called and
until read() or cancelRead() was called to finish or
cancel the operation.
boolean canTransfer()
read() can be called.
Note that this is not equivalent to isReading() &&
isWriting(). This is only true in the first update
after that has been true and later.
short read()
throws java.lang.IllegalStateException
Make sure not to call this if the pipe is not in the correct state, or
an exception will be thrown. use canTransfer() to check for this.
This will cause Module.onWriteComplete(Port) to be called on the
module currently writing to the pipe, then reset the pipe (i.e. call
cancelRead() and cancelWrite()) prior to returning the
read value.
java.lang.IllegalStateException - if the pipe is in an incorrect state.