propagate from branch 'i2p.i2p' (head 4a63eba1606a8ba2448352876b4177d9e4c753a1)

to branch 'i2p.i2p.unittests' (head 051ea486db9f6f5a4327038827763f350369f932)
This commit is contained in:
str4d
2015-10-09 10:17:03 +00:00
395 changed files with 32801 additions and 26284 deletions

View File

@ -25,7 +25,7 @@ class ConnThrottler {
* @param totalMax for all peers, 0 for unlimited
* @param period ms
*/
ConnThrottler(int max, int totalMax, long period) {
ConnThrottler(int max, int totalMax, long period, SimpleTimer2 timer) {
_max = max;
_totalMax = totalMax;
this.counter = new ObjectCounter<Hash>();
@ -33,9 +33,9 @@ class ConnThrottler {
// shorten the initial period by a random amount
// to prevent correlation across destinations
// and identification of router startup time
SimpleTimer2.getInstance().addPeriodicEvent(new Cleaner(),
(period / 2) + RandomSource.getInstance().nextLong(period / 2),
period);
timer.addPeriodicEvent(new Cleaner(),
(period / 2) + RandomSource.getInstance().nextLong(period / 2),
period);
}
/*

View File

@ -793,7 +793,7 @@ class Connection {
private boolean scheduleDisconnectEvent() {
if (!_disconnectScheduledOn.compareAndSet(0, _context.clock().now()))
return false;
_context.simpleTimer2().addEvent(new DisconnectEvent(), DISCONNECT_TIMEOUT);
schedule(new DisconnectEvent(), DISCONNECT_TIMEOUT);
return true;
}
@ -808,6 +808,24 @@ class Connection {
}
}
/**
* Called from SchedulerImpl
*
* @since 0.9.23 moved here so we can use our timer
*/
public void scheduleConnectionEvent(long msToWait) {
schedule(_connectionEvent, msToWait);
}
/**
* Schedule something on our timer.
*
* @since 0.9.23
*/
public void schedule(SimpleTimer.TimedEvent event, long msToWait) {
_timer.addEvent(event, msToWait);
}
private boolean _remotePeerSet = false;
/** who are we talking with
* @return peer Destination
@ -1254,8 +1272,6 @@ class Connection {
return buf.toString();
}
public SimpleTimer.TimedEvent getConnectionEvent() { return _connectionEvent; }
/**
* fired to reschedule event notification
*/
@ -1295,7 +1311,9 @@ class Connection {
}
public long getNextSendTime() { return _nextSend; }
public void timeReached() { retransmit(); }
/**
* Retransmit the packet if we need to.
*
@ -1307,7 +1325,7 @@ class Connection {
*
* @return true if the packet was sent, false if it was not
*/
public boolean retransmit() {
private boolean retransmit() {
if (_packet.getAckTime() > 0)
return false;

View File

@ -9,6 +9,7 @@ import net.i2p.I2PAppContext;
import net.i2p.data.Destination;
import net.i2p.util.Log;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
/**
* Receive new connection attempts
@ -23,6 +24,7 @@ class ConnectionHandler {
private final Log _log;
private final ConnectionManager _manager;
private final LinkedBlockingQueue<Packet> _synQueue;
private final SimpleTimer2 _timer;
private volatile boolean _active;
private int _acceptTimeout;
@ -37,10 +39,11 @@ class ConnectionHandler {
private static final int MAX_QUEUE_SIZE = 64;
/** Creates a new instance of ConnectionHandler */
public ConnectionHandler(I2PAppContext context, ConnectionManager mgr) {
public ConnectionHandler(I2PAppContext context, ConnectionManager mgr, SimpleTimer2 timer) {
_context = context;
_log = context.logManager().getLog(ConnectionHandler.class);
_manager = mgr;
_timer = timer;
_synQueue = new LinkedBlockingQueue<Packet>(MAX_QUEUE_SIZE);
_acceptTimeout = DEFAULT_ACCEPT_TIMEOUT;
}
@ -96,7 +99,7 @@ class ConnectionHandler {
// also check if expiration of the head is long past for overload detection with peek() ?
boolean success = _synQueue.offer(packet); // fail immediately if full
if (success) {
_context.simpleTimer2().addEvent(new TimeoutSyn(packet), _acceptTimeout);
_timer.addEvent(new TimeoutSyn(packet), _acceptTimeout);
} else {
if (_log.shouldLog(Log.WARN))
_log.warn("Dropping new SYN request, as the queue is full");

View File

@ -74,11 +74,11 @@ class ConnectionManager {
_pendingPings = new ConcurrentHashMap<Long,PingRequest>(4);
_messageHandler = new MessageHandler(_context, this);
_packetHandler = new PacketHandler(_context, this);
_connectionHandler = new ConnectionHandler(_context, this);
_schedulerChooser = new SchedulerChooser(_context);
_conPacketHandler = new ConnectionPacketHandler(_context);
_timer = new RetransmissionTimer(_context, "Streaming Timer " +
session.getMyDestination().calculateHash().toBase64().substring(0, 4));
_connectionHandler = new ConnectionHandler(_context, this, _timer);
_tcbShare = new TCBShare(_context, _timer);
// PROTO_ANY is for backward compatibility (pre-0.7.1)
// TODO change proto to PROTO_STREAMING someday.
@ -88,7 +88,7 @@ class ConnectionManager {
// As of 0.9.1, listen on configured port (default 0 = all)
int protocol = defaultOptions.getEnforceProtocol() ? I2PSession.PROTO_STREAMING : I2PSession.PROTO_ANY;
_session.addMuxedSessionListener(_messageHandler, protocol, defaultOptions.getLocalPort());
_outboundQueue = new PacketQueue(_context);
_outboundQueue = new PacketQueue(_context, _timer);
_recentlyClosed = new LHMCache<Long, Object>(32);
/** Socket timeout for accept() */
_soTimeout = -1;
@ -178,21 +178,24 @@ class ConnectionManager {
if ((_defaultOptions.getMaxConnsPerMinute() > 0 || _defaultOptions.getMaxTotalConnsPerMinute() > 0) &&
_minuteThrottler == null) {
_context.statManager().createRateStat("stream.con.throttledMinute", "Dropped for conn limit", "Stream", new long[] { 5*60*1000 });
_minuteThrottler = new ConnThrottler(_defaultOptions.getMaxConnsPerMinute(), _defaultOptions.getMaxTotalConnsPerMinute(), 60*1000);
_minuteThrottler = new ConnThrottler(_defaultOptions.getMaxConnsPerMinute(), _defaultOptions.getMaxTotalConnsPerMinute(),
60*1000, _timer);
} else if (_minuteThrottler != null) {
_minuteThrottler.updateLimits(_defaultOptions.getMaxConnsPerMinute(), _defaultOptions.getMaxTotalConnsPerMinute());
}
if ((_defaultOptions.getMaxConnsPerHour() > 0 || _defaultOptions.getMaxTotalConnsPerHour() > 0) &&
_hourThrottler == null) {
_context.statManager().createRateStat("stream.con.throttledHour", "Dropped for conn limit", "Stream", new long[] { 5*60*1000 });
_hourThrottler = new ConnThrottler(_defaultOptions.getMaxConnsPerHour(), _defaultOptions.getMaxTotalConnsPerHour(), 60*60*1000);
_hourThrottler = new ConnThrottler(_defaultOptions.getMaxConnsPerHour(), _defaultOptions.getMaxTotalConnsPerHour(),
60*60*1000, _timer);
} else if (_hourThrottler != null) {
_hourThrottler.updateLimits(_defaultOptions.getMaxConnsPerHour(), _defaultOptions.getMaxTotalConnsPerHour());
}
if ((_defaultOptions.getMaxConnsPerDay() > 0 || _defaultOptions.getMaxTotalConnsPerDay() > 0) &&
_dayThrottler == null) {
_context.statManager().createRateStat("stream.con.throttledDay", "Dropped for conn limit", "Stream", new long[] { 5*60*1000 });
_dayThrottler = new ConnThrottler(_defaultOptions.getMaxConnsPerDay(), _defaultOptions.getMaxTotalConnsPerDay(), 24*60*60*1000);
_dayThrottler = new ConnThrottler(_defaultOptions.getMaxConnsPerDay(), _defaultOptions.getMaxTotalConnsPerDay(),
24*60*60*1000, _timer);
} else if (_dayThrottler != null) {
_dayThrottler.updateLimits(_defaultOptions.getMaxConnsPerDay(), _defaultOptions.getMaxTotalConnsPerDay());
}
@ -839,7 +842,7 @@ class ConnectionManager {
private final PingNotifier _notifier;
public PingFailed(Long id, PingNotifier notifier) {
super(_context.simpleTimer2());
super(_timer);
_id = id;
_notifier = notifier;
}

View File

@ -207,7 +207,7 @@ class ConnectionPacketHandler {
final long delay = nextSendTime - now;
if (_log.shouldLog(Log.INFO))
_log.info("scheduling ack in " + delay);
_context.simpleTimer2().addEvent(new AckDup(con), delay);
con.schedule(new AckDup(con), delay);
}
} else {

View File

@ -1,5 +1,6 @@
package net.i2p.client.streaming.impl;
import java.io.Closeable;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
@ -25,7 +26,7 @@ import net.i2p.util.SimpleTimer2;
*<p>
* MessageOutputStream -> ConnectionDataReceiver -> Connection -> PacketQueue -> I2PSession
*/
class PacketQueue implements SendMessageStatusListener {
class PacketQueue implements SendMessageStatusListener, Closeable {
private final I2PAppContext _context;
private final Log _log;
private final ByteCache _cache = ByteCache.getInstance(64, 36*1024);
@ -44,11 +45,11 @@ class PacketQueue implements SendMessageStatusListener {
private static final long REMOVE_EXPIRED_TIME = 67*1000;
private static final boolean ENABLE_STATUS_LISTEN = true;
public PacketQueue(I2PAppContext context) {
public PacketQueue(I2PAppContext context, SimpleTimer2 timer) {
_context = context;
_log = context.logManager().getLog(PacketQueue.class);
_messageStatusMap = new ConcurrentHashMap<Long, Connection>(16);
new RemoveExpired();
new RemoveExpired(timer);
// all createRateStats in ConnectionManager
}
@ -327,8 +328,8 @@ class PacketQueue implements SendMessageStatusListener {
*/
private class RemoveExpired extends SimpleTimer2.TimedEvent {
public RemoveExpired() {
super(_context.simpleTimer2(), REMOVE_EXPIRED_TIME);
public RemoveExpired(SimpleTimer2 timer) {
super(timer, REMOVE_EXPIRED_TIME);
}
public void timeReached() {

View File

@ -1,8 +1,10 @@
package net.i2p.client.streaming.impl;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
@ -41,7 +43,7 @@ import net.i2p.data.DataHelper;
*
* @since 0.9.4
*/
public class PcapWriter {
public class PcapWriter implements Closeable, Flushable {
/** big-endian, see file format ref - 24 bytes */
private static final byte[] FILE_HEADER = { (byte) 0xa1, (byte) 0xb2, (byte) 0xc3, (byte) 0xd4,

View File

@ -16,7 +16,7 @@ abstract class SchedulerImpl implements TaskScheduler {
}
protected void reschedule(long msToWait, Connection con) {
_context.simpleTimer2().addEvent(con.getConnectionEvent(), msToWait);
con.scheduleConnectionEvent(msToWait);
}
@Override