From afeeec67db863c137a67877ddcff4fd5273f0d28 Mon Sep 17 00:00:00 2001 From: Gideon le Grange Date: Sun, 29 Mar 2015 23:14:32 +0200 Subject: [PATCH] Some cleanups. Fixed timeout corner case in #16 --- .../mikrotik/impl/ApiConnectionImpl.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java b/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java index 699a209..43c75d7 100644 --- a/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java +++ b/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java @@ -82,7 +82,7 @@ public final class ApiConnectionImpl extends ApiConnection { @Override public List> execute(String cmd) throws MikrotikApiException { - return execute(Parser.parse(cmd), timeOut); + return execute(Parser.parse(cmd), timeout); } @Override @@ -97,23 +97,23 @@ public final class ApiConnectionImpl extends ApiConnection { @Override public int getTimeout() { - return timeOut; + return timeout; } @Override public void setTimeout(int timeout) throws MikrotikApiException { - if (timeout >=0) { - timeOut = timeout; + if (timeout > 0) { + this.timeout = timeout; } else { - throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive or 0", timeout)); + throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout)); } } - private List> execute(Command cmd, int timeOut) throws MikrotikApiException { + private List> execute(Command cmd, int timeout) throws MikrotikApiException { SyncListener l = new SyncListener(); execute(cmd, l); - return l.getResults(timeOut); + return l.getResults(timeout); } private String execute(Command cmd, ResultListener lis) throws MikrotikApiException { @@ -201,7 +201,7 @@ public final class ApiConnectionImpl extends ApiConnection { private Processor processor; private final Map listeners; private Integer _tag = 0; - private int timeOut = ApiConnection.DEFAULT_COMMAND_TIMEOUT; + private int timeout = ApiConnection.DEFAULT_COMMAND_TIMEOUT; /** * thread to read data from the socket and process it into Strings @@ -478,16 +478,16 @@ public final class ApiConnectionImpl extends ApiConnection { results.add(result); } - private List> getResults(int timeOut) throws MikrotikApiException { + private List> getResults(int timeout) throws MikrotikApiException { try { synchronized (this) { // don't wait if we already have a result. - int waitTime = timeOut; + int waitTime = timeout; while (!complete && (waitTime > 0)) { long start = System.currentTimeMillis(); wait(waitTime); waitTime = waitTime - (int)(System.currentTimeMillis() - start); - if ((waitTime < 0) && !complete) { - err = new ApiConnectionException(String.format("Command timed out after %d ms", timeOut)); + if ((waitTime <= 0) && !complete) { + err = new ApiConnectionException(String.format("Command timed out after %d ms", timeout)); } } }