Skip to content

Commit

Permalink
Close #18 & #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingvord committed Oct 25, 2019
1 parent 6b39f3f commit 426df6c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 175 deletions.
133 changes: 0 additions & 133 deletions src/main/java/hzg/wpn/idl/EventDevStateAwaitor.java

This file was deleted.

12 changes: 7 additions & 5 deletions src/main/java/hzg/wpn/idl/IDLDeviceProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ public IDLDeviceProxy(String name) {
*/
public IDLDeviceProxy(String name, boolean useEventsForWaitUntil) {
logger.debug("Creating proxy for device[{},useEventsForWaitUntil={}]", name, useEventsForWaitUntil);
if (useEventsForWaitUntil)
throw handler.handle(new UnsupportedOperationException("useEventsForWaitUntil=true is no longer supported use false"));
try {
this.proxy = TangoProxies.newDeviceProxyWrapper(name);
this.awaitor = useEventsForWaitUntil ? new EventDevStateAwaitor(this.proxy, handler) : new PollDevStateAwaitor(this.proxy, handler);
this.awaitor = new PollDevStateAwaitor(this.proxy, handler);
} catch (TangoProxyException devFailed) {
throw handler.handle(devFailed);
}
Expand Down Expand Up @@ -236,11 +238,11 @@ public void waitUntil(String state) {
* @param state desired state in String format, i.e. "ON","RUNNING" etc (case insensitive)
* @throws RuntimeException
*/
public void waitUntil(String state, long delay) {
public void waitUntil(String state, long timeout) {
logger.trace("Waiting until {}/{}", proxy.getName(), state);
try {
DevState targetDevState = devStates.get(state.toUpperCase());
awaitor.waitUntil(targetDevState, delay);
awaitor.waitUntil(targetDevState, timeout, PollDevStateAwaitor.SLEEP_GRANULARITY);
logger.trace("Done waiting.");
} catch (Exception e) {
if (e.getCause() instanceof ReadAttributeException) {
Expand Down Expand Up @@ -281,11 +283,11 @@ public void waitUntilNot(String state) {
}
}

public void waitUntilNot(String state, long delay) {
public void waitUntilNot(String state, long timeout) {
logger.trace("Waiting until not {}/{}", proxy.getName(), state);
try {
DevState targetDevState = devStates.get(state.toUpperCase());
awaitor.waitUntilNot(targetDevState, delay);
awaitor.waitUntilNot(targetDevState, timeout, PollDevStateAwaitor.SLEEP_GRANULARITY);
logger.trace("Done waiting.");
} catch (Exception e) {
if (e.getCause() instanceof ReadAttributeException) {
Expand Down
62 changes: 40 additions & 22 deletions src/main/java/hzg/wpn/idl/PollDevStateAwaitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,74 +36,92 @@
import org.tango.client.ez.proxy.TangoProxy;
import org.tango.client.ez.proxy.TangoProxyException;

import java.util.concurrent.TimeoutException;

/**
* @author Igor Khokhriakov <igor.khokhriakov@hzg.de>
* @since 06.06.12
*/
public class PollDevStateAwaitor extends TangoDevStateAwaitor {
public static final long SLEEP_GRANULARITY = 10L;
public static final long DEFAULT_TIMEOUT = 30_000L;

public PollDevStateAwaitor(TangoProxy proxy, TangoProxyExceptionHandler handler) {
super(proxy);
}

@Override
public void waitUntil(DevState targetState, long delay) {
public void waitUntil(DevState targetState, long timeout, long delay) throws Exception {
Preconditions.checkArgument(timeout > 0L, "timeout can not be less or equal to 0");
Preconditions.checkArgument(delay > 0L, "delay can not be less or equal to 0");
while (true) {
long start = System.currentTimeMillis();
long tooLate = start + timeout;


while (!Thread.currentThread().isInterrupted()) {
try {
pollCrtState();
//do not wait if state are the same
if (targetStateReached(targetState)) {
return;
} else if (tooLate < System.currentTimeMillis()) {
throw new TimeoutException(String.format("Timeout has exceed! %s did not reach state %s within %d ms", getProxy().getName(), targetState, timeout));
} else {
Thread.sleep(delay);
}
} catch (TangoProxyException devFailed) {
throw getHandler().handle(devFailed);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw getHandler().handle(e);
} catch (NoSuchAttributeException e) {
throw getHandler().handle(e);
throw e;
}
}
}

@Override
public void waitUntil(DevState targetState) {
waitUntil(targetState, SLEEP_GRANULARITY);
public void waitUntil(DevState targetState, long delay) throws Exception {
waitUntil(targetState, DEFAULT_TIMEOUT, delay);
}

@Override
public void waitUntil(DevState targetState) throws Exception {
waitUntil(targetState, DEFAULT_TIMEOUT, SLEEP_GRANULARITY);
}

public void waitUntilNot(DevState targetState, long delay) {
Preconditions.checkArgument(delay > 0L, "dealy can not be less or equal to 0");
while (true) {
public void waitUntilNot(DevState targetState, long timeout, long delay) throws Exception {
Preconditions.checkArgument(timeout > 0L, "timeout can not be less or equal to 0");
Preconditions.checkArgument(delay > 0L, "delay can not be less or equal to 0");
long start = System.currentTimeMillis();
long tooLate = start + timeout;

while (!Thread.currentThread().isInterrupted()) {
try {
pollCrtState();
//wait if states are the same
if (targetStateReached(targetState)) {
if (tooLate < System.currentTimeMillis()) {
throw new TimeoutException(String.format("Timeout has exceed! %s did not change state %s within %d ms", getProxy().getName(), targetState, timeout));
} else if (targetStateReached(targetState)) {
Thread.sleep(delay);
} else {
return;
}
} catch (TangoProxyException devFailed) {
throw getHandler().handle(devFailed);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw getHandler().handle(e);
} catch (NoSuchAttributeException e) {
throw getHandler().handle(e);
throw e;
}
}
}

@Override
public void waitUntilNot(DevState targetState) {
waitUntilNot(targetState, SLEEP_GRANULARITY);
public void waitUntilNot(DevState targetState, long timeout) throws Exception {
waitUntilNot(targetState, timeout, SLEEP_GRANULARITY);
}

private void pollCrtState() throws TangoProxyException, NoSuchAttributeException {
DevState crtState = getProxy().readAttribute(STATE);
setCrtDevState(crtState);
}

public PollDevStateAwaitor(TangoProxy proxy, TangoProxyExceptionHandler handler) {
super(proxy, handler);
@Override
public void waitUntilNot(DevState targetState) throws Exception {
waitUntilNot(targetState, DEFAULT_TIMEOUT, SLEEP_GRANULARITY);
}
}
33 changes: 22 additions & 11 deletions src/main/java/hzg/wpn/idl/TangoDevStateAwaitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@
public abstract class TangoDevStateAwaitor {
public static final String STATE = "State";
private final TangoProxy proxy;
private final TangoProxyExceptionHandler handler;
private final AtomicReference<DevState> crtDevState = new AtomicReference<DevState>();

protected TangoDevStateAwaitor(TangoProxy proxy, TangoProxyExceptionHandler handler) {
protected TangoDevStateAwaitor(TangoProxy proxy) {
this.proxy = proxy;
this.handler = handler;
}

/**
Expand All @@ -56,31 +54,48 @@ protected TangoDevStateAwaitor(TangoProxy proxy, TangoProxyExceptionHandler hand
* @param targetState wait until the state
* @throws RuntimeException
*/
public abstract void waitUntil(DevState targetState);
public abstract void waitUntil(DevState targetState) throws Exception;

/**
* Blocks current thread until the device is in targetState
*
* @param targetState current device state
* @throws RuntimeException
*/
public abstract void waitUntilNot(DevState targetState);
public abstract void waitUntilNot(DevState targetState) throws Exception;

/**
* Blocks current thread until device state changes to targetState
*
* @param targetState wait until the state
* @throws RuntimeException
*/
public abstract void waitUntil(DevState targetState, long delay);
public abstract void waitUntil(DevState targetState, long timeout, long delay) throws Exception;

/**
* Blocks current thread until device state changes to targetState
*
* @param targetState wait until the state
* @throws RuntimeException
*/
public abstract void waitUntil(DevState targetState, long delay) throws Exception;

/**
* Blocks current thread until the device is in targetState
*
* @param targetState current device state
* @throws RuntimeException
*/
public abstract void waitUntilNot(DevState targetState, long delay);
public abstract void waitUntilNot(DevState targetState, long timeout, long delay) throws Exception;


/**
* Blocks current thread until the device is in targetState
*
* @param targetState current device state
* @throws RuntimeException
*/
public abstract void waitUntilNot(DevState targetState, long delay) throws Exception;


protected boolean targetStateReached(DevState targetState) {
Expand All @@ -94,8 +109,4 @@ protected void setCrtDevState(DevState state) {
protected TangoProxy getProxy() {
return proxy;
}

protected TangoProxyExceptionHandler getHandler() {
return handler;
}
}
8 changes: 4 additions & 4 deletions src/test/java/hzg/wpn/idl/IDLDeviceProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ public void testExecuteCommand_State() throws Exception {

//@Test
public void testAwaitUntil() throws Exception {
IDLDeviceProxy instance = new IDLDeviceProxy(TEST_TANGO);
IDLDeviceProxy instance = new IDLDeviceProxy("tango://localhost:10000/sys/tg_test/1");

long nanosStart = System.nanoTime();
instance.waitUntil("running");
instance.waitUntil("running", 3000);
long nanosEnd = System.nanoTime();
long awaited = nanosEnd - nanosStart;
assertTrue(awaited > 409343233);
Expand All @@ -324,11 +324,11 @@ public void testAwaitUntil() throws Exception {

//@Test
public void testAwaitUntilNot() throws Exception {
IDLDeviceProxy instance = new IDLDeviceProxy(TEST_TANGO);
IDLDeviceProxy instance = new IDLDeviceProxy("tango://localhost:10000/sys/tg_test/1");

long nanosStart = System.nanoTime();
//device is started in fault state
instance.waitUntilNot("fault");
instance.waitUntilNot("fault", 3000);
long nanosEnd = System.nanoTime();
long awaited = nanosEnd - nanosStart;
assertTrue(awaited > 409343233);
Expand Down

0 comments on commit 426df6c

Please sign in to comment.