Changed Transl._ to allow for plurality via Translate.
Changed method and classname to be more descriptive. Recieve RouterInfo netstatus via enumerator rather than strings for increased translatability.
This commit is contained in:
@ -36,10 +36,10 @@ import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.I2PControl.I2P_CONTROL;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.NetworkSetting.NETWORK_SETTING;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.RouterRunner.ROUTER_RUNNER;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.RouterManager.ROUTER_MANAGER;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.SetI2PControl;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.SetRouterRunner;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager;
|
||||
import net.i2p.itoopie.security.CertificateHelper;
|
||||
import net.i2p.itoopie.security.ItoopieHostnameVerifier;
|
||||
|
||||
@ -95,7 +95,7 @@ public class Main {
|
||||
// Popup Main window.
|
||||
WindowHandler.toggleFrames();
|
||||
|
||||
// testStuff(); // Delete Me
|
||||
testStuff(); // Delete Me
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
@ -133,6 +133,7 @@ public class Main {
|
||||
|
||||
|
||||
// Try port switching
|
||||
/*
|
||||
System.out.println("\nI2PControl - Port Switch");
|
||||
try {
|
||||
HashMap<I2P_CONTROL, String> hm = new HashMap<I2P_CONTROL, String>();
|
||||
@ -261,6 +262,7 @@ public class Main {
|
||||
} catch (InvalidParametersException e) {
|
||||
System.out.println("Bad parameters sent..");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
// Test reading all router info
|
||||
@ -276,6 +278,7 @@ public class Main {
|
||||
System.out.println("Invalid password..");
|
||||
} catch (JSONRPC2SessionException e) {
|
||||
System.out.println("Connection failed..");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Test restart - worked at one point :) Possibly now as well.
|
||||
|
@ -36,8 +36,8 @@ import net.i2p.itoopie.i18n.Transl;
|
||||
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
|
||||
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.GetNetworkSetting;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.RouterRunner.ROUTER_RUNNER;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.SetRouterRunner;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.RouterManager.ROUTER_MANAGER;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.NetworkSetting.NETWORK_SETTING;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting;
|
||||
|
||||
@ -396,7 +396,7 @@ public class ConfigurationTab extends TabLogoPanel {
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
if (n == JOptionPane.YES_OPTION){
|
||||
try {
|
||||
SetRouterRunner.execute(ROUTER_RUNNER.RESTART);
|
||||
SetRouterManager.execute(ROUTER_MANAGER.RESTART);
|
||||
StatusHandler.setStatus(Transl._("Restarting I2P node.. "));
|
||||
} catch (InvalidPasswordException e) {
|
||||
StatusHandler.setStatus(Transl._("Restart failed: ") + DEFAULT_STATUS.INVALID_PASSWORD);
|
||||
|
@ -26,7 +26,9 @@ import net.i2p.itoopie.gui.component.multilinelabel.MultiLineLabelUI;
|
||||
import net.i2p.itoopie.i18n.Transl;
|
||||
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo.NETWORK_STATUS;
|
||||
import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
|
||||
import net.i2p.itoopie.util.DataHelper;
|
||||
|
||||
public class OverviewTab extends TabLogoPanel {
|
||||
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
|
||||
@ -142,10 +144,12 @@ public class OverviewTab extends TabLogoPanel {
|
||||
|
||||
|
||||
lblVersionSpecified.setText((String) em.get(ROUTER_INFO.VERSION));
|
||||
lblUptimeSpecified.setText((String) em.get(ROUTER_INFO.UPTIME));
|
||||
lblUptimeSpecified.revalidate();
|
||||
lblUptimeSpecified.setText(DataHelper.formatDuration((Long) em.get(ROUTER_INFO.UPTIME)));
|
||||
lblStatusSpecified.setText((String) em.get(ROUTER_INFO.STATUS));
|
||||
lblNetworkStatusSpecified.setText(((String) em.get(ROUTER_INFO.NETWORK_STATUS)).replace("-", " "));
|
||||
Long netStatus = (Long) em.get(ROUTER_INFO.NETWORK_STATUS);
|
||||
Integer intNetStatus = netStatus.intValue();
|
||||
NETWORK_STATUS enumNetStatus = GetRouterInfo.getEnum(intNetStatus);
|
||||
lblNetworkStatusSpecified.setText(enumNetStatus.toString());
|
||||
|
||||
|
||||
this.getRootPane().repaint(); // Repainting jlabel or jpanel is not enough.
|
||||
@ -155,6 +159,8 @@ public class OverviewTab extends TabLogoPanel {
|
||||
StatusHandler.setDefaultStatus(DEFAULT_STATUS.INVALID_PASSWORD);
|
||||
} catch (JSONRPC2SessionException e) {
|
||||
StatusHandler.setDefaultStatus(DEFAULT_STATUS.NOT_CONNECTED);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,8 +9,38 @@ public class Transl {
|
||||
return Translate.getString(s, BUNDLE_NAME);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* translate a string with a parameter
|
||||
* This is a lot more expensive than getString(s, ctx), so use sparingly.
|
||||
*
|
||||
* @param s string to be translated containing {0}
|
||||
* The {0} will be replaced by the parameter.
|
||||
* Single quotes must be doubled, i.e. ' -> '' in the string.
|
||||
* @param o parameter, not translated.
|
||||
* To tranlslate parameter also, use _("foo {0} bar", _("baz"))
|
||||
* Do not double the single quotes in the parameter.
|
||||
* Use autoboxing to call with ints, longs, floats, etc.
|
||||
*/
|
||||
public static String _(String s, Object o) {
|
||||
return Translate.getString(s, o, BUNDLE_NAME);
|
||||
}
|
||||
|
||||
/** for {0} and {1} */
|
||||
public static String _(String s, Object o, Object o2){
|
||||
return Translate.getString(s, o, o2, BUNDLE_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use GNU ngettext
|
||||
* For .po file format see http://www.gnu.org/software/gettext/manual/gettext.html.gz#Translating-plural-forms
|
||||
*
|
||||
* @param n how many
|
||||
* @param s singluar string, optionally with {0} e.g. "one tunnel"
|
||||
* @param p plural string optionally with {0} e.g. "{0} tunnels"
|
||||
*/
|
||||
public static String getString(int n, String s, String p) {
|
||||
return Translate.getString( n, s, p, BUNDLE_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import java.util.Map.Entry;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import net.i2p.itoopie.i18n.Transl;
|
||||
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
|
||||
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
|
||||
import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface;
|
||||
@ -23,10 +24,37 @@ import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
|
||||
|
||||
public class GetRouterInfo {
|
||||
private final static Log _log = LogFactory.getLog(GetRouterInfo.class);
|
||||
private static HashMap<Integer, NETWORK_STATUS> enumMap;
|
||||
|
||||
public static enum NETWORK_STATUS{
|
||||
OK { public String toString(){ return Transl._("Ok."); }},
|
||||
TESTING { public String toString(){ return Transl._("Testing."); }},
|
||||
FIREWALLED { public String toString(){ return Transl._("Firewalled."); }},
|
||||
HIDDEN { public String toString(){ return Transl._("Hidden."); }},
|
||||
WARN_FIREWALLED_AND_FAST { public String toString(){ return Transl._("Warning, firewalled and fast."); }},
|
||||
WARN_FIREWALLED_AND_FLOODFILL { public String toString(){ return Transl._("Warning, firewalled and floodfill."); }},
|
||||
WARN_FIREWALLED_WITH_INBOUND_TCP { public String toString(){ return Transl._("Warning, firewalled with inbound TCP enabled."); }},
|
||||
WARN_FIREWALLED_WITH_UDP_DISABLED { public String toString(){ return Transl._("Warning, firewalled with UDP disabled."); }},
|
||||
ERROR_I2CP { public String toString(){ return Transl._("Error, I2CP issue. Check logs."); }},
|
||||
ERROR_CLOCK_SKEW { public String toString(){ return Transl._("Error, clock skew. Try setting system clock."); }},
|
||||
ERROR_PRIVATE_TCP_ADDRESS { public String toString(){ return Transl._("Error, private TCP address."); }},
|
||||
ERROR_SYMMETRIC_NAT { public String toString(){ return Transl._("Error, behind symmetric NAT. Can't recieve connections."); }},
|
||||
ERROR_UDP_PORT_IN_USE { public String toString(){ return Transl._("Error, UDP port already in use."); }},
|
||||
ERROR_NO_ACTIVE_PEERS_CHECK_CONNECTION_AND_FIREWALL { public String toString(){ return Transl._("Error, no active peers. Check connection and firewall."); }},
|
||||
ERROR_UDP_DISABLED_AND_TCP_UNSET { public String toString(){ return Transl._("Error, UDP disabled and TCP unset."); }}
|
||||
};
|
||||
|
||||
|
||||
static {
|
||||
enumMap = new HashMap<Integer, NETWORK_STATUS>();
|
||||
for (NETWORK_STATUS n : NETWORK_STATUS.values()){
|
||||
enumMap.put(n.ordinal(), n);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@ -67,4 +95,9 @@ public class GetRouterInfo {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static NETWORK_STATUS getEnum(Integer key){
|
||||
return enumMap.get(key);
|
||||
}
|
||||
}
|
||||
|
@ -50,8 +50,27 @@ public class RouterInfo{
|
||||
|
||||
TUNNELS_PARTICIPATING { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "i2p.router.net.tunnels.participating"; }}
|
||||
public String toString() { return "i2p.router.net.tunnels.participating"; }},
|
||||
|
||||
KNOWN_PEERS { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "i2p.router.netdb.knownpeers"; }},
|
||||
|
||||
ACTIVE_PEERS { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "i2p.router.netdb.activepeers"; }},
|
||||
|
||||
FAST_PEERS { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "i2p.router.netdb.fastpeers"; }},
|
||||
|
||||
HIGH_CAPACITY_PEERS { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "i2p.router.netdb.highcapacitypeers"; }},
|
||||
|
||||
IS_RESEEDING { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "i2p.router.netdb.isreseeding"; }}
|
||||
};
|
||||
|
||||
static {
|
||||
|
@ -7,15 +7,15 @@ 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;
|
||||
public class RouterManager{
|
||||
private final static HashMap<String,ROUTER_MANAGER> enumMap;
|
||||
|
||||
|
||||
/**
|
||||
* Describes the ways a I2P router can be restarted.
|
||||
* @author hottuna
|
||||
*/
|
||||
public enum ROUTER_RUNNER implements Remote{
|
||||
public enum ROUTER_MANAGER implements Remote{
|
||||
RESTART { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "Restart"; }},
|
||||
@ -30,17 +30,21 @@ public class RouterRunner{
|
||||
|
||||
SHUTDOWN_GRACEFUL { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "ShutdownGraceful"; }}
|
||||
public String toString() { return "ShutdownGraceful"; }},
|
||||
|
||||
RESEED { public boolean isReadable(){ return true;}
|
||||
public boolean isWritable(){ return false;}
|
||||
public String toString() { return "Reseed"; }}
|
||||
};
|
||||
|
||||
static {
|
||||
enumMap = new HashMap<String,ROUTER_RUNNER>();
|
||||
for (ROUTER_RUNNER n : ROUTER_RUNNER.values()){
|
||||
enumMap = new HashMap<String,ROUTER_MANAGER>();
|
||||
for (ROUTER_MANAGER n : ROUTER_MANAGER.values()){
|
||||
enumMap.put(n.toString(), n);
|
||||
}
|
||||
}
|
||||
|
||||
public static ROUTER_RUNNER getEnum(String key){
|
||||
public static ROUTER_MANAGER getEnum(String key){
|
||||
return enumMap.get(key);
|
||||
}
|
||||
}
|
@ -14,20 +14,20 @@ 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 net.i2p.itoopie.i2pcontrol.methods.RouterManager.ROUTER_MANAGER;
|
||||
|
||||
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 class SetRouterManager {
|
||||
private final static Log _log = LogFactory.getLog(SetRouterManager.class);
|
||||
|
||||
|
||||
public static EnumMap<ROUTER_RUNNER, Object> execute(ROUTER_RUNNER cmd)
|
||||
public static EnumMap<ROUTER_MANAGER, Object> execute(ROUTER_MANAGER cmd)
|
||||
throws InvalidPasswordException, JSONRPC2SessionException{
|
||||
|
||||
JSONRPC2Request req = new JSONRPC2Request("RouterRunner", JSONRPC2Interface.incrNonce());
|
||||
JSONRPC2Request req = new JSONRPC2Request("RouterManager", JSONRPC2Interface.incrNonce());
|
||||
|
||||
Map outParams = new HashMap();
|
||||
|
||||
@ -41,11 +41,11 @@ public class SetRouterRunner {
|
||||
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);
|
||||
EnumMap<ROUTER_MANAGER, Object> output = new EnumMap<ROUTER_MANAGER, Object>(ROUTER_MANAGER.class);
|
||||
// Present the result as an <Enum,Object> map.
|
||||
for (Entry e: inputSet){
|
||||
String key = (String) e.getKey();
|
||||
ROUTER_RUNNER RR = RouterRunner.getEnum(key);
|
||||
ROUTER_MANAGER RR = RouterManager.getEnum(key);
|
||||
// If the enum exists. They should exists, but safety first.
|
||||
if (RR != null){
|
||||
output.put(RR, e.getValue());
|
||||
@ -53,10 +53,10 @@ public class SetRouterRunner {
|
||||
}
|
||||
return output;
|
||||
} else {
|
||||
return new EnumMap<ROUTER_RUNNER, Object>(ROUTER_RUNNER.class);
|
||||
return new EnumMap<ROUTER_MANAGER, Object>(ROUTER_MANAGER.class);
|
||||
}
|
||||
} catch (UnrecoverableFailedRequestException e) {
|
||||
_log.error("setRouterRunner failed.", e);
|
||||
_log.error("setRouterManager failed.", e);
|
||||
} catch (InvalidParametersException e) {
|
||||
_log.error("Remote host rejected provided parameters: " + req.toJSON().toJSONString());
|
||||
}
|
42
src/net/i2p/itoopie/util/DataHelper.java
Normal file
42
src/net/i2p/itoopie/util/DataHelper.java
Normal file
@ -0,0 +1,42 @@
|
||||
package net.i2p.itoopie.util;
|
||||
|
||||
import net.i2p.itoopie.i18n.Transl;
|
||||
|
||||
public class DataHelper {
|
||||
|
||||
|
||||
/**
|
||||
* Like formatDuration but with a non-breaking space after the number,
|
||||
* 0 is unitless, and the unit is translated.
|
||||
* This seems consistent with most style guides out there.
|
||||
* Thresholds are a little lower than in formatDuration() also,
|
||||
* as precision is less important in the GUI than in logging.
|
||||
*/
|
||||
public static String formatDuration(long ms) {
|
||||
String t;
|
||||
if (ms == 0) {
|
||||
return "0";
|
||||
} else if (ms < 3 * 1000) {
|
||||
// milliseconds
|
||||
// Note to translators, may be negative or zero, 2999 maximum.
|
||||
// {0,number,####} prevents 1234 from being output as 1,234 in the English locale.
|
||||
// If you want the digit separator in your locale, translate as {0}.
|
||||
return Transl._("1 ms", "{0,number,####} ms", (int) ms);
|
||||
} else if (ms < 2 * 60 * 1000) {
|
||||
// seconds
|
||||
return Transl._("1 sec", "{0} sec", (int) (ms / 1000));
|
||||
} else if (ms < 120 * 60 * 1000) {
|
||||
// minutes
|
||||
return Transl._("1 min", "{0} min", (int) (ms / (60 * 1000)));
|
||||
} else if (ms < 2 * 24 * 60 * 60 * 1000) {
|
||||
// hours
|
||||
return Transl._("1 hour", "{0} hours", (int) (ms / (60 * 60 * 1000)));
|
||||
} else if (ms > 1000l * 24l * 60l * 60l * 1000l) {
|
||||
// >1000 days
|
||||
return Transl._("n/a");
|
||||
} else {
|
||||
// days
|
||||
return Transl._("1 day", "{0} days", (int) (ms / (24 * 60 * 60 * 1000)));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user