Changeset f3c4a26 for apps/ministreaming


Ignore:
Timestamp:
Jul 10, 2013 6:54:25 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
70a8ab1
Parents:
9a1e1a9
Message:
  • I2CP/I2PTunnel locking fixes (partial fixes for tickets 650. 815, 946, 947, 953):
    • I2PSocketManagerFactory: New createDisconnectedManager(), javadocs
    • I2PSessionImpl: Rewrite state management and locking, prevent multiple connect() calls, but allow disconnect() to interrupt connect()
    • I2PSimpleSession: Changes to match I2PSessionImpl
    • I2PTunnelServer: Don't connect in constructor, use createDisconnectedManager() for a final manager, finals and cleanups Lightly tested. Todo: I2PTunnelClientBase
File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketManagerFactory.java

    r9a1e1a9 rf3c4a26  
    2727
    2828    public static final String PROP_MANAGER = "i2p.streaming.manager";
    29     //public static final String DEFAULT_MANAGER = "net.i2p.client.streaming.I2PSocketManagerImpl";
    3029    public static final String DEFAULT_MANAGER = "net.i2p.client.streaming.I2PSocketManagerFull";
    3130   
     
    4847     * Blocks for a long time while the router builds tunnels.
    4948     *
    50      * @param opts I2CP options
     49     * @param opts Streaming and I2CP options, may be null
    5150     * @return the newly created socket manager, or null if there were errors
    5251     */
     
    6160     * Blocks for a long time while the router builds tunnels.
    6261     *
    63      * @param host I2CP host
    64      * @param port I2CP port
     62     * @param host I2CP host null to use default
     63     * @param port I2CP port <= 0 to use default
    6564     * @return the newly created socket manager, or null if there were errors
    6665     */
     
    7574     * Blocks for a long time while the router builds tunnels.
    7675     *
    77      * @param i2cpHost I2CP host
    78      * @param i2cpPort I2CP port
    79      * @param opts I2CP options
     76     * @param i2cpHost I2CP host null to use default
     77     * @param i2cpPort I2CP port <= 0 to use default
     78     * @param opts Streaming and I2CP options, may be null
    8079     * @return the newly created socket manager, or null if there were errors
    8180     */
     
    103102     *
    104103     * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
     104     *                           or null for a transient destination. Caller must close.
    105105     * @return the newly created socket manager, or null if there were errors
    106106     */
     
    116116     *
    117117     * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
    118      * @param opts I2CP options
     118     *                           or null for a transient destination. Caller must close.
     119     * @param opts Streaming and I2CP options, may be null
    119120     * @return the newly created socket manager, or null if there were errors
    120121     */
     
    131132     *
    132133     * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
    133      * @param i2cpHost I2CP host
    134      * @param i2cpPort I2CP port
    135      * @param opts I2CP options
     134     *                           or null for a transient destination. Caller must close.
     135     * @param i2cpHost I2CP host null to use default
     136     * @param i2cpPort I2CP port <= 0 to use default
     137     * @param opts Streaming and I2CP options, may be null
    136138     * @return the newly created socket manager, or null if there were errors
    137139     */
    138140    public static I2PSocketManager createManager(InputStream myPrivateKeyStream, String i2cpHost, int i2cpPort,
    139141                                                 Properties opts) {
     142        try {
     143            return createManager(myPrivateKeyStream, i2cpHost, i2cpPort, opts, true);
     144        } catch (I2PSessionException ise) {
     145            getLog().error("Error creating session for socket manager", ise);
     146            return null;
     147        }
     148    }
     149   
     150    /**
     151     * Create a disconnected socket manager using the destination loaded from the given private key
     152     * stream, or null for a transient destination.
     153     *
     154     * Non-blocking. Does not connect to the router or build tunnels.
     155     * For servers, caller MUST call getSession().connect() to build tunnels and start listening.
     156     * For clients, caller may do that to build tunnels in advance;
     157     * otherwise, the first call to connect() will initiate a connection to the router,
     158     * with significant delay for tunnel building.
     159     *
     160     * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
     161     *                           or null for a transient destination. Caller must close.
     162     * @param i2cpHost I2CP host null to use default
     163     * @param i2cpPort I2CP port <= 0 to use default
     164     * @param opts Streaming and I2CP options, may be null
     165     * @return the newly created socket manager, non-null (throws on error)
     166     * @since 0.9.8
     167     */
     168    public static I2PSocketManager createDisconnectedManager(InputStream myPrivateKeyStream, String i2cpHost,
     169                                                             int i2cpPort, Properties opts) throws I2PSessionException {
     170        if (myPrivateKeyStream == null) {
     171            I2PClient client = I2PClientFactory.createClient();
     172            ByteArrayOutputStream keyStream = new ByteArrayOutputStream(512);
     173            try {
     174                client.createDestination(keyStream);
     175            } catch (Exception e) {
     176                throw new I2PSessionException("Error creating keys", e);
     177            }
     178            myPrivateKeyStream = new ByteArrayInputStream(keyStream.toByteArray());
     179        }
     180        return createManager(myPrivateKeyStream, i2cpHost, i2cpPort, opts, false);
     181    }
     182   
     183    /**
     184     * Create a socket manager using the destination loaded from the given private key
     185     * stream and connected to the I2CP router on the specified machine on the given
     186     * port.
     187     *
     188     * Blocks for a long time while the router builds tunnels if connect is true.
     189     *
     190     * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
     191     *                           non-null. Caller must close.
     192     * @param i2cpHost I2CP host null to use default
     193     * @param i2cpPort I2CP port <= 0 to use default
     194     * @param opts Streaming and I2CP options, may be null
     195     * @param connect true to connect (blocking)
     196     * @return the newly created socket manager, non-null (throws on error)
     197     * @since 0.9.7
     198     */
     199    private static I2PSocketManager createManager(InputStream myPrivateKeyStream, String i2cpHost, int i2cpPort,
     200                                                 Properties opts, boolean connect) throws I2PSessionException {
    140201        I2PClient client = I2PClientFactory.createClient();
    141202        if (opts == null)
     
    147208                opts.setProperty(name, (String) e.getValue());
    148209        }
    149         //boolean oldLib = DEFAULT_MANAGER.equals(opts.getProperty(PROP_MANAGER, DEFAULT_MANAGER));
    150         //if (oldLib && false) {
    151             // for the old streaming lib
    152         //    opts.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_GUARANTEED);
    153             //opts.setProperty("tunnels.depthInbound", "0");
    154         //} else {
    155             // for new streaming lib:
    156             //opts.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_BEST_EFFORT);
    157             // as of 0.8.1 (I2CP default is BestEffort)
    158             if (!opts.containsKey(I2PClient.PROP_RELIABILITY))
    159                 opts.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_NONE);
    160             //p.setProperty("tunnels.depthInbound", "0");
    161         //}
     210        // as of 0.8.1 (I2CP default is BestEffort)
     211        if (!opts.containsKey(I2PClient.PROP_RELIABILITY))
     212            opts.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_NONE);
    162213
    163214        if (i2cpHost != null)
     
    166217            opts.setProperty(I2PClient.PROP_TCP_PORT, "" + i2cpPort);
    167218       
    168         try {
    169             I2PSession session = client.createSession(myPrivateKeyStream, opts);
     219        I2PSession session = client.createSession(myPrivateKeyStream, opts);
     220        if (connect)
    170221            session.connect();
    171             I2PSocketManager sockMgr = createManager(session, opts, "manager");
    172             return sockMgr;
    173         } catch (I2PSessionException ise) {
    174             getLog().error("Error creating session for socket manager", ise);
    175             return null;
    176         }
     222        I2PSocketManager sockMgr = createManager(session, opts, "manager");
     223        return sockMgr;
    177224    }
    178225
Note: See TracChangeset for help on using the changeset viewer.