Search in sources :

Example 21 with I2PAppThread

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

the class PeerConnectionOut method startup.

public void startup() {
    thread = new I2PAppThread(this, "Snark sender " + _id + ": " + peer);
    thread.start();
}
Also used : I2PAppThread(net.i2p.util.I2PAppThread)

Example 22 with I2PAppThread

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

the class MUXlisten method run.

/**
 * MUX sockets, fire off a thread to connect, get destination info, and do I/O
 */
public void run() {
    I2PServerSocket SS = null;
    Thread t = null;
    Thread q = null;
    try {
        wlock();
        try {
            try {
                info.add("RUNNING", Boolean.TRUE);
            } catch (Exception e) {
                lock.set(false);
                return;
            }
        } catch (Exception e) {
            lock.set(false);
            return;
        } finally {
            wunlock();
        }
        lives.set(true);
        lock.set(false);
        quit: {
            try {
                tg = new ThreadGroup(N);
                {
                    if (go_out) {
                        // I2P -> TCP
                        SS = socketManager.getServerSocket();
                        I2Plistener conn = new I2Plistener(SS, socketManager, info, database, _log, lives);
                        t = new I2PAppThread(tg, conn, "BOBI2Plistener " + N);
                        t.start();
                    }
                    if (come_in) {
                        // TCP -> I2P
                        TCPlistener conn = new TCPlistener(listener, socketManager, info, database, _log, lives);
                        q = new I2PAppThread(tg, conn, "BOBTCPlistener " + N);
                        q.start();
                    }
                    wlock();
                    try {
                        try {
                            info.add("STARTING", Boolean.FALSE);
                        } catch (Exception e) {
                            break quit;
                        }
                    } catch (Exception e) {
                        break quit;
                    } finally {
                        wunlock();
                    }
                    boolean spin = true;
                    while (spin && lives.get()) {
                        try {
                            // sleep for 1 second
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            break quit;
                        }
                        rlock();
                        try {
                            try {
                                spin = info.get("STOPPING").equals(Boolean.FALSE);
                            } catch (Exception e) {
                                break quit;
                            }
                        } catch (Exception e) {
                            break quit;
                        } finally {
                            runlock();
                        }
                    }
                }
            // die
            } catch (Exception e) {
                // System.out.println("MUXlisten: Caught an exception" + e);
                break quit;
            }
        }
    // quit
    } finally {
        lives.set(false);
        // Some grace time.
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
        }
        try {
            wlock();
            try {
                info.add("STARTING", Boolean.FALSE);
                info.add("STOPPING", Boolean.TRUE);
                info.add("RUNNING", Boolean.FALSE);
            } catch (Exception e) {
                lock.set(false);
                return;
            }
        } catch (Exception e) {
        } finally {
            wunlock();
        }
        // Start cleanup.
        while (!lock.compareAndSet(false, true)) {
        // wait
        }
        if (SS != null) {
            try {
                SS.close();
            } catch (I2PException ex) {
            }
        }
        if (listener != null) {
            try {
                listener.close();
            } catch (IOException e) {
            }
        }
        // Some grace time.
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
        }
        // Hopefully nuke stuff here...
        {
            String groupName = tg.getName();
            try {
                _log.warn("destroySocketManager " + groupName);
                socketManager.destroySocketManager();
                _log.warn("destroySocketManager Successful" + groupName);
            } catch (Exception e) {
                // nop
                _log.warn("destroySocketManager Failed" + groupName);
                _log.warn(e.toString());
            }
        }
        // zero out everything.
        try {
            wlock();
            try {
                info.add("STARTING", Boolean.FALSE);
                info.add("STOPPING", Boolean.FALSE);
                info.add("RUNNING", Boolean.FALSE);
            } catch (Exception e) {
                lock.set(false);
                return;
            } finally {
                wunlock();
            }
        } catch (Exception e) {
        }
        // Should we force waiting for all threads??
        lock.set(false);
        // Wait around till all threads are collected.
        if (tg != null) {
            String groupName = tg.getName();
            // System.out.println("BOB: MUXlisten: Starting thread collection for: " + groupName);
            _log.warn("BOB: MUXlisten: Starting thread collection for: " + groupName);
            if (tg.activeCount() + tg.activeGroupCount() != 0) {
                // visit(tg, 0, groupName);
                int foo = tg.activeCount() + tg.activeGroupCount();
                // Happily spin forever :-(
                while (foo != 0) {
                    foo = tg.activeCount() + tg.activeGroupCount();
                    // bar = lives;
                    try {
                        // sleep for 100 ms (One tenth second)
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                    // nop
                    }
                }
            }
            // System.out.println("BOB: MUXlisten: Threads went away. Success: " + groupName);
            _log.warn("BOB: MUXlisten: Threads went away. Success: " + groupName);
            tg.destroy();
            // Zap reference to the ThreadGroup so the JVM can GC it.
            tg = null;
        }
        try {
            socketManager.destroySocketManager();
        } catch (Exception e) {
        // nop
        }
    }
}
Also used : I2PException(net.i2p.I2PException) IOException(java.io.IOException) I2PServerSocket(net.i2p.client.streaming.I2PServerSocket) IOException(java.io.IOException) I2PException(net.i2p.I2PException) I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread)

Example 23 with I2PAppThread

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

the class TCPlistener method run.

/**
 * Simply listen on TCP port, and thread connections
 */
public void run() {
    boolean g = false;
    int conn = 0;
    Socket server = null;
    try {
        try {
            // We don't block, we cycle and check.
            listener.setSoTimeout(50);
            while (lives.get()) {
                try {
                    server = listener.accept();
                    server.setKeepAlive(true);
                    g = true;
                } catch (SocketTimeoutException ste) {
                    g = false;
                }
                if (g) {
                    conn++;
                    // toss the connection to a new thread.
                    TCPtoI2P conn_c = new TCPtoI2P(socketManager, server, info, database, lives);
                    Thread t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " TCPtoI2P " + conn);
                    t.start();
                    g = false;
                }
            }
        } catch (IOException ioe) {
        }
    } finally {
        try {
            listener.close();
        } catch (IOException ex) {
        }
    // System.out.println("TCPlistener: " + Thread.currentThread().getName() +  "Done.");
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) I2PServerSocket(net.i2p.client.streaming.I2PServerSocket) I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread)

Example 24 with I2PAppThread

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

the class PersistentMailCache method locked_getMails.

private Collection<Mail> locked_getMails() {
    Queue<File> fq = new LinkedBlockingQueue<File>();
    for (int j = 0; j < B64.length(); j++) {
        File subdir = new File(_cacheDir, DIR_PREFIX + B64.charAt(j));
        File[] files = subdir.listFiles();
        if (files == null)
            continue;
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (!f.isFile())
                continue;
            // Threaded, handle below
            // Mail mail = load(f);
            // if (mail != null)
            // rv.add(mail);
            fq.offer(f);
        }
    }
    int sz = fq.size();
    if (sz <= 0)
        return Collections.emptyList();
    // thread the read-in
    long begin = _context.clock().now();
    Queue<Mail> rv = new LinkedBlockingQueue<Mail>();
    int tcnt = Math.max(1, Math.min(sz / 4, Math.min(SystemVersion.getCores(), 16)));
    List<Thread> threads = new ArrayList<Thread>(tcnt);
    for (int i = 0; i < tcnt; i++) {
        Thread t = new I2PAppThread(new Loader(fq, rv), "Email loader " + i);
        t.start();
        threads.add(t);
    }
    for (int i = 0; i < tcnt; i++) {
        try {
            threads.get(i).join();
        } catch (InterruptedException ie) {
            break;
        }
    }
    long end = _context.clock().now();
    Debug.debug(Debug.DEBUG, "Loaded " + sz + " emails with " + tcnt + " threads in " + DataHelper.formatDuration(end - begin));
    return rv;
}
Also used : ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Example 25 with I2PAppThread

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

the class Router method startupStuff.

/**
 *  Initializes the RouterContext.
 *  Starts some threads. Does not install updates.
 *  All this was in the constructor.
 *
 *  Could block for 10 seconds or forever if waiting for entropy
 *
 *  @since 0.8.12
 */
private void startupStuff() {
    // *********  Start no threads before here ********* //
    _log = _context.logManager().getLog(Router.class);
    // NOW we can start the ping file thread.
    if (!SystemVersion.isAndroid())
        beginMarkingLiveliness();
    // Apps may use this as an easy way to determine if they are in the router JVM
    // But context.isRouterContext() is even easier...
    // Both of these as of 0.7.9
    // As of 0.9.34, this is FULL_VERSION, not VERSION, which was the same as CoreVersion.VERSION
    // and thus not particularly useful.
    System.setProperty("router.version", RouterVersion.FULL_VERSION);
    // crypto init may block for 10 seconds waiting for entropy
    // we want to do this before context.initAll()
    // which will fire up several things that could block on the PRNG init
    warmupCrypto();
    // NOW we start all the activity
    _context.initAll();
    // If we don't have a wrapper, RouterLaunch does this for us.
    if (_context.hasWrapper()) {
        File f = new File(System.getProperty("java.io.tmpdir"), "wrapper.log");
        if (!f.exists())
            f = new File(_context.getBaseDir(), "wrapper.log");
        if (f.exists())
            SecureFileOutputStream.setPerms(f);
    }
    CryptoChecker.warnUnavailableCrypto(_context);
    _routerInfo = null;
    if (_log.shouldLog(Log.INFO))
        _log.info("New router created with config file " + _configFilename);
    _oomListener = new OOMListener(_context);
    _shutdownHook = new ShutdownHook(_context);
    _gracefulShutdownDetector = new I2PAppThread(new GracefulShutdown(_context), "Graceful ShutdownHook", true);
    _gracefulShutdownDetector.setPriority(Thread.NORM_PRIORITY + 1);
    _gracefulShutdownDetector.start();
    _watchdog = new RouterWatchdog(_context);
    _watchdogThread = new I2PAppThread(_watchdog, "RouterWatchdog", true);
    _watchdogThread.setPriority(Thread.NORM_PRIORITY + 1);
    _watchdogThread.start();
}
Also used : File(java.io.File) I2PAppThread(net.i2p.util.I2PAppThread)

Aggregations

I2PAppThread (net.i2p.util.I2PAppThread)52 IOException (java.io.IOException)18 I2PException (net.i2p.I2PException)9 I2PSocket (net.i2p.client.streaming.I2PSocket)7 InterruptedIOException (java.io.InterruptedIOException)6 ConnectException (java.net.ConnectException)5 Socket (java.net.Socket)5 I2PSessionException (net.i2p.client.I2PSessionException)5 Destination (net.i2p.data.Destination)5 File (java.io.File)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ServerSocket (java.net.ServerSocket)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 Properties (java.util.Properties)3 I2PServerSocket (net.i2p.client.streaming.I2PServerSocket)3 DataFormatException (net.i2p.data.DataFormatException)3 FileNotFoundException (java.io.FileNotFoundException)2