* Router: Move addShutdownTask from Router to I2PAppContext
so that apps can register more easily
This commit is contained in:
@ -81,8 +81,7 @@ public class SnarkManager implements Snark.CompleteListener {
|
|||||||
I2PAppThread monitor = new I2PAppThread(new DirMonitor(), "Snark DirMonitor");
|
I2PAppThread monitor = new I2PAppThread(new DirMonitor(), "Snark DirMonitor");
|
||||||
monitor.setDaemon(true);
|
monitor.setDaemon(true);
|
||||||
monitor.start();
|
monitor.start();
|
||||||
if (_context instanceof RouterContext)
|
_context.addShutdownTask(new SnarkManagerShutdown());
|
||||||
((RouterContext)_context).router().addShutdownTask(new SnarkManagerShutdown());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** hook to I2PSnarkUtil for the servlet */
|
/** hook to I2PSnarkUtil for the servlet */
|
||||||
@ -539,7 +538,7 @@ public class SnarkManager implements Snark.CompleteListener {
|
|||||||
String announce = info.getAnnounce();
|
String announce = info.getAnnounce();
|
||||||
// basic validation of url
|
// basic validation of url
|
||||||
if ((!announce.startsWith("http://")) ||
|
if ((!announce.startsWith("http://")) ||
|
||||||
(announce.indexOf(".i2p/") < 0))
|
(announce.indexOf(".i2p/") < 0)) // need to do better than this
|
||||||
return "Non-i2p tracker in " + info.getName() + ", deleting it";
|
return "Non-i2p tracker in " + info.getName() + ", deleting it";
|
||||||
List files = info.getFiles();
|
List files = info.getFiles();
|
||||||
if ( (files != null) && (files.size() > MAX_FILES_PER_TORRENT) ) {
|
if ( (files != null) && (files.size() > MAX_FILES_PER_TORRENT) ) {
|
||||||
|
@ -237,7 +237,7 @@ public class ConfigNetHandler extends FormHandler {
|
|||||||
|
|
||||||
private void hiddenSwitch() {
|
private void hiddenSwitch() {
|
||||||
// Full restart required to generate new keys
|
// Full restart required to generate new keys
|
||||||
_context.router().addShutdownTask(new UpdateWrapperManagerAndRekeyTask(Router.EXIT_GRACEFUL_RESTART));
|
_context.addShutdownTask(new UpdateWrapperManagerAndRekeyTask(Router.EXIT_GRACEFUL_RESTART));
|
||||||
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,20 +25,20 @@ public class ConfigRestartBean {
|
|||||||
String systemNonce = getNonce();
|
String systemNonce = getNonce();
|
||||||
if ( (nonce != null) && (systemNonce.equals(nonce)) && (action != null) ) {
|
if ( (nonce != null) && (systemNonce.equals(nonce)) && (action != null) ) {
|
||||||
if ("shutdownImmediate".equals(action)) {
|
if ("shutdownImmediate".equals(action)) {
|
||||||
ctx.router().addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_HARD));
|
ctx.addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_HARD));
|
||||||
//ctx.router().shutdown(Router.EXIT_HARD); // never returns
|
//ctx.router().shutdown(Router.EXIT_HARD); // never returns
|
||||||
ctx.router().shutdownGracefully(Router.EXIT_HARD); // give the UI time to respond
|
ctx.router().shutdownGracefully(Router.EXIT_HARD); // give the UI time to respond
|
||||||
} else if ("cancelShutdown".equals(action)) {
|
} else if ("cancelShutdown".equals(action)) {
|
||||||
ctx.router().cancelGracefulShutdown();
|
ctx.router().cancelGracefulShutdown();
|
||||||
} else if ("restartImmediate".equals(action)) {
|
} else if ("restartImmediate".equals(action)) {
|
||||||
ctx.router().addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_HARD_RESTART));
|
ctx.addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_HARD_RESTART));
|
||||||
//ctx.router().shutdown(Router.EXIT_HARD_RESTART); // never returns
|
//ctx.router().shutdown(Router.EXIT_HARD_RESTART); // never returns
|
||||||
ctx.router().shutdownGracefully(Router.EXIT_HARD_RESTART); // give the UI time to respond
|
ctx.router().shutdownGracefully(Router.EXIT_HARD_RESTART); // give the UI time to respond
|
||||||
} else if ("restart".equals(action)) {
|
} else if ("restart".equals(action)) {
|
||||||
ctx.router().addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
|
ctx.addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
|
||||||
ctx.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
ctx.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
||||||
} else if ("shutdown".equals(action)) {
|
} else if ("shutdown".equals(action)) {
|
||||||
ctx.router().addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_GRACEFUL));
|
ctx.addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_GRACEFUL));
|
||||||
ctx.router().shutdownGracefully();
|
ctx.router().shutdownGracefully();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,31 +53,31 @@ public class ConfigServiceHandler extends FormHandler {
|
|||||||
if (_action == null) return;
|
if (_action == null) return;
|
||||||
|
|
||||||
if ("Shutdown gracefully".equals(_action)) {
|
if ("Shutdown gracefully".equals(_action)) {
|
||||||
_context.router().addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL));
|
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL));
|
||||||
_context.router().shutdownGracefully();
|
_context.router().shutdownGracefully();
|
||||||
addFormNotice("Graceful shutdown initiated");
|
addFormNotice("Graceful shutdown initiated");
|
||||||
} else if ("Shutdown immediately".equals(_action)) {
|
} else if ("Shutdown immediately".equals(_action)) {
|
||||||
_context.router().addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD));
|
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD));
|
||||||
_context.router().shutdown(Router.EXIT_HARD);
|
_context.router().shutdown(Router.EXIT_HARD);
|
||||||
addFormNotice("Shutdown immediately! boom bye bye bad bwoy");
|
addFormNotice("Shutdown immediately! boom bye bye bad bwoy");
|
||||||
} else if ("Cancel graceful shutdown".equals(_action)) {
|
} else if ("Cancel graceful shutdown".equals(_action)) {
|
||||||
_context.router().cancelGracefulShutdown();
|
_context.router().cancelGracefulShutdown();
|
||||||
addFormNotice("Graceful shutdown cancelled");
|
addFormNotice("Graceful shutdown cancelled");
|
||||||
} else if ("Graceful restart".equals(_action)) {
|
} else if ("Graceful restart".equals(_action)) {
|
||||||
_context.router().addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
|
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
|
||||||
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
||||||
addFormNotice("Graceful restart requested");
|
addFormNotice("Graceful restart requested");
|
||||||
} else if ("Hard restart".equals(_action)) {
|
} else if ("Hard restart".equals(_action)) {
|
||||||
_context.router().addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD_RESTART));
|
_context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD_RESTART));
|
||||||
_context.router().shutdown(Router.EXIT_HARD_RESTART);
|
_context.router().shutdown(Router.EXIT_HARD_RESTART);
|
||||||
addFormNotice("Hard restart requested");
|
addFormNotice("Hard restart requested");
|
||||||
} else if ("Rekey and Restart".equals(_action)) {
|
} else if ("Rekey and Restart".equals(_action)) {
|
||||||
addFormNotice("Rekeying after graceful restart");
|
addFormNotice("Rekeying after graceful restart");
|
||||||
_context.router().addShutdownTask(new UpdateWrapperManagerAndRekeyTask(Router.EXIT_GRACEFUL_RESTART));
|
_context.addShutdownTask(new UpdateWrapperManagerAndRekeyTask(Router.EXIT_GRACEFUL_RESTART));
|
||||||
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
||||||
} else if ("Rekey and Shutdown".equals(_action)) {
|
} else if ("Rekey and Shutdown".equals(_action)) {
|
||||||
addFormNotice("Rekeying after graceful shutdown");
|
addFormNotice("Rekeying after graceful shutdown");
|
||||||
_context.router().addShutdownTask(new UpdateWrapperManagerAndRekeyTask(Router.EXIT_GRACEFUL));
|
_context.addShutdownTask(new UpdateWrapperManagerAndRekeyTask(Router.EXIT_GRACEFUL));
|
||||||
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL);
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL);
|
||||||
} else if ("Run I2P on startup".equals(_action)) {
|
} else if ("Run I2P on startup".equals(_action)) {
|
||||||
installService();
|
installService();
|
||||||
|
@ -185,7 +185,7 @@ public class UpdateHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void restart() {
|
private void restart() {
|
||||||
_context.router().addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
|
_context.addShutdownTask(new ConfigServiceHandler.UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
|
||||||
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import net.i2p.crypto.SessionKeyManager;
|
|||||||
import net.i2p.data.RoutingKeyGenerator;
|
import net.i2p.data.RoutingKeyGenerator;
|
||||||
import net.i2p.stat.StatManager;
|
import net.i2p.stat.StatManager;
|
||||||
import net.i2p.util.Clock;
|
import net.i2p.util.Clock;
|
||||||
|
import net.i2p.util.ConcurrentHashSet;
|
||||||
import net.i2p.util.FortunaRandomSource;
|
import net.i2p.util.FortunaRandomSource;
|
||||||
import net.i2p.util.KeyRing;
|
import net.i2p.util.KeyRing;
|
||||||
import net.i2p.util.LogManager;
|
import net.i2p.util.LogManager;
|
||||||
@ -94,6 +95,7 @@ public class I2PAppContext {
|
|||||||
private volatile boolean _randomInitialized;
|
private volatile boolean _randomInitialized;
|
||||||
private volatile boolean _keyGeneratorInitialized;
|
private volatile boolean _keyGeneratorInitialized;
|
||||||
protected volatile boolean _keyRingInitialized; // used in RouterContext
|
protected volatile boolean _keyRingInitialized; // used in RouterContext
|
||||||
|
private Set<Runnable> _shutdownTasks;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -152,6 +154,7 @@ public class I2PAppContext {
|
|||||||
_elGamalAESEngineInitialized = false;
|
_elGamalAESEngineInitialized = false;
|
||||||
_logManagerInitialized = false;
|
_logManagerInitialized = false;
|
||||||
_keyRingInitialized = false;
|
_keyRingInitialized = false;
|
||||||
|
_shutdownTasks = new ConcurrentHashSet(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -557,4 +560,13 @@ public class I2PAppContext {
|
|||||||
_randomInitialized = true;
|
_randomInitialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addShutdownTask(Runnable task) {
|
||||||
|
_shutdownTasks.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Runnable> getShutdownTasks() {
|
||||||
|
return new HashSet(_shutdownTasks);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,6 @@ public class Router {
|
|||||||
private I2PThread.OOMEventListener _oomListener;
|
private I2PThread.OOMEventListener _oomListener;
|
||||||
private ShutdownHook _shutdownHook;
|
private ShutdownHook _shutdownHook;
|
||||||
private I2PThread _gracefulShutdownDetector;
|
private I2PThread _gracefulShutdownDetector;
|
||||||
private Set _shutdownTasks;
|
|
||||||
|
|
||||||
public final static String PROP_CONFIG_FILE = "router.configLocation";
|
public final static String PROP_CONFIG_FILE = "router.configLocation";
|
||||||
|
|
||||||
@ -171,7 +170,6 @@ public class Router {
|
|||||||
watchdog.setDaemon(true);
|
watchdog.setDaemon(true);
|
||||||
watchdog.start();
|
watchdog.start();
|
||||||
|
|
||||||
_shutdownTasks = new HashSet(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -491,13 +489,12 @@ public class Router {
|
|||||||
*/
|
*/
|
||||||
public void rebuildNewIdentity() {
|
public void rebuildNewIdentity() {
|
||||||
killKeys();
|
killKeys();
|
||||||
try {
|
for (Runnable task : _context.getShutdownTasks()) {
|
||||||
for (Iterator iter = _shutdownTasks.iterator(); iter.hasNext(); ) {
|
try {
|
||||||
Runnable task = (Runnable)iter.next();
|
|
||||||
task.run();
|
task.run();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
_log.log(Log.CRIT, "Error running shutdown task", t);
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
|
||||||
_log.log(Log.CRIT, "Error running shutdown task", t);
|
|
||||||
}
|
}
|
||||||
// hard and ugly
|
// hard and ugly
|
||||||
finalShutdown(EXIT_HARD_RESTART);
|
finalShutdown(EXIT_HARD_RESTART);
|
||||||
@ -782,12 +779,6 @@ public class Router {
|
|||||||
buf.setLength(0);
|
buf.setLength(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addShutdownTask(Runnable task) {
|
|
||||||
synchronized (_shutdownTasks) {
|
|
||||||
_shutdownTasks.add(task);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final int EXIT_GRACEFUL = 2;
|
public static final int EXIT_GRACEFUL = 2;
|
||||||
public static final int EXIT_HARD = 3;
|
public static final int EXIT_HARD = 3;
|
||||||
public static final int EXIT_OOM = 10;
|
public static final int EXIT_OOM = 10;
|
||||||
@ -800,13 +791,12 @@ public class Router {
|
|||||||
I2PThread.removeOOMEventListener(_oomListener);
|
I2PThread.removeOOMEventListener(_oomListener);
|
||||||
// Run the shutdown hooks first in case they want to send some goodbye messages
|
// Run the shutdown hooks first in case they want to send some goodbye messages
|
||||||
// Maybe we need a delay after this too?
|
// Maybe we need a delay after this too?
|
||||||
try {
|
for (Runnable task : _context.getShutdownTasks()) {
|
||||||
for (Iterator iter = _shutdownTasks.iterator(); iter.hasNext(); ) {
|
try {
|
||||||
Runnable task = (Runnable)iter.next();
|
|
||||||
task.run();
|
task.run();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
_log.log(Log.CRIT, "Error running shutdown task", t);
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
|
||||||
_log.log(Log.CRIT, "Error running shutdown task", t);
|
|
||||||
}
|
}
|
||||||
try { _context.clientManager().shutdown(); } catch (Throwable t) { _log.log(Log.CRIT, "Error shutting down the client manager", t); }
|
try { _context.clientManager().shutdown(); } catch (Throwable t) { _log.log(Log.CRIT, "Error shutting down the client manager", t); }
|
||||||
try { _context.jobQueue().shutdown(); } catch (Throwable t) { _log.log(Log.CRIT, "Error shutting down the job queue", t); }
|
try { _context.jobQueue().shutdown(); } catch (Throwable t) { _log.log(Log.CRIT, "Error shutting down the job queue", t); }
|
||||||
|
Reference in New Issue
Block a user