Add clearer example java api usage

This commit is contained in:
Jeremy Lakeman 2014-02-20 15:06:05 +10:30
parent b8971e51f4
commit 693d1e9b60
4 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package org.servalproject.servaldna;
import java.util.List;
/**
* Created by jeremy on 20/02/14.
*/
public class ResultList<T> implements AsyncResult<T> {
private final List<T> results;
public ResultList(List<T> results){
this.results = results;
}
@Override
public void result(T nextResult) {
results.add(nextResult);
}
}

View File

@ -115,12 +115,29 @@ public class ServalDCommand
if (columnName.equals("tries"))
tries = (int)value;
}
@Override
public String toString() {
return "Status{" +
"pid=" + pid +
", tries=" + tries +
", instancePath='" + instancePath + '\'' +
", status='" + status + '\'' +
'}';
}
}
/** Start the servald server process if it is not already running.
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
public static Status serverStart()
throws ServalDFailureException {
Status result = new Status();
result.setResult(command(result, "start"));
return result;
}
public static Status serverStart(String execPath)
throws ServalDFailureException {
Status result = new Status();
@ -170,6 +187,15 @@ public class ServalDCommand
e.printStackTrace();
}
}
@Override
public String toString() {
return "IdentityResult{" +
"did='" + did + '\'' +
", name='" + name + '\'' +
", subscriberId=" + subscriberId +
'}';
}
}
public static IdentityResult keyringAdd()
@ -480,6 +506,7 @@ public class ServalDCommand
return (int)result.count;
}
// Note that the result values will only have a subscriber id
public static int idPeers(AsyncResult<IdentityResult> results) throws ServalDFailureException {
return idPeers(new JniResultList<IdentityResult>(results) {
@Override

View File

@ -0,0 +1,46 @@
package org.servalproject.test;
import org.servalproject.servaldna.ResultList;
import org.servalproject.servaldna.ServalDCommand;
import org.servalproject.servaldna.ServalDFailureException;
import java.util.LinkedList;
import java.util.List;
/**
* Created by jeremy on 20/02/14.
*/
public class CommandLine {
static void getPeers() throws ServalDFailureException {
List<ServalDCommand.IdentityResult> peers = new LinkedList<ServalDCommand.IdentityResult>();
ServalDCommand.idPeers(new ResultList<ServalDCommand.IdentityResult>(peers));
for(ServalDCommand.IdentityResult i:peers){
ServalDCommand.IdentityResult details = ServalDCommand.reverseLookup(i.subscriberId);
System.out.println(details.getResult()==0?details.toString():i.toString());
}
}
public static void main(String... args){
if (args.length<1)
return;
try {
String methodName = args[0];
Object result=null;
if (methodName.equals("start"))
result=ServalDCommand.serverStart();
if (methodName.equals("stop"))
result=ServalDCommand.serverStop();
if (methodName.equals("peers"))
getPeers();
if (result!=null)
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -87,5 +87,26 @@ test_help() {
assertStdoutGrep 'Serval DNA version '
}
doc_PeerList="Get peer details via JNI"
setup_PeerList() {
configure_servald_server() {
add_servald_interface
}
setup
foreach_instance +A +B create_single_identity
start_servald_instances +A +B
set_instance +A
}
test_PeerList() {
execute --core-backtrace java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" org.servalproject.test.CommandLine 'peers'
assertStdoutGrep "$SIDB"
tfw_cat --stdout
}
teardown_PeerList() {
stop_all_servald_servers
kill_all_servald_processes
assert_no_servald_processes
report_all_servald_servers
}
runTests "$@"