Skip to content

Commit

Permalink
Removed bug: put and get were blocking if operation fails. Now except…
Browse files Browse the repository at this point in the history
…ion is thrown.
  • Loading branch information
alexgobbo committed Mar 2, 2016
1 parent 65db72a commit 08bd6e9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -6,4 +6,5 @@ ch.psi.jcae.iml
.project
.settings
.classpath
.gradle
.gradle
/.nb-gradle/
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'maven'

sourceCompatibility = 1.7

version = '2.8.1'
version = '2.9.0'

repositories {
mavenCentral()
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/ch/psi/jcae/impl/ChannelAccessException.java
@@ -0,0 +1,13 @@
package ch.psi.jcae.impl;

import gov.aps.jca.CAStatus;
import java.util.concurrent.ExecutionException;

/**
*
*/
class ChannelAccessException extends ExecutionException{
ChannelAccessException(CAStatus status){
super(status.getMessage());
}
}
17 changes: 11 additions & 6 deletions src/main/java/ch/psi/jcae/impl/GetFuture.java
Expand Up @@ -28,6 +28,7 @@ public class GetFuture<T> implements GetListener, Future<T>
*/
private T value;
private Class<T> type;
CAStatus status;

private final CountDownLatch latch = new CountDownLatch(1);

Expand All @@ -38,21 +39,19 @@ public GetFuture(Class<T> type){
@SuppressWarnings("unchecked")
@Override
public void getCompleted(GetEvent ev) {

status = ev.getStatus();
try{
value = (T) Handlers.HANDLERS.get(type).getValue(ev.getDBR());
}
catch(CAStatusException e){
e.printStackTrace();
}

if (ev.getStatus() == CAStatus.NORMAL){
latch.countDown();
}
else{

if (ev.getStatus() != CAStatus.NORMAL){
logger.warning("Get failed with status: "+ev.getStatus());
// latch.notifyAll();
}
latch.countDown();
}


Expand Down Expand Up @@ -86,6 +85,9 @@ public boolean isDone() {
@Override
public T get() throws InterruptedException, ExecutionException {
latch.await();
if (status != CAStatus.NORMAL){
throw new ChannelAccessException(status);
}
return value;
}

Expand All @@ -98,6 +100,9 @@ public T get(long timeout, TimeUnit unit) throws InterruptedException, Execution
if(!latch.await(timeout, unit)){
throw new TimeoutException("Timeout ["+timeout+"] occured while getting value"); // from which channel ?
}
if (status != CAStatus.NORMAL){
throw new ChannelAccessException(status);
}
return value;
}
}
14 changes: 10 additions & 4 deletions src/main/java/ch/psi/jcae/impl/SetFuture.java
Expand Up @@ -23,20 +23,20 @@ public class SetFuture<T> implements PutListener, Future<T>

private final CountDownLatch latch = new CountDownLatch(1);
private T value;
CAStatus status;

public SetFuture(T value){
this.value=value;
}

@Override
public void putCompleted(PutEvent ev) {
if(ev.getStatus() == CAStatus.NORMAL){
latch.countDown();
}
else{
status = ev.getStatus();
if(ev.getStatus() != CAStatus.NORMAL){
logger.warning("Set failed with status: "+ev.getStatus());
// latch.notifyAll();
}
latch.countDown();
}


Expand All @@ -62,6 +62,9 @@ public boolean isCancelled() {
@Override
public T get() throws InterruptedException, ExecutionException {
latch.await();
if (status != CAStatus.NORMAL){
throw new ChannelAccessException(status);
}
return value;
}

Expand All @@ -73,6 +76,9 @@ public T get(long timeout, TimeUnit unit) throws InterruptedException, Execution
if(!latch.await(timeout, unit)){
throw new TimeoutException("Timeout occured while setting value to channel");
}
if (status != CAStatus.NORMAL){
throw new ChannelAccessException(status);
}
return value;
}

Expand Down

0 comments on commit 08bd6e9

Please sign in to comment.