SimpleTimer (ticket #653):

- Move all remaining uses to SimpleTimer2
    - Deprecate
This commit is contained in:
zzz
2012-09-01 13:14:15 +00:00
parent 94f370e76c
commit 6bfd916fef
12 changed files with 80 additions and 52 deletions

View File

@ -13,7 +13,6 @@ import net.i2p.data.Destination;
import net.i2p.data.Hash;
import net.i2p.data.SessionKey;
import net.i2p.util.Log;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
/**
@ -494,7 +493,8 @@ class ConnectionManager {
}
_pendingPings.remove(id);
} else {
SimpleTimer.getInstance().addEvent(new PingFailed(id, notifier), timeoutMs);
PingFailed pf = new PingFailed(id, notifier);
pf.schedule(timeoutMs);
}
boolean ok = req.pongReceived();
@ -505,11 +505,12 @@ class ConnectionManager {
public void pingComplete(boolean ok);
}
private class PingFailed implements SimpleTimer.TimedEvent {
private class PingFailed extends SimpleTimer2.TimedEvent {
private final Long _id;
private final PingNotifier _notifier;
public PingFailed(Long id, PingNotifier notifier) {
super(_context.simpleTimer2());
_id = id;
_notifier = notifier;
}

View File

@ -953,6 +953,7 @@ public class I2PAppContext {
/**
* Use instead of SimpleTimer.getInstance()
* @since 0.9 to replace static instance in the class
* @deprecated use SimpleTimer2
*/
public SimpleTimer simpleTimer() {
if (!_simpleTimerInitialized)
@ -960,6 +961,9 @@ public class I2PAppContext {
return _simpleTimer;
}
/**
* @deprecated use SimpleTimer2
*/
private void initializeSimpleTimer() {
synchronized (_lock19) {
if (_simpleTimer == null)

View File

@ -5,6 +5,7 @@
package net.i2p.util;
/**
* Deprecated - used only by SimpleTimer
*
* @author sponge
*/

View File

@ -21,6 +21,7 @@ public class SimpleTimer {
/**
* If you have a context, use context.simpleTimer() instead
* @deprecated use SimpleTimer2
*/
public static SimpleTimer getInstance() {
return I2PAppContext.getGlobalContext().simpleTimer();
@ -39,6 +40,7 @@ public class SimpleTimer {
/**
* To be instantiated by the context.
* Others should use context.simpleTimer() instead
* @deprecated use SimpleTimer2
*/
public SimpleTimer(I2PAppContext context) {
this(context, "SimpleTimer");
@ -47,6 +49,7 @@ public class SimpleTimer {
/**
* To be instantiated by the context.
* Others should use context.simpleTimer() instead
* @deprecated use SimpleTimer2
*/
private SimpleTimer(I2PAppContext context, String name) {
runn = new SimpleStore(true);
@ -146,6 +149,7 @@ public class SimpleTimer {
}
}
}
// FIXME if you plan to use this class again
while (_events.containsKey(time))
time = new Long(time.longValue() + 1);
_events.put(time, event);

View File

@ -205,10 +205,8 @@ public class SimpleTimer2 {
}
/**
* More efficient than reschedule().
* Only call this after calling the non-scheduling constructor,
* or from within timeReached(), or you will get duplicates on the queue.
* Otherwise use reschedule().
* Slightly more efficient than reschedule().
* Does nothing if already scheduled.
*/
public synchronized void schedule(long timeoutMs) {
if (_log.shouldLog(Log.DEBUG))
@ -236,7 +234,8 @@ public class SimpleTimer2 {
/**
* Use the earliest of the new time and the old time
* Do not call from within timeReached()
* May be called from within timeReached(), but schedule() is
* better there.
*
* @param timeoutMs
*/
@ -245,8 +244,8 @@ public class SimpleTimer2 {
}
/**
* useEarliestTime must be false if called from within timeReached(), as
* it won't be rescheduled, in favor of the currently running task
* May be called from within timeReached(), but schedule() is
* better there.
*
* @param timeoutMs
* @param useEarliestTime if its already scheduled, use the earlier of the

View File

@ -14,7 +14,7 @@ import java.util.Date;
*
* Use socket.setsotimeout instead?
*/
public class SocketTimeout implements SimpleTimer.TimedEvent {
public class SocketTimeout extends SimpleTimer2.TimedEvent {
private Socket _targetSocket;
private long _startTime;
private long _inactivityDelay;
@ -24,12 +24,13 @@ public class SocketTimeout implements SimpleTimer.TimedEvent {
private Runnable _command;
public SocketTimeout(long delay) { this(null, delay); }
public SocketTimeout(Socket socket, long delay) {
super(SimpleTimer2.getInstance());
_inactivityDelay = delay;
_targetSocket = socket;
_cancelled = false;
_lastActivity = _startTime = System.currentTimeMillis();
_totalTimeoutTime = -1;
SimpleTimer.getInstance().addEvent(SocketTimeout.this, delay);
schedule(delay);
}
public void timeReached() {
if (_cancelled) return;
@ -44,13 +45,13 @@ public class SocketTimeout implements SimpleTimer.TimedEvent {
}
if (_command != null) _command.run();
} else {
SimpleTimer.getInstance().addEvent(SocketTimeout.this, _inactivityDelay);
schedule(_inactivityDelay);
}
}
public void cancel() {
public boolean cancel() {
_cancelled = true;
SimpleTimer.getInstance().removeEvent(SocketTimeout.this);
return super.cancel();
}
public void setSocket(Socket s) { _targetSocket = s; }
public void resetTimer() { _lastActivity = System.currentTimeMillis(); }

View File

@ -25,7 +25,7 @@ import net.i2p.router.ReplyJob;
import net.i2p.router.RouterContext;
import net.i2p.util.ConcurrentHashSet;
import net.i2p.util.Log;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
/**
* Tracks outbound messages.
@ -254,10 +254,11 @@ public class OutboundMessageRegistry {
/** @deprecated unused */
public void renderStatusHTML(Writer out) throws IOException {}
private class CleanupTask implements SimpleTimer.TimedEvent {
private class CleanupTask extends SimpleTimer2.TimedEvent {
private long _nextExpire;
public CleanupTask() {
super(_context.simpleTimer2());
_nextExpire = -1;
}
@ -312,14 +313,14 @@ public class OutboundMessageRegistry {
if (_nextExpire <= now)
_nextExpire = now + 10*1000;
SimpleTimer.getInstance().addEvent(CleanupTask.this, _nextExpire - now);
schedule(_nextExpire - now);
}
public void scheduleExpiration(MessageSelector sel) {
long now = _context.clock().now();
if ( (_nextExpire <= now) || (sel.getExpiration() < _nextExpire) ) {
_nextExpire = sel.getExpiration();
SimpleTimer.getInstance().addEvent(CleanupTask.this, _nextExpire - now);
reschedule(_nextExpire - now);
}
}
}

View File

@ -42,6 +42,7 @@ import net.i2p.util.ConcurrentHashSet;
import net.i2p.util.Log;
import net.i2p.util.SimpleScheduler;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
import net.i2p.util.Translate;
/**
@ -369,7 +370,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
// _flooder.startup();
_expireEvent.setIsAlive(true);
_testEvent.setIsAlive(true); // this queues it for 3-6 minutes in the future...
SimpleTimer.getInstance().addEvent(_testEvent, 10*1000); // lets requeue it for Real Soon
_testEvent.reschedule(10*1000); // lets requeue it for Real Soon
}
public void shutdown() {
@ -681,7 +682,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
_context.router().rebuildRouterInfo();
}
_testEvent.forceRun();
SimpleTimer.getInstance().addEvent(_testEvent, 5*1000);
_testEvent.reschedule(5*1000);
return updated;
}
@ -859,7 +860,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
if (getReachabilityStatus() != CommSystemFacade.STATUS_OK) {
_testEvent.forceRun();
SimpleTimer.getInstance().addEvent(_testEvent, 0);
_testEvent.reschedule(0);
}
return true;
}
@ -933,7 +934,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
}
private class RemoveDropList implements SimpleTimer.TimedEvent {
private RemoteHostId _peer;
private final RemoteHostId _peer;
public RemoveDropList(RemoteHostId peer) { _peer = peer; }
public void timeReached() {
_dropList.remove(_peer);
@ -2361,12 +2362,13 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
public String toString() { return "UDP bid @ " + getLatencyMs(); }
}
private class ExpirePeerEvent implements SimpleTimer.TimedEvent {
private class ExpirePeerEvent extends SimpleTimer2.TimedEvent {
private final Set<PeerState> _expirePeers;
private final List<PeerState> _expireBuffer;
private volatile boolean _alive;
public ExpirePeerEvent() {
super(_context.simpleTimer2());
_expirePeers = new ConcurrentHashSet(128);
_expireBuffer = new ArrayList();
}
@ -2403,7 +2405,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
_expireBuffer.clear();
if (_alive)
SimpleTimer.getInstance().addEvent(ExpirePeerEvent.this, 30*1000);
schedule(30*1000);
}
public void add(PeerState peer) {
_expirePeers.add(peer);
@ -2414,9 +2416,9 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
public void setIsAlive(boolean isAlive) {
_alive = isAlive;
if (isAlive) {
SimpleTimer.getInstance().addEvent(ExpirePeerEvent.this, 30*1000);
reschedule(30*1000);
} else {
SimpleTimer.getInstance().removeEvent(ExpirePeerEvent.this);
cancel();
_expirePeers.clear();
}
}
@ -2515,12 +2517,16 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
//return ( (val != null) && ("true".equals(val)) );
}
private class PeerTestEvent implements SimpleTimer.TimedEvent {
private class PeerTestEvent extends SimpleTimer2.TimedEvent {
private volatile boolean _alive;
/** when did we last test our reachability */
private long _lastTested;
private boolean _forceRun;
PeerTestEvent() {
super(_context.simpleTimer2());
}
public void timeReached() {
if (shouldTest()) {
long now = _context.clock().now();
@ -2532,7 +2538,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
long delay = (TEST_FREQUENCY / 2) + _context.random().nextInt(TEST_FREQUENCY);
if (delay <= 0)
throw new RuntimeException("wtf, delay is " + delay);
SimpleTimer.getInstance().addEvent(PeerTestEvent.this, delay);
schedule(delay);
}
}
@ -2558,9 +2564,9 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
_alive = isAlive;
if (isAlive) {
long delay = _context.random().nextInt(2*TEST_FREQUENCY);
SimpleTimer.getInstance().addEvent(PeerTestEvent.this, delay);
reschedule(delay);
} else {
SimpleTimer.getInstance().removeEvent(PeerTestEvent.this);
cancel();
}
}
}

View File

@ -16,7 +16,7 @@ import net.i2p.util.ByteCache;
import net.i2p.util.HexDump;
import net.i2p.util.Log;
import net.i2p.util.SimpleByteCache;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
/**
* Handle fragments at the endpoint of a tunnel, peeling off fully completed
@ -369,7 +369,7 @@ class FragmentHandler {
_fragmentedMessages.remove(Long.valueOf(messageId));
}
if (msg.getExpireEvent() != null)
SimpleTimer.getInstance().removeEvent(msg.getExpireEvent());
msg.getExpireEvent().cancel();
receiveComplete(msg);
} else {
noteReception(msg.getMessageId(), 0, msg);
@ -378,7 +378,7 @@ class FragmentHandler {
msg.setExpireEvent(evt);
if (_log.shouldLog(Log.DEBUG))
_log.debug("In " + MAX_DEFRAGMENT_TIME + " dropping " + messageId);
SimpleTimer.getInstance().addEvent(evt, MAX_DEFRAGMENT_TIME);
evt.schedule(MAX_DEFRAGMENT_TIME);
}
}
}
@ -437,7 +437,7 @@ class FragmentHandler {
_fragmentedMessages.remove(Long.valueOf(messageId));
}
if (msg.getExpireEvent() != null)
SimpleTimer.getInstance().removeEvent(msg.getExpireEvent());
msg.getExpireEvent().cancel();
_context.statManager().addRateData("tunnel.fragmentedComplete", msg.getFragmentCount(), msg.getLifetime());
receiveComplete(msg);
} else {
@ -447,7 +447,7 @@ class FragmentHandler {
msg.setExpireEvent(evt);
if (_log.shouldLog(Log.DEBUG))
_log.debug("In " + MAX_DEFRAGMENT_TIME + " dropping " + msg.getMessageId() + "/" + fragmentNum);
SimpleTimer.getInstance().addEvent(evt, MAX_DEFRAGMENT_TIME);
evt.schedule(MAX_DEFRAGMENT_TIME);
}
}
}
@ -548,10 +548,11 @@ class FragmentHandler {
public void receiveComplete(I2NPMessage msg, Hash toRouter, TunnelId toTunnel);
}
private class RemoveFailed implements SimpleTimer.TimedEvent {
private class RemoveFailed extends SimpleTimer2.TimedEvent {
private final FragmentedMessage _msg;
public RemoveFailed(FragmentedMessage msg) {
super(_context.simpleTimer2());
_msg = msg;
}

View File

@ -7,7 +7,7 @@ import net.i2p.data.Hash;
import net.i2p.data.TunnelId;
import net.i2p.util.ByteCache;
import net.i2p.util.Log;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
/**
* Gather fragments of I2NPMessages at a tunnel endpoint, making them available
@ -28,7 +28,7 @@ class FragmentedMessage {
private final long _createdOn;
private boolean _completed;
private long _releasedAfter;
private SimpleTimer.TimedEvent _expireEvent;
private SimpleTimer2.TimedEvent _expireEvent;
private static final ByteCache _cache = ByteCache.getInstance(512, TrivialPreprocessor.PREPROCESSED_SIZE);
// 64 is pretty absurd, 32 is too, most likely
@ -160,9 +160,11 @@ class FragmentedMessage {
found++;
return found;
}
/** used in the fragment handler so we can cancel the expire event on success */
SimpleTimer.TimedEvent getExpireEvent() { return _expireEvent; }
void setExpireEvent(SimpleTimer.TimedEvent evt) { _expireEvent = evt; }
public SimpleTimer2.TimedEvent getExpireEvent() { return _expireEvent; }
public void setExpireEvent(SimpleTimer2.TimedEvent evt) { _expireEvent = evt; }
/** have we received all of the fragments? */
public boolean isComplete() {

View File

@ -10,7 +10,7 @@ import net.i2p.data.i2np.TunnelGatewayMessage;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import net.i2p.util.Log;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
/**
* Serve as the gatekeeper for a tunnel, accepting messages, coallescing and/or
@ -124,7 +124,7 @@ class TunnelGateway {
}
if (delayedFlush) {
_context.simpleTimer().addEvent(_delayedFlush, delayAmount);
_delayedFlush.reschedule(delayAmount);
}
_context.statManager().addRateData("tunnel.lockedGatewayAdd", afterAdded-beforeLock, remaining);
if (_log.shouldLog(Log.DEBUG)) {
@ -278,7 +278,11 @@ class TunnelGateway {
public long getLifetime() { return _context.clock().now()-_created; }
}
private class DelayedFlush implements SimpleTimer.TimedEvent {
protected class DelayedFlush extends SimpleTimer2.TimedEvent {
DelayedFlush() {
super(_context.simpleTimer2());
}
public void timeReached() {
boolean wantRequeue = false;
int remaining = 0;
@ -304,7 +308,7 @@ class TunnelGateway {
}
if (wantRequeue)
_context.simpleTimer().addEvent(_delayedFlush, delayAmount);
schedule(delayAmount);
else
_lastFlush = _context.clock().now();

View File

@ -7,7 +7,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
import org.xlattice.crypto.filters.BloomSHA1;
@ -38,7 +38,7 @@ public class DecayingBloomFilter {
private final long _longToEntryMask;
protected long _currentDuplicates;
protected volatile boolean _keepDecaying;
protected final SimpleTimer.TimedEvent _decayEvent;
protected final SimpleTimer2.TimedEvent _decayEvent;
/** just for logging */
protected final String _name;
/** synchronize against this lock when switching double buffers */
@ -64,7 +64,7 @@ public class DecayingBloomFilter {
context.addShutdownTask(new Shutdown());
_decayEvent = new DecayEvent();
_keepDecaying = true;
SimpleTimer.getInstance().addEvent(_decayEvent, _durationMs);
_decayEvent.schedule(_durationMs);
}
/**
@ -118,7 +118,7 @@ public class DecayingBloomFilter {
}
_decayEvent = new DecayEvent();
_keepDecaying = true;
SimpleTimer.getInstance().addEvent(_decayEvent, _durationMs);
_decayEvent.schedule(_durationMs);
if (_log.shouldLog(Log.WARN))
_log.warn("New DBF " + name + " m = " + m + " k = " + k + " entryBytes = " + entryBytes +
" numExtenders = " + numExtenders + " cycle (s) = " + (durationMs / 1000));
@ -274,7 +274,7 @@ public class DecayingBloomFilter {
public void stopDecaying() {
_keepDecaying = false;
SimpleTimer.getInstance().removeEvent(_decayEvent);
_decayEvent.cancel();
}
protected void decay() {
@ -310,11 +310,15 @@ public class DecayingBloomFilter {
}
}
private class DecayEvent implements SimpleTimer.TimedEvent {
private class DecayEvent extends SimpleTimer2.TimedEvent {
DecayEvent() {
super(_context.simpleTimer2());
}
public void timeReached() {
if (_keepDecaying) {
decay();
SimpleTimer.getInstance().addEvent(DecayEvent.this, _durationMs);
schedule(_durationMs);
}
}
}