Search in sources :

Example 1 with I2PAppThread

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

the class SAMv3StreamSession method connect.

/**
 * Connect the SAM STREAM session to the specified Destination
 * for a single connection, using the socket stolen from the handler.
 *
 * @param handler The handler that communicates with the requesting client
 * @param dest Base64-encoded Destination to connect to
 * @param props Options to be used for connection
 *
 * @throws DataFormatException if the destination is not valid
 * @throws ConnectException if the destination refuses connections
 * @throws NoRouteToHostException if the destination can't be reached
 * @throws InterruptedIOException if the connection timeouts
 * @throws I2PException if there's another I2P-related error
 * @throws IOException
 */
public void connect(SAMv3Handler handler, String dest, Properties props) throws I2PException, ConnectException, NoRouteToHostException, DataFormatException, InterruptedIOException, IOException {
    boolean verbose = !Boolean.parseBoolean(props.getProperty("SILENT"));
    Destination d = SAMUtils.getDest(dest);
    I2PSocketOptions opts = socketMgr.buildOptions(props);
    if (props.getProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT) == null)
        opts.setConnectTimeout(60 * 1000);
    String fromPort = props.getProperty("FROM_PORT");
    if (fromPort != null) {
        try {
            opts.setLocalPort(Integer.parseInt(fromPort));
        } catch (NumberFormatException nfe) {
            throw new I2PException("Bad port " + fromPort);
        }
    }
    String toPort = props.getProperty("TO_PORT");
    if (toPort != null) {
        try {
            opts.setPort(Integer.parseInt(toPort));
        } catch (NumberFormatException nfe) {
            throw new I2PException("Bad port " + toPort);
        }
    }
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Connecting new I2PSocket...");
    // blocking connection (SAMv3)
    I2PSocket i2ps = socketMgr.connect(d, opts);
    SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
    if (rec == null)
        throw new InterruptedIOException();
    handler.notifyStreamResult(verbose, "OK", null);
    handler.stealSocket();
    ReadableByteChannel fromClient = handler.getClientSocket();
    ReadableByteChannel fromI2P = Channels.newChannel(i2ps.getInputStream());
    WritableByteChannel toClient = handler.getClientSocket();
    WritableByteChannel toI2P = Channels.newChannel(i2ps.getOutputStream());
    SAMBridge bridge = handler.getBridge();
    (new I2PAppThread(rec.getThreadGroup(), new Pipe(fromClient, toI2P, bridge), "ConnectV3 SAMPipeClientToI2P")).start();
    (new I2PAppThread(rec.getThreadGroup(), new Pipe(fromI2P, toClient, bridge), "ConnectV3 SAMPipeI2PToClient")).start();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) InterruptedIOException(java.io.InterruptedIOException) ReadableByteChannel(java.nio.channels.ReadableByteChannel) I2PSocket(net.i2p.client.streaming.I2PSocket) WritableByteChannel(java.nio.channels.WritableByteChannel) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions) I2PAppThread(net.i2p.util.I2PAppThread)

Example 2 with I2PAppThread

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

the class SAMReader method startReading.

public synchronized void startReading() {
    if (_live)
        throw new IllegalStateException();
    _live = true;
    I2PAppThread t = new I2PAppThread(new Runner(), "SAM reader " + _count.incrementAndGet());
    t.start();
    _thread = t;
}
Also used : I2PAppThread(net.i2p.util.I2PAppThread)

Example 3 with I2PAppThread

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

the class SAMStreamSend method send.

private void send(OutputStream samOut, SAMEventHandler eventHandler, int mode) throws IOException {
    Sender sender = new Sender(samOut, eventHandler, mode);
    boolean ok = sender.openConnection();
    if (ok) {
        I2PAppThread t = new I2PAppThread(sender, "Sender");
        t.start();
    } else {
        throw new IOException("Sender failed to connect");
    }
}
Also used : IOException(java.io.IOException) I2PAppThread(net.i2p.util.I2PAppThread)

Example 4 with I2PAppThread

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

the class SAMHandler method startHandling.

/**
 * Start handling the SAM connection, detaching an handling thread.
 */
public final void startHandling() {
    thread = new I2PAppThread(this, getClass().getSimpleName());
    thread.start();
}
Also used : I2PAppThread(net.i2p.util.I2PAppThread)

Example 5 with I2PAppThread

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

the class I2PTunnelIRCClient method clientConnectionRun.

protected void clientConnectionRun(Socket s) {
    if (_log.shouldLog(Log.INFO))
        _log.info("New connection local addr is: " + s.getLocalAddress() + " from: " + s.getInetAddress());
    I2PSocket i2ps = null;
    I2PSocketAddress addr = pickDestination();
    try {
        if (addr == null)
            throw new UnknownHostException("No valid destination configured");
        Destination clientDest = addr.getAddress();
        if (clientDest == null)
            throw new UnknownHostException("Could not resolve " + addr.getHostName());
        int port = addr.getPort();
        i2ps = createI2PSocket(clientDest, port);
        i2ps.setReadTimeout(readTimeout);
        StringBuffer expectedPong = new StringBuffer();
        DCCHelper dcc = _dccEnabled ? new DCC(s.getLocalAddress().getAddress()) : null;
        Thread in = new I2PAppThread(new IrcInboundFilter(s, i2ps, expectedPong, _log, dcc), "IRC Client " + _clientId + " in", true);
        in.start();
        // Thread out = new I2PAppThread(new IrcOutboundFilter(s,i2ps, expectedPong, _log, dcc), "IRC Client " + _clientId + " out", true);
        Runnable out = new IrcOutboundFilter(s, i2ps, expectedPong, _log, dcc);
        // we are called from an unlimited thread pool, so run inline
        // out.start();
        out.run();
    } catch (IOException ex) {
        // generally NoRouteToHostException
        if (_log.shouldLog(Log.WARN))
            _log.warn("Error connecting", ex);
        // l.log("Error connecting: " + ex.getMessage());
        try {
            // Send a response so the user doesn't just see a disconnect
            // and blame his router or the network.
            String name = addr != null ? addr.getHostName() : "undefined";
            String msg = ":" + name + " 499 you :" + ex + "\r\n";
            s.getOutputStream().write(DataHelper.getUTF8(msg));
        } catch (IOException ioe) {
        }
    } catch (I2PException ex) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Error connecting", ex);
        // l.log("Error connecting: " + ex.getMessage());
        try {
            // Send a response so the user doesn't just see a disconnect
            // and blame his router or the network.
            String name = addr != null ? addr.getHostName() : "undefined";
            String msg = ":" + name + " 499 you :" + ex + "\r\n";
            s.getOutputStream().write(DataHelper.getUTF8(msg));
        } catch (IOException ioe) {
        }
    } finally {
        // only because we are running it inline
        closeSocket(s);
        if (i2ps != null) {
            try {
                i2ps.close();
            } catch (IOException ioe) {
            }
            synchronized (sockLock) {
                mySockets.remove(i2ps);
            }
        }
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) UnknownHostException(java.net.UnknownHostException) I2PSocketAddress(net.i2p.client.streaming.I2PSocketAddress) I2PSocket(net.i2p.client.streaming.I2PSocket) IOException(java.io.IOException) DCCHelper(net.i2p.i2ptunnel.irc.DCCHelper) I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread) IrcOutboundFilter(net.i2p.i2ptunnel.irc.IrcOutboundFilter) IrcInboundFilter(net.i2p.i2ptunnel.irc.IrcInboundFilter)

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