Changeset 2caa6ad9


Ignore:
Timestamp:
Jul 10, 2013 6:12:47 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
732eddd
Parents:
d3e0161
Message:
  • Router I2CP:
    • Make classes extensible for router-side test stubs
    • Add router-side local-only test implementation, no full router required. Only tested with external clients, probably doesn't work in-JVM.
    • Don't start threads in ClientManager? constructor
    • Remove unused Reader param in ClientMessageEventListener? methods
    • Cleanups, volatiles, finals, javadocs
Location:
router/java
Files:
4 added
4 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/client/ClientConnectionRunner.java

    rd3e0161 r2caa6ad9  
    5959    protected final Log _log;
    6060    protected final RouterContext _context;
    61     private final ClientManager _manager;
     61    protected final ClientManager _manager;
    6262    /** socket for this particular peer connection */
    6363    private final Socket _socket;
     
    138138                throw new IllegalStateException();
    139139            _reader = new I2CPMessageReader(new BufferedInputStream(_socket.getInputStream(), BUF_SIZE),
    140                                             new ClientMessageEventListener(_context, this, true));
     140                                            createListener());
    141141            _writer = new ClientWriterRunner(_context, this);
    142142            I2PThread t = new I2PThread(_writer);
     
    149149    }
    150150   
     151    /**
     152     *  Allow override for testing
     153     *  @since 0.9.8
     154     */
     155    protected I2CPMessageReader.I2CPMessageEventListener createListener() {
     156        return new ClientMessageEventListener(_context, this, true);
     157    }
     158
    151159    /**
    152160     *  Die a horrible death. Cannot be restarted.
     
    461469     *            signed version (as well as any changed/added/removed Leases)
    462470     * @param expirationTime ms to wait before failing
    463      * @param onCreateJob Job to run after the LeaseSet is authorized
    464      * @param onFailedJob Job to run after the timeout passes without receiving authorization
     471     * @param onCreateJob Job to run after the LeaseSet is authorized, null OK
     472     * @param onFailedJob Job to run after the timeout passes without receiving authorization, null OK
    465473     */
    466474    void requestLeaseSet(LeaseSet set, long expirationTime, Job onCreateJob, Job onFailedJob) {
  • router/java/src/net/i2p/router/client/ClientManager.java

    rd3e0161 r2caa6ad9  
    1111import java.io.IOException;
    1212import java.io.Writer;
     13import java.util.Collections;
    1314import java.util.HashMap;
    1415import java.util.HashSet;
     
    4546class ClientManager {
    4647    private final Log _log;
    47     private ClientListenerRunner _listener;
     48    protected ClientListenerRunner _listener;
    4849    // Destination --> ClientConnectionRunner
    4950    // Locked for adds/removes but not lookups
     
    5455    // ClientConnectionRunner for clients w/out a Dest yet
    5556    private final Set<ClientConnectionRunner> _pendingRunners;
    56     private final RouterContext _ctx;
    57     private volatile boolean _isStarted;
     57    protected final RouterContext _ctx;
     58    protected final int _port;
     59    protected volatile boolean _isStarted;
    5860
    5961    /** Disable external interface, allow internal clients only @since 0.8.3 */
     
    6668    private static final long REQUEST_LEASESET_TIMEOUT = 60*1000;
    6769
     70    /**
     71     *  Does not start the listeners.
     72     *  Caller must call start()
     73     */
    6874    public ClientManager(RouterContext context, int port) {
    6975        _ctx = context;
     
    7682        _runnersByHash = new ConcurrentHashMap();
    7783        _pendingRunners = new HashSet();
    78         startListeners(port);
     84        _port = port;
    7985        // following are for RequestLeaseSetJob
    8086        _ctx.statManager().createRateStat("client.requestLeaseSetSuccess", "How frequently the router requests successfully a new leaseSet?", "ClientMessages", new long[] { 60*60*1000 });
     
    8389    }
    8490
     91    /** @since 0.9.8 */
     92    public synchronized void start() {
     93        startListeners();
     94    }
     95
    8596    /** Todo: Start a 3rd listener for IPV6? */
    86     private void startListeners(int port) {
     97    protected void startListeners() {
    8798        if (!_ctx.getBooleanProperty(PROP_DISABLE_EXTERNAL)) {
    8899            // there's no option to start both an SSL and non-SSL listener
    89100            if (_ctx.getBooleanProperty(PROP_ENABLE_SSL))
    90                 _listener = new SSLClientListenerRunner(_ctx, this, port);
     101                _listener = new SSLClientListenerRunner(_ctx, this, _port);
    91102            else
    92                 _listener = new ClientListenerRunner(_ctx, this, port);
    93             Thread t = new I2PThread(_listener, "ClientListener:" + port, true);
     103                _listener = new ClientListenerRunner(_ctx, this, _port);
     104            Thread t = new I2PThread(_listener, "ClientListener:" + _port, true);
    94105            t.start();
    95106        }
     
    103114        try { Thread.sleep(2*1000); } catch (InterruptedException ie) {}
    104115       
    105         int port = _ctx.getProperty(ClientManagerFacadeImpl.PROP_CLIENT_PORT,
    106                                     ClientManagerFacadeImpl.DEFAULT_PORT);
    107         startListeners(port);
     116        startListeners();
    108117    }
    109118   
     
    405414    }
    406415   
     416    /**
     417     *  @return unmodifiable, not a copy
     418     */
    407419    Set<Destination> getRunnerDestinations() {
    408         Set<Destination> dests = new HashSet();
    409         dests.addAll(_runners.keySet());
    410         return dests;
    411     }
    412    
     420        return Collections.unmodifiableSet(_runners.keySet());
     421    }
     422   
     423    /**
     424     *  Unused
     425     *
     426     *  @param dest null for all local destinations
     427     */
    413428    public void reportAbuse(Destination dest, String reason, int severity) {
    414429        if (dest != null) {
     
    418433            }
    419434        } else {
    420             Set dests = getRunnerDestinations();
    421             for (Iterator iter = dests.iterator(); iter.hasNext(); ) {
    422                 Destination d = (Destination)iter.next();
     435            for (Destination d : _runners.keySet()) {
    423436                reportAbuse(d, reason, severity);
    424437            }
  • router/java/src/net/i2p/router/client/ClientManagerFacadeImpl.java

    rd3e0161 r2caa6ad9  
    5757        int port = _context.getProperty(PROP_CLIENT_PORT, DEFAULT_PORT);
    5858        _manager = new ClientManager(_context, port);
     59        _manager.start();
    5960    }   
    6061   
     
    8384
    8485    private static final long MAX_TIME_TO_REBUILD = 10*60*1000;
     86
    8587    @Override
    8688    public boolean verifyClientLiveliness() {
    8789        if (_manager == null) return true;
    8890        boolean lively = true;
    89         for (Iterator iter = _manager.getRunnerDestinations().iterator(); iter.hasNext(); ) {
    90             Destination dest = (Destination)iter.next();
     91        for (Destination dest : _manager.getRunnerDestinations()) {
    9192            ClientConnectionRunner runner = _manager.getRunner(dest);
    9293            if ( (runner == null) || (runner.getIsDead())) continue;
  • router/java/src/net/i2p/router/client/ClientMessageEventListener.java

    rd3e0161 r2caa6ad9  
    4747class ClientMessageEventListener implements I2CPMessageReader.I2CPMessageEventListener {
    4848    private final Log _log;
    49     private final RouterContext _context;
    50     private final ClientConnectionRunner _runner;
     49    protected final RouterContext _context;
     50    protected final ClientConnectionRunner _runner;
    5151    private final boolean  _enforceAuth;
    5252   
     
    7474        switch (message.getType()) {
    7575            case GetDateMessage.MESSAGE_TYPE:
    76                 handleGetDate(reader, (GetDateMessage)message);
     76                handleGetDate((GetDateMessage)message);
    7777                break;
    7878            case SetDateMessage.MESSAGE_TYPE:
    79                 handleSetDate(reader, (SetDateMessage)message);
     79                handleSetDate((SetDateMessage)message);
    8080                break;
    8181            case CreateSessionMessage.MESSAGE_TYPE:
    82                 handleCreateSession(reader, (CreateSessionMessage)message);
     82                handleCreateSession((CreateSessionMessage)message);
    8383                break;
    8484            case SendMessageMessage.MESSAGE_TYPE:
    85                 handleSendMessage(reader, (SendMessageMessage)message);
     85                handleSendMessage((SendMessageMessage)message);
    8686                break;
    8787            case SendMessageExpiresMessage.MESSAGE_TYPE:
    88                 handleSendMessage(reader, (SendMessageExpiresMessage)message);
     88                handleSendMessage((SendMessageExpiresMessage)message);
    8989                break;
    9090            case ReceiveMessageBeginMessage.MESSAGE_TYPE:
    91                 handleReceiveBegin(reader, (ReceiveMessageBeginMessage)message);
     91                handleReceiveBegin((ReceiveMessageBeginMessage)message);
    9292                break;
    9393            case ReceiveMessageEndMessage.MESSAGE_TYPE:
    94                 handleReceiveEnd(reader, (ReceiveMessageEndMessage)message);
     94                handleReceiveEnd((ReceiveMessageEndMessage)message);
    9595                break;
    9696            case CreateLeaseSetMessage.MESSAGE_TYPE:
    97                 handleCreateLeaseSet(reader, (CreateLeaseSetMessage)message);
     97                handleCreateLeaseSet((CreateLeaseSetMessage)message);
    9898                break;
    9999            case DestroySessionMessage.MESSAGE_TYPE:
    100                 handleDestroySession(reader, (DestroySessionMessage)message);
     100                handleDestroySession((DestroySessionMessage)message);
    101101                break;
    102102            case DestLookupMessage.MESSAGE_TYPE:
    103                 handleDestLookup(reader, (DestLookupMessage)message);
     103                handleDestLookup((DestLookupMessage)message);
    104104                break;
    105105            case ReconfigureSessionMessage.MESSAGE_TYPE:
    106                 handleReconfigureSession(reader, (ReconfigureSessionMessage)message);
     106                handleReconfigureSession((ReconfigureSessionMessage)message);
    107107                break;
    108108            case GetBandwidthLimitsMessage.MESSAGE_TYPE:
    109                 handleGetBWLimits(reader, (GetBandwidthLimitsMessage)message);
     109                handleGetBWLimits((GetBandwidthLimitsMessage)message);
    110110                break;
    111111            default:
     
    132132    }
    133133   
    134     private void handleGetDate(I2CPMessageReader reader, GetDateMessage message) {
     134    private void handleGetDate(GetDateMessage message) {
    135135        // sent by clients >= 0.8.7
    136136        String clientVersion = message.getVersion();
     
    149149     *  As of 0.8.7, does nothing. Do not allow a client to set the router's clock.
    150150     */
    151     private void handleSetDate(I2CPMessageReader reader, SetDateMessage message) {
     151    private void handleSetDate(SetDateMessage message) {
    152152        //_context.clock().setNow(message.getDate().getTime());
    153153    }
     
    161161     * So keep it simple.
    162162     */
    163     private void handleCreateSession(I2CPMessageReader reader, CreateSessionMessage message) {
     163    private void handleCreateSession(CreateSessionMessage message) {
    164164        SessionConfig in = message.getSessionConfig();
    165165        if (in.verifySignature()) {
     
    210210        if (_log.shouldLog(Log.DEBUG))
    211211            _log.debug("after sessionEstablished for " + message.getSessionConfig().getDestination().calculateHash().toBase64());
    212 
     212        startCreateSessionJob();
     213    }
     214   
     215    /**
     216     *  Override for testing
     217     *  @since 0.9.8
     218     *
     219     */
     220    protected void startCreateSessionJob() {
    213221        _context.jobQueue().addJob(new CreateSessionJob(_context, _runner));
    214222    }
    215    
    216223   
    217224    /**
     
    220227     *
    221228     */
    222     private void handleSendMessage(I2CPMessageReader reader, SendMessageMessage message) {
     229    private void handleSendMessage(SendMessageMessage message) {
    223230        if (_log.shouldLog(Log.DEBUG))
    224231            _log.debug("handleSendMessage called");
     
    237244     *
    238245     */
    239     private void handleReceiveBegin(I2CPMessageReader reader, ReceiveMessageBeginMessage message) {
     246    private void handleReceiveBegin(ReceiveMessageBeginMessage message) {
    240247        if (_runner.isDead()) return;
    241248        if (_log.shouldLog(Log.DEBUG))
     
    267274     *
    268275     */
    269     private void handleReceiveEnd(I2CPMessageReader reader, ReceiveMessageEndMessage message) {
     276    private void handleReceiveEnd(ReceiveMessageEndMessage message) {
    270277        _runner.removePayload(new MessageId(message.getMessageId()));
    271278    }
    272279   
    273     private void handleDestroySession(I2CPMessageReader reader, DestroySessionMessage message) {
     280    private void handleDestroySession(DestroySessionMessage message) {
    274281        if (_log.shouldLog(Log.INFO))
    275282            _log.info("Destroying client session " + _runner.getSessionId());
     
    277284    }
    278285   
    279     private void handleCreateLeaseSet(I2CPMessageReader reader, CreateLeaseSetMessage message) {       
     286    /** override for testing */
     287    protected void handleCreateLeaseSet(CreateLeaseSetMessage message) {       
    280288        if ( (message.getLeaseSet() == null) || (message.getPrivateKey() == null) || (message.getSigningPrivateKey() == null) ) {
    281289            if (_log.shouldLog(Log.ERROR))
     
    294302    }
    295303
    296     private void handleDestLookup(I2CPMessageReader reader, DestLookupMessage message) {
     304    /** override for testing */
     305    protected void handleDestLookup(DestLookupMessage message) {
    297306        _context.jobQueue().addJob(new LookupDestJob(_context, _runner, message.getHash()));
    298307    }
     
    306315     * ClientConnectionRunner.sessionEstablished(). Those can't be changed later.
    307316     */
    308     private void handleReconfigureSession(I2CPMessageReader reader, ReconfigureSessionMessage message) {
     317    private void handleReconfigureSession(ReconfigureSessionMessage message) {
    309318        if (_log.shouldLog(Log.INFO))
    310319            _log.info("Updating options - old: " + _runner.getConfig() + " new: " + message.getSessionConfig());
     
    344353     * But it's not enforced anywhere.
    345354     */
    346     private void handleGetBWLimits(I2CPMessageReader reader, GetBandwidthLimitsMessage message) {
     355    protected void handleGetBWLimits(GetBandwidthLimitsMessage message) {
    347356        if (_log.shouldLog(Log.INFO))
    348357            _log.info("Got BW Limits request");
Note: See TracChangeset for help on using the changeset viewer.