<commit_before>package net.i2p.router.tunnelmanager;
/*
 * free (adj.): unencumbered; not under the control of others
 * Written by jrandom in 2003 and released into the public domain
 * with no warranty of any kind, either expressed or implied.
 * It probably won't make your computer catch on fire, or eat
 * your children, but it might.  Use at your own risk.
 *
 */

import java.util.Date;
import java.util.List;

import net.i2p.data.Hash;
import net.i2p.data.TunnelId;
import net.i2p.data.i2np.DeliveryStatusMessage;
import net.i2p.data.i2np.I2NPMessage;
import net.i2p.router.JobImpl;
import net.i2p.router.MessageSelector;
import net.i2p.router.ReplyJob;
import net.i2p.router.RouterContext;
import net.i2p.router.TunnelInfo;
import net.i2p.router.TunnelSelectionCriteria;
import net.i2p.router.message.SendTunnelMessageJob;
import net.i2p.util.Log;

class TestTunnelJob extends JobImpl {
    private Log _log;
    /** tunnel that we want to test */
    private TunnelId _primaryId;
    /** tunnel that is used to help test the primary id */
    private TunnelId _secondaryId;
    private TunnelPool _pool;
    private long _nonce;
    
    public TestTunnelJob(RouterContext ctx, TunnelId id, TunnelPool pool) {
        super(ctx);
        _log = ctx.logManager().getLog(TestTunnelJob.class);
        _primaryId = id;
        _pool = pool;
        _nonce = ctx.random().nextInt(Integer.MAX_VALUE);
    }
    public String getName() { return "Test Tunnel"; }
    public void runJob() {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Testing tunnel " + _primaryId.getTunnelId());
        TunnelInfo info = _pool.getTunnelInfo(_primaryId);
        if (info == null) {
            _log.error("wtf, why are we testing a tunnel that we do not know about? [" 
                       + _primaryId.getTunnelId() + "]", getAddedBy());
            return;
        }
        
        // mark it as something we're testing
        info.setLastTested(_context.clock().now());
        if (isOutbound(info)) {
            testOutbound(info);
        } else {
            testInbound(info);
        }
    }
    
    private boolean isOutbound(TunnelInfo info) {
        if (info == null) {
            _log.error("wtf, null info?", new Exception("Who checked a null tunnel info?"));
            return false;
        }
        if (_context.routerHash().equals(info.getThisHop()))
            return true;
        else
            return false;
    }
    
    private final static long TEST_TIMEOUT = 30*1000; // 30 seconds for a test to succeed
    private final static int TEST_PRIORITY = 100;
    
    /**
     * Send a message out the tunnel with instructions to send the message back
     * to ourselves and wait for it to arrive.
     */
    private void testOutbound(TunnelInfo info) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Testing outbound tunnel " + info);
        DeliveryStatusMessage msg = new DeliveryStatusMessage(_context);
        msg.setArrival(new Date(_context.clock().now()));
        msg.setMessageId(_nonce);
        Hash us = _context.routerHash();
        _secondaryId = getReplyTunnel();
        if (_secondaryId == null) {
            _context.jobQueue().addJob(new TestFailedJob());
            return;
        }
        
        TunnelInfo inboundInfo = _pool.getTunnelInfo(_secondaryId);
        inboundInfo.setLastTested(_context.clock().now());
        
        TestFailedJob failureJob = new TestFailedJob();
        MessageSelector selector = new TestMessageSelector(msg.getMessageId(), info.getTunnelId().getTunnelId());
        SendTunnelMessageJob testJob = new SendTunnelMessageJob(_context, msg, info.getTunnelId(), us, _secondaryId, null, new TestSuccessfulJob(), failureJob, selector, TEST_TIMEOUT, TEST_PRIORITY);
        _context.jobQueue().addJob(testJob);
    }

    /**
     * Send a message to the gateway and wait for it to arrive.
     */
    private void testInbound(TunnelInfo info) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Testing inbound tunnel " + info);
        DeliveryStatusMessage msg = new DeliveryStatusMessage(_context);
        msg.setArrival(new Date(_context.clock().now()));
        msg.setMessageId(_nonce);
        
        _secondaryId = getOutboundTunnel();
        if (_secondaryId == null) {
            _context.jobQueue().addJob(new TestFailedJob());
            return;
        }
        
        TunnelInfo outboundInfo = _pool.getTunnelInfo(_secondaryId);
        outboundInfo.setLastTested(_context.clock().now());
        
        TestFailedJob failureJob = new TestFailedJob();
        MessageSelector selector = new TestMessageSelector(msg.getMessageId(), info.getTunnelId().getTunnelId());
        SendTunnelMessageJob j = new SendTunnelMessageJob(_context, msg, _secondaryId, info.getThisHop(), info.getTunnelId(), null, new TestSuccessfulJob(), failureJob, selector, _context.clock().now()+TEST_TIMEOUT, TEST_PRIORITY);
        _context.jobQueue().addJob(j);
    }
    <commit_msg>type safety?  we dont need no stinkin' type safety! (aka expiration vs timeoutMs, as shown by tunnel test messages that don't expire for another 30 years) this'll leak memory for failed tunnels, so we'll push this in a new rev in the next day or two, or maybe later today if it gets really bad.<commit_after>package net.i2p.router.tunnelmanager;
/*
 * free (adj.): unencumbered; not under the control of others
 * Written by jrandom in 2003 and released into the public domain
 * with no warranty of any kind, either expressed or implied.
 * It probably won't make your computer catch on fire, or eat
 * your children, but it might.  Use at your own risk.
 *
 */

import java.util.Date;
import java.util.List;

import net.i2p.data.Hash;
import net.i2p.data.TunnelId;
import net.i2p.data.i2np.DeliveryStatusMessage;
import net.i2p.data.i2np.I2NPMessage;
import net.i2p.router.JobImpl;
import net.i2p.router.MessageSelector;
import net.i2p.router.ReplyJob;
import net.i2p.router.RouterContext;
import net.i2p.router.TunnelInfo;
import net.i2p.router.TunnelSelectionCriteria;
import net.i2p.router.message.SendTunnelMessageJob;
import net.i2p.util.Log;

class TestTunnelJob extends JobImpl {
    private Log _log;
    /** tunnel that we want to test */
    private TunnelId _primaryId;
    /** tunnel that is used to help test the primary id */
    private TunnelId _secondaryId;
    private TunnelPool _pool;
    private long _nonce;
    
    public TestTunnelJob(RouterContext ctx, TunnelId id, TunnelPool pool) {
        super(ctx);
        _log = ctx.logManager().getLog(TestTunnelJob.class);
        _primaryId = id;
        _pool = pool;
        _nonce = ctx.random().nextInt(Integer.MAX_VALUE);
    }
    public String getName() { return "Test Tunnel"; }
    public void runJob() {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Testing tunnel " + _primaryId.getTunnelId());
        TunnelInfo info = _pool.getTunnelInfo(_primaryId);
        if (info == null) {
            _log.error("wtf, why are we testing a tunnel that we do not know about? [" 
                       + _primaryId.getTunnelId() + "]", getAddedBy());
            return;
        }
        
        // mark it as something we're testing
        info.setLastTested(_context.clock().now());
        if (isOutbound(info)) {
            testOutbound(info);
        } else {
            testInbound(info);
        }
    }
    
    private boolean isOutbound(TunnelInfo info) {
        if (info == null) {
            _log.error("wtf, null info?", new Exception("Who checked a null tunnel info?"));
            return false;
        }
        if (_context.routerHash().equals(info.getThisHop()))
            return true;
        else
            return false;
    }
    
    private final static long TEST_TIMEOUT = 30*1000; // 30 seconds for a test to succeed
    private final static int TEST_PRIORITY = 100;
    
    /**
     * Send a message out the tunnel with instructions to send the message back
     * to ourselves and wait for it to arrive.
     */
    private void testOutbound(TunnelInfo info) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Testing outbound tunnel " + info);
        DeliveryStatusMessage msg = new DeliveryStatusMessage(_context);
        msg.setArrival(new Date(_context.clock().now()));
        msg.setMessageId(_nonce);
        Hash us = _context.routerHash();
        _secondaryId = getReplyTunnel();
        if (_secondaryId == null) {
            _context.jobQueue().addJob(new TestFailedJob());
            return;
        }
        
        TunnelInfo inboundInfo = _pool.getTunnelInfo(_secondaryId);
        inboundInfo.setLastTested(_context.clock().now());
        
        TestFailedJob failureJob = new TestFailedJob();
        MessageSelector selector = new TestMessageSelector(msg.getMessageId(), info.getTunnelId().getTunnelId());
        SendTunnelMessageJob testJob = new SendTunnelMessageJob(_context, msg, info.getTunnelId(), us, _secondaryId, null, new TestSuccessfulJob(), failureJob, selector, TEST_TIMEOUT, TEST_PRIORITY);
        _context.jobQueue().addJob(testJob);
    }

    /**
     * Send a message to the gateway and wait for it to arrive.
     */
    private void testInbound(TunnelInfo info) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Testing inbound tunnel " + info);
        DeliveryStatusMessage msg = new DeliveryStatusMessage(_context);
        msg.setArrival(new Date(_context.clock().now()));
        msg.setMessageId(_nonce);
        
        _secondaryId = getOutboundTunnel();
        if (_secondaryId == null) {
            _context.jobQueue().addJob(new TestFailedJob());
            return;
        }
        
        TunnelInfo outboundInfo = _pool.getTunnelInfo(_secondaryId);
        outboundInfo.setLastTested(_context.clock().now());
        
        TestFailedJob failureJob = new TestFailedJob();
        MessageSelector selector = new TestMessageSelector(msg.getMessageId(), info.getTunnelId().getTunnelId());
        SendTunnelMessageJob j = new SendTunnelMessageJob(_context, msg, _secondaryId, info.getThisHop(), info.getTunnelId(), null, new TestSuccessfulJob(), failureJob, selector, TEST_TIMEOUT, TEST_PRIORITY);
        _context.jobQueue().addJob(j);
    }
    
{'fraction_non_alphanumeric': 0.1026811734991257, 'fraction_numerical': 0.006217213911016126, 'mean_word_length': 2.7531899380240614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7149908', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2957, 'n_words': 792}