From 1d2a896f7e3dfb7883b7c33a687d49335fab2009 Mon Sep 17 00:00:00 2001 From: GideonLeGrange Date: Mon, 30 Dec 2013 13:41:06 +0200 Subject: [PATCH] Edited examples --- README.md | 13 ++++++-- src/main/java/examples/Example6.java | 49 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/main/java/examples/Example6.java diff --git a/README.md b/README.md index 6e7f261..94db6e6 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ con.execute("/system/reboot"); // execute a command con.disconnect(); // disconnect from router ``` +Reading data +------------ + 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> 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 @@ -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) - is cancelled like this: +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 canceled like this: ```java con.cancel(tag); diff --git a/src/main/java/examples/Example6.java b/src/main/java/examples/Example6.java new file mode 100644 index 0000000..8bc4903 --- /dev/null +++ b/src/main/java/examples/Example6.java @@ -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 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); + } +}