diff --git a/java/org/servalproject/servaldna/ResultList.java b/java/org/servalproject/servaldna/ResultList.java new file mode 100644 index 00000000..2ef11955 --- /dev/null +++ b/java/org/servalproject/servaldna/ResultList.java @@ -0,0 +1,18 @@ +package org.servalproject.servaldna; + +import java.util.List; + +/** + * Created by jeremy on 20/02/14. + */ +public class ResultList implements AsyncResult { + private final List results; + public ResultList(List results){ + this.results = results; + } + + @Override + public void result(T nextResult) { + results.add(nextResult); + } +} diff --git a/java/org/servalproject/servaldna/ServalDCommand.java b/java/org/servalproject/servaldna/ServalDCommand.java index 4feb0851..315336e1 100644 --- a/java/org/servalproject/servaldna/ServalDCommand.java +++ b/java/org/servalproject/servaldna/ServalDCommand.java @@ -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 */ + 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 results) throws ServalDFailureException { return idPeers(new JniResultList(results) { @Override diff --git a/java/org/servalproject/test/CommandLine.java b/java/org/servalproject/test/CommandLine.java new file mode 100644 index 00000000..635a7cd0 --- /dev/null +++ b/java/org/servalproject/test/CommandLine.java @@ -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 peers = new LinkedList(); + ServalDCommand.idPeers(new ResultList(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(); + } + } +} diff --git a/tests/jni b/tests/jni index 2df34f00..88edd45f 100755 --- a/tests/jni +++ b/tests/jni @@ -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 "$@"