Merge 9b75364617 into db373c41d8
This commit is contained in:
commit
b7349aab9f
|
|
@ -5,12 +5,14 @@ import me.legrange.mikrotik.ApiConnection;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author gideon
|
* @author gideon
|
||||||
|
* @author clairtonluz
|
||||||
*/
|
*/
|
||||||
abstract class Example {
|
abstract class Example {
|
||||||
|
|
||||||
protected void connect() throws Exception {
|
protected ApiConnection connect() throws Exception {
|
||||||
con = ApiConnection.connect(Config.HOST, ApiConnection.DEFAULT_PORT, 2000);
|
con = ApiConnection.connect(Config.HOST, ApiConnection.DEFAULT_PORT, 2000);
|
||||||
con.login(Config.USERNAME, Config.PASSWORD);
|
con.login(Config.USERNAME, Config.PASSWORD);
|
||||||
|
return con;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void disconnect() throws Exception {
|
protected void disconnect() throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package examples;
|
||||||
|
|
||||||
|
import me.legrange.mikrotik.ApiConnection;
|
||||||
|
import me.legrange.mikrotik.MikrotikApiException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example 10: The try-with-resources Statement
|
||||||
|
*
|
||||||
|
* @author clairtonluz
|
||||||
|
*/
|
||||||
|
public class Example10 extends Example {
|
||||||
|
|
||||||
|
public static void main(String... args) throws Exception {
|
||||||
|
Example10 ex = new Example10();
|
||||||
|
try (ApiConnection c = ex.connect()) {
|
||||||
|
ex.test();
|
||||||
|
}
|
||||||
|
System.out.printf("\nisConnected = %B",ex.con.isConnected());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void test() throws MikrotikApiException, InterruptedException {
|
||||||
|
List<Map<String, String>> res = con.execute("/interface/ethernet/print");
|
||||||
|
for (Map<String, String> r : res) {
|
||||||
|
System.out.println(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,55 +1,68 @@
|
||||||
package me.legrange.mikrotik;
|
package me.legrange.mikrotik;
|
||||||
|
|
||||||
|
import me.legrange.mikrotik.impl.ApiConnectionImpl;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import me.legrange.mikrotik.impl.ApiConnectionImpl;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Mikrotik API connection. This is the class used to connect to a remote
|
* The Mikrotik API connection. This is the class used to connect to a remote
|
||||||
* Mikrotik and send commands to it.
|
* Mikrotik and send commands to it.
|
||||||
*
|
*
|
||||||
* @author GideonLeGrange
|
* @author GideonLeGrange
|
||||||
|
* @author clairtonluz
|
||||||
*/
|
*/
|
||||||
public abstract class ApiConnection {
|
public abstract class ApiConnection implements AutoCloseable {
|
||||||
|
|
||||||
/** default TCP port used by Mikrotik API */
|
/**
|
||||||
|
* default TCP port used by Mikrotik API
|
||||||
|
*/
|
||||||
public static final int DEFAULT_PORT = 8728;
|
public static final int DEFAULT_PORT = 8728;
|
||||||
/** default TCP TLS port used by Mikrotik API */
|
/**
|
||||||
|
* default TCP TLS port used by Mikrotik API
|
||||||
|
*/
|
||||||
public static final int DEFAULT_TLS_PORT = 8729;
|
public static final int DEFAULT_TLS_PORT = 8729;
|
||||||
/** default connection timeout to use when opening the connection */
|
/**
|
||||||
|
* default connection timeout to use when opening the connection
|
||||||
|
*/
|
||||||
public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
|
public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
|
||||||
/** default command timeout used for synchronous commands */
|
/**
|
||||||
|
* default command timeout used for synchronous commands
|
||||||
|
*/
|
||||||
public static final int DEFAULT_COMMAND_TIMEOUT = 60000;
|
public static final int DEFAULT_COMMAND_TIMEOUT = 60000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new API connection to the give device on the supplied port, using anonymous TLS for encryption.
|
* Create a new API connection to the give device on the supplied port, using anonymous TLS for encryption.
|
||||||
* @param host The host to which to connect.
|
*
|
||||||
* @param port The TCP port to use.
|
* @param host The host to which to connect.
|
||||||
|
* @param port The TCP port to use.
|
||||||
* @param timeout The connection timeout to use when opening the connection.
|
* @param timeout The connection timeout to use when opening the connection.
|
||||||
* @return The ApiConnection
|
* @return The ApiConnection
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
||||||
*/
|
*/
|
||||||
public static ApiConnection connectTLS(String host, int port, int timeout) throws MikrotikApiException {
|
public static ApiConnection connectTLS(String host, int port, int timeout) throws MikrotikApiException {
|
||||||
return ApiConnectionImpl.connect(host, port, true, timeout);
|
return ApiConnectionImpl.connect(host, port, true, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new API connection to the give device on the supplied port, using anonymous TLS for encryption.
|
* Create a new API connection to the give device on the supplied port, using anonymous TLS for encryption.
|
||||||
|
*
|
||||||
* @param host The host to which to connect.
|
* @param host The host to which to connect.
|
||||||
* @param port The TCP port to use.
|
* @param port The TCP port to use.
|
||||||
* @return The ApiConnection
|
* @return The ApiConnection
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
||||||
*/
|
*/
|
||||||
public static ApiConnection connectTLS(String host, int port) throws MikrotikApiException {
|
public static ApiConnection connectTLS(String host, int port) throws MikrotikApiException {
|
||||||
return ApiConnectionImpl.connect(host, port, true, DEFAULT_CONNECTION_TIMEOUT);
|
return ApiConnectionImpl.connect(host, port, true, DEFAULT_CONNECTION_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new API connection to the give device on the default API port, using anonymous TLS for encryption.
|
* Create a new API connection to the give device on the default API port, using anonymous TLS for encryption.
|
||||||
|
*
|
||||||
* @param host The host to which to connect.
|
* @param host The host to which to connect.
|
||||||
* @return The ApiConnection
|
* @return The ApiConnection
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
||||||
*/
|
*/
|
||||||
public static ApiConnection connectTLS(String host) throws MikrotikApiException {
|
public static ApiConnection connectTLS(String host) throws MikrotikApiException {
|
||||||
|
|
@ -58,10 +71,11 @@ public abstract class ApiConnection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new API connection to the give device on the supplied port
|
* Create a new API connection to the give device on the supplied port
|
||||||
* @param host The host to which to connect.
|
*
|
||||||
* @param port The TCP port to use.
|
* @param host The host to which to connect.
|
||||||
|
* @param port The TCP port to use.
|
||||||
* @param timeout The connection timeout to use when opening the connection.
|
* @param timeout The connection timeout to use when opening the connection.
|
||||||
* @return The ApiConnection
|
* @return The ApiConnection
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
||||||
*/
|
*/
|
||||||
public static ApiConnection connect(String host, int port, int timeout) throws MikrotikApiException {
|
public static ApiConnection connect(String host, int port, int timeout) throws MikrotikApiException {
|
||||||
|
|
@ -70,9 +84,10 @@ public abstract class ApiConnection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new API connection to the give device on the supplied port
|
* Create a new API connection to the give device on the supplied port
|
||||||
|
*
|
||||||
* @param host The host to which to connect.
|
* @param host The host to which to connect.
|
||||||
* @param port The TCP port to use.
|
* @param port The TCP port to use.
|
||||||
* @return The ApiConnection
|
* @return The ApiConnection
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
||||||
*/
|
*/
|
||||||
public static ApiConnection connect(String host, int port) throws MikrotikApiException {
|
public static ApiConnection connect(String host, int port) throws MikrotikApiException {
|
||||||
|
|
@ -81,11 +96,12 @@ public abstract class ApiConnection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new API connection to the give device on the default API port.
|
* Create a new API connection to the give device on the default API port.
|
||||||
|
*
|
||||||
* @param host The host to which to connect.
|
* @param host The host to which to connect.
|
||||||
* @return The ApiConnection
|
* @return The ApiConnection
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem connecting
|
||||||
*/
|
*/
|
||||||
public static ApiConnection connect(String host) throws MikrotikApiException {
|
public static ApiConnection connect(String host) throws MikrotikApiException {
|
||||||
return connect(host, DEFAULT_PORT);
|
return connect(host, DEFAULT_PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -98,51 +114,59 @@ public abstract class ApiConnection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect from the remote API
|
* Disconnect from the remote API
|
||||||
|
*
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem disconnecting
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem disconnecting
|
||||||
*/
|
*/
|
||||||
public abstract void disconnect() throws MikrotikApiException;
|
public abstract void disconnect() throws MikrotikApiException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log in to the remote router.
|
* Log in to the remote router.
|
||||||
*
|
*
|
||||||
* @param username - username of the user on the router
|
* @param username - username of the user on the router
|
||||||
* @param password - password for the user
|
* @param password - password for the user
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException
|
* @throws me.legrange.mikrotik.MikrotikApiException
|
||||||
* @throws java.lang.InterruptedException
|
* @throws java.lang.InterruptedException
|
||||||
*/
|
*/
|
||||||
public abstract void login(String username, String password) throws MikrotikApiException, InterruptedException;
|
public abstract void login(String username, String password) throws MikrotikApiException, InterruptedException;
|
||||||
|
|
||||||
/** execute a command and return a list of results.
|
/**
|
||||||
|
* execute a command and return a list of results.
|
||||||
|
*
|
||||||
* @param cmd Command to execute
|
* @param cmd Command to execute
|
||||||
* @return The list of results
|
* @return The list of results
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException
|
* @throws me.legrange.mikrotik.MikrotikApiException
|
||||||
*/
|
*/
|
||||||
public abstract List<Map<String, String>> execute(String cmd) throws MikrotikApiException;
|
public abstract List<Map<String, String>> execute(String cmd) throws MikrotikApiException;
|
||||||
|
|
||||||
/** execute a command and attach a result listener to receive it's results.
|
/**
|
||||||
*
|
* execute a command and attach a result listener to receive it's results.
|
||||||
|
*
|
||||||
* @param cmd Command to execute
|
* @param cmd Command to execute
|
||||||
* @param lis ResultListener that will receive the results
|
* @param lis ResultListener that will receive the results
|
||||||
* @return A command object that can be used to cancel the command.
|
* @return A command object that can be used to cancel the command.
|
||||||
* @throws MikrotikApiException
|
* @throws MikrotikApiException
|
||||||
*/
|
*/
|
||||||
public abstract String execute(String cmd, ResultListener lis) throws MikrotikApiException;
|
public abstract String execute(String cmd, ResultListener lis) throws MikrotikApiException;
|
||||||
|
|
||||||
/** cancel a command
|
/**
|
||||||
|
* cancel a command
|
||||||
|
*
|
||||||
* @param tag The tag of the command to cancel
|
* @param tag The tag of the command to cancel
|
||||||
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem canceling the command */
|
* @throws me.legrange.mikrotik.MikrotikApiException Thrown if there is a problem canceling the command
|
||||||
|
*/
|
||||||
public abstract void cancel(String tag) throws MikrotikApiException;
|
public abstract void cancel(String tag) throws MikrotikApiException;
|
||||||
|
|
||||||
|
|
||||||
/** set the command timeout. The command timeout is used to time out API
|
|
||||||
* commands after a specific time.
|
/**
|
||||||
*
|
* set the command timeout. The command timeout is used to time out API
|
||||||
|
* commands after a specific time.
|
||||||
|
* <p/>
|
||||||
* Note: This is not the same as the timeout value passed in the connect() and
|
* Note: This is not the same as the timeout value passed in the connect() and
|
||||||
* connectTLS() methods. This timeout is specific to synchronous commands, that
|
* connectTLS() methods. This timeout is specific to synchronous commands, that
|
||||||
* timeout is applied to opening the API socket.
|
* timeout is applied to opening the API socket.
|
||||||
*
|
*
|
||||||
* @param timeout The time out in milliseconds.
|
* @param timeout The time out in milliseconds.
|
||||||
* @throws MikrotikApiException Thrown if the timeout specified is invalid.
|
* @throws MikrotikApiException Thrown if the timeout specified is invalid.
|
||||||
*/
|
*/
|
||||||
public abstract void setTimeout(int timeout) throws MikrotikApiException;
|
public abstract void setTimeout(int timeout) throws MikrotikApiException;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -200,6 +200,11 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
private Integer _tag = 0;
|
private Integer _tag = 0;
|
||||||
private int timeout = ApiConnection.DEFAULT_COMMAND_TIMEOUT;
|
private int timeout = ApiConnection.DEFAULT_COMMAND_TIMEOUT;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws ApiConnectionException {
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* thread to read data from the socket and process it into Strings
|
* thread to read data from the socket and process it into Strings
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue