Fixed a bug in the async interface. Wrote an async example

This commit is contained in:
GideonLeGrange 2013-08-30 15:41:04 +02:00
parent b7a3cdfaea
commit 713084cd9d
5 changed files with 46 additions and 6 deletions

View File

@ -7,9 +7,9 @@ package examples;
public class Config { public class Config {
public static final String USERNAME = "admin"; public static final String USERNAME = "adept";
public static final String PASSWORD = ""; public static final String PASSWORD = "tzrx21j";
public static final String HOST = "10.0.1.3"; public static final String HOST = "196.44.35.96";
} }

View File

@ -0,0 +1,38 @@
package examples;
import java.util.Map;
import me.legrange.mikrotik.MikrotikApiException;
import me.legrange.mikrotik.ResultListener;
/**
* Example 4: Asynchronous results. Run a command and receive results for it asynchronously with a ResultListener
*
* @author gideon
*/
public class Example4 extends Example {
public static void main(String... args) throws Exception {
Example4 ex = new Example4();
ex.connect();
ex.test();
ex.disconnect();
}
private void test() throws MikrotikApiException, InterruptedException {
String id = con.execute("/interface/wireless/monitor .id=wlan1", new ResultListener() {
private int prev = 0;
public void receive(Map<String, String> result) {
int val = Integer.parseInt(result.get("signal-strength"));
String sym = (val == prev) ? " " : ((val < prev) ? "-" : "+");
System.out.printf("%d %s\n", val, sym);
prev = val;
}
});
// let it run for 60 seconds
Thread.sleep(60000);
con.cancel(id);
}
}

View File

@ -430,7 +430,7 @@ public class ApiConnection {
notify(); notify();
} }
public void receive(Result result) { public void receive(Map<String, String> result) {
results.add(result); results.add(result);
} }

View File

@ -4,7 +4,7 @@ package me.legrange.mikrotik;
* Internal representation of !done * Internal representation of !done
* @author GideonLeGrange * @author GideonLeGrange
*/ */
class Done extends Responsex { class Done extends Response {
Done(String tag) { Done(String tag) {
super(tag); super(tag);

View File

@ -1,11 +1,13 @@
package me.legrange.mikrotik; package me.legrange.mikrotik;
import java.util.Map;
/** /**
* Implement this interface to receive command results from the Mikrotik Api. * Implement this interface to receive command results from the Mikrotik Api.
* @author GideonLeGrange * @author GideonLeGrange
*/ */
public interface ResultListener { public interface ResultListener {
void receive(Result result); void receive(Map<String, String> result);
} }