Search in sources :

Example 41 with I2PAppThread

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

the class SAMMessageSession method start.

/*
     * @since 0.9.25
     */
public void start() {
    Thread t = new I2PAppThread(handler, "SAMMessageSessionHandler");
    t.start();
}
Also used : I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread)

Example 42 with I2PAppThread

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

the class SAMStreamSession method createSocketHandler.

/**
 * Create a new SAM STREAM session socket handler, detaching its thread.
 *
 * @param s Socket to be handled
 * @param id Socket id, or 0 if it must be auto-generated
 *
 * @return An id associated to the socket handler
 */
protected int createSocketHandler(I2PSocket s, int id) {
    SAMStreamSessionSocketReader reader = null;
    StreamSender sender = null;
    if (id == 0) {
        id = createUniqueId();
    }
    try {
        reader = newSAMStreamSessionSocketReader(s, id);
        sender = newStreamSender(s, id);
    } catch (IOException e) {
        _log.error("IOException when creating SAM STREAM session socket handler", e);
        recv.stopStreamReceiving();
        return 0;
    }
    synchronized (handlersMap) {
        handlersMap.put(Integer.valueOf(id), reader);
        sendersMap.put(Integer.valueOf(id), sender);
    }
    I2PAppThread t = new I2PAppThread(reader, "SAMReader" + id);
    t.start();
    t = new I2PAppThread(sender, "SAMSender" + id);
    t.start();
    return id;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) I2PAppThread(net.i2p.util.I2PAppThread)

Example 43 with I2PAppThread

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

the class SAMStreamSession method start.

/*
     * @since 0.9.25
     */
public void start() {
    if (server != null) {
        Thread t = new I2PAppThread(server, "SAMStreamSessionServer");
        t.start();
    }
}
Also used : I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread)

Example 44 with I2PAppThread

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

the class TCPtoI2P method run.

/**
 * TCP stream to I2P stream thread starter
 */
public void run() {
    String line, input;
    InputStream Iin = null;
    OutputStream Iout = null;
    InputStream in = null;
    OutputStream out = null;
    Thread t = null;
    Thread q = null;
    try {
        try {
            in = sock.getInputStream();
            out = sock.getOutputStream();
            line = lnRead(in);
            input = line.toLowerCase(Locale.US);
            Destination dest = null;
            if (input.endsWith(".i2p")) {
                // dest = I2PTunnel.destFromName(input);
                dest = I2PAppContext.getGlobalContext().namingService().lookup(input);
                if (dest != null) {
                    line = dest.toBase64();
                } else {
                    Emsg("Can't find destination: " + input, out);
                    return;
                }
            }
            dest = new Destination();
            dest.fromBase64(line);
            try {
                // get a client socket
                I2P = socketManager.connect(dest);
                // temp bugfix, this *SHOULD* be the default
                I2P.setReadTimeout(0);
                // make readers/writers
                Iin = I2P.getInputStream();
                Iout = I2P.getOutputStream();
                // setup to cross the streams
                // app -> I2P
                TCPio conn_c = new TCPio(in, Iout, lives);
                // I2P -> app
                TCPio conn_a = new TCPio(Iin, out, lives);
                t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " TCPioA");
                q = new I2PAppThread(conn_a, Thread.currentThread().getName() + " TCPioB");
                // Fire!
                t.start();
                q.start();
                while (t.isAlive() && q.isAlive() && lives.get()) {
                    // AND is used here to kill off the other thread
                    // sleep for 10 ms
                    Thread.sleep(10);
                }
            } catch (I2PException e) {
                Emsg(e.toString(), out);
            } catch (ConnectException e) {
                Emsg(e.toString(), out);
            } catch (NoRouteToHostException e) {
                Emsg(e.toString(), out);
            }
        } catch (InterruptedIOException e) {
        // We're breaking away.
        } catch (InterruptedException e) {
        // ditto
        } catch (IOException e) {
            try {
                Emsg(e.toString(), out);
            } catch (IOException ex) {
            // ditto
            }
        } catch (DataFormatException e) {
            try {
                Emsg(e.toString(), out);
            } catch (IOException ex) {
            // ditto
            }
        }
    } finally {
        try {
            t.interrupt();
        } catch (Exception e) {
        }
        try {
            q.interrupt();
        } catch (Exception e) {
        }
        try {
            in.close();
        } catch (Exception e) {
        }
        try {
            out.close();
        } catch (Exception e) {
        }
        try {
            Iin.close();
        } catch (Exception e) {
        }
        try {
            Iout.close();
        } catch (Exception e) {
        }
        try {
            // System.out.println("TCPtoI2P: Close I2P");
            I2P.close();
        } catch (Exception e) {
        }
        try {
            // System.out.println("TCPtoI2P: Close sock");
            sock.close();
        } catch (Exception e) {
        }
    }
// System.out.println("TCPtoI2P: Done.");
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) InterruptedIOException(java.io.InterruptedIOException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) I2PAppThread(net.i2p.util.I2PAppThread) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) I2PException(net.i2p.I2PException) ConnectException(java.net.ConnectException) NoRouteToHostException(java.net.NoRouteToHostException) I2PAppThread(net.i2p.util.I2PAppThread) DataFormatException(net.i2p.data.DataFormatException) ConnectException(java.net.ConnectException)

Example 45 with I2PAppThread

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

the class I2Plistener method run.

/**
 * Simply listen on I2P port, and thread connections
 */
public void run() {
    boolean g = false;
    I2PSocket sessSocket = null;
    int conn = 0;
    try {
        try {
            serverSocket.setSoTimeout(50);
            while (lives.get()) {
                try {
                    sessSocket = serverSocket.accept();
                    g = true;
                } catch (ConnectException ce) {
                    g = false;
                } catch (SocketTimeoutException ste) {
                    g = false;
                }
                if (g) {
                    g = false;
                    conn++;
                    // toss the connection to a new thread.
                    I2PtoTCP conn_c = new I2PtoTCP(sessSocket, info, database, lives);
                    Thread t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " I2PtoTCP " + conn);
                    t.start();
                }
            }
        } catch (I2PException e) {
            // bad stuff
            System.out.println("Exception " + e);
        }
    } finally {
        try {
            serverSocket.close();
        } catch (I2PException ex) {
        }
    // System.out.println("I2Plistener: Close");
    }
}
Also used : I2PException(net.i2p.I2PException) SocketTimeoutException(java.net.SocketTimeoutException) I2PSocket(net.i2p.client.streaming.I2PSocket) I2PAppThread(net.i2p.util.I2PAppThread) ConnectException(java.net.ConnectException) 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