Created version 2.0.0 branch to fix a few non-backwards compatibile problems.
- Added error() and completed() to ResultListener - Removed ResponseListener - Refactored internals and examples to account for these changes.
This commit is contained in:
parent
f2f3614ff8
commit
3d04b20dca
|
|
@ -7,7 +7,7 @@ package examples;
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
|
|
||||||
public static final String HOST = "10.0.1.134";
|
public static final String HOST = "10.0.1.1";
|
||||||
public static final String USERNAME = "admin";
|
public static final String USERNAME = "admin";
|
||||||
public static final String PASSWORD = "";
|
public static final String PASSWORD = "";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,14 +19,24 @@ public class Example4 extends Example {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void test() throws MikrotikApiException, InterruptedException {
|
private void test() throws MikrotikApiException, InterruptedException {
|
||||||
String id = con.execute("/interface/wireless/monitor .id=wlan1", new ResultListener() {
|
String id = con.execute("/interface/wireless/monitor .id=wlan1 .proplist=signal-strength", new ResultListener() {
|
||||||
private int prev = 0;
|
private int prev = 0;
|
||||||
|
|
||||||
public void receive(Map<String, String> result) {
|
public void receive(Map<String, String> result) {
|
||||||
int val = Integer.parseInt(result.get("signal-strength"));
|
System.out.println(result);
|
||||||
|
/* int val = Integer.parseInt(result.get("signal-strength"));
|
||||||
String sym = (val == prev) ? " " : ((val < prev) ? "-" : "+");
|
String sym = (val == prev) ? " " : ((val < prev) ? "-" : "+");
|
||||||
System.out.printf("%d %s\n", val, sym);
|
System.out.printf("%d %s\n", val, sym);
|
||||||
prev = val;
|
prev = val;
|
||||||
|
*/ }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void error(MikrotikApiException ex) {
|
||||||
|
throw new RuntimeException(ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void completed() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package examples;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import me.legrange.mikrotik.MikrotikApiException;
|
import me.legrange.mikrotik.MikrotikApiException;
|
||||||
import me.legrange.mikrotik.ResponseListener;
|
import me.legrange.mikrotik.ResultListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example 5: Asynchronous results, with error and completion. Run a command and receive results, errors and completion notification for it asynchronously with a ResponseListener
|
* Example 5: Asynchronous results, with error and completion. Run a command and receive results, errors and completion notification for it asynchronously with a ResponseListener
|
||||||
|
|
@ -20,7 +20,7 @@ public class Example5 extends Example {
|
||||||
|
|
||||||
private void test() throws MikrotikApiException, InterruptedException {
|
private void test() throws MikrotikApiException, InterruptedException {
|
||||||
boolean completed = false;
|
boolean completed = false;
|
||||||
String id = con.execute("/interface/wireless/monitor .id=wlan1", new ResponseListener() {
|
String id = con.execute("/interface/wireless/monitor .id=wlan1", new ResultListener() {
|
||||||
private int prev = 0;
|
private int prev = 0;
|
||||||
|
|
||||||
public void receive(Map<String, String> result) {
|
public void receive(Map<String, String> result) {
|
||||||
|
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
package me.legrange.mikrotik;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A listener that receives life cycle command events from the Mikrotik API,
|
|
||||||
* and not just results.
|
|
||||||
* @author GideonLeGrange
|
|
||||||
*/
|
|
||||||
public interface ResponseListener extends ResultListener {
|
|
||||||
|
|
||||||
/** called if the command associated with this listener experiences an error */
|
|
||||||
void error(MikrotikApiException ex);
|
|
||||||
|
|
||||||
/** called when the command associated with this listener is done */
|
|
||||||
void completed();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -9,5 +9,12 @@ import java.util.Map;
|
||||||
public interface ResultListener {
|
public interface ResultListener {
|
||||||
|
|
||||||
void receive(Map<String, String> result);
|
void receive(Map<String, String> result);
|
||||||
|
|
||||||
|
/** called if the command associated with this listener experiences an error
|
||||||
|
* @param ex Exception encountered */
|
||||||
|
void error(MikrotikApiException ex);
|
||||||
|
|
||||||
|
/** called when the command associated with this listener is done */
|
||||||
|
void completed();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
* @param port The TCP port to use.
|
* @param port The TCP port to use.
|
||||||
* @param secure Is TLS required
|
* @param secure Is TLS required
|
||||||
* @return The ApiConnection
|
* @return The ApiConnection
|
||||||
* @throws me.legrange.mikrotik.ApiConnectionException Thrown if there is a problem connecting
|
* @throws me.legrange.mikrotik.ApiConnectionException Thrown if there is a
|
||||||
|
* problem connecting
|
||||||
*/
|
*/
|
||||||
public static ApiConnection connect(String host, int port, boolean secure) throws ApiConnectionException {
|
public static ApiConnection connect(String host, int port, boolean secure) throws ApiConnectionException {
|
||||||
ApiConnectionImpl con = new ApiConnectionImpl();
|
ApiConnectionImpl con = new ApiConnectionImpl();
|
||||||
|
|
@ -53,7 +54,9 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect from the remote API
|
* Disconnect from the remote API
|
||||||
* @throws me.legrange.mikrotik.ApiConnectionException Thrown if there is a problem disconnecting
|
*
|
||||||
|
* @throws me.legrange.mikrotik.ApiConnectionException Thrown if there is a
|
||||||
|
* problem disconnecting
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void disconnect() throws ApiConnectionException {
|
public void disconnect() throws ApiConnectionException {
|
||||||
|
|
@ -284,22 +287,15 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
if (l != null) {
|
if (l != null) {
|
||||||
if (res instanceof Result) {
|
if (res instanceof Result) {
|
||||||
l.receive((Result) res);
|
l.receive((Result) res);
|
||||||
} else {
|
} else if (res instanceof Done) {
|
||||||
if (res instanceof Done) {
|
if (l instanceof SyncListener) {
|
||||||
listeners.remove(res.getTag());
|
((SyncListener) l).completed((Done) res);
|
||||||
}
|
} else {
|
||||||
if (l instanceof ResponseListener) {
|
l.completed();
|
||||||
ResponseListener rl = (ResponseListener) l;
|
|
||||||
if (res instanceof Done) {
|
|
||||||
if (rl instanceof SyncListener) {
|
|
||||||
((SyncListener) rl).completed((Done) res);
|
|
||||||
} else {
|
|
||||||
rl.completed();
|
|
||||||
}
|
|
||||||
} else if (res instanceof Error) {
|
|
||||||
rl.error(new ApiCommandException((Error) res));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
listeners.remove(res.getTag());
|
||||||
|
} else if (res instanceof Error) {
|
||||||
|
l.error(new ApiCommandException((Error) res));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -322,16 +318,17 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
if (line == null) {
|
if (line == null) {
|
||||||
nextLine();
|
nextLine();
|
||||||
}
|
}
|
||||||
if (line.equals("!re")) {
|
switch (line) {
|
||||||
return unpackRe();
|
case "!re":
|
||||||
} else if (line.equals("!done")) {
|
return unpackRe();
|
||||||
return unpackDone();
|
case "!done":
|
||||||
} else if (line.equals("!trap")) {
|
return unpackDone();
|
||||||
return unpackError();
|
case "!trap":
|
||||||
} else if (line.equals("!halt")) {
|
return unpackError();
|
||||||
return unpackError();
|
case "!halt":
|
||||||
} else {
|
return unpackError();
|
||||||
throw new ApiDataException(String.format("Unexpected line '%s'", line));
|
default:
|
||||||
|
throw new ApiDataException(String.format("Unexpected line '%s'", line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -346,8 +343,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
if (parts.length == 3) {
|
if (parts.length == 3) {
|
||||||
if (!parts[2].endsWith("\r")) {
|
if (!parts[2].endsWith("\r")) {
|
||||||
res.put(parts[1], parts[2]);
|
res.put(parts[1], parts[2]);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
res.put(parts[1], unpackMultiLine(parts[2]));
|
res.put(parts[1], unpackMultiLine(parts[2]));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -379,7 +375,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
} while (line.endsWith("\r"));
|
} while (line.endsWith("\r"));
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Done unpackDone() throws MikrotikApiException {
|
private Done unpackDone() throws MikrotikApiException {
|
||||||
Done done = new Done(null);
|
Done done = new Done(null);
|
||||||
if (hasNextLine()) {
|
if (hasNextLine()) {
|
||||||
|
|
@ -451,7 +447,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
private String line;
|
private String line;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SyncListener implements ResponseListener {
|
private class SyncListener implements ResultListener {
|
||||||
|
|
||||||
public synchronized void error(MikrotikApiException ex) {
|
public synchronized void error(MikrotikApiException ex) {
|
||||||
this.err = ex;
|
this.err = ex;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue