2014-06-20 01:25:09 +00:00
|
|
|
package org.servalproject.servaldna;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jeremy on 20/06/14.
|
|
|
|
*/
|
|
|
|
public class ServerControl {
|
|
|
|
private String instancePath;
|
|
|
|
private final String execPath;
|
|
|
|
private int loopbackMdpPort;
|
|
|
|
private int httpPort=0;
|
|
|
|
private int pid;
|
|
|
|
private static final String restfulUsername="ServalDClient";
|
|
|
|
private ServalDClient client;
|
|
|
|
|
|
|
|
public ServerControl(){
|
|
|
|
this(null);
|
|
|
|
}
|
|
|
|
public ServerControl(String execPath){
|
|
|
|
this.execPath = execPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getInstancePath(){
|
|
|
|
return instancePath;
|
|
|
|
}
|
|
|
|
|
2014-07-07 05:38:21 +00:00
|
|
|
public String getExecPath() {
|
|
|
|
return this.execPath;
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:09:26 +00:00
|
|
|
public int getLoopbackMdpPort() {
|
|
|
|
return loopbackMdpPort;
|
|
|
|
}
|
|
|
|
|
2015-05-25 02:16:37 +00:00
|
|
|
public int getPid() {
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setStatus(String instancePath, int pid, int mdpInetPort, int httpPort){
|
|
|
|
this.instancePath = instancePath;
|
|
|
|
this.pid = pid;
|
|
|
|
this.loopbackMdpPort = mdpInetPort;
|
|
|
|
this.httpPort = httpPort;
|
|
|
|
}
|
|
|
|
|
2014-06-20 01:25:09 +00:00
|
|
|
private void setStatus(ServalDCommand.Status result){
|
|
|
|
loopbackMdpPort = result.mdpInetPort;
|
|
|
|
pid = result.pid;
|
|
|
|
httpPort = result.httpPort;
|
|
|
|
instancePath = result.instancePath;
|
|
|
|
}
|
|
|
|
|
2015-05-25 02:16:37 +00:00
|
|
|
protected void clearStatus(){
|
2014-06-20 01:25:09 +00:00
|
|
|
loopbackMdpPort = 0;
|
|
|
|
pid = 0;
|
|
|
|
httpPort = 0;
|
|
|
|
client = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void start() throws ServalDFailureException {
|
|
|
|
if (execPath==null)
|
|
|
|
setStatus(ServalDCommand.serverStart());
|
|
|
|
else
|
|
|
|
setStatus(ServalDCommand.serverStart(execPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stop() throws ServalDFailureException {
|
|
|
|
try{
|
|
|
|
ServalDCommand.serverStop();
|
|
|
|
}finally{
|
|
|
|
clearStatus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void restart() throws ServalDFailureException {
|
|
|
|
try {
|
|
|
|
stop();
|
|
|
|
} catch (ServalDFailureException e) {
|
|
|
|
// ignore failures, at least we tried...
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isRunning() throws ServalDFailureException {
|
|
|
|
ServalDCommand.Status s = ServalDCommand.serverStatus();
|
|
|
|
|
|
|
|
if (s.status.equals("running")) {
|
|
|
|
setStatus(s);
|
|
|
|
}else{
|
|
|
|
clearStatus();
|
|
|
|
}
|
|
|
|
return pid!=0;
|
|
|
|
}
|
|
|
|
|
2014-06-20 05:44:00 +00:00
|
|
|
public MdpServiceLookup getMdpServiceLookup(ChannelSelector selector, AsyncResult<MdpServiceLookup.ServiceResult> results) throws ServalDInterfaceException, IOException {
|
2014-06-20 01:25:09 +00:00
|
|
|
if (!isRunning())
|
|
|
|
throw new ServalDInterfaceException("server is not running");
|
|
|
|
return new MdpServiceLookup(selector, this.loopbackMdpPort, results);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MdpDnaLookup getMdpDnaLookup(ChannelSelector selector, AsyncResult<ServalDCommand.LookupResult> results) throws ServalDInterfaceException, IOException {
|
|
|
|
if (!isRunning())
|
|
|
|
throw new ServalDInterfaceException("server is not running");
|
|
|
|
return new MdpDnaLookup(selector, this.loopbackMdpPort, results);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ServalDClient getRestfulClient() throws ServalDInterfaceException {
|
|
|
|
if (!isRunning())
|
|
|
|
throw new ServalDInterfaceException("server is not running");
|
|
|
|
if (client==null) {
|
2014-06-19 07:57:05 +00:00
|
|
|
/* TODO: replace the following username/password configuration with a better scheme
|
|
|
|
* (outside the scope of this API) that does not require any invocations of the JNI, and
|
|
|
|
* allows any application (user) on the local host to request authorisation to use the
|
|
|
|
* RESTful interface. The authorisation must then be supplied to the restful client
|
|
|
|
* object before requests can be made.
|
|
|
|
*/
|
2014-06-26 06:55:19 +00:00
|
|
|
String restfulPassword = ServalDCommand.getConfigItem("api.restful.users." + restfulUsername + ".password");
|
2014-06-20 01:25:09 +00:00
|
|
|
if (restfulPassword == null) {
|
2014-06-23 05:49:18 +00:00
|
|
|
restfulPassword = new BigInteger(130, new SecureRandom()).toString(32);
|
|
|
|
ServalDCommand.configActions(
|
2014-06-26 06:55:19 +00:00
|
|
|
ServalDCommand.ConfigAction.set, "api.restful.users." + restfulUsername + ".password", restfulPassword,
|
2014-06-23 05:49:18 +00:00
|
|
|
ServalDCommand.ConfigAction.sync
|
|
|
|
);
|
2014-06-20 01:25:09 +00:00
|
|
|
}
|
|
|
|
client = new ServalDClient(this.httpPort, restfulUsername, restfulPassword);
|
|
|
|
}
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|