diff --git a/src/main/java/examples/Config.java b/src/main/java/examples/Config.java index 50a1eb9..0e81b2a 100644 --- a/src/main/java/examples/Config.java +++ b/src/main/java/examples/Config.java @@ -9,7 +9,7 @@ public class Config { public static final String USERNAME = "admin"; public static final String PASSWORD = ""; - public static final String HOST = "10.0.0.1"; + public static final String HOST = "10.0.1.3"; } diff --git a/src/main/java/me/legrange/mikrotik/ApiConnection.java b/src/main/java/me/legrange/mikrotik/ApiConnection.java index 08b619a..9d0042b 100644 --- a/src/main/java/me/legrange/mikrotik/ApiConnection.java +++ b/src/main/java/me/legrange/mikrotik/ApiConnection.java @@ -24,6 +24,9 @@ public class ApiConnection { /** * 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. + * @return The ApiConnection */ public static ApiConnection connect(String host, int port) throws ApiConnectionException { ApiConnection con = new ApiConnection(); @@ -32,16 +35,18 @@ public 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. + * @return The ApiConnection */ - public static ApiConnection connect(String host) throws ApiConnectionException { + public static ApiConnection connect(String host) throws ApiConnectionException { return connect(host, DEFAULT_PORT); } /** - * State of connection + * Check the state of connection. * - * @return - if connection is established to router it returns true. + * @return if connection is established to router it returns true. */ public boolean isConnected() { return connected; @@ -63,6 +68,40 @@ public class ApiConnection { } } + /** + * Log in to the remote router. + * + * @param username - username of the user on the router + * @param password - password for the user + */ + public void login(String username, String password) throws MikrotikApiException, ApiCommandException, InterruptedException { + List list = execute("/login"); + Result res = list.get(0); + String hash = res.get("ret"); + String chal = Util.hexStrToStr("00") + new String(makePass(password)) + Util.hexStrToStr(hash); + chal = Util.hashMD5(chal); + execute("/login name=" + username + " response=00" + chal); + } + + /** execute a command and return a list of results. + * @param cmd Command to execute + * @return The list of results + */ + public List execute(String cmd) throws MikrotikApiException { + return execute(Parser.parse(cmd)); + } + + /** execute a command and attach a result listener to receive it's results. + * + * @param cmd Command to execute + * @param lis ResultListener that will receive the results + * @return A command object that can be used to cancel the command. + * @throws MikrotikApiException + */ + public Command execute(String cmd, ResultListener lis) throws MikrotikApiException { + return execute(Parser.parse(cmd), lis); + } + /** cancel a command */ public void cancel(Command can) throws MikrotikApiException { Command cmd = new Command("/cancel"); @@ -70,35 +109,12 @@ public class ApiConnection { execute(cmd); } - /** - * set up method that will log you in - * - * @param name - username of the user on the router - * @param password - password for the user - * @return - */ - public void login(String name, String pwd) throws MikrotikApiException, ApiCommandException, InterruptedException { - List list = execute("/login"); - Result res = list.get(0); - String hash = res.get("ret"); - String chal = Util.hexStrToStr("00") + new String(makePass(pwd)) + Util.hexStrToStr(hash); - chal = Util.hashMD5(chal); - execute("/login name=" + name + " response=00" + chal); - } - private List execute(Command cmd) throws MikrotikApiException { SyncListener l = new SyncListener(); execute(cmd, l); return l.getResults(); } - public List execute(String cmd) throws MikrotikApiException { - return execute(Parser.parse(cmd)); - } - - public Command execute(String cmd, ResultListener lis) throws MikrotikApiException { - return execute(Parser.parse(cmd), lis); - } private Command execute(Command cmd, ResultListener lis) throws MikrotikApiException { String tag = nextTag(); @@ -424,6 +440,7 @@ public class ApiConnection { } return results; } + private List results = new LinkedList(); private Error err; } diff --git a/src/main/java/me/legrange/mikrotik/Result.java b/src/main/java/me/legrange/mikrotik/Result.java index 1d42f17..9fc9c70 100644 --- a/src/main/java/me/legrange/mikrotik/Result.java +++ b/src/main/java/me/legrange/mikrotik/Result.java @@ -11,11 +11,6 @@ import java.util.Set; */ public class Result extends Response implements Map { - public Result() { - super(null); - this.map = new HashMap(); - } - public String get(String key) { return map.get(key); } @@ -72,7 +67,12 @@ public class Result extends Response implements Map { public Set> entrySet() { return map.entrySet(); } - + + Result() { + super(null); + this.map = new HashMap(); + } + private final Map map; }