org.diirt.datasource.PVConfiguration Maven / Gradle / Ivy
Show all versions of datasource Show documentation
/**
* Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
*/
package org.diirt.datasource;
import java.util.concurrent.Executor;
import org.diirt.datasource.expression.DesiredRateReadWriteExpression;
import org.diirt.util.time.TimeDuration;
/**
* Allows to configure the type of read/write PV to create.
*
* @param the read payload
* @param the write payload
* @author carcassi
*/
public class PVConfiguration extends CommonConfiguration {
private final PVReaderConfiguration pvReaderConfiguration;
private final PVWriterConfiguration pvWriterConfiguration;
PVConfiguration(DesiredRateReadWriteExpression readWriteExpression) {
pvReaderConfiguration = new PVReaderConfiguration(readWriteExpression);
pvWriterConfiguration = new PVWriterConfiguration(readWriteExpression);
}
@Override
public PVConfiguration from(DataSource dataSource) {
pvReaderConfiguration.from(dataSource);
pvWriterConfiguration.from(dataSource);
return this;
}
@Override
public PVConfiguration notifyOn(Executor onThread) {
pvReaderConfiguration.notifyOn(onThread);
pvWriterConfiguration.notifyOn(onThread);
return this;
}
/**
* Sets a timeout for both reader and writer.
*
* @param timeout duration of the timeout
* @return this expression
*/
@Override
public PVConfiguration timeout(TimeDuration timeout) {
pvReaderConfiguration.timeout(timeout);
pvWriterConfiguration.timeout(timeout);
return this;
}
/**
* Sets a timeout with the given message for both read and writer.
*
* @param timeout duration of the timeout
* @param timeoutMessage message for the timeout
* @return this expression
*/
@Override
public PVConfiguration timeout(TimeDuration timeout, String timeoutMessage) {
pvReaderConfiguration.timeout(timeout, timeoutMessage);
pvWriterConfiguration.timeout(timeout, timeoutMessage);
return this;
}
/**
* Specifies a timeout, with a different message for the read and the write.
*
* @param timeout time before notification
* @param readMessage exception message for the read timeout
* @param writeMessage exception message for the write timeout
* @return this
*/
public PVConfiguration timeout(TimeDuration timeout, String readMessage, String writeMessage) {
pvReaderConfiguration.timeout(timeout, readMessage);
pvWriterConfiguration.timeout(timeout, writeMessage);
return this;
}
/**
* Adds a listener for the read events.
*
* @param listener the new listener
* @return this expression
*/
public PVConfiguration readListener(PVReaderListener super R> listener) {
pvReaderConfiguration.readListener(listener);
return this;
}
/**
* Adds a listener for the write events.
*
* @param listener the new listener
* @return this expression
*/
public PVConfiguration writeListener(PVWriterListener extends W> listener) {
pvWriterConfiguration.writeListener(listener);
return this;
}
/**
* Forwards exception to the given exception handler. No thread switch
* is done, so the handler is notified on the thread where the exception
* was thrown.
*
* Giving a custom exception handler will disable the default handler,
* so {@link PV#lastException() } and {@link PV#lastWriteException() }
* is no longer set and no notification
* is done.
*
* @param exceptionHandler an exception handler
* @return this
*/
public PVConfiguration routeExceptionsTo(ExceptionHandler exceptionHandler) {
pvReaderConfiguration.routeExceptionsTo(exceptionHandler);
pvWriterConfiguration.routeExceptionsTo(exceptionHandler);
return this;
}
/**
* Creates the pv such that writes are synchronous and read notifications
* comes at most at the rate specified.
*
* @param period minimum time between read notifications
* @return a new PV
*/
public PV synchWriteAndMaxReadRate(TimeDuration period) {
PVReader pvReader = pvReaderConfiguration.maxRate(period);
PVWriter pvWriter = pvWriterConfiguration.sync();
return new PV(pvReader, pvWriter);
}
/**
* Creates the pv such that writes are asynchronous and read notifications
* comes at most at the rate specified.
*
* @param period minimum time between read notifications
* @return a new PV
*/
public PV asynchWriteAndMaxReadRate(TimeDuration period) {
PVReader pvReader = pvReaderConfiguration.maxRate(period);
PVWriter pvWriter = pvWriterConfiguration.async();
PV pv = new PV(pvReader, pvWriter);
// XXX: This should really be set before the scanning starts
PVReaderImpl.implOf(pvReader).setReaderForNotification(pv);
PVWriterImpl.implOf(pvWriter).setWriterForNotification(pv);
return pv;
}
}