Merge branch 'master' of github.com:GideonLeGrange/mikrotik-java

This commit is contained in:
Gideon le Grange 2017-08-07 21:02:04 +02:00
commit b7a162099f
8 changed files with 39 additions and 71 deletions

1
.travis.yml Normal file
View File

@ -0,0 +1 @@
language: java

View File

@ -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);
} }

View File

@ -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);
} }

View File

@ -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
@ -176,7 +177,7 @@ public final class ApiConnectionImpl extends ApiConnection {
private Reader() { private Reader() {
super("Mikrotik API Reader"); super("Mikrotik API Reader");
} }
private String take() throws ApiConnectionException, ApiDataException { private String take() throws ApiConnectionException, ApiDataException {
Object val = null; Object val = null;
try { try {
@ -195,15 +196,13 @@ public final class ApiConnectionImpl extends ApiConnection {
private boolean isEmpty() { private boolean isEmpty() {
return queue.isEmpty(); return queue.isEmpty();
} }
@Override @Override
public void run() { public void run() {
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) {
@ -232,7 +231,7 @@ public final class ApiConnectionImpl extends ApiConnection {
private Processor() { private Processor() {
super("Mikrotik API Result Processor"); super("Mikrotik API Result Processor");
} }
@Override @Override
public void run() { public void run() {
while (connected) { while (connected) {
@ -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

View File

@ -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 {

View File

@ -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
*/ */

View File

@ -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();
} }
/** /**

View File

@ -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")); */
}
}