Search in sources :

Example 1 with DestLookupMessage

use of net.i2p.data.i2cp.DestLookupMessage in project i2p.i2p by i2p.

the class I2PSessionImpl method lookupDest.

/**
 *  Blocking.
 *  @param maxWait ms
 *  @since 0.8.3
 *  @return null on failure
 */
public Destination lookupDest(Hash h, long maxWait) throws I2PSessionException {
    synchronized (_lookupCache) {
        Destination rv = _lookupCache.get(h);
        if (rv != null)
            return rv;
    }
    synchronized (_stateLock) {
        // not before GOTDATE
        if (_state == State.CLOSED || _state == State.INIT || _state == State.OPENING) {
            if (_log.shouldLog(Log.INFO))
                _log.info("Session closed, cannot lookup " + h);
            return null;
        }
    }
    LookupWaiter waiter;
    long nonce;
    if (_routerSupportsHostLookup) {
        nonce = _lookupID.incrementAndGet() & 0x7fffffff;
        waiter = new LookupWaiter(h, nonce);
    } else {
        // won't be used
        nonce = 0;
        waiter = new LookupWaiter(h);
    }
    _pendingLookups.offer(waiter);
    Destination rv = null;
    try {
        if (_routerSupportsHostLookup) {
            if (_log.shouldLog(Log.INFO))
                _log.info("Sending HostLookup for " + h);
            SessionId id = _sessionId;
            if (id == null)
                id = new SessionId(65535);
            sendMessage_unchecked(new HostLookupMessage(id, h, nonce, maxWait));
        } else {
            if (_log.shouldLog(Log.INFO))
                _log.info("Sending DestLookup for " + h);
            sendMessage_unchecked(new DestLookupMessage(h));
        }
        try {
            synchronized (waiter) {
                waiter.wait(maxWait);
                rv = waiter.destination;
            }
        } catch (InterruptedException ie) {
            throw new I2PSessionException("Interrupted", ie);
        }
    } finally {
        _pendingLookups.remove(waiter);
    }
    return rv;
}
Also used : Destination(net.i2p.data.Destination) HostLookupMessage(net.i2p.data.i2cp.HostLookupMessage) I2PSessionException(net.i2p.client.I2PSessionException) DestLookupMessage(net.i2p.data.i2cp.DestLookupMessage) SessionId(net.i2p.data.i2cp.SessionId)

Aggregations

I2PSessionException (net.i2p.client.I2PSessionException)1 Destination (net.i2p.data.Destination)1 DestLookupMessage (net.i2p.data.i2cp.DestLookupMessage)1 HostLookupMessage (net.i2p.data.i2cp.HostLookupMessage)1 SessionId (net.i2p.data.i2cp.SessionId)1