diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/TunnelControllerGroup.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/TunnelControllerGroup.java index cddc0e220..555bcf379 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/TunnelControllerGroup.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/TunnelControllerGroup.java @@ -218,6 +218,14 @@ public class TunnelControllerGroup implements ClientApp { } } + /** + * Helper + * @since 0.9.49 + */ + public I2PAppContext getContext() { + return _context; + } + /** * ClientApp interface * @throws IllegalArgumentException if unable to load config from file @@ -375,6 +383,8 @@ public class TunnelControllerGroup implements ClientApp { * @throws IllegalArgumentException if unable to load from file */ private synchronized void loadControllers(File cfgFile, boolean shouldMigrate) { + if (_log.shouldInfo()) + _log.info("Getting controllers from config file " + cfgFile); File dir = new SecureDirectory(_context.getConfigDir(), CONFIG_DIR); List props = null; if (cfgFile.exists()) { @@ -960,9 +970,6 @@ public class TunnelControllerGroup implements ClientApp { * @since 0.9.42 */ private List getControllers(File cfgFile) { - if (_log.shouldInfo()) - _log.info("Getting controllers from config file " + cfgFile); - synchronized (this) { if (!_controllersLoaded) loadControllers(cfgFile); diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/ui/GeneralHelper.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/ui/GeneralHelper.java index 9a8114f1f..d7161110f 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/ui/GeneralHelper.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/ui/GeneralHelper.java @@ -88,24 +88,53 @@ public class GeneralHelper { return null; } + /** + * Save the configuration for a new or existing tunnel to disk. + * For new tunnels, adds to controller and (if configured) starts it. + */ public List saveTunnel(int tunnel, TunnelConfig config) { return saveTunnel(_context, _group, tunnel, config); } - public static List saveTunnel( - I2PAppContext context, TunnelControllerGroup tcg, int tunnel, TunnelConfig config) { - List msgs = updateTunnelConfig(tcg, tunnel, config); - msgs.addAll(saveConfig(context, tcg, tunnel)); + /** + * Save the configuration for a new or existing tunnel to disk. + * For new tunnels, adds to controller and (if configured) starts it. + * + * @param context unused, taken from tcg + */ + public static List saveTunnel(I2PAppContext context, TunnelControllerGroup tcg, int tunnel, TunnelConfig config) { + List msgs = new ArrayList(); + TunnelController cur = updateTunnelConfig(tcg, tunnel, config, msgs); + msgs.addAll(saveConfig(tcg, cur)); return msgs; } + /** + * Update the config and if shared, adjust and save the config of other shared clients. + * If a new tunnel, this will call tcg.addController(), and start it if so configured. + * This does NOT save this tunnel's config. Caller must call saveConfig() also. + */ protected static List updateTunnelConfig(TunnelControllerGroup tcg, int tunnel, TunnelConfig config) { + List msgs = new ArrayList(); + updateTunnelConfig(tcg, tunnel, config, msgs); + return msgs; + } + + /** + * Update the config and if shared, adjust and save the config of other shared clients. + * If a new tunnel, this will call tcg.addController(), and start it if so configured. + * This does NOT save this tunnel's config. Caller must call saveConfig() also. + * + * @param msgs out parameter, messages will be added + * @return the old or new controller, non-null. + * @since 0.9.49 + */ + private static TunnelController updateTunnelConfig(TunnelControllerGroup tcg, int tunnel, TunnelConfig config, List msgs) { // Get current tunnel controller TunnelController cur = getController(tcg, tunnel); Properties props = config.getConfig(); - List msgs = new ArrayList(); String type = props.getProperty(TunnelController.PROP_TYPE); if (TunnelController.TYPE_STD_CLIENT.equals(type) || TunnelController.TYPE_IRC_CLIENT.equals(type)) { // @@ -154,20 +183,8 @@ public class GeneralHelper { tcg.addController(cur); if (cur.getStartOnLoad()) cur.startTunnelBackground(); - try { - tcg.saveConfig(cur); - } catch (IOException ioe) { - msgs.add("Failed to save initial tunnel config after creation " + - cur.getName() + ", check logs:" + ioe); - } } else { cur.setConfig(props, ""); - try { - tcg.saveConfig(cur); - } catch (IOException ioe) { - msgs.add("Failed to save initial tunnel config after creation " + - cur.getName() + ", check logs:" + ioe); - } } // Only modify other shared tunnels // if the current tunnel is shared, and of supported type @@ -188,14 +205,13 @@ public class GeneralHelper { try { tcg.saveConfig(c); } catch (IOException ioe) { - msgs.add("Failed to save initial tunnel config after creation " + - cur.getName() + ", check logs:" + ioe); + msgs.add(0, _t("Failed to save configuration", tcg.getContext()) + ": " + ioe); } } } } - return msgs; + return cur; } /** @@ -240,16 +256,35 @@ public class GeneralHelper { to.setConfig(cOpt, ""); } - protected static List saveConfig( - I2PAppContext context, TunnelControllerGroup tcg, int tunnel) { + /** + * Save the configuration for an existing tunnel to disk. + * New tunnels must use saveConfig(..., TunnelController). + * + * @param context unused, taken from tcg + * @param tunnel must already exist + * @since 0.9.49 + */ + protected static List saveConfig(I2PAppContext context, TunnelControllerGroup tcg, int tunnel) { + TunnelController cur = getController(tcg, tunnel); + if (cur == null) { + List rv = tcg.clearAllMessages(); + rv.add("Invalid tunnel number"); + return rv; + } + return saveConfig(tcg, cur); + } + + /** + * Save the configuration to disk. + * For new and existing tunnels. + * Does NOT call tcg.addController() for new tunnels. See udpateConfig() + * + * @since 0.9.49 + */ + private static List saveConfig(TunnelControllerGroup tcg, TunnelController cur) { + I2PAppContext context = tcg.getContext(); List rv = tcg.clearAllMessages(); try { - TunnelController cur = getController(tcg, tunnel); - if (cur == null) { - //List msgs = new ArrayList(); - rv.add("Invalid tunnel number"); - return rv; - } tcg.saveConfig(cur); rv.add(0, _t("Configuration changes saved", context)); } catch (IOException ioe) {