Merge 0152035958 into 0b80064e46
This commit is contained in:
commit
a45f160cb0
|
|
@ -14,7 +14,9 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import javax.net.ssl.SSLSocket;
|
import javax.net.ssl.SSLSocket;
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
import me.legrange.mikrotik.ApiConnection;
|
import me.legrange.mikrotik.ApiConnection;
|
||||||
|
|
@ -88,6 +90,11 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
@Override
|
@Override
|
||||||
public void setTimeout(int timeout) throws MikrotikApiException {
|
public void setTimeout(int timeout) throws MikrotikApiException {
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
|
try {
|
||||||
|
sock.setSoTimeout(timeout);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new MikrotikApiException(e.getMessage(), e);
|
||||||
|
}
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
} else {
|
} else {
|
||||||
throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout));
|
throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout));
|
||||||
|
|
@ -452,16 +459,18 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
|
|
||||||
private class SyncListener implements ResultListener {
|
private class SyncListener implements ResultListener {
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void error(MikrotikApiException ex) {
|
public synchronized void error(MikrotikApiException ex) {
|
||||||
this.err = ex;
|
this.err = ex;
|
||||||
notify();
|
latch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void completed() {
|
public synchronized void completed() {
|
||||||
complete = true;
|
complete = true;
|
||||||
notify();
|
latch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized void completed(Done done) {
|
synchronized void completed(Done done) {
|
||||||
|
|
@ -471,7 +480,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
results.add(res);
|
results.add(res);
|
||||||
}
|
}
|
||||||
complete = true;
|
complete = true;
|
||||||
notify();
|
latch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -481,22 +490,10 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
|
|
||||||
private List<Map<String, String>> getResults(int timeout) throws MikrotikApiException {
|
private List<Map<String, String>> getResults(int timeout) throws MikrotikApiException {
|
||||||
try {
|
try {
|
||||||
synchronized (this) { // don't wait if we already have a result.
|
latch.await(timeout, TimeUnit.MILLISECONDS);
|
||||||
int waitTime = timeout;
|
} catch (Exception ex) {
|
||||||
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));
|
err = new ApiConnectionException(String.format("Command timed out after %d ms", timeout));
|
||||||
}
|
throw new MikrotikApiException(ex.getMessage(), err);
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (InterruptedException ex) {
|
|
||||||
throw new ApiConnectionException(ex.getMessage(), ex);
|
|
||||||
}
|
|
||||||
if (err != null) {
|
|
||||||
throw new MikrotikApiException(err.getMessage(), err);
|
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue