org.epics.pvmanager
Class MultiplexedChannelHandler<ConnectionPayload,MessagePayload>

java.lang.Object
  extended by org.epics.pvmanager.ChannelHandler
      extended by org.epics.pvmanager.MultiplexedChannelHandler<ConnectionPayload,MessagePayload>
Type Parameters:
ConnectionPayload - type of the payload for the connection
MessagePayload - type of the payload for each message

public abstract class MultiplexedChannelHandler<ConnectionPayload,MessagePayload>
extends ChannelHandler

Implements a ChannelHandler on top of a single subscription and multiplexes all reads on top of it.

Author:
carcassi

Constructor Summary
MultiplexedChannelHandler(java.lang.String channelName)
          Creates a new channel handler.
 
Method Summary
protected  void addMonitor(ChannelHandlerReadSubscription subscription)
          Used by the data source to add a read request on the channel managed by this handler.
protected  void addWriter(WriteCache<?> cache, ExceptionHandler handler)
          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 read PVs are open on this channel.
 int getUsageCounter()
          Returns how many read or write PVs are open on this channel.
 int getWriteUsageCounter()
          Returns how many write PVs 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.
protected  void processConnection(ConnectionPayload connectionPayload)
          Process the next connection payload.
protected  void processMessage(MessagePayload payload)
          Process the payload for this channel.
protected  void removeMonitor(Collector<?> collector)
          Used by the data source to remove a read request.
protected  void removeWrite(WriteCache<?> cache, ExceptionHandler exceptionHandler)
          Used by the data source to conclude writes to the channel managed by this handler.
protected  void reportExceptionToAllReadersAndWriters(java.lang.Exception ex)
          Notifies all readers and writers of an error condition.
protected abstract  void write(java.lang.Object newValue, ChannelWriteCallback callback)
          Implements a write operation.
 
Methods inherited from class org.epics.pvmanager.ChannelHandler
getChannelName, getProperties
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiplexedChannelHandler

public MultiplexedChannelHandler(java.lang.String channelName)
Creates a new channel handler.

Parameters:
channelName - the name of the channel this handler will be responsible of
Method Detail

reportExceptionToAllReadersAndWriters

protected final void reportExceptionToAllReadersAndWriters(java.lang.Exception ex)
Notifies all readers and 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 -

findTypeAdapter

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. By overriding this method a datasource can implement their own matching logic. One can use the logic provided in DataSourceTypeSupport as a good first implementation.

Parameters:
cache - the cache that will store the data
connection - the connection payload
Returns:
the matched type adapter

getUsageCounter

public int getUsageCounter()
Returns how many read or write PVs are open on this channel.

Specified by:
getUsageCounter in class ChannelHandler
Returns:
the number of open read/writes

getReadUsageCounter

public int getReadUsageCounter()
Returns how many read PVs are open on this channel.

Specified by:
getReadUsageCounter in class ChannelHandler
Returns:
the number of open reads

getWriteUsageCounter

public int getWriteUsageCounter()
Returns how many write PVs are open on this channel.

Specified by:
getWriteUsageCounter in class ChannelHandler
Returns:
the number of open writes

addMonitor

protected void addMonitor(ChannelHandlerReadSubscription subscription)
Used by the data source to add a read request on the channel managed by this handler.

Specified by:
addMonitor in class ChannelHandler
Parameters:
subscription - the data required for a subscription

removeMonitor

protected void removeMonitor(Collector<?> collector)
Used by the data source to remove a read request.

Specified by:
removeMonitor in class ChannelHandler
Parameters:
collector - the collector that does not need to be notified anymore

addWriter

protected void addWriter(WriteCache<?> cache,
                         ExceptionHandler handler)
Used by the data source to prepare the channel managed by this handler for write.

Specified by:
addWriter in class ChannelHandler
Parameters:
handler - to be notified in case of errors

removeWrite

protected void removeWrite(WriteCache<?> cache,
                           ExceptionHandler exceptionHandler)
Used by the data source to conclude writes to the channel managed by this handler.

Specified by:
removeWrite in class ChannelHandler
Parameters:
exceptionHandler - to be notified in case of errors

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 correct DataSourceTypeAdapter for each read monitor that was setup.

Parameters:
payload - the payload of for this type of channel

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.


write

protected abstract void write(java.lang.Object newValue,
                              ChannelWriteCallback callback)
Implements a write operation. Write the newValues to the channel and call the callback when done.

Specified by:
write in class ChannelHandler
Parameters:
newValue - new value to be written
callback - called when done or on error

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

isConnected

public final boolean isConnected()
Returns true if it is connected.

Specified by:
isConnected in class ChannelHandler
Returns:
true if underlying channel is connected