Added complex async exmple

This commit is contained in:
GideonLeGrange 2013-12-23 11:52:07 +02:00
parent daf1ff9518
commit f242144496
4 changed files with 53 additions and 47 deletions

45
pom.xml
View File

@ -43,7 +43,7 @@
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version> <version>2.9.1</version>
<configuration> <configuration>
<excludePackageNames>example</excludePackageNames> <excludePackageNames>example;*.impl</excludePackageNames>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -62,46 +62,3 @@
</dependencies> </dependencies>
</project> </project>
##
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven</groupId>
<artifactId>maven</artifactId>
<packaging>jar</packaging>
<name>Maven core</name>
<version>2.0</version>
<description>The maven main core project description</description>
<url>http://maven.apache.org</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>http://svn.apache.org/viewcvs.cgi/maven</url>
<connection>http://svn.apache.org/viewcvs.cgi/maven</connection>
</scm>
<developers>
<developer>
<id>...</id>
<name>...</name>
<email>...</email>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</dependency>
...
</dependencies>
<!--
AVOID RELEASE REPOSITORY/PLUGINREPOSITORY:
<repositories></repositories>
<pluginRepositories></pluginRepositories>
-->
</project>

View File

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

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

View File

@ -12,9 +12,9 @@ import me.legrange.mikrotik.impl.ApiConnectionImpl;
*/ */
public abstract class ApiConnection { public abstract class ApiConnection {
/** default TCP port used by Mikrotik API */
public static final int DEFAULT_PORT = 8728; public static final int DEFAULT_PORT = 8728;
/** /**
* 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 host The host to which to connect. * @param host The host to which to connect.