use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.
the class UrlLauncher method startup.
/**
* ClientApp interface
* @since 0.9.18
*/
public void startup() {
String url = _args[0];
if (!validateUrlFormat(url)) {
changeState(START_FAILED, new MalformedURLException("Bad url: " + url));
return;
}
changeState(STARTING);
Thread t = new I2PAppThread(new Runner(), "UrlLauncher", true);
t.start();
}
use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.
the class SubSession method connect.
/**
* Connect to the router and establish a session. This call blocks until
* a session is granted.
*
* Should be threadsafe, other threads will block until complete.
* Disconnect / destroy from another thread may be called simultaneously and
* will (should?) interrupt the connect.
*
* Connecting a subsession will automatically connect the primary session
* if not previously connected.
*
* @throws I2PSessionException if there is a configuration error or the router is
* not reachable
*/
@Override
public void connect() throws I2PSessionException {
synchronized (_stateLock) {
if (_state != State.OPEN) {
changeState(State.OPENING);
}
}
boolean success = false;
try {
_primary.connect();
// wait until we have created a lease set
int waitcount = 0;
while (_leaseSet == null) {
if (waitcount++ > 5 * 60) {
throw new IOException("No tunnels built after waiting 5 minutes. Your network connection may be down, or there is severe network congestion.");
}
synchronized (_leaseSetWait) {
// InterruptedException caught below
_leaseSetWait.wait(1000);
}
}
synchronized (_stateLock) {
if (_state != State.OPEN) {
Thread notifier = new I2PAppThread(_availabilityNotifier, "ClientNotifier " + getPrefix(), true);
notifier.start();
changeState(State.OPEN);
}
}
success = true;
} catch (InterruptedException ie) {
throw new I2PSessionException("Interrupted", ie);
} catch (IOException ioe) {
throw new I2PSessionException(getPrefix() + "Cannot connect to the router on " + _hostname + ':' + _portNum, ioe);
} finally {
if (!success) {
_availabilityNotifier.stopNotifying();
changeState(State.CLOSED);
}
}
}
use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.
the class ClientWriterRunner method startWriting.
/**
* @since 0.9.11
*/
public void startWriting() {
Thread t = new I2PAppThread(this, "I2CP Client Writer " + __Id.incrementAndGet(), true);
t.start();
}
use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.
the class Router method shutdown2.
/**
* Cancel the JVM runtime hook before calling this.
* Called by the ShutdownHook.
* NOT to be called by others, use shutdown().
*
* @param exitCode one of the EXIT_* values, non-negative
* @throws IllegalArgumentException if exitCode negative
*/
public synchronized void shutdown2(int exitCode) {
if (exitCode < 0)
throw new IllegalArgumentException();
changeState(State.FINAL_SHUTDOWN_2);
// help us shut down esp. after OOM
int priority = (exitCode == EXIT_OOM) ? Thread.MAX_PRIORITY - 1 : Thread.NORM_PRIORITY + 2;
Thread.currentThread().setPriority(priority);
_log.log(Log.CRIT, "Starting final shutdown(" + exitCode + ')');
// No, you can't do Thread.currentThread.setDaemon(false)
if (_killVMOnEnd) {
try {
(new Spinner()).start();
} catch (Throwable t) {
}
}
((RouterClock) _context.clock()).removeShiftListener(this);
_context.random().saveSeed();
I2PThread.removeOOMEventListener(_oomListener);
// Run the shutdown hooks first in case they want to send some goodbye messages
// Maybe we need a delay after this too?
LinkedList<Thread> tasks = new LinkedList<Thread>();
for (Runnable task : _context.getShutdownTasks()) {
// System.err.println("Running shutdown task " + task.getClass());
if (_log.shouldLog(Log.WARN))
_log.warn("Running shutdown task " + task.getClass());
try {
// task.run();
Thread t = new I2PAppThread(task, "Shutdown task " + task.getClass().getName());
t.setDaemon(true);
t.start();
tasks.add(t);
} catch (Throwable t) {
_log.log(Log.CRIT, "Error running shutdown task", t);
}
}
long waitSecs = SHUTDOWN_WAIT_SECS;
if (SystemVersion.isARM())
waitSecs *= 2;
final long maxWait = System.currentTimeMillis() + (waitSecs * 1000);
Thread th;
while ((th = tasks.poll()) != null) {
long toWait = maxWait - System.currentTimeMillis();
if (toWait <= 0) {
_log.logAlways(Log.WARN, "Shutdown tasks took more than " + waitSecs + " seconds to run");
tasks.clear();
break;
}
try {
th.join(toWait);
} catch (InterruptedException ie) {
}
if (th.isAlive()) {
_log.logAlways(Log.WARN, "Shutdown task took more than " + waitSecs + " seconds to run: " + th.getName());
tasks.clear();
break;
} else if (_log.shouldInfo()) {
_log.info("Shutdown task complete: " + th.getName());
}
}
// Set the last version to the current version, since 0.8.13
if (!RouterVersion.VERSION.equals(_config.get("router.previousVersion"))) {
saveConfig("router.previousVersion", RouterVersion.VERSION);
}
_context.removeShutdownTasks();
try {
_context.clientManager().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the client manager", t);
}
try {
_context.namingService().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the naming service", t);
}
try {
_context.jobQueue().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the job queue", t);
}
try {
_context.tunnelManager().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the tunnel manager", t);
}
try {
_context.tunnelDispatcher().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the tunnel dispatcher", t);
}
try {
_context.netDb().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the networkDb", t);
}
try {
_context.commSystem().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the comm system", t);
}
try {
_context.bandwidthLimiter().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the comm system", t);
}
try {
_context.peerManager().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the peer manager", t);
}
try {
_context.messageRegistry().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the message registry", t);
}
try {
_context.messageValidator().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the message validator", t);
}
try {
_context.inNetMessagePool().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the inbound net pool", t);
}
try {
_context.clientMessagePool().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the client msg pool", t);
}
try {
_context.sessionKeyManager().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the session key manager", t);
}
try {
_context.messageHistory().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the message history logger", t);
}
// do stat manager last to reduce chance of NPEs in other threads
try {
_context.statManager().shutdown();
} catch (Throwable t) {
_log.error("Error shutting down the stats manager", t);
}
_context.deleteTempDir();
List<RouterContext> contexts = RouterContext.getContexts();
contexts.remove(_context);
try {
_context.elGamalEngine().shutdown();
} catch (Throwable t) {
_log.log(Log.CRIT, "Error shutting elGamal", t);
}
if (contexts.isEmpty()) {
// any thing else to shut down?
} else {
_log.logAlways(Log.WARN, "Warning - " + contexts.size() + " routers remaining in this JVM, not releasing all resources");
}
try {
((FortunaRandomSource) _context.random()).shutdown();
} catch (Throwable t) {
_log.log(Log.CRIT, "Error shutting random()", t);
}
// logManager shut down in finalShutdown()
_watchdog.shutdown();
_watchdogThread.interrupt();
_eventLog.addEvent(EventLog.STOPPED, Integer.toString(exitCode));
finalShutdown(exitCode);
}
use of net.i2p.util.I2PAppThread in project i2p.i2p-bote by i2p.
the class I2PBote method awaitShutdown.
/**
* Waits up to <code>timeout</code> milliseconds for the background threads to end.
* @param timeout In milliseconds
*/
private void awaitShutdown(long timeout) {
// the time at which any background threads that are still running are interrupted
long deadline = System.currentTimeMillis() + timeout;
for (I2PAppThread thread : backgroundThreads) if (thread != null)
try {
// the time until the original timeout
long remainingTime = deadline - System.currentTimeMillis();
if (remainingTime < 0)
return;
thread.join(remainingTime);
} catch (InterruptedException e) {
log.error("Interrupted while waiting for thread <" + thread.getName() + "> to exit", e);
return;
}
}
Aggregations