package net.i2p.client.streaming;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import net.i2p.I2PException;
import net.i2p.client.I2PClient;
import net.i2p.client.I2PClientFactory;
import net.i2p.client.I2PSession;
import net.i2p.client.I2PSessionException;
import net.i2p.data.Destination;
import net.i2p.util.Log;

/**
 * Simplify the creation of I2PSession and transient I2P Destination objects if 
 * necessary to create a socket manager.  This class is most likely how classes
 * will begin their use of the socket library
 *
 */
public class I2PSocketManagerFactory {
    private final static Log _log = new Log(I2PSocketManagerFactory.class);

    /**
     * Create a socket manager using a brand new destination connected to the
     * I2CP router on the local machine on the default port (7654).
     * 
     * @return the newly created socket manager, or null if there were errors
     */
    public static I2PSocketManager createManager() {
        return createManager("localhost", 7654, new Properties());
    }

    /**
     * Create a socket manager using a brand new destination connected to the
     * I2CP router on the given machine reachable through the given port.
     *
     * @return the newly created socket manager, or null if there were errors
     */
    public static I2PSocketManager createManager(String i2cpHost, int i2cpPort, Properties opts) {
        I2PClient client = I2PClientFactory.createClient();
        ByteArrayOutputStream keyStream = new ByteArrayOutputStream(512);
        try {
            Destination dest = client.createDestination(keyStream);
            ByteArrayInputStream in = new ByteArrayInputStream(keyStream.toByteArray());
            return createManager(in, i2cpHost, i2cpPort, opts);
        } catch (IOException ioe) {
            _log.error("Error creating the destination for socket manager", ioe);
            return null;
        } catch (I2PException ie) {
            _log.error("Error creating the destination for socket manager", ie);
            return null;
        }
    }

    /**
     * Create a socket manager using the destination loaded from the given private key
     * stream and connected to the I2CP router on the specified machine on the given
     * port
     *
     * @return the newly created socket manager, or null if there were errors
     */
    public static I2PSocketManager createManager(InputStream myPrivateKeyStream, String i2cpHost, int i2cpPort,
                                                 Properties opts) {
        I2PClient client = I2PClientFactory.createClient();
        opts.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_GUARANTEED);
        opts.setProperty(I2PClient.PROP_TCP_HOST, i2cpHost);
        opts.setProperty(I2PClient.PROP_TCP_PORT, "" + i2cpPort);
        try {
            I2PSession session = client.createSession(myPrivateKeyStream, opts);
            session.connect();
            return createManager(session);
        } catch (I2PSessionException ise) {
            _log.error("Error creating session for socket manager", ise);
            return null;
        }
    }

    private static I2PSocketManager createManager(I2PSession session) {
        I2PSocketManager mgr = new I2PSocketManager();
        mgr.setSession(session);
        mgr.setDefaultOptions(new I2PSocketOptions());
        return mgr;
    }
}
@@ -1074,52 +1074,465 @@
     
-return createManager(%22localhost%22, 7654, new 
+String i2cpHost = System.getProperty(I2PClient.PROP_TCP_HOST, %22localhost%22);%0A        int i2cpPort = 7654;%0A        String i2cpPortStr = System.getProperty(I2PClient.PROP_TCP_PORT);%0A        if (i2cpPortStr != null) %7B%0A            try %7B%0A                i2cpPort = Integer.parseInt(i2cpPortStr);%0A            %7D catch (NumberFormatException nfe) %7B%0A                // gobble gobble%0A            %7D%0A        %7D%0A%0A        return createManager(i2cpHost, i2cpPort, System.get
 Prop

use the env defaults when no i2cp info is specified