ConnectionPayload
- type of the payload for the connectionMessagePayload
- type of the payload for each messagepublic abstract class MultiplexedChannelHandler<ConnectionPayload,MessagePayload> extends ChannelHandler
ChannelHandler
on top of a single subscription and
multiplexes all reads on top of it.
This abstract handler takes care of forwarding the connection and message events of a single connection to multiple readers and writers. One needs to:
connect()
and disconnect()
function
to add the protocol specific connection and disconnection logic; the resources
shared across multiple channels should be left in the datasourceprocessConnection(java.lang.Object)
,
which will trigger the proper connection notification mechanism;
the type chosen as connection payload should be one that stores all the
information about the channel of communicationsprocessMessage(java.lang.Object)
, which
will trigger the proper value notification mechanismisConnected(java.lang.Object)
and isWriteConnected(java.lang.Object)
with the logic to extract the connection information from the connection payloadreportExceptionToAllReadersAndWriters(java.lang.Exception)
to report errorsDataSourceTypeAdapter
that can convert
the payload to types for pvmanager consumption; the connection payload and
message payload never leave this handler, only value types created by the
type adaptersConstructor and Description |
---|
MultiplexedChannelHandler(String channelName)
Creates a new channel handler.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addReader(ChannelHandlerReadSubscription subscription)
Used by the data source to add a read request on the channel managed
by this handler.
|
protected void |
addWriter(ChannelHandlerWriteSubscription subscription)
Used by the data source to prepare the channel managed by this handler
for write.
|
protected abstract void |
connect()
Used by the handler to open the connection.
|
protected abstract void |
disconnect()
Used by the handler to close the connection.
|
protected DataSourceTypeAdapter<ConnectionPayload,MessagePayload> |
findTypeAdapter(ValueCache<?> cache,
ConnectionPayload connection)
Finds the right adapter to use for the particular cache given the information
of the channels in the connection payload.
|
protected ConnectionPayload |
getConnectionPayload()
The last processes connection payload.
|
protected MessagePayload |
getLastMessagePayload()
The last processed message payload.
|
int |
getReadUsageCounter()
Returns how many readers are open on this channel.
|
int |
getUsageCounter()
Returns how many readers or writers are open on
this channel.
|
int |
getWriteUsageCounter()
Returns how many writers are open on this channel.
|
boolean |
isConnected()
Returns true if it is connected.
|
protected boolean |
isConnected(ConnectionPayload payload)
Determines from the payload whether the channel is connected or not.
|
boolean |
isWriteConnected()
Returns true if it is channel can be written to.
|
protected boolean |
isWriteConnected(ConnectionPayload payload)
Determines from the payload whether the channel can be written to.
|
protected void |
processConnection(ConnectionPayload connectionPayload)
Process the next connection payload.
|
protected void |
processMessage(MessagePayload payload)
Process the payload for this channel.
|
protected void |
removeReader(ChannelHandlerReadSubscription subscription)
Used by the data source to remove a read request.
|
protected void |
removeWrite(ChannelHandlerWriteSubscription subscription)
Used by the data source to conclude writes to the channel managed by this handler.
|
protected void |
reportExceptionToAllReadersAndWriters(Exception ex)
Notifies all readers and writers of an error condition.
|
protected void |
reportExceptionToAllWriters(Exception ex)
Notifies all writers of an error condition.
|
protected void |
resetMessage()
Resets the last message to null.
|
protected boolean |
saveMessageAfterDisconnect()
Signals whether the last message received after the disconnect should
be kept so that it is available at reconnect.
|
protected void |
setProcessMessageOnDisconnect(boolean processMessageOnDisconnect)
Determines whether
processConnection(java.lang.Object) should
trigger processMessage(java.lang.Object) with the same (non-null)
payload in case the channel has been disconnected. |
protected void |
setProcessMessageOnReconnect(boolean processMessageOnReconnect)
Determines whether
processConnection(java.lang.Object) should
trigger processMessage(java.lang.Object) with the same (non-null)
payload in case the channel has reconnected. |
protected abstract void |
write(Object newValue,
ChannelWriteCallback callback)
Implements a write operation.
|
getChannelName, getProperties
public MultiplexedChannelHandler(String channelName)
channelName
- the name of the channel this handler will be responsible ofprotected final void reportExceptionToAllReadersAndWriters(Exception ex)
ex
- the exception to notifyprotected final void reportExceptionToAllWriters(Exception ex)
ex
- the exception to notifyprotected final ConnectionPayload getConnectionPayload()
protected final MessagePayload getLastMessagePayload()
protected final void processConnection(ConnectionPayload connectionPayload)
connectionPayload
- protected DataSourceTypeAdapter<ConnectionPayload,MessagePayload> findTypeAdapter(ValueCache<?> cache, ConnectionPayload connection)
DataSourceTypeSupport
as
a good first implementation.cache
- the cache that will store the dataconnection
- the connection payloadpublic int getUsageCounter()
ChannelHandler
getUsageCounter
in class ChannelHandler
public int getReadUsageCounter()
ChannelHandler
getReadUsageCounter
in class ChannelHandler
public int getWriteUsageCounter()
ChannelHandler
getWriteUsageCounter
in class ChannelHandler
protected void addReader(ChannelHandlerReadSubscription subscription)
ChannelHandler
addReader
in class ChannelHandler
subscription
- the data required for a subscriptionprotected void removeReader(ChannelHandlerReadSubscription subscription)
ChannelHandler
removeReader
in class ChannelHandler
subscription
- the subscription to removeprotected void addWriter(ChannelHandlerWriteSubscription subscription)
ChannelHandler
addWriter
in class ChannelHandler
subscription
- the data required for the subscriptionprotected void removeWrite(ChannelHandlerWriteSubscription subscription)
ChannelHandler
removeWrite
in class ChannelHandler
subscription
- the subscription to removeprotected final void resetMessage()
protected final void processMessage(MessagePayload payload)
DataSourceTypeAdapter
for each read monitor that was setup.payload
- the payload of for this type of channelprotected boolean saveMessageAfterDisconnect()
By default, the message is discarded so that no memory is kept allocated.
protected abstract void connect()
protected abstract void disconnect()
protected abstract void write(Object newValue, ChannelWriteCallback callback)
ChannelHandler
write
in class ChannelHandler
newValue
- new value to be writtencallback
- called when done or on errorprotected boolean isConnected(ConnectionPayload payload)
By default, this uses the usage counter to determine whether it's connected or not. One should override this to use the actual connection payload to check whether the actual protocol connection has been established.
payload
- the connection payloadprotected boolean isWriteConnected(ConnectionPayload payload)
By default, this always return false. One should override this if it's implementing a write-able data source.
payload
- public final boolean isConnected()
ChannelHandler
isConnected
in class ChannelHandler
public final boolean isWriteConnected()
protected final void setProcessMessageOnDisconnect(boolean processMessageOnDisconnect)
processConnection(java.lang.Object)
should
trigger processMessage(java.lang.Object)
with the same (non-null)
payload in case the channel has been disconnected. Default is true.processMessageOnDisconnect
- whether to process the message on disconnectprotected final void setProcessMessageOnReconnect(boolean processMessageOnReconnect)
processConnection(java.lang.Object)
should
trigger processMessage(java.lang.Object)
with the same (non-null)
payload in case the channel has reconnected. Default is true.processMessageOnReconnect
- whether to process the message on disconnectCopyright © 2010–2015. All rights reserved.