Changeset 93df048


Ignore:
Timestamp:
Jul 5, 2013 11:54:18 AM (7 years ago)
Author:
zab2 <zab2@…>
Branches:
master
Children:
18531f0
Parents:
2927382
Message:

Cleanups, make I2PSocket a Closeable

Location:
apps
Files:
2 edited

Legend:

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

    r2927382 r93df048  
    11package net.i2p.client.streaming;
    22
     3import java.io.Closeable;
    34import java.io.IOException;
    45import java.io.InputStream;
     
    1314 * defined in the "official" streaming api.
    1415 */
    15 public interface I2PSocket {
     16public interface I2PSocket extends Closeable {
    1617    /**
    1718     * @return the Destination of this side of the socket.
     
    6869    public void setReadTimeout(long ms);
    6970
    70     /**
    71      * Closes the socket if not closed yet
    72      * @throws IOException on failure
    73      */
    74     public void close() throws IOException;
    75    
    7671    public boolean isClosed();
    7772
  • apps/streaming/java/src/net/i2p/client/streaming/I2PSocketManagerFull.java

    r2927382 r93df048  
    99import java.util.Properties;
    1010import java.util.Set;
     11import java.util.concurrent.atomic.AtomicBoolean;
     12import java.util.concurrent.atomic.AtomicInteger;
    1113
    1214import net.i2p.I2PAppContext;
     
    3739    private long _acceptTimeout;
    3840    private String _name;
    39     private static int __managerId = 0;
     41    private static final AtomicInteger __managerId = new AtomicInteger(0);
    4042    private final ConnectionManager _connectionManager;
    41     private volatile boolean _isDestroyed;
     43    private final AtomicBoolean _isDestroyed = new AtomicBoolean(false);
    4244   
    4345    /**
     
    7779        _log = _context.logManager().getLog(I2PSocketManagerFull.class);
    7880       
    79         _name = name + " " + (++__managerId);
     81        _name = name + " " + (__managerId.incrementAndGet());
    8082        _acceptTimeout = ACCEPT_TIMEOUT_DEFAULT;
    8183        _defaultOptions = new ConnectionOptions(opts);
     
    202204
    203205    private void verifySession() throws I2PException {
    204         if (_isDestroyed)
     206        if (_isDestroyed.get())
    205207            throw new I2PException("destroyed");
    206208        if (!_connectionManager.getSession().isClosed())
     
    313315     */
    314316    public void destroySocketManager() {
    315         _isDestroyed = true;
     317        if (!_isDestroyed.compareAndSet(false,true)) {
     318            // shouldn't happen, log a stack trace to find out why it happened
     319            if (_log.shouldLog(Log.WARN)) {
     320                String log = "over-destroying manager "+getName();
     321                _log.log(Log.WARN,log,new Exception("check stack trace"));
     322            }
     323            return;
     324        }
    316325        _connectionManager.setAllowIncomingConnections(false);
    317326        _connectionManager.shutdown();
     
    324333                _log.warn("Unable to destroy the session", ise);
    325334            }
    326             if (pcapWriter != null)
    327                 pcapWriter.flush();
     335            PcapWriter pcap = null;
     336            synchronized(_pcapInitLock) {
     337                pcap = pcapWriter;
     338            }
     339            if (pcap != null)
     340                pcap.flush();
    328341        }
    329342    }
     
    336349    public Set<I2PSocket> listSockets() {
    337350        Set<Connection> connections = _connectionManager.listConnections();
    338         Set<I2PSocket> rv = new HashSet(connections.size());
     351        Set<I2PSocket> rv = new HashSet<I2PSocket>(connections.size());
    339352        for (Connection con : connections) {
    340353            if (con.getSocket() != null)
Note: See TracChangeset for help on using the changeset viewer.