Code cleanups
This commit is contained in:
parent
1f9eef7d02
commit
293c9b36fb
|
|
@ -6,10 +6,20 @@ package me.legrange.mikrotik;
|
||||||
*/
|
*/
|
||||||
public class ApiConnectionException extends MikrotikApiException {
|
public class ApiConnectionException extends MikrotikApiException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new exception.
|
||||||
|
*
|
||||||
|
* @param msg The message
|
||||||
|
*/
|
||||||
public ApiConnectionException(String msg) {
|
public ApiConnectionException(String msg) {
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new exception
|
||||||
|
* @param msg The message
|
||||||
|
* @param err The underlying cause
|
||||||
|
*/
|
||||||
public ApiConnectionException(String msg, Throwable err) {
|
public ApiConnectionException(String msg, Throwable err) {
|
||||||
super(msg, err);
|
super(msg, err);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,21 @@ package me.legrange.mikrotik;
|
||||||
*/
|
*/
|
||||||
public class MikrotikApiException extends Exception {
|
public class MikrotikApiException extends Exception {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new exception
|
||||||
|
* @param msg The message
|
||||||
|
*/
|
||||||
public MikrotikApiException(String msg) {
|
public MikrotikApiException(String msg) {
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new exception
|
||||||
|
* @param msg The message
|
||||||
|
* @param err The underlying cause
|
||||||
|
*/
|
||||||
public MikrotikApiException(String msg, Throwable err) {
|
public MikrotikApiException(String msg, Throwable err) {
|
||||||
super(msg, err);
|
super(msg, err);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ public final class ApiConnectionImpl extends 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 fact The socket factory used to construct the connection socket.
|
||||||
* @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.
|
||||||
* @param timeOut The connection timeout
|
* @param timeOut The connection timeout
|
||||||
|
|
@ -201,9 +202,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
while (connected) {
|
while (connected) {
|
||||||
try {
|
try {
|
||||||
String s = Util.decode(in);
|
String s = Util.decode(in);
|
||||||
if (s != null) {
|
|
||||||
put(s);
|
put(s);
|
||||||
}
|
|
||||||
} catch (ApiDataException ex) {
|
} catch (ApiDataException ex) {
|
||||||
put(ex);
|
put(ex);
|
||||||
} catch (ApiConnectionException ex) {
|
} catch (ApiConnectionException ex) {
|
||||||
|
|
@ -420,35 +419,22 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void queue(Response res) {
|
|
||||||
String tag = res.getTag();
|
|
||||||
if (tag != null) {
|
|
||||||
ResultListener rl = listeners.get(tag);
|
|
||||||
if (rl != null) {
|
|
||||||
if (res instanceof Result) {
|
|
||||||
rl.receive((Result) res);
|
|
||||||
} else {
|
|
||||||
// rl.error((Error)res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private final List<String> lines = new LinkedList<>();
|
private final List<String> lines = new LinkedList<>();
|
||||||
private String line;
|
private String line;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SyncListener implements ResultListener {
|
private static class SyncListener implements ResultListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void error(MikrotikApiException ex) {
|
public synchronized void error(MikrotikApiException ex) {
|
||||||
this.err = ex;
|
this.err = ex;
|
||||||
notify();
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void completed() {
|
public synchronized void completed() {
|
||||||
complete = true;
|
complete = true;
|
||||||
notify();
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized void completed(Done done) {
|
synchronized void completed(Done done) {
|
||||||
|
|
@ -458,7 +444,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
results.add(res);
|
results.add(res);
|
||||||
}
|
}
|
||||||
complete = true;
|
complete = true;
|
||||||
notify();
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -175,7 +175,7 @@ class Parser {
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
cmd.addProperty(props.toArray(new String[]{}));
|
cmd.addProperty(props.toArray(new String[props.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expect(Token...tokens) throws ParseException {
|
private void expect(Token...tokens) throws ParseException {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package me.legrange.mikrotik.impl;
|
package me.legrange.mikrotik.impl;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
import static me.legrange.mikrotik.impl.Scanner.Token.*;
|
import static me.legrange.mikrotik.impl.Scanner.Token.*;
|
||||||
/**
|
/**
|
||||||
* A simple scanner.
|
* A simple scanner.
|
||||||
|
|
@ -117,7 +118,7 @@ class Scanner {
|
||||||
text.append(c);
|
text.append(c);
|
||||||
nextChar();
|
nextChar();
|
||||||
}
|
}
|
||||||
String val = text.toString().toLowerCase();
|
String val = text.toString().toLowerCase(Locale.getDefault());
|
||||||
switch (val) {
|
switch (val) {
|
||||||
case "where":
|
case "where":
|
||||||
return WHERE;
|
return WHERE;
|
||||||
|
|
@ -184,17 +185,6 @@ class Scanner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* look ahead one character
|
|
||||||
*/
|
|
||||||
private char peek() {
|
|
||||||
if (pos < line.length()) {
|
|
||||||
return line.charAt(pos);
|
|
||||||
} else {
|
|
||||||
return '\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if the character matches the give expression
|
* check if the character matches the give expression
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -71,7 +72,7 @@ final class Util {
|
||||||
}
|
}
|
||||||
buf[i] = (byte) (c & 0xFF);
|
buf[i] = (byte) (c & 0xFF);
|
||||||
}
|
}
|
||||||
String res = new String(buf);
|
String res = new String(buf, Charset.forName("UTF-8"));
|
||||||
if (result.length() > 0) {
|
if (result.length() > 0) {
|
||||||
result.append("\n");
|
result.append("\n");
|
||||||
}
|
}
|
||||||
|
|
@ -121,11 +122,11 @@ final class Util {
|
||||||
* @return - converted string.
|
* @return - converted string.
|
||||||
*/
|
*/
|
||||||
static String hexStrToStr(String s) {
|
static String hexStrToStr(String s) {
|
||||||
String ret = "";
|
StringBuilder ret = new StringBuilder();
|
||||||
for (int i = 0; i < s.length(); i += 2) {
|
for (int i = 0; i < s.length(); i += 2) {
|
||||||
ret += (char) Integer.parseInt(s.substring(i, i + 2), 16);
|
ret.append((char) Integer.parseInt(s.substring(i, i + 2), 16));
|
||||||
}
|
}
|
||||||
return ret;
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
package me.legrange.mikrotik;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
|
|
||||||
public class ApiConnectionTest extends TestCase {
|
|
||||||
|
|
||||||
private static final String HOSTNAME = "192.168.88.1";
|
|
||||||
private static final String ADMIN = "admin";
|
|
||||||
private static final String PASSWORD = "plasma";
|
|
||||||
|
|
||||||
public void testConnectTLSStringInt() throws MikrotikApiException, InterruptedException {
|
|
||||||
/* ApiConnection connect = ApiConnection.connectTLS(HOSTNAME, 8729);
|
|
||||||
connect.login(ADMIN, PASSWORD);
|
|
||||||
List<Map<String, String>> result = connect.execute("/system/resource/print");
|
|
||||||
|
|
||||||
assertEquals(1, result.size());
|
|
||||||
assertEquals("MikroTik", result.get(0).get("platform")); */
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testConnectStringInt() throws MikrotikApiException, InterruptedException {
|
|
||||||
/* ApiConnection connect = ApiConnection.connect(HOSTNAME, 8728);
|
|
||||||
connect.login(ADMIN, PASSWORD);
|
|
||||||
List<Map<String, String>> result = connect.execute("/system/resource/print");
|
|
||||||
|
|
||||||
assertEquals(1, result.size());
|
|
||||||
assertEquals("MikroTik", result.get(0).get("platform")); */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue