2016-10-12 17:08:26 +10:30
|
|
|
/**
|
|
|
|
* Copyright (C) 2016 Flinders University
|
|
|
|
* Copyright (C) 2014 Serval Project Inc.
|
|
|
|
*
|
|
|
|
* This file is part of Serval Software (http://www.servalproject.org)
|
|
|
|
*
|
|
|
|
* Serval Software is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This source code is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this source code; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2014-02-12 13:07:31 +10:30
|
|
|
package org.servalproject.test;
|
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
import org.servalproject.servaldna.AsyncResult;
|
|
|
|
import org.servalproject.servaldna.ChannelSelector;
|
|
|
|
import org.servalproject.servaldna.IJniServer;
|
|
|
|
import org.servalproject.servaldna.MdpDnaLookup;
|
|
|
|
import org.servalproject.servaldna.MdpServiceLookup;
|
|
|
|
import org.servalproject.servaldna.ResultList;
|
2014-02-12 13:07:31 +10:30
|
|
|
import org.servalproject.servaldna.ServalDCommand;
|
2014-02-24 09:57:51 +10:30
|
|
|
import org.servalproject.servaldna.ServalDFailureException;
|
2016-10-12 17:08:26 +10:30
|
|
|
import org.servalproject.servaldna.ServalDInterfaceException;
|
|
|
|
import org.servalproject.servaldna.ServerControl;
|
|
|
|
import org.servalproject.servaldna.SubscriberId;
|
2014-02-12 13:07:31 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2014-02-12 13:07:31 +10:30
|
|
|
|
|
|
|
class ServalDTests
|
|
|
|
{
|
2016-10-12 17:08:26 +10:30
|
|
|
static void log(String msg) {
|
|
|
|
System.err.println(new Date().toString()+" "+msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-24 09:57:51 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
static void lookup(String did) throws IOException, InterruptedException, ServalDInterfaceException {
|
|
|
|
MdpDnaLookup lookup = new ServerControl().getMdpDnaLookup(new ChannelSelector(), new AsyncResult<ServalDCommand.LookupResult>() {
|
2014-02-24 09:57:51 +10:30
|
|
|
@Override
|
2016-10-12 17:08:26 +10:30
|
|
|
public void result(ServalDCommand.LookupResult nextResult) {
|
|
|
|
System.out.println(nextResult.toString());
|
2014-02-24 09:57:51 +10:30
|
|
|
}
|
2016-10-12 17:08:26 +10:30
|
|
|
});
|
|
|
|
lookup.sendRequest(SubscriberId.broadcastSid, did);
|
|
|
|
Thread.sleep(3000);
|
|
|
|
lookup.close();
|
|
|
|
}
|
2014-02-24 09:57:51 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
static void service(String pattern) throws IOException, InterruptedException, ServalDInterfaceException {
|
|
|
|
MdpServiceLookup lookup = new ServerControl().getMdpServiceLookup(new ChannelSelector(), new AsyncResult<MdpServiceLookup.ServiceResult>() {
|
2014-02-24 09:57:51 +10:30
|
|
|
@Override
|
2016-10-12 17:08:26 +10:30
|
|
|
public void result(MdpServiceLookup.ServiceResult nextResult) {
|
|
|
|
System.out.println(nextResult.toString());
|
2014-02-24 09:57:51 +10:30
|
|
|
}
|
2016-10-12 17:08:26 +10:30
|
|
|
});
|
|
|
|
lookup.sendRequest(SubscriberId.broadcastSid, pattern);
|
|
|
|
Thread.sleep(3000);
|
|
|
|
lookup.close();
|
|
|
|
}
|
2014-02-24 09:57:51 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
private static class ServerRunnable implements Runnable {
|
|
|
|
public boolean running = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ServalDCommand.server(new IJniServer() {
|
|
|
|
@Override
|
|
|
|
public long aboutToWait(long now, long nextRun, long nextWake) {
|
|
|
|
return nextWake;
|
2014-02-24 09:57:51 +10:30
|
|
|
}
|
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
@Override
|
|
|
|
public void wokeUp() {
|
|
|
|
}
|
2014-02-24 09:57:51 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
@Override
|
|
|
|
public void started(String instancePath, int pid, int mdpPort, int httpPort) {
|
|
|
|
System.out.println("Started server pid=" + pid + " instance=" + instancePath);
|
|
|
|
synchronized (server) {
|
|
|
|
running = true;
|
|
|
|
server.notifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "", new String[]{""});
|
2014-02-24 09:57:51 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
synchronized (server) {
|
|
|
|
running = false;
|
|
|
|
server.notifyAll();
|
2014-02-24 09:57:51 +10:30
|
|
|
}
|
2016-10-12 17:08:26 +10:30
|
|
|
}
|
|
|
|
}
|
2014-02-24 09:57:51 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
private static ServerRunnable server = new ServerRunnable();
|
|
|
|
|
|
|
|
private static class ServerStopped extends RuntimeException{}
|
|
|
|
|
|
|
|
private static void server() throws InterruptedException, ServalDFailureException {
|
|
|
|
System.out.println("Starting server thread");
|
|
|
|
Thread serverThread = new Thread(server, "server");
|
|
|
|
serverThread.start();
|
|
|
|
|
|
|
|
synchronized (server) {
|
|
|
|
while (!server.running) {
|
|
|
|
System.out.println("Waiting for server to start");
|
|
|
|
server.wait();
|
2014-02-24 09:57:51 +10:30
|
|
|
}
|
2016-10-12 17:08:26 +10:30
|
|
|
}
|
2014-02-24 09:57:51 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
synchronized (server) {
|
|
|
|
while (server.running) {
|
|
|
|
System.out.println("Waiting for server to stop");
|
|
|
|
server.wait();
|
2014-02-24 09:57:51 +10:30
|
|
|
}
|
2016-10-12 17:08:26 +10:30
|
|
|
}
|
2014-02-24 09:57:51 +10:30
|
|
|
}
|
|
|
|
|
2014-02-19 15:35:08 +10:30
|
|
|
public static void main(String... args)
|
2014-02-12 13:07:31 +10:30
|
|
|
{
|
|
|
|
try {
|
2016-10-12 17:08:26 +10:30
|
|
|
String methodName = args[0];
|
|
|
|
Object result = null;
|
|
|
|
if (methodName.equals("server"))
|
|
|
|
server();
|
|
|
|
else if (methodName.equals("start"))
|
|
|
|
result=ServalDCommand.serverStart();
|
|
|
|
else if (methodName.equals("stop"))
|
|
|
|
result=ServalDCommand.serverStop();
|
|
|
|
else if (methodName.equals("peers"))
|
|
|
|
getPeers();
|
|
|
|
else if (methodName.equals("lookup"))
|
|
|
|
lookup(args.length >= 2 ? args[1] : "");
|
|
|
|
else if (methodName.equals("service"))
|
|
|
|
service(args.length >= 2 ? args[1] : "");
|
|
|
|
else
|
|
|
|
throw new Exception("unknown command: " + methodName);
|
2014-02-12 13:07:31 +10:30
|
|
|
|
2016-10-12 17:08:26 +10:30
|
|
|
if (result != null)
|
|
|
|
System.err.println(result.toString());
|
2014-02-12 13:07:31 +10:30
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
}
|