2014-02-20 04:36:05 +00:00
|
|
|
package org.servalproject.test;
|
|
|
|
|
2014-02-21 06:09:47 +00:00
|
|
|
import org.servalproject.servaldna.AsyncResult;
|
|
|
|
import org.servalproject.servaldna.ChannelSelector;
|
2015-05-25 02:16:37 +00:00
|
|
|
import org.servalproject.servaldna.IJniServer;
|
2014-02-21 06:09:47 +00:00
|
|
|
import org.servalproject.servaldna.MdpDnaLookup;
|
2014-05-09 00:32:44 +00:00
|
|
|
import org.servalproject.servaldna.MdpServiceLookup;
|
2014-02-20 04:36:05 +00:00
|
|
|
import org.servalproject.servaldna.ResultList;
|
|
|
|
import org.servalproject.servaldna.ServalDCommand;
|
|
|
|
import org.servalproject.servaldna.ServalDFailureException;
|
2014-06-20 01:25:09 +00:00
|
|
|
import org.servalproject.servaldna.ServalDInterfaceException;
|
|
|
|
import org.servalproject.servaldna.ServerControl;
|
2014-02-21 06:09:47 +00:00
|
|
|
import org.servalproject.servaldna.SubscriberId;
|
2014-02-20 04:36:05 +00:00
|
|
|
|
2014-02-21 06:09:47 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Date;
|
2014-02-20 04:36:05 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jeremy on 20/02/14.
|
|
|
|
*/
|
|
|
|
public class CommandLine {
|
|
|
|
|
2014-02-21 06:09:47 +00:00
|
|
|
static void log(String msg){
|
2015-12-14 00:47:50 +00:00
|
|
|
System.err.println(new Date().toString()+" "+msg);
|
2014-02-21 06:09:47 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:36:05 +00:00
|
|
|
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);
|
2016-01-11 05:53:32 +00:00
|
|
|
System.out.println(details.getResult()==0?details.toString():i.toString());
|
2014-02-20 04:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 01:25:09 +00:00
|
|
|
static void lookup(String did) throws IOException, InterruptedException, ServalDInterfaceException {
|
|
|
|
MdpDnaLookup lookup = new ServerControl().getMdpDnaLookup(new ChannelSelector(), new AsyncResult<ServalDCommand.LookupResult>() {
|
2014-02-21 06:09:47 +00:00
|
|
|
@Override
|
|
|
|
public void result(ServalDCommand.LookupResult nextResult) {
|
2016-01-11 05:53:32 +00:00
|
|
|
System.out.println(nextResult.toString());
|
2014-02-21 06:09:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
lookup.sendRequest(SubscriberId.broadcastSid, did);
|
|
|
|
Thread.sleep(3000);
|
|
|
|
lookup.close();
|
|
|
|
}
|
|
|
|
|
2014-06-20 01:25:09 +00:00
|
|
|
static void service(String pattern) throws IOException, InterruptedException, ServalDInterfaceException {
|
2014-06-20 05:44:00 +00:00
|
|
|
MdpServiceLookup lookup = new ServerControl().getMdpServiceLookup(new ChannelSelector(), new AsyncResult<MdpServiceLookup.ServiceResult>() {
|
2014-05-09 00:32:44 +00:00
|
|
|
@Override
|
|
|
|
public void result(MdpServiceLookup.ServiceResult nextResult) {
|
2016-01-11 05:53:32 +00:00
|
|
|
System.out.println(nextResult.toString());
|
2014-05-09 00:32:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
lookup.sendRequest(SubscriberId.broadcastSid, pattern);
|
|
|
|
Thread.sleep(3000);
|
|
|
|
lookup.close();
|
|
|
|
}
|
|
|
|
|
2015-05-25 02:16:37 +00:00
|
|
|
private static Runnable server = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-12-14 00:47:50 +00:00
|
|
|
ServalDCommand.server(new IJniServer() {
|
|
|
|
@Override
|
|
|
|
public long aboutToWait(long now, long nextRun, long nextWake) {
|
|
|
|
return nextWake;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void wokeUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void started(String instancePath, int pid, int mdpPort, int httpPort) {
|
|
|
|
System.err.println("Started instance " + instancePath);
|
|
|
|
synchronized (server) {
|
|
|
|
server.notifyAll();
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
2015-12-14 00:47:50 +00:00
|
|
|
}
|
|
|
|
}, "", new String[]{""});
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private static class ServerStopped extends RuntimeException{}
|
|
|
|
|
|
|
|
private static void server() throws InterruptedException, ServalDFailureException {
|
2015-12-14 00:47:50 +00:00
|
|
|
System.err.println("Starting server thread");
|
2015-05-25 02:16:37 +00:00
|
|
|
Thread serverThread = new Thread(server, "server");
|
|
|
|
serverThread.start();
|
|
|
|
|
2015-12-14 00:47:50 +00:00
|
|
|
System.err.println("Waiting for server to start");
|
|
|
|
synchronized (server) {
|
2015-05-25 02:16:37 +00:00
|
|
|
server.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
ServalDCommand.configSync();
|
2015-12-14 00:47:50 +00:00
|
|
|
Thread.sleep(500);
|
2015-05-25 02:16:37 +00:00
|
|
|
|
2015-12-14 00:47:50 +00:00
|
|
|
// Note, we don't really support stopping the server cleanly from within the JVM.
|
|
|
|
System.exit(0);
|
2015-05-25 02:16:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:36:05 +00:00
|
|
|
public static void main(String... args){
|
|
|
|
if (args.length<1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
String methodName = args[0];
|
|
|
|
Object result=null;
|
2015-05-25 02:16:37 +00:00
|
|
|
if (methodName.equals("server"))
|
|
|
|
server();
|
2014-02-20 04:36:05 +00:00
|
|
|
if (methodName.equals("start"))
|
|
|
|
result=ServalDCommand.serverStart();
|
|
|
|
if (methodName.equals("stop"))
|
|
|
|
result=ServalDCommand.serverStop();
|
|
|
|
if (methodName.equals("peers"))
|
|
|
|
getPeers();
|
2014-02-21 06:09:47 +00:00
|
|
|
if (methodName.equals("lookup"))
|
2014-05-09 00:32:44 +00:00
|
|
|
lookup(args.length >= 2 ? args[1] : "");
|
|
|
|
if (methodName.equals("service"))
|
|
|
|
service(args.length >= 2 ? args[1] : "");
|
2014-02-20 04:36:05 +00:00
|
|
|
|
|
|
|
if (result!=null)
|
2015-12-14 00:47:50 +00:00
|
|
|
System.err.println(result.toString());
|
2014-02-20 04:36:05 +00:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|