Edited examples

This commit is contained in:
GideonLeGrange 2013-12-30 13:41:06 +02:00
parent 3bfaaa26a9
commit 1d2a896f7e
2 changed files with 59 additions and 3 deletions

View File

@ -26,6 +26,9 @@ con.execute("/system/reboot"); // execute a command
con.disconnect(); // disconnect from router con.disconnect(); // disconnect from router
``` ```
Reading data
------------
A simple example that returns a result: Print all interfaces. A simple example that returns a result: Print all interfaces.
@ -48,7 +51,11 @@ The same query, but we only want certain result fields names Print all interface
List<Map<String, String>> rs = con.execute("/interface/print where type=vlan return name"); List<Map<String, String>> rs = con.execute("/interface/print where type=vlan return name");
``` ```
We can run asynchrynous commands:
Asynchronous commands
---------------------
We can run asynchronous commands:
This example shows how to run '/interface wireless monitor' and have the result sent to a listener object, which prints it This example shows how to run '/interface wireless monitor' and have the result sent to a listener object, which prints it
@ -64,8 +71,8 @@ String tag = con.execute("/interface/wireless/monitor .id=wlan1 return signal-to
); );
``` ```
The above command will run and send results asynchrynously as they become available, until it is canceled. The command (identified by the unique String retruned) The above command will run and send results asynchronously as they become available, until it is canceled. The command (identified by the unique String retruned)
is cancelled like this: is canceled like this:
```java ```java
con.cancel(tag); con.cancel(tag);

View File

@ -0,0 +1,49 @@
package examples;
import java.util.Map;
import me.legrange.mikrotik.MikrotikApiException;
import me.legrange.mikrotik.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
*
* @author gideon
*/
public class Example5 extends Example {
public static void main(String... args) throws Exception {
Example5 ex = new Example5();
ex.connect();
ex.test();
ex.disconnect();
}
private void test() throws MikrotikApiException, InterruptedException {
boolean completed = false;
String id = con.execute("/interface/wireless/monitor .id=wlan1", new ResponseListener() {
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;
}
public void error(MikrotikApiException ex) {
System.out.printf("An error ocurred: %s\n", ex.getMessage());
ex.printStackTrace();
}
public void completed() {
System.out.printf("The request has been completed\n");
}
});
// let it run for 60 seconds
Thread.sleep(10000);
con.cancel(id);
Thread.sleep(2000);
}
}