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;
|
|
|
|
import org.servalproject.servaldna.MdpDnaLookup;
|
|
|
|
import org.servalproject.servaldna.MdpSocket;
|
2014-02-20 04:36:05 +00:00
|
|
|
import org.servalproject.servaldna.ResultList;
|
|
|
|
import org.servalproject.servaldna.ServalDCommand;
|
|
|
|
import org.servalproject.servaldna.ServalDFailureException;
|
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){
|
|
|
|
System.out.println(new Date().toString()+" "+msg);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
System.out.println(details.getResult()==0?details.toString():i.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 06:09:47 +00:00
|
|
|
static void lookup(String did) throws IOException, InterruptedException, ServalDFailureException {
|
|
|
|
MdpSocket.loopbackMdpPort = Integer.parseInt(System.getenv("SERVAL_MDP_INET_PORT"));
|
|
|
|
ChannelSelector selector = new ChannelSelector();
|
|
|
|
MdpDnaLookup lookup = new MdpDnaLookup(selector, new AsyncResult<ServalDCommand.LookupResult>() {
|
|
|
|
@Override
|
|
|
|
public void result(ServalDCommand.LookupResult nextResult) {
|
|
|
|
System.out.println(nextResult.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
lookup.sendRequest(SubscriberId.broadcastSid, did);
|
|
|
|
Thread.sleep(3000);
|
|
|
|
lookup.close();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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"))
|
|
|
|
lookup(args.length>=2 ? args[1] : "");
|
2014-02-20 04:36:05 +00:00
|
|
|
|
|
|
|
if (result!=null)
|
|
|
|
System.out.println(result.toString());
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|