From 1c18b4273e694c850557e48eb625050702ddb359 Mon Sep 17 00:00:00 2001 From: Alexandre Gobbo Date: Thu, 6 Aug 2020 10:40:24 +0200 Subject: [PATCH] Added Channel.setElementsToRead --- README.md | 4 +-- build.gradle | 2 +- src/main/java/org/epics/ca/Channel.java | 9 +++++ .../java/org/epics/ca/impl/ChannelImpl.java | 34 +++++++++++++++++-- .../ca/impl/requests/MonitorRequest.java | 15 ++------ .../ca/impl/requests/ReadNotifyRequest.java | 6 +--- .../java/org/epics/ca/LibraryVersionTest.java | 2 +- 7 files changed, 48 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5b17b40b..0c91239e 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Add the following dependency: org.epics ca - 1.3.2 + 1.4.0 ``` @@ -76,7 +76,7 @@ and the following repository: Add the following dependency: ```gradle -compile 'org.epics:ca:1.3.2' +compile 'org.epics:ca:1.4.0' ``` and the following repository: diff --git a/build.gradle b/build.gradle index 94268e6c..5d15cbe9 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ println "The Java version being used for this gradle build is: ${JavaVersion.cur // Define the coordinates for the build products. group = 'org.epics' -version = '1.3.2' +version = '1.4.0' // Note: currently (2020-06-01) the inspection error flagged up by IntelliJ is incorrect and has been suppressed. //noinspection GroovyUnusedAssignment diff --git a/src/main/java/org/epics/ca/Channel.java b/src/main/java/org/epics/ca/Channel.java index ce46427e..24a023b5 100644 --- a/src/main/java/org/epics/ca/Channel.java +++ b/src/main/java/org/epics/ca/Channel.java @@ -95,6 +95,15 @@ default > Monitor addMonitor( Class getProperties(); + //Number of elements to read. + //Default: 0 (defined by type or protocol) + //If negative then requests the native element count. + void setElementsToRead( int elementsToRead ); + + int getElementsToRead(); + + int getNativeElementCount(); + // suppresses AutoCloseable.close() exception @Override void close(); diff --git a/src/main/java/org/epics/ca/impl/ChannelImpl.java b/src/main/java/org/epics/ca/impl/ChannelImpl.java index 535afea0..4e83fcd6 100644 --- a/src/main/java/org/epics/ca/impl/ChannelImpl.java +++ b/src/main/java/org/epics/ca/impl/ChannelImpl.java @@ -6,6 +6,7 @@ import org.apache.commons.lang3.Validate; import org.epics.ca.*; +import org.epics.ca.annotation.CaChannel; import org.epics.ca.data.Metadata; import org.epics.ca.impl.TypeSupports.TypeSupport; import org.epics.ca.impl.monitor.MonitorNotificationService; @@ -18,8 +19,10 @@ import java.lang.reflect.Array; import java.nio.ByteBuffer; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -76,6 +79,8 @@ public class ChannelImpl implements Channel, TransportClient private volatile int nativeElementCount = 0; + private volatile int elementsToRead = 0; + // on every connection loss the value gets incremented private final AtomicInteger connectionLossId = new AtomicInteger (); @@ -374,6 +379,23 @@ public Map getProperties() return properties; } + @Override + public void setElementsToRead( int elementsToRead ) + { + this.elementsToRead = elementsToRead; + } + + @Override + public int getElementsToRead() + { + return getElementsToRead(typeSupport); + } + + @Override + public int getNativeElementCount() + { + return nativeElementCount; + } /*- Public non-interface methods ---------------------------------------------*/ @@ -395,9 +417,17 @@ public synchronized TcpTransport getTcpTransport() return tcpTransport; } - public int getNativeElementCount() + public int getElementsToRead(TypeSupport typeSupport) { - return nativeElementCount; + int ret = (elementsToRead < 0) ? nativeElementCount : elementsToRead; + ret = Math.max(Math.min(ret,nativeElementCount),0); + if(ret <=0) { + ret = typeSupport.getForcedElementCount(); + if (ret == 0 && getTcpTransport().getMinorRevision() < 13) { + ret = nativeElementCount; + } + } + return ret; } /** diff --git a/src/main/java/org/epics/ca/impl/requests/MonitorRequest.java b/src/main/java/org/epics/ca/impl/requests/MonitorRequest.java index 540bf33f..2ffe4be5 100644 --- a/src/main/java/org/epics/ca/impl/requests/MonitorRequest.java +++ b/src/main/java/org/epics/ca/impl/requests/MonitorRequest.java @@ -142,13 +142,7 @@ public void cancel() public void resubscribe( Transport transport ) { - int dataCount = typeSupport.getForcedElementCount (); - - if ( dataCount == 0 && channel.getTcpTransport().getMinorRevision () < 13 ) - { - dataCount = channel.getNativeElementCount(); - } - + final int dataCount = channel.getElementsToRead(typeSupport); Messages.createSubscriptionMessage ( transport, typeSupport.getDataType (), dataCount, channel.getSID (), ioid, mask ); transport.flush (); } @@ -198,12 +192,7 @@ public void close() return; } - int dataCount = typeSupport.getForcedElementCount(); - - if ( dataCount == 0 && channel.getTcpTransport().getMinorRevision () < 13 ) - { - dataCount = channel.getNativeElementCount(); - } + final int dataCount = channel.getElementsToRead(typeSupport); try { diff --git a/src/main/java/org/epics/ca/impl/requests/ReadNotifyRequest.java b/src/main/java/org/epics/ca/impl/requests/ReadNotifyRequest.java index 2908c7c2..872348da 100644 --- a/src/main/java/org/epics/ca/impl/requests/ReadNotifyRequest.java +++ b/src/main/java/org/epics/ca/impl/requests/ReadNotifyRequest.java @@ -60,11 +60,7 @@ public ReadNotifyRequest( ChannelImpl channel, Transport transport, int sid, this.sid = sid; this.typeSupport = typeSupport; - final int minorRevision = channel.getTcpTransport().getMinorRevision(); - final int forcedElementCount = typeSupport.getForcedElementCount(); - final int nativeElementCount = channel.getNativeElementCount(); - - final int dataCount = ( forcedElementCount == 0 ) && ( minorRevision < 13 ) ? nativeElementCount : forcedElementCount; + final int dataCount = channel.getElementsToRead(typeSupport); logger.finest( "Receive data count is: " + dataCount ); context = transport.getContext(); ioid = context.registerResponseRequest( this ); diff --git a/src/test/java/org/epics/ca/LibraryVersionTest.java b/src/test/java/org/epics/ca/LibraryVersionTest.java index 2c18d43c..5c59eb75 100644 --- a/src/test/java/org/epics/ca/LibraryVersionTest.java +++ b/src/test/java/org/epics/ca/LibraryVersionTest.java @@ -42,7 +42,7 @@ void testLibraryVersion() // Note: the version string will have to be updated on every new release. // However, new releases should not be published until all the unit tests pass. logger.info( "Test is running from JAR. Version is: " + versionString + "'." ); - assertThat( LibraryVersion.getAsString(), is("1.3.2" ) ); + assertThat( LibraryVersion.getAsString(), is("1.4.0" ) ); } else {