Implemented java.lang.AutoCloseable
This commit is contained in:
parent
db373c41d8
commit
61691cb9a6
|
|
@ -10,7 +10,7 @@ import me.legrange.mikrotik.impl.ApiConnectionImpl;
|
|||
*
|
||||
* @author GideonLeGrange
|
||||
*/
|
||||
public abstract class ApiConnection {
|
||||
public abstract class ApiConnection implements AutoCloseable {
|
||||
|
||||
/** default TCP port used by Mikrotik API */
|
||||
public static final int DEFAULT_PORT = 8728;
|
||||
|
|
@ -146,4 +146,8 @@ public abstract class ApiConnection {
|
|||
*/
|
||||
public abstract void setTimeout(int timeout) throws MikrotikApiException;
|
||||
|
||||
@Override
|
||||
public abstract void close() throws ApiConnectionException;
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -54,19 +54,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
|||
|
||||
@Override
|
||||
public void disconnect() throws ApiConnectionException {
|
||||
if (!connected) {
|
||||
throw new ApiConnectionException(("Not/no longer connected to remote Mikrotik"));
|
||||
}
|
||||
connected = false;
|
||||
processor.interrupt();
|
||||
reader.interrupt();
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
sock.close();
|
||||
} catch (IOException ex) {
|
||||
throw new ApiConnectionException(String.format("Error closing socket: %s", ex.getMessage()), ex);
|
||||
}
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -101,12 +89,28 @@ public final class ApiConnectionImpl extends ApiConnection {
|
|||
public void setTimeout(int timeout) throws MikrotikApiException {
|
||||
if (timeout > 0) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws ApiConnectionException {
|
||||
if (!connected) {
|
||||
throw new ApiConnectionException(("Not/no longer connected to remote Mikrotik"));
|
||||
}
|
||||
connected = false;
|
||||
processor.interrupt();
|
||||
reader.interrupt();
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
sock.close();
|
||||
} catch (IOException ex) {
|
||||
throw new ApiConnectionException(String.format("Error closing socket: %s", ex.getMessage()), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Map<String, String>> execute(Command cmd, int timeout) throws MikrotikApiException {
|
||||
SyncListener l = new SyncListener();
|
||||
execute(cmd, l);
|
||||
|
|
|
|||
Loading…
Reference in New Issue