Added support for the RouterInfo API call.

Added support for the RouterRunner API call.
This commit is contained in:
dev
2011-07-15 12:48:14 +00:00
parent e6a9514bd8
commit 6b398fcd1f
6 changed files with 246 additions and 8 deletions

View File

@ -242,8 +242,8 @@ public class Main {
System.out.println("Connection failed..");
}
// Test restart
// Test restart - worked at one point :) Possibly now as well.
/*
System.out.println("\nSetRouterRunner: Restart");
try {
SetRouterRunner.execute(ROUTER_RUNNER.RESTART);
@ -251,9 +251,9 @@ public class Main {
System.out.println("Invalid password..");
} catch (JSONRPC2SessionException e) {
System.out.println("Connection failed..");
}
}*/
// Test restart graceful
// Test restart graceful - worked at one point :) Possibly now as well.
/*
System.out.println("\nSetRouterRunner: Restart Graceful");
try {
@ -263,5 +263,16 @@ public class Main {
} catch (JSONRPC2SessionException e) {
System.out.println("Connection failed..");
}*/
/*
System.out.println("\nSetRouterRunner: Shutdown ");
try {
SetRouterRunner.execute(ROUTER_RUNNER.SHUTDOWN);
} catch (InvalidPasswordException e1) {
System.out.println("Invalid password..");
} catch (JSONRPC2SessionException e) {
System.out.println("Connection failed..");
}
*/
}
}

View File

@ -261,12 +261,12 @@ public class ConfigurationTab extends TabLogoPanel {
} catch (NumberFormatException e){
JOptionPane.showConfirmDialog(
this,
Transl._("' "+tcpPort + " ' can not be interpreted as a TCP port.\r\n" +
"\r\nA port has to be a number in the range 1-65535." +
"\r\n\r\nWould you like to ignore that the TCP port isn't set?"),
Transl._("' "+udpPort + " ' can not be interpreted as a UDP port.\r\n" +
"\r\nA port has to be a number in the range 1-65535."),
Transl._("Invalid port."),
JOptionPane.YES_NO_OPTION,
JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE);
return SAVE_STATUS.SAVE_FAILED_LOCALLY;
}
//Check UDP port

View File

@ -0,0 +1,70 @@
package net.i2p.itoopie.i2pcontrol.methods;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface;
import net.i2p.itoopie.i2pcontrol.UnrecoverableFailedRequestException;
import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
public class GetRouterInfo {
private final static Log _log = LogFactory.getLog(GetRouterInfo.class);
public static EnumMap<ROUTER_INFO, Object> execute(ROUTER_INFO ... info)
throws InvalidPasswordException, JSONRPC2SessionException{
JSONRPC2Request req = new JSONRPC2Request("RouterInfo", JSONRPC2Interface.incrNonce());
@SuppressWarnings("rawtypes")
Map outParams = new HashMap();
List<ROUTER_INFO> list = Arrays.asList(info);
for (ROUTER_INFO e : list){
if(e.isReadable()){
outParams.put(e.toString(), null);
}
}
req.setParams(outParams);
JSONRPC2Response resp = null;
try {
resp = JSONRPC2Interface.sendReq(req);
HashMap map = (HashMap) resp.getResult();
if (map != null){
Set<Entry> set = map.entrySet();
EnumMap<ROUTER_INFO, Object> output = new EnumMap<ROUTER_INFO, Object>(ROUTER_INFO.class);
// Present the result as an <Enum,Object> map.
for (Entry e: set){
String key = (String) e.getKey();
ROUTER_INFO RI = RouterInfo.getEnum(key);
// If the enum exists. They should exists, but safety first.
if (RI != null){
output.put(RI, e.getValue());
}
}
return output;
} else {
return new EnumMap<ROUTER_INFO, Object>(ROUTER_INFO.class);
}
} catch (UnrecoverableFailedRequestException e) {
_log.error("getRouterInfo failed.", e);
} catch (InvalidParametersException e) {
_log.error("Remote host rejected provided parameters: " + req.toJSON().toJSONString());
}
return null;
}
}

View File

@ -0,0 +1,46 @@
package net.i2p.itoopie.i2pcontrol.methods;
import java.util.HashMap;
/**
* Describes very basic I2P router information.
* @author hottuna
*/
public class RouterInfo{
private final static HashMap<String,ROUTER_INFO> enumMap;
/**
* Describes very basic I2P router information.
* @author hottuna
*/
public enum ROUTER_INFO implements Remote{
VERSION { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "i2p.router.version"; }},
UPTIME { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "i2p.router.uptime"; }},
STATUS { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "i2p.router.status"; }},
NETWORK_STATUS { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "i2p.router.net.status"; }}
};
static {
enumMap = new HashMap<String,ROUTER_INFO>();
for (ROUTER_INFO n : ROUTER_INFO.values()){
enumMap.put(n.toString(), n);
}
}
public static ROUTER_INFO getEnum(String key){
return enumMap.get(key);
}
}

View File

@ -0,0 +1,46 @@
package net.i2p.itoopie.i2pcontrol.methods;
import java.util.HashMap;
/**
* Describes the ways a I2P router can be restarted.
* @author hottuna
*/
public class RouterRunner{
private final static HashMap<String,ROUTER_RUNNER> enumMap;
/**
* Describes the ways a I2P router can be restarted.
* @author hottuna
*/
public enum ROUTER_RUNNER implements Remote{
RESTART { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "Restart"; }},
SHUTDOWN { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "Shutdown"; }},
RESTART_GRACEFUL { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "RestartGraceful"; }},
SHUTDOWN_GRACEFUL { public boolean isReadable(){ return true;}
public boolean isWritable(){ return false;}
public String toString() { return "ShutdownGraceful"; }}
};
static {
enumMap = new HashMap<String,ROUTER_RUNNER>();
for (ROUTER_RUNNER n : ROUTER_RUNNER.values()){
enumMap.put(n.toString(), n);
}
}
public static ROUTER_RUNNER getEnum(String key){
return enumMap.get(key);
}
}

View File

@ -0,0 +1,65 @@
package net.i2p.itoopie.i2pcontrol.methods;
import java.security.InvalidParameterException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface;
import net.i2p.itoopie.i2pcontrol.UnrecoverableFailedRequestException;
import net.i2p.itoopie.i2pcontrol.methods.RouterRunner.ROUTER_RUNNER;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
public class SetRouterRunner {
private final static Log _log = LogFactory.getLog(SetRouterRunner.class);
public static EnumMap<ROUTER_RUNNER, Object> execute(ROUTER_RUNNER cmd)
throws InvalidPasswordException, JSONRPC2SessionException{
JSONRPC2Request req = new JSONRPC2Request("RouterRunner", JSONRPC2Interface.incrNonce());
Map outParams = new HashMap();
outParams.put(cmd.toString(), null);
req.setParams(outParams);
JSONRPC2Response resp = null;
try {
resp = JSONRPC2Interface.sendReq(req);
HashMap map = (HashMap) resp.getResult();
if (map != null){
Set<Entry> inputSet = map.entrySet();
EnumMap<ROUTER_RUNNER, Object> output = new EnumMap<ROUTER_RUNNER, Object>(ROUTER_RUNNER.class);
// Present the result as an <Enum,Object> map.
for (Entry e: inputSet){
String key = (String) e.getKey();
ROUTER_RUNNER RR = RouterRunner.getEnum(key);
// If the enum exists. They should exists, but safety first.
if (RR != null){
output.put(RR, e.getValue());
}
}
return output;
} else {
return new EnumMap<ROUTER_RUNNER, Object>(ROUTER_RUNNER.class);
}
} catch (UnrecoverableFailedRequestException e) {
_log.error("setRouterRunner failed.", e);
} catch (InvalidParametersException e) {
_log.error("Remote host rejected provided parameters: " + req.toJSON().toJSONString());
}
return null;
}
}