Class MultiplexedChannelHandler<ConnectionPayload,MessagePayload>
- java.lang.Object
-
- org.epics.gpclient.datasource.ChannelHandler
-
- org.epics.gpclient.datasource.MultiplexedChannelHandler<ConnectionPayload,MessagePayload>
-
- Type Parameters:
ConnectionPayload
- type of the payload for the connectionMessagePayload
- type of the payload for each message
public abstract class MultiplexedChannelHandler<ConnectionPayload,MessagePayload> extends ChannelHandler
Implements aChannelHandler
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:
- implement the
connect()
anddisconnect()
function to add the protocol specific connection and disconnection logic; the resources shared across multiple channels should be left in the datasource - every time the connection state changes, call
processConnection(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 communications - every time an event is sent, call
processMessage(java.lang.Object)
, which will trigger the proper value notification mechanism - implement
isConnected(java.lang.Object)
andisWriteConnected(java.lang.Object)
with the logic to extract the connection information from the connection payload - use
reportExceptionToAllReadersAndWriters(java.lang.Exception)
to report errors - implement a set of
DataSourceTypeAdapter
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 adapters
- Author:
- carcassi
-
-
Constructor Summary
Constructors Constructor Description MultiplexedChannelHandler(String channelName)
Creates a new channel handler.MultiplexedChannelHandler(String channelName, boolean readOnly)
Creates a new channel handler.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addReader(ReadCollector subscription)
Starts sending read notification to the given collector.protected void
addWriter(WriteCollector subscription)
Starts sending/receiving write notification to the given collector.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(ReadCollector<?,?> 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
processWriteRequest(WriteCollector.WriteRequest<?> request)
Process the write request.protected void
removeReader(ReadCollector subscription)
Stops sending read notification to the given collector.protected void
removeWriter(WriteCollector subscription)
Stops sending/receiving write notification to the given collector.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 whetherprocessConnection(java.lang.Object)
should triggerprocessMessage(java.lang.Object)
with the same (non-null) payload in case the channel has been disconnected.protected void
setProcessMessageOnReconnect(boolean processMessageOnReconnect)
Determines whetherprocessConnection(java.lang.Object)
should triggerprocessMessage(java.lang.Object)
with the same (non-null) payload in case the channel has reconnected.protected void
write(Object newValue)
Write the value.-
Methods inherited from class org.epics.gpclient.datasource.ChannelHandler
getChannelName, getProperties
-
-
-
-
Constructor Detail
-
MultiplexedChannelHandler
public MultiplexedChannelHandler(String channelName)
Creates a new channel handler.- Parameters:
channelName
- the name of the channel this handler will be responsible of
-
MultiplexedChannelHandler
public MultiplexedChannelHandler(String channelName, boolean readOnly)
Creates a new channel handler.- Parameters:
channelName
- the name of the channel this handler will be responsible ofreadOnly
- whether the channel is read-only
-
-
Method Detail
-
reportExceptionToAllReadersAndWriters
protected final void reportExceptionToAllReadersAndWriters(Exception ex)
Notifies all readers and writers of an error condition.- Parameters:
ex
- the exception to notify
-
reportExceptionToAllWriters
protected final void reportExceptionToAllWriters(Exception ex)
Notifies all writers of an error condition.- Parameters:
ex
- the exception to notify
-
getConnectionPayload
protected final ConnectionPayload getConnectionPayload()
The last processes connection payload.- Returns:
- the connection payload or null
-
getLastMessagePayload
protected final MessagePayload getLastMessagePayload()
The last processed message payload.- Returns:
- the message payload or null
-
processConnection
protected final void processConnection(ConnectionPayload connectionPayload)
Process the next connection payload. This should be called whenever the connection state has changed.- Parameters:
connectionPayload
- connection payload; not null
-
findTypeAdapter
protected DataSourceTypeAdapter<ConnectionPayload,MessagePayload> findTypeAdapter(ReadCollector<?,?> cache, ConnectionPayload connection)
Finds the right adapter to use for the particular cache given the information of the channels in the connection payload. By overriding this method a datasource can implement their own matching logic. One can use the logic provided inDataSourceTypeSupport
as a good first implementation.- Parameters:
cache
- the cache that will store the dataconnection
- the connection payload- Returns:
- the matched type adapter
-
getUsageCounter
public int getUsageCounter()
Description copied from class:ChannelHandler
Returns how many readers or writers are open on this channel.- Specified by:
getUsageCounter
in classChannelHandler
- Returns:
- the number of open readers and writers
-
getReadUsageCounter
public int getReadUsageCounter()
Description copied from class:ChannelHandler
Returns how many readers are open on this channel.- Specified by:
getReadUsageCounter
in classChannelHandler
- Returns:
- the number of open readers
-
getWriteUsageCounter
public int getWriteUsageCounter()
Description copied from class:ChannelHandler
Returns how many writers are open on this channel.- Specified by:
getWriteUsageCounter
in classChannelHandler
- Returns:
- the number of open writers
-
addReader
protected void addReader(ReadCollector subscription)
Description copied from class:ChannelHandler
Starts sending read notification to the given collector.- Specified by:
addReader
in classChannelHandler
- Parameters:
subscription
- the data collector
-
removeReader
protected void removeReader(ReadCollector subscription)
Description copied from class:ChannelHandler
Stops sending read notification to the given collector.- Specified by:
removeReader
in classChannelHandler
- Parameters:
subscription
- the data collector
-
addWriter
protected void addWriter(WriteCollector subscription)
Description copied from class:ChannelHandler
Starts sending/receiving write notification to the given collector.- Specified by:
addWriter
in classChannelHandler
- Parameters:
subscription
- the data collector
-
processWriteRequest
protected void processWriteRequest(WriteCollector.WriteRequest<?> request)
Process the write request. Override this method to implement writes asynchronously. Take the value from the request and, when the response arrives, calls eitherWriteCollector.WriteRequest.writeSuccessful()
orWriteCollector.WriteRequest.writeFailed(java.lang.Exception)
.To implement writes, either this method or
write(java.lang.Object)
should be overriden.- Parameters:
request
- the request to be processed
-
write
protected void write(Object newValue)
Write the value. Override this method to implement writes synchronously. Simply return if the write was successful or throw an exception if it wasn't. The exception is propagated up to the client code, so the error message should be short but descriptive.To implement writes, either this method or
write(java.lang.Object)
should be overriden.- Parameters:
newValue
- the new value to write.
-
removeWriter
protected void removeWriter(WriteCollector subscription)
Description copied from class:ChannelHandler
Stops sending/receiving write notification to the given collector.- Specified by:
removeWriter
in classChannelHandler
- Parameters:
subscription
- the data collector
-
resetMessage
protected final void resetMessage()
Resets the last message to null. This can be used to invalidate the last message without triggering a notification. It is useful when a reconnect should behave as the first connection.
-
processMessage
protected final void processMessage(MessagePayload payload)
Process the payload for this channel. This should be called whenever a new value needs to be processed. The handler will take care of using the correctDataSourceTypeAdapter
for each read monitor that was setup.- Parameters:
payload
- the payload of for this type of channel
-
saveMessageAfterDisconnect
protected boolean saveMessageAfterDisconnect()
Signals whether the last message received after the disconnect should be kept so that it is available at reconnect.By default, the message is discarded so that no memory is kept allocated.
- Returns:
- true if the message should be kept
-
connect
protected abstract void connect()
Used by the handler to open the connection. This is called whenever the first read or write request is made.
-
disconnect
protected abstract void disconnect()
Used by the handler to close the connection. This is called whenever the last reader or writer is de-registered.
-
isConnected
protected boolean isConnected(ConnectionPayload payload)
Determines from the payload whether the channel is connected or not.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.
- Parameters:
payload
- the connection payload- Returns:
- true if connected
-
isWriteConnected
protected boolean isWriteConnected(ConnectionPayload payload)
Determines from the payload whether the channel can be written to.By default, this always return false. One should override this if it's implementing a write-able data source.
- Parameters:
payload
- connection payload; not null- Returns:
- true if ready for writes
-
isConnected
public final boolean isConnected()
Description copied from class:ChannelHandler
Returns true if it is connected.- Specified by:
isConnected
in classChannelHandler
- Returns:
- true if underlying channel is connected
-
isWriteConnected
public final boolean isWriteConnected()
Returns true if it is channel can be written to.- Returns:
- true if underlying channel is write ready
-
setProcessMessageOnDisconnect
protected final void setProcessMessageOnDisconnect(boolean processMessageOnDisconnect)
Determines whetherprocessConnection(java.lang.Object)
should triggerprocessMessage(java.lang.Object)
with the same (non-null) payload in case the channel has been disconnected. Default is true.- Parameters:
processMessageOnDisconnect
- whether to process the message on disconnect
-
setProcessMessageOnReconnect
protected final void setProcessMessageOnReconnect(boolean processMessageOnReconnect)
Determines whetherprocessConnection(java.lang.Object)
should triggerprocessMessage(java.lang.Object)
with the same (non-null) payload in case the channel has reconnected. Default is true.- Parameters:
processMessageOnReconnect
- whether to process the message on disconnect
-
-