class Protocol
extends java.lang.Object
Protocol
is responsible for
notifying queue about I/O errors.
The reading and writing loops implement the proper callbacks for the
StateSynchronizer
implementation of bidirectional state
synchronization. This processing can be activated using the
registerSynchronizer
method.
WARNING: the core loops of the reader and writer threads are controlled by a variable which they read outside of a synchronized block. This means that it is possible (depending on the JVM implementation) to get inconsistent or corrupt results because it may be read in the middle of being changed. At this time, we are not introducing a lock object since this would cause potential contention to occur between the reader and writer threads. This is something that needs to be tested to ensure its impact is understood but it DOES need to be fixed.
Modifier and Type | Class and Description |
---|---|
class |
Protocol.ByteArrayOutputStream2
Custom version of the ByteArrayOutputStream with access to underlying buffer
|
private class |
Protocol.Reader
This helper class implements functionality of Reader thread.
|
private class |
Protocol.Writer
This helper class implements functionality of Writer thread.
|
Modifier and Type | Field and Description |
---|---|
private static SimpleCounter |
INCOMING
Counter for measuring incoming traffic
|
private static java.util.logging.Logger |
LOG
Logger.
|
private static SimpleCounter |
OUTGOING
Counter for measuring outgoing traffic
|
private static java.lang.ThreadGroup |
protoThreads
Thread group for the protocol threads.
|
private Queue |
queue
The queue representing the session with which protocol is working.
|
private java.lang.Thread |
readerThread
Reader thread.
|
private boolean |
running
Flag indicating status for Reader/Writer threads.
|
private java.lang.Object |
slock
Synchronizes access to the state synchronizer.
|
private StateSynchronizer |
ssync
The object used to implement state synchronization if enabled.
|
private java.lang.Object |
tlock
Synchronizes termination processing.
|
private static NanoTimer |
TO_BYTE_ARRAY_TIMER
timer for measuring objToByteArray time
|
private static int |
TYPE_MESSAGE
Constant identifying a
Message instance. |
private static int |
TYPE_SYNC_MESSAGE
Constant identifying a
SyncMessage instance. |
private java.lang.Thread |
writerThread
Writer thread.
|
Constructor and Description |
---|
Protocol(Queue queue)
Constructs an instance using the provided queue.
|
Modifier and Type | Method and Description |
---|---|
(package private) Message |
applyChanges(Message inbound)
If there is a registered state synchronizer and if there are any
attached state updates sent from the other side of the connection,
these updates are passed to the state synchronizer to be applied to
this node's state.
|
(package private) Message |
attachChanges(Message outbound)
Checks if there is a registered state synchronizer and if so, it is
queried to obtain the latest changes in state that must be sent to the
other side of the connection.
|
boolean |
isRunning()
Returns the operational state of this protocol driver.
|
(package private) void |
registerSynchronizer(StateSynchronizer ssync)
Sets the instance of the state synchronizer the reader and writer
threads should use, if and only if there is no other state synchronizer
already in use.
|
(package private) void |
setNames()
Force the reader and writer threads to have more descriptive names
to aid debugging.
|
(package private) void |
start()
Starts Reader and Writer threads as daemon threads.
|
(package private) void |
stop()
Stops Reader and Writer threads.
|
private void |
stopQueue(java.lang.Exception result)
Stop the queue with SilentUnwindException checking.
|
private static final SimpleCounter INCOMING
private static final SimpleCounter OUTGOING
private static final NanoTimer TO_BYTE_ARRAY_TIMER
private static final java.util.logging.Logger LOG
private static final int TYPE_MESSAGE
Message
instance.private static final int TYPE_SYNC_MESSAGE
SyncMessage
instance.private static java.lang.ThreadGroup protoThreads
private volatile boolean running
private Queue queue
private java.lang.Thread readerThread
private java.lang.Thread writerThread
private java.lang.Object slock
private StateSynchronizer ssync
private java.lang.Object tlock
Protocol(Queue queue)
queue
- Instance of the Queue
to/from which messages
are written/read.void start()
void stop()
void setNames()
public boolean isRunning()
true
if the protocol is running (operational)
and false
if the session is closed.void registerSynchronizer(StateSynchronizer ssync)
ssync
- The synchronizer to register or null
if the
currently registered synchronizer should be deregistered.Message attachChanges(Message outbound)
The state synchronization is allowed only for conversation mode or other processing which is known to be synchronous.
For async replies or requests, no state services are used, as the state is something which is required to be the same (and in sync) between both sides. The very nature of async messages may make the state to be inconsistent between the running threads.
outbound
- The message that was dequeued for transmission.Message applyChanges(Message inbound)
inbound
- The message that is about to be enqueued for processing.private void stopQueue(java.lang.Exception result)
result
- The managed exception.