Changeset 1a200a1


Ignore:
Timestamp:
Jun 7, 2019 3:19:06 PM (10 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
5ed953e
Parents:
e50bf00
Message:

Make TunnelPool? final in PooledTunnelCreatorConfig?
Don't pass around both pool and cfg in args
Remove unused methods
Cleanup multiple now() and getSettings() calls

Location:
router/java/src/net/i2p/router/tunnel
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/tunnel/TunnelCreatorConfig.java

    re50bf00 r1a200a1  
    1717 * peers are in the tunnel and what their configuration is
    1818 *
     19 * See PooledTunnelCreatorConfig for the non-abstract class
    1920 */
    20 public class TunnelCreatorConfig implements TunnelInfo {
     21public abstract class TunnelCreatorConfig implements TunnelInfo {
    2122    protected final RouterContext _context;
    2223    /** only necessary for client tunnels */
  • router/java/src/net/i2p/router/tunnel/pool/BuildExecutor.java

    re50bf00 r1a200a1  
    416416                                if (_log.shouldLog(Log.DEBUG))
    417417                                    _log.debug("Configuring new tunnel " + i + " for " + pool + ": " + cfg);
    418                                 buildTunnel(pool, cfg);
     418                                buildTunnel(cfg);
    419419                                //realBuilt++;
    420420                            } else {
     
    508508                    if (_log.shouldLog(Log.DEBUG))
    509509                        _log.debug("Configuring short tunnel " + i + " for " + pool + ": " + cfg);
    510                     buildTunnel(pool, cfg);
     510                    buildTunnel(cfg);
    511511                    if (cfg.getLength() > 1) {
    512512                        allowed--; // oops... shouldn't have done that, but hey, its not that bad...
     
    525525    public boolean isRunning() { return _isRunning; }
    526526   
    527     void buildTunnel(TunnelPool pool, PooledTunnelCreatorConfig cfg) {
     527    void buildTunnel(PooledTunnelCreatorConfig cfg) {
    528528        long beforeBuild = System.currentTimeMillis();
    529529        if (cfg.getLength() > 1) {
     
    533533            } while (addToBuilding(cfg)); // if a dup, go araound again
    534534        }
    535         boolean ok = BuildRequestor.request(_context, pool, cfg, this);
     535        boolean ok = BuildRequestor.request(_context, cfg, this);
    536536        if (!ok)
    537537            return;
     
    557557     *  so we don't build too many.
    558558     */
    559     public void buildComplete(PooledTunnelCreatorConfig cfg, TunnelPool pool) {
     559    public void buildComplete(PooledTunnelCreatorConfig cfg) {
    560560        if (_log.shouldLog(Log.DEBUG))
    561561            _log.debug("Build complete for " + cfg, new Exception());
    562         pool.buildComplete(cfg);
     562        cfg.getTunnelPool().buildComplete(cfg);
    563563        if (cfg.getLength() > 1)
    564564            removeFromBuilding(cfg.getReplyMessageId());
  • router/java/src/net/i2p/router/tunnel/pool/BuildHandler.java

    re50bf00 r1a200a1  
    323323                    _log.error("Bad status index " + i);
    324324                    // don't leak
    325                     _exec.buildComplete(cfg, cfg.getTunnelPool());
     325                    _exec.buildComplete(cfg);
    326326                    return;
    327327                }
     
    380380                    // creating the config, but we don't track IDs for builds in progress.
    381381                    _context.statManager().addRateData("tunnel.ownDupID", 1);
    382                     _exec.buildComplete(cfg, cfg.getTunnelPool());
     382                    _exec.buildComplete(cfg);
    383383                    if (_log.shouldLog(Log.WARN))
    384384                        _log.warn("Dup ID for our own tunnel " + cfg);
     
    387387                cfg.getTunnelPool().addTunnel(cfg); // self.self.self.foo!
    388388                // call buildComplete() after addTunnel() so we don't try another build.
    389                 _exec.buildComplete(cfg, cfg.getTunnelPool());
     389                _exec.buildComplete(cfg);
    390390                _exec.buildSuccessful(cfg);
    391391
     
    422422                }
    423423               
    424                 ExpireJob expireJob = new ExpireJob(_context, cfg, cfg.getTunnelPool());
    425                 cfg.setExpireJob(expireJob);
     424                ExpireJob expireJob = new ExpireJob(_context, cfg);
    426425                _context.jobQueue().addJob(expireJob);
    427426                if (cfg.getDestination() == null)
     
    431430            } else {
    432431                // someone is no fun
    433                 _exec.buildComplete(cfg, cfg.getTunnelPool());
     432                _exec.buildComplete(cfg);
    434433                if (cfg.getDestination() == null)
    435434                    _context.statManager().addRateData("tunnel.buildExploratoryReject", rtt);
     
    442441            _context.statManager().addRateData("tunnel.corruptBuildReply", 1);
    443442            // don't leak
    444             _exec.buildComplete(cfg, cfg.getTunnelPool());
     443            _exec.buildComplete(cfg);
    445444        }
    446445    }
     
    724723        // As of 0.9.16, allow + 5 minutes to - 65 minutes.
    725724        long time = req.readRequestTime();
    726         long now = (_context.clock().now() / (60l*60l*1000l)) * (60*60*1000);
    727         long timeDiff = now - time;
     725        long now = _context.clock().now();
     726        long roundedNow = (now / (60l*60l*1000l)) * (60*60*1000);
     727        long timeDiff = roundedNow - time;
    728728        if (timeDiff > MAX_REQUEST_AGE) {
    729729            _context.statManager().addRateData("tunnel.rejectTooOld", 1);
     
    764764        //    response = TunnelHistory.TUNNEL_REJECT_PROBABALISTIC_REJECT;
    765765       
    766         long recvDelay = _context.clock().now()-state.recvTime;
     766        long recvDelay = now - state.recvTime;
    767767
    768768        if (response == 0) {
     
    834834        if (response == 0) {
    835835            cfg = new HopConfig();
    836             cfg.setCreation(_context.clock().now());
    837             cfg.setExpiration(_context.clock().now() + 10*60*1000);
     836            cfg.setCreation(now);
     837            cfg.setExpiration(now + 10*60*1000);
    838838            cfg.setIVKey(req.readIVKey());
    839839            cfg.setLayerKey(req.readLayerKey());
     
    936936
    937937        // now actually send the response
    938         long expires = _context.clock().now() + NEXT_HOP_SEND_TIMEOUT;
     938        long expires = now + NEXT_HOP_SEND_TIMEOUT;
    939939        if (!isOutEnd) {
    940940            state.msg.setUniqueId(req.readReplyMessageId());
  • router/java/src/net/i2p/router/tunnel/pool/BuildRequestor.java

    re50bf00 r1a200a1  
    1717import net.i2p.router.TunnelInfo;
    1818import net.i2p.router.TunnelManagerFacade;
     19import net.i2p.router.TunnelPoolSettings;
    1920import net.i2p.router.tunnel.BuildMessageGenerator;
    2021import net.i2p.util.Log;
     
    118119     *  @return success
    119120     */
    120     public static boolean request(RouterContext ctx, TunnelPool pool,
     121    public static boolean request(RouterContext ctx,
    121122                                  PooledTunnelCreatorConfig cfg, BuildExecutor exec) {
    122123        // new style crypto fills in all the blanks, while the old style waits for replies to fill in the next hop, etc
     
    124125       
    125126        if (cfg.getLength() <= 1) {
    126             buildZeroHop(ctx, pool, cfg, exec);
     127            buildZeroHop(ctx, cfg, exec);
    127128            return true;
    128129        }
    129130       
    130131        Log log = ctx.logManager().getLog(BuildRequestor.class);
    131         cfg.setTunnelPool(pool);
     132        final TunnelPool pool = cfg.getTunnelPool();
     133        final TunnelPoolSettings settings = pool.getSettings();
    132134       
    133135        TunnelInfo pairedTunnel = null;
    134136        Hash farEnd = cfg.getFarEnd();
    135137        TunnelManagerFacade mgr = ctx.tunnelManager();
    136         boolean isInbound = pool.getSettings().isInbound();
    137         if (pool.getSettings().isExploratory() || !usePairedTunnels(ctx)) {
     138        boolean isInbound = settings.isInbound();
     139        if (settings.isExploratory() || !usePairedTunnels(ctx)) {
    138140            if (isInbound)
    139141                pairedTunnel = mgr.selectOutboundExploratoryTunnel(farEnd);
     
    143145            // building a client tunnel
    144146            if (isInbound)
    145                 pairedTunnel = mgr.selectOutboundTunnel(pool.getSettings().getDestination(), farEnd);
     147                pairedTunnel = mgr.selectOutboundTunnel(settings.getDestination(), farEnd);
    146148            else
    147                 pairedTunnel = mgr.selectInboundTunnel(pool.getSettings().getDestination(), farEnd);
     149                pairedTunnel = mgr.selectInboundTunnel(settings.getDestination(), farEnd);
    148150            if (pairedTunnel == null) {   
    149151                if (isInbound) {
     
    179181            if (log.shouldLog(Log.WARN))
    180182                log.warn("Tunnel build failed, as we couldn't find a paired tunnel for " + cfg);
    181             exec.buildComplete(cfg, pool);
     183            exec.buildComplete(cfg);
    182184            // Not even an exploratory tunnel? We are in big trouble.
    183185            // Let's not spin through here too fast.
    184186            // But don't let a client tunnel waiting for exploratories slow things down too much,
    185187            // as there may be other tunnel pools who can build
    186             int ms = pool.getSettings().isExploratory() ? 250 : 25;
     188            int ms = settings.isExploratory() ? 250 : 25;
    187189            try { Thread.sleep(ms); } catch (InterruptedException ie) {}
    188190            return false;
     
    195197            if (log.shouldLog(Log.WARN))
    196198                log.warn("Tunnel build failed, as we couldn't create the tunnel build message for " + cfg);
    197             exec.buildComplete(cfg, pool);
     199            exec.buildComplete(cfg);
    198200            return false;
    199201        }
     
    226228                if (log.shouldLog(Log.WARN))
    227229                    log.warn("Could not find the next hop to send the outbound request to: " + cfg);
    228                 exec.buildComplete(cfg, pool);
     230                exec.buildComplete(cfg);
    229231                return false;
    230232            }
    231233            OutNetMessage outMsg = new OutNetMessage(ctx, msg, ctx.clock().now() + FIRST_HOP_TIMEOUT, PRIORITY, peer);
    232             outMsg.setOnFailedSendJob(new TunnelBuildFirstHopFailJob(ctx, pool, cfg, exec));
     234            outMsg.setOnFailedSendJob(new TunnelBuildFirstHopFailJob(ctx, cfg, exec));
    233235            try {
    234236                ctx.outNetMessagePool().add(outMsg);
     
    366368    }
    367369   
    368     private static void buildZeroHop(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, BuildExecutor exec) {
     370    private static void buildZeroHop(RouterContext ctx, PooledTunnelCreatorConfig cfg, BuildExecutor exec) {
    369371        Log log = ctx.logManager().getLog(BuildRequestor.class);
    370372        if (log.shouldLog(Log.DEBUG))
    371373            log.debug("Build zero hop tunnel " + cfg);           
    372374
    373         exec.buildComplete(cfg, pool);
     375        exec.buildComplete(cfg);
    374376        if (cfg.isInbound())
    375377            ctx.tunnelDispatcher().joinInbound(cfg);
    376378        else
    377379            ctx.tunnelDispatcher().joinOutbound(cfg);
    378         pool.addTunnel(cfg);
     380        cfg.getTunnelPool().addTunnel(cfg);
    379381        exec.buildSuccessful(cfg);
    380         ExpireJob expireJob = new ExpireJob(ctx, cfg, pool);
    381         cfg.setExpireJob(expireJob);
     382        ExpireJob expireJob = new ExpireJob(ctx, cfg);
    382383        ctx.jobQueue().addJob(expireJob);
    383384        // can it get much easier?
     
    394395     */
    395396    private static class TunnelBuildFirstHopFailJob extends JobImpl {
    396         private final TunnelPool _pool;
    397397        private final PooledTunnelCreatorConfig _cfg;
    398398        private final BuildExecutor _exec;
    399         private TunnelBuildFirstHopFailJob(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, BuildExecutor exec) {
     399        private TunnelBuildFirstHopFailJob(RouterContext ctx, PooledTunnelCreatorConfig cfg, BuildExecutor exec) {
    400400            super(ctx);
    401401            _cfg = cfg;
    402402            _exec = exec;
    403             _pool = pool;
    404403        }
    405404        public String getName() { return "Timeout contacting first peer for OB tunnel"; }
    406405        public void runJob() {
    407             _exec.buildComplete(_cfg, _pool);
     406            _exec.buildComplete(_cfg);
    408407            getContext().profileManager().tunnelTimedOut(_cfg.getPeer(1));
    409408            getContext().statManager().addRateData("tunnel.buildFailFirstHop", 1, 0);
  • router/java/src/net/i2p/router/tunnel/pool/ExpireJob.java

    re50bf00 r1a200a1  
    66import net.i2p.router.Router;
    77import net.i2p.router.RouterContext;
    8 import net.i2p.router.tunnel.TunnelCreatorConfig;
    98
    109/**
     
    1413 */
    1514class ExpireJob extends JobImpl {
    16     private final TunnelPool _pool;
    17     private final TunnelCreatorConfig _cfg;
     15    private final PooledTunnelCreatorConfig _cfg;
    1816    private final AtomicBoolean _leaseUpdated = new AtomicBoolean(false);
    1917    private final long _dropAfter;
     
    2220    private static final long IB_EARLY_EXPIRE = OB_EARLY_EXPIRE + 7500;
    2321
    24     public ExpireJob(RouterContext ctx, TunnelCreatorConfig cfg, TunnelPool pool) {
     22    public ExpireJob(RouterContext ctx, PooledTunnelCreatorConfig cfg) {
    2523        super(ctx);
    26         _pool = pool;
    2724        _cfg = cfg;
    2825        // we act as if this tunnel expires a random skew before it actually does
     
    3229        // Also skew the inbound away from the outbound
    3330        long expire = cfg.getExpiration();
    34         if (_pool.getSettings().isInbound()) {
     31        if (cfg.getTunnelPool().getSettings().isInbound()) {
    3532            // wait extra long for IB so we don't drop msgs that
    3633            // got all the way to us.
     
    5249    public void runJob() {
    5350        if (_leaseUpdated.compareAndSet(false,true)) {
     51            TunnelPool pool = _cfg.getTunnelPool();
    5452            // First run
    55             _pool.removeTunnel(_cfg);
     53            pool.removeTunnel(_cfg);
    5654            // noop for outbound
    57             _pool.refreshLeaseSet();
     55            pool.refreshLeaseSet();
    5856            long timeToDrop = _dropAfter - getContext().clock().now();
    5957            requeue(timeToDrop);
  • router/java/src/net/i2p/router/tunnel/pool/PooledTunnelCreatorConfig.java

    re50bf00 r1a200a1  
    44
    55import net.i2p.data.Hash;
    6 import net.i2p.router.Job;
    76import net.i2p.router.RouterContext;
    8 import net.i2p.router.TunnelInfo;
    97import net.i2p.router.tunnel.TunnelCreatorConfig;
    10 import net.i2p.util.Log;
    118
    129/**
     
    1411 */
    1512class PooledTunnelCreatorConfig extends TunnelCreatorConfig {
    16     private TunnelPool _pool;
    17     private TestJob _testJob;
    18     /** Creates a new instance of PooledTunnelCreatorConfig */
    19    
    20     public PooledTunnelCreatorConfig(RouterContext ctx, int length, boolean isInbound) {
    21         this(ctx, length, isInbound, null);
    22     }
    23     public PooledTunnelCreatorConfig(RouterContext ctx, int length, boolean isInbound, Hash destination) {
     13    private final TunnelPool _pool;
     14
     15    /**
     16     *  Creates a new instance of PooledTunnelCreatorConfig
     17     *
     18     *  @param destination may be null
     19     *  @param pool non-null
     20     */
     21    public PooledTunnelCreatorConfig(RouterContext ctx, int length, boolean isInbound,
     22                                     Hash destination, TunnelPool pool) {
    2423        super(ctx, length, isInbound, destination);
    25     }
    26    
    27     /** calls TestJob */
    28     @Override
    29     public void testSuccessful(int ms) {
    30         if (_testJob != null)
    31             _testJob.testSuccessful(ms);
    32         super.testSuccessful(ms);
     24        _pool = pool;
    3325    }
    3426   
    3527    /** called from TestJob */
    3628    public void testJobSuccessful(int ms) {
    37         super.testSuccessful(ms);
     29        testSuccessful(ms);
    3830    }
    3931   
     
    5244            // to prevent collapse (loss of all tunnels)
    5345            _pool.tunnelFailed(this);
    54             if (_testJob != null) // just in case...
    55                 _context.jobQueue().removeJob(_testJob);
    5646        }
    5747        return rv;
     
    6050    @Override
    6151    public Properties getOptions() {
    62         if (_pool == null) return null;
    6352        return _pool.getSettings().getUnknownOptions();
    6453    }
    6554   
    66     public void setTunnelPool(TunnelPool pool) {
    67         if (pool != null) {
    68             _pool = pool;
    69         } else {
    70             Log log = _context.logManager().getLog(getClass());
    71             log.error("Null tunnel pool?", new Exception("foo"));
    72         }
    73     }
     55    /**
     56     *  @return non-null
     57     */
    7458    public TunnelPool getTunnelPool() { return _pool; }
    75    
    76 
    77     /** @deprecated unused, which makes _testJob unused - why is it here */
    78     @Deprecated
    79     void setTestJob(TestJob job) { _testJob = job; }
    80     /** does nothing, to be deprecated */
    81     public void setExpireJob(Job job) { /* _expireJob = job; */ }
    82    
    83     /**
    84      * @deprecated Fix memory leaks caused by references if you need to use pairedTunnel
    85      */
    86     @Deprecated
    87     public void setPairedTunnel(TunnelInfo tunnel) { /* _pairedTunnel = tunnel; */}
    88     // public TunnelInfo getPairedTunnel() { return _pairedTunnel; }
    8959}
  • router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java

    re50bf00 r1a200a1  
    662662           
    663663            // runs inline, since its 0hop
    664             _manager.getExecutor().buildTunnel(this, configureNewTunnel(true));
     664            _manager.getExecutor().buildTunnel(configureNewTunnel(true));
    665665            return true;
    666666        }
     
    11731173
    11741174        PooledTunnelCreatorConfig cfg = new PooledTunnelCreatorConfig(_context, peers.size(),
    1175                                                 settings.isInbound(), settings.getDestination());
    1176         cfg.setTunnelPool(this);
     1175                                                settings.isInbound(), settings.getDestination(),
     1176                                                this);
    11771177        // peers list is ordered endpoint first, but cfg.getPeer() is ordered gateway first
    11781178        for (int i = 0; i < peers.size(); i++) {
     
    12051205    void buildComplete(PooledTunnelCreatorConfig cfg) {
    12061206        synchronized (_inProgress) { _inProgress.remove(cfg); }
    1207         cfg.setTunnelPool(this);
    12081207        //_manager.buildComplete(cfg);
    12091208    }
  • router/java/src/net/i2p/router/tunnel/pool/TunnelPoolManager.java

    re50bf00 r1a200a1  
    130130        if (_log.shouldLog(Log.ERROR))
    131131            _log.error("Want the inbound tunnel for " + destination.toBase32() +
    132                      " but there isn't a pool?");
     132                     " but there isn't a pool?", new Exception());
    133133        return null;
    134134    }
     
    206206        if (_log.shouldLog(Log.ERROR))
    207207            _log.error("Want the inbound tunnel for " + destination.toBase32() +
    208                      " but there isn't a pool?");
     208                     " but there isn't a pool?", new Exception());
    209209        return null;
    210210    }
     
    562562             _context.router().getRouterInfo().getAddressCount() <= 0)) {
    563563            TunnelPool pool = cfg.getTunnelPool();
    564             if (pool == null) {
    565                 // never seen this before, do we reallly need to bother
    566                 // trying so hard to find his pool?
    567                 _log.error("How does this not have a pool?  " + cfg, new Exception("baf"));
    568                 if (cfg.getDestination() != null) {
    569                     if (cfg.isInbound()) {
    570                             pool = _clientInboundPools.get(cfg.getDestination());
    571                     } else {
    572                             pool = _clientOutboundPools.get(cfg.getDestination());
    573                     }
    574                 } else {
    575                     if (cfg.isInbound()) {
    576                         pool = _inboundExploratory;
    577                     } else {
    578                         pool = _outboundExploratory;
    579                     }
    580                 }
    581                 cfg.setTunnelPool(pool);
    582             }
    583564            _context.jobQueue().addJob(new TestJob(_context, cfg, pool));
    584565        }
Note: See TracChangeset for help on using the changeset viewer.