private static class LowLevelSocketImpl.ReadListener
extends java.lang.Object
implements java.lang.Runnable
reader
can read some data without
blocking. If so, it will post a READ-RESPONSE event on the server (the server-side is
responsible of reaching back to the client and posting the event, as the server-side holds
the implementation).
Once a byte can be read, it will wait for a LowLevelSocketImpl.read(int, com.goldencode.p2j.util.memptr, long, long, int)
to be executed,
as it must not post more than a READ-RESPONSE event per "incoming data" case.
Modifier and Type | Field and Description |
---|---|
private boolean |
inRead
Flag indicating we are executing a READ.
|
private java.util.concurrent.CountDownLatch |
ready
The signal to use when this thread is ready.
|
private LowLevelSocketImpl.SocketData |
sd
The associated
LowLevelSocketImpl.SocketData instance. |
private boolean |
terminated
Flag indicating the listener must been terminated.
|
Constructor and Description |
---|
ReadListener(LowLevelSocketImpl.SocketData sd,
java.util.concurrent.CountDownLatch ready)
Initialize this instance.
|
Modifier and Type | Method and Description |
---|---|
void |
endRead()
Called when a REAd is ended.
|
boolean |
isTerminated()
Check if the listener thread is terminated.
|
void |
run()
Execute a loop to post READ-RESPONSE events until the
isTerminated() returns. |
void |
setTerminated(boolean terminated)
Set the
terminated flag to the specified value. |
void |
start()
Start a new thread, in which this instance will be executed.
|
void |
startRead()
Called when a READ is started.
|
private void |
waitForNotify(boolean inRead)
Wait for notification that a read has either started or ended.
|
private final LowLevelSocketImpl.SocketData sd
LowLevelSocketImpl.SocketData
instance.private boolean inRead
private boolean terminated
private java.util.concurrent.CountDownLatch ready
public ReadListener(LowLevelSocketImpl.SocketData sd, java.util.concurrent.CountDownLatch ready)
sd
- The LowLevelSocketImpl.SocketData
instance.public void setTerminated(boolean terminated)
terminated
flag to the specified value.terminated
- The new value for the terminated
flag.public boolean isTerminated()
LowLevelSocketImpl.SocketData.socket
is
closed or the terminated
flag is true.public void run()
isTerminated()
returns.
true
.
Once the loop has been terminated, a final READ-RESPONSE event will be posted.
run
in interface java.lang.Runnable
public void start()
Socket listener [%d]
, where %d
is replaced with the
LowLevelSocketImpl.SocketData.id
value.
This thread is marked as async, as all outgoing requests must be processed async, in
their own dispatcher
.
public void startRead()
inRead
flag to true and notify
all listeners.public void endRead()
inRead
flag to false and notify
all listeners.private void waitForNotify(boolean inRead)
inRead
- true
to wait for a READ start, false
to wait for a
READ end.