* I2PTunnel:

- More client options cleanups
   - Options changes now propagate to running
     socket managers and sessions, and through to the router
 * SocketManager:
   - Simplify factory, use 4-arg constructor,
     make fields final, deprecate 0-arg constructor
   - Improve how options are updated
   - Javadocs
This commit is contained in:
zzz
2012-06-14 19:44:47 +00:00
parent 64221fb3fb
commit e522ffad4e
10 changed files with 255 additions and 115 deletions

View File

@@ -237,7 +237,33 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
*/
public ConnectionOptions(ConnectionOptions opts) {
super(opts);
if (opts != null) {
if (opts != null)
update(opts);
}
/**
* Update everything by copying over from opts
* @param opts non-null
* @since 0.9.1
*/
public void updateAll(ConnectionOptions opts) {
// user is unlikely to change these 6 between buildOptions() and setDefaultOptions(),
// since they may be updated directly, but just in case...
setConnectTimeout(opts.getConnectTimeout());
setReadTimeout(opts.getReadTimeout());
setWriteTimeout(opts.getWriteTimeout());
setMaxBufferSize(opts.getMaxBufferSize());
setLocalPort(opts.getLocalPort());
setPort(opts.getPort());
update(opts);
}
/**
* Update everything (except super) by copying over from opts
* @param opts non-null
* @since 0.9.1
*/
private void update(ConnectionOptions opts) {
setMaxWindowSize(opts.getMaxWindowSize());
setConnectDelay(opts.getConnectDelay());
setProfile(opts.getProfile());
@@ -265,7 +291,6 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
_maxTotalConnsPerMinute = opts.getMaxTotalConnsPerMinute();
_maxTotalConnsPerHour = opts.getMaxTotalConnsPerHour();
_maxTotalConnsPerDay = opts.getMaxTotalConnsPerDay();
}
}
@Override
@@ -301,6 +326,9 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
_maxTotalConnsPerDay = getInt(opts, PROP_MAX_TOTAL_CONNS_DAY, 0);
}
/**
* Note: NOT part of the interface
*/
@Override
public void setProperties(Properties opts) {
super.setProperties(opts);
@@ -611,30 +639,35 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
private void initLists(Properties opts) {
_accessListEnabled = getBool(opts, PROP_ENABLE_ACCESS_LIST, false);
_blackListEnabled = getBool(opts, PROP_ENABLE_BLACKLIST, false);
// Don't think these would ever be accessed simultaneously,
// but avoid concurrent modification just in case
Set<Hash> accessList, blackList;
if (_accessListEnabled)
_accessList = new HashSet();
accessList = new HashSet();
else
_accessList = Collections.EMPTY_SET;
accessList = Collections.EMPTY_SET;
if (_blackListEnabled)
_blackList = new HashSet();
blackList = new HashSet();
else
_blackList = Collections.EMPTY_SET;
if (!(_accessListEnabled || _blackListEnabled))
return;
String hashes = opts.getProperty(PROP_ACCESS_LIST);
if (hashes == null)
return;
StringTokenizer tok = new StringTokenizer(hashes, ", ");
while (tok.hasMoreTokens()) {
String hashstr = tok.nextToken();
Hash h = ConvertToHash.getHash(hashstr);
if (h == null)
error("bad list hash: " + hashstr);
else if (_blackListEnabled)
_blackList.add(h);
else
_accessList.add(h);
blackList = Collections.EMPTY_SET;
if (_accessListEnabled || _blackListEnabled) {
String hashes = opts.getProperty(PROP_ACCESS_LIST);
if (hashes == null)
return;
StringTokenizer tok = new StringTokenizer(hashes, ", ");
while (tok.hasMoreTokens()) {
String hashstr = tok.nextToken();
Hash h = ConvertToHash.getHash(hashstr);
if (h == null)
error("bad list hash: " + hashstr);
else if (_blackListEnabled)
blackList.add(h);
else
accessList.add(h);
}
}
_accessList = accessList;
_blackList = blackList;
if (_accessListEnabled && _accessList.isEmpty())
error("Connection access list enabled but no valid entries; no peers can connect");
else if (_blackListEnabled && _blackList.isEmpty())
@@ -647,9 +680,10 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
log.error(s);
}
@Override
/** doesn't include everything */
@Override
public String toString() {
StringBuilder buf = new StringBuilder(128);
StringBuilder buf = new StringBuilder(256);
buf.append("conDelay=").append(_connectDelay);
buf.append(" maxSize=").append(_maxMessageSize);
buf.append(" rtt=").append(_rtt);
@@ -663,6 +697,14 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
buf.append(" inactivityTimeout=").append(_inactivityTimeout);
buf.append(" inboundBuffer=").append(_inboundBufferSize);
buf.append(" maxWindowSize=").append(_maxWindowSize);
buf.append(" blacklistSize=").append(_blackList.size());
buf.append(" whitelistSize=").append(_accessList.size());
buf.append(" maxConns=").append(_maxConnsPerMinute).append('/')
.append(_maxConnsPerHour).append('/')
.append(_maxConnsPerDay);
buf.append(" maxTotalConns=").append(_maxTotalConnsPerMinute).append('/')
.append(_maxTotalConnsPerHour).append('/')
.append(_maxTotalConnsPerDay);
return buf.toString();
}

View File

@@ -28,28 +28,46 @@ import net.i2p.util.Log;
* Direct instantiation by others is deprecated.
*/
public class I2PSocketManagerFull implements I2PSocketManager {
private I2PAppContext _context;
private Log _log;
private I2PSession _session;
private I2PServerSocketFull _serverSocket;
private final I2PAppContext _context;
private final Log _log;
private final I2PSession _session;
private final I2PServerSocketFull _serverSocket;
private StandardServerSocket _realServerSocket;
private ConnectionOptions _defaultOptions;
private final ConnectionOptions _defaultOptions;
private long _acceptTimeout;
private String _name;
private int _maxStreams;
private static int __managerId = 0;
private ConnectionManager _connectionManager;
private final ConnectionManager _connectionManager;
/**
* How long to wait for the client app to accept() before sending back CLOSE?
* This includes the time waiting in the queue. Currently set to 5 seconds.
*/
private static final long ACCEPT_TIMEOUT_DEFAULT = 5*1000;
/**
* @deprecated use 4-arg constructor
* @throws UnsupportedOperationException always
*/
public I2PSocketManagerFull() {
throw new UnsupportedOperationException();
}
/** how many streams will we allow at once? */
public static final String PROP_MAX_STREAMS = "i2p.streaming.maxConcurrentStreams";
/**
* @deprecated use 4-arg constructor
* @throws UnsupportedOperationException always
*/
public void init(I2PAppContext context, I2PSession session, Properties opts, String name) {
throw new UnsupportedOperationException();
}
/**
* This is what I2PSocketManagerFactory.createManager() returns.
* Direct instantiation by others is deprecated.
*
* @param context
* @param session
@@ -57,22 +75,6 @@ public class I2PSocketManagerFull implements I2PSocketManager {
* @param name
*/
public I2PSocketManagerFull(I2PAppContext context, I2PSession session, Properties opts, String name) {
this();
init(context, session, opts, name);
}
/** how many streams will we allow at once? */
public static final String PROP_MAX_STREAMS = "i2p.streaming.maxConcurrentStreams";
/**
*
*
* @param context
* @param session
* @param opts
* @param name
*/
public void init(I2PAppContext context, I2PSession session, Properties opts, String name) {
_context = context;
_session = session;
_log = _context.logManager().getLog(I2PSocketManagerFull.class);
@@ -98,7 +100,15 @@ public class I2PSocketManagerFull implements I2PSocketManager {
}
}
/**
* Create a copy of the current options, to be used in a setDefaultOptions() call.
*/
public I2PSocketOptions buildOptions() { return buildOptions(null); }
/**
* Create a modified copy of the current options, to be used in a setDefaultOptions() call.
* @param opts The new options, may be null
*/
public I2PSocketOptions buildOptions(Properties opts) {
ConnectionOptions curOpts = new ConnectionOptions(_defaultOptions);
curOpts.setProperties(opts);
@@ -159,10 +169,24 @@ public class I2PSocketManagerFull implements I2PSocketManager {
public void setAcceptTimeout(long ms) { _acceptTimeout = ms; }
public long getAcceptTimeout() { return _acceptTimeout; }
/**
* Update the options on a running socket manager.
* Parameters in the I2PSocketOptions interface may be changed directly
* with the setters; no need to use this method for those.
* This does NOT update the underlying I2CP or tunnel options; use getSession().updateOptions() for that.
* @param options as created from a call to buildOptions(properties), non-null
*/
public void setDefaultOptions(I2PSocketOptions options) {
_defaultOptions = new ConnectionOptions((ConnectionOptions) options);
if (!(options instanceof ConnectionOptions))
throw new IllegalArgumentException();
if (_log.shouldLog(Log.WARN))
_log.warn("Changing options from:\n " + _defaultOptions + "\nto:\n " + options);
_defaultOptions.updateAll((ConnectionOptions) options);
}
/**
* Current options, not a copy, setters may be used to make changes.
*/
public I2PSocketOptions getDefaultOptions() {
return _defaultOptions;
}