Search in sources :

Example 1 with SimpleTimer2

use of net.i2p.util.SimpleTimer2 in project i2p.i2p by i2p.

the class TrackerClient method halt.

/**
 * Interrupts this Thread to stop it.
 * @param fast if true, limit the life of the unannounce threads
 */
public synchronized void halt(boolean fast) {
    boolean wasStopped = stop;
    if (wasStopped) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Already stopped: " + _threadName);
    } else {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Stopping: " + _threadName);
        stop = true;
    }
    SimpleTimer2.TimedEvent e = _event;
    if (e != null) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Cancelling next announce " + _threadName);
        e.cancel();
        _event = null;
    }
    Thread t = _thread;
    if (t != null) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Interrupting " + t.getName());
        t.interrupt();
    }
    _fastUnannounce = true;
    if (!wasStopped)
        unannounce();
}
Also used : SimpleTimer2(net.i2p.util.SimpleTimer2) I2PAppThread(net.i2p.util.I2PAppThread)

Example 2 with SimpleTimer2

use of net.i2p.util.SimpleTimer2 in project i2p.i2p by i2p.

the class PluginStarter method runClientApps.

/**
 *  @param action "start" or "stop" or "uninstall"
 *  @throws Exception just about anything if an app has a delay less than zero, caller would be wise to catch Throwable
 *  If no apps have a delay less than zero, it shouldn't throw anything
 */
private static void runClientApps(RouterContext ctx, File pluginDir, List<ClientAppConfig> apps, String action) throws Exception {
    Log log = ctx.logManager().getLog(PluginStarter.class);
    // initialize pluginThreadGroup and _pendingPluginClients
    String pluginName = pluginDir.getName();
    if (!pluginThreadGroups.containsKey(pluginName))
        pluginThreadGroups.put(pluginName, new ThreadGroup(pluginName));
    ThreadGroup pluginThreadGroup = pluginThreadGroups.get(pluginName);
    if (action.equals("start"))
        _pendingPluginClients.put(pluginName, new ConcurrentHashSet<SimpleTimer2.TimedEvent>());
    for (ClientAppConfig app : apps) {
        // bypass all the logic below.
        if (action.equals("stop")) {
            String[] argVal = LoadClientAppsJob.parseArgs(app.args);
            // Do this after parsing so we don't need to worry about quoting
            for (int i = 0; i < argVal.length; i++) {
                if (argVal[i].indexOf('$') >= 0) {
                    argVal[i] = argVal[i].replace("$I2P", ctx.getBaseDir().getAbsolutePath());
                    argVal[i] = argVal[i].replace("$CONFIG", ctx.getConfigDir().getAbsolutePath());
                    argVal[i] = argVal[i].replace("$PLUGIN", pluginDir.getAbsolutePath());
                }
            }
            ClientApp ca = ctx.routerAppManager().getClientApp(app.className, argVal);
            if (ca != null) {
                // even if (ca.getState() != ClientAppState.RUNNING), we do this, we don't want to fall thru
                try {
                    ca.shutdown(LoadClientAppsJob.parseArgs(app.stopargs));
                } catch (Throwable t) {
                    throw new Exception(t);
                }
                continue;
            }
        }
        if (action.equals("start") && app.disabled)
            continue;
        String[] argVal;
        if (action.equals("start")) {
            // start
            argVal = LoadClientAppsJob.parseArgs(app.args);
        } else {
            String args;
            if (action.equals("stop"))
                args = app.stopargs;
            else if (action.equals("uninstall"))
                args = app.uninstallargs;
            else
                throw new IllegalArgumentException("bad action");
            // args must be present
            if (args == null || args.length() <= 0)
                continue;
            argVal = LoadClientAppsJob.parseArgs(args);
        }
        // do this after parsing so we don't need to worry about quoting
        for (int i = 0; i < argVal.length; i++) {
            if (argVal[i].indexOf('$') >= 0) {
                argVal[i] = argVal[i].replace("$I2P", ctx.getBaseDir().getAbsolutePath());
                argVal[i] = argVal[i].replace("$CONFIG", ctx.getConfigDir().getAbsolutePath());
                argVal[i] = argVal[i].replace("$PLUGIN", pluginDir.getAbsolutePath());
            }
        }
        ClassLoader cl = null;
        if (app.classpath != null) {
            String cp = app.classpath;
            if (cp.indexOf('$') >= 0) {
                cp = cp.replace("$I2P", ctx.getBaseDir().getAbsolutePath());
                cp = cp.replace("$CONFIG", ctx.getConfigDir().getAbsolutePath());
                cp = cp.replace("$PLUGIN", pluginDir.getAbsolutePath());
            }
            // Old way - add for the whole JVM
            // addToClasspath(cp, app.clientName, log);
            // New way - add only for this client
            // We cache the ClassLoader we start the client with, so
            // we can reuse it for stopping and uninstalling.
            // If we don't, the client won't be able to find its
            // static members.
            String clCacheKey = pluginName + app.className + app.args;
            if (!action.equals("start"))
                cl = _clCache.get(clCacheKey);
            if (cl == null) {
                URL[] urls = classpathToURLArray(cp, app.clientName, log);
                if (urls != null) {
                    cl = new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
                    if (action.equals("start"))
                        _clCache.put(clCacheKey, cl);
                }
            }
        }
        if (app.delay < 0 && action.equals("start")) {
            // this will throw exceptions
            LoadClientAppsJob.runClientInline(app.className, app.clientName, argVal, log, cl);
        } else if (app.delay == 0 || !action.equals("start")) {
            // quick check, will throw ClassNotFoundException on error
            LoadClientAppsJob.testClient(app.className, cl);
            // run this guy now
            LoadClientAppsJob.runClient(app.className, app.clientName, argVal, ctx, log, pluginThreadGroup, cl);
        } else {
            // If it bombs after that, then we throw the ClassNotFoundException.
            try {
                // quick check
                LoadClientAppsJob.testClient(app.className, cl);
            } catch (ClassNotFoundException ex) {
                // Under normal circumstances there will be no delay at all.
                try {
                    if (app.delay > 1) {
                        Thread.sleep(2000);
                    } else {
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException ie) {
                }
                // quick check, will throw ClassNotFoundException on error
                LoadClientAppsJob.testClient(app.className, cl);
            }
            // wait before firing it up
            SimpleTimer2.TimedEvent evt = new TrackedDelayedClient(pluginName, ctx.simpleTimer2(), ctx, app.className, app.clientName, argVal, pluginThreadGroup, cl);
            evt.schedule(app.delay);
        }
    }
}
Also used : Log(net.i2p.util.Log) ClientApp(net.i2p.app.ClientApp) IOException(java.io.IOException) URL(java.net.URL) ConcurrentHashSet(net.i2p.util.ConcurrentHashSet) URLClassLoader(java.net.URLClassLoader) ClientAppConfig(net.i2p.router.startup.ClientAppConfig) URLClassLoader(java.net.URLClassLoader) SimpleTimer2(net.i2p.util.SimpleTimer2)

Example 3 with SimpleTimer2

use of net.i2p.util.SimpleTimer2 in project i2p.i2p by i2p.

the class BOB method loadConfig.

/**
 * @since 0.9.10
 */
private void loadConfig() {
    int i = 0;
    boolean save = false;
    // Set up all defaults to be passed forward to other threads.
    // Re-reading the config file in each thread is pretty damn stupid.
    String configLocation = System.getProperty(PROP_CONFIG_LOCATION, "bob.config");
    // This is here just to ensure there is no interference with our threadgroups.
    SimpleTimer2 Y2 = SimpleTimer2.getInstance();
    i = Y2.hashCode();
    {
        File cfg = new File(configLocation);
        if (!cfg.isAbsolute()) {
            cfg = new File(I2PAppContext.getGlobalContext().getConfigDir(), configLocation);
        }
        FileInputStream fi = null;
        try {
            fi = new FileInputStream(cfg);
            props.load(fi);
        } catch (FileNotFoundException fnfe) {
            _log.warn("Unable to load up the BOB config file " + cfg.getAbsolutePath() + ", Using defaults.", fnfe);
            save = true;
        } catch (IOException ioe) {
            _log.warn("IOException on BOB config file " + cfg.getAbsolutePath() + ", using defaults.", ioe);
        } finally {
            if (fi != null)
                try {
                    fi.close();
                } catch (IOException ioe) {
                }
        }
    }
    // Global router and client API configurations that are missing are set to defaults here.
    if (!props.containsKey(I2PClient.PROP_TCP_HOST)) {
        props.setProperty(I2PClient.PROP_TCP_HOST, "localhost");
        save = true;
    }
    if (!props.containsKey(I2PClient.PROP_TCP_PORT)) {
        props.setProperty(I2PClient.PROP_TCP_PORT, "7654");
        save = true;
    }
    if (!props.containsKey(PROP_BOB_PORT)) {
        // 0xB0B
        props.setProperty(PROP_BOB_PORT, "2827");
        save = true;
    }
    if (!props.containsKey("inbound.length")) {
        props.setProperty("inbound.length", "3");
        save = true;
    }
    if (!props.containsKey("outbound.length")) {
        props.setProperty("outbound.length", "3");
        save = true;
    }
    if (!props.containsKey("inbound.lengthVariance")) {
        props.setProperty("inbound.lengthVariance", "0");
        save = true;
    }
    if (!props.containsKey("outbound.lengthVariance")) {
        props.setProperty("outbound.lengthVariance", "0");
        save = true;
    }
    if (!props.containsKey(PROP_BOB_HOST)) {
        props.setProperty(PROP_BOB_HOST, "localhost");
        save = true;
    }
    // PROP_RELIABILITY_NONE, PROP_RELIABILITY_BEST_EFFORT, PROP_RELIABILITY_GUARANTEED
    if (!props.containsKey(PROP_CFG_VER)) {
        props.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_NONE);
        props.setProperty(PROP_CFG_VER, "1");
        save = true;
    }
    if (save) {
        File cfg = new File(configLocation);
        if (!cfg.isAbsolute()) {
            cfg = new File(I2PAppContext.getGlobalContext().getConfigDir(), configLocation);
        }
        FileOutputStream fo = null;
        try {
            _log.warn("Writing new defaults file " + cfg.getAbsolutePath());
            fo = new FileOutputStream(cfg);
            props.store(fo, cfg.getAbsolutePath());
        } catch (IOException ioe) {
            _log.error("IOException on BOB config file " + cfg.getAbsolutePath(), ioe);
        } finally {
            if (fo != null)
                try {
                    fo.close();
                } catch (IOException ioe) {
                }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) SimpleTimer2(net.i2p.util.SimpleTimer2) FileInputStream(java.io.FileInputStream)

Example 4 with SimpleTimer2

use of net.i2p.util.SimpleTimer2 in project i2p.i2p by i2p.

the class Main method main.

/**
 * @param args the command line arguments, these are not used yet
 */
public static void main(String[] args) {
    // THINK THINK THINK THINK THINK THINK
    SimpleTimer2 Y2 = SimpleTimer2.getInstance();
    BOB.main(args);
    Y2.stop();
}
Also used : SimpleTimer2(net.i2p.util.SimpleTimer2)

Aggregations

SimpleTimer2 (net.i2p.util.SimpleTimer2)4 IOException (java.io.IOException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ClientApp (net.i2p.app.ClientApp)1 ClientAppConfig (net.i2p.router.startup.ClientAppConfig)1 ConcurrentHashSet (net.i2p.util.ConcurrentHashSet)1 I2PAppThread (net.i2p.util.I2PAppThread)1 Log (net.i2p.util.Log)1