Search in sources :

Example 36 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class I2PSessionImpl method lookupDest.

/**
 *  Ask the router to lookup a Destination by host name.
 *  Blocking. See above for details.
 *  @param maxWait ms
 *  @since 0.9.11
 *  @return null on failure
 */
public Destination lookupDest(String name, long maxWait) throws I2PSessionException {
    if (name.length() == 0)
        return null;
    // Shortcut for b64
    if (name.length() >= 516) {
        try {
            return new Destination(name);
        } catch (DataFormatException dfe) {
            return null;
        }
    }
    // won't fit in Mapping
    if (name.length() >= 256 && !_context.isRouterContext())
        return null;
    synchronized (_lookupCache) {
        Destination rv = _lookupCache.get(name);
        if (rv != null)
            return rv;
    }
    if (isClosed()) {
        if (_log.shouldLog(Log.INFO))
            _log.info("Session closed, cannot lookup " + name);
        return null;
    }
    if (!_routerSupportsHostLookup) {
        // do them a favor and convert to Hash lookup
        if (name.length() == 60 && name.toLowerCase(Locale.US).endsWith(".b32.i2p"))
            return lookupDest(Hash.create(Base32.decode(name.toLowerCase(Locale.US).substring(0, 52))), maxWait);
        // else unsupported
        if (_log.shouldLog(Log.WARN))
            _log.warn("Router does not support HostLookup for " + name);
        return null;
    }
    int nonce = _lookupID.incrementAndGet() & 0x7fffffff;
    LookupWaiter waiter = new LookupWaiter(name, nonce);
    _pendingLookups.offer(waiter);
    Destination rv = null;
    try {
        if (_log.shouldLog(Log.INFO))
            _log.info("Sending HostLookup for " + name);
        SessionId id = _sessionId;
        if (id == null)
            id = new SessionId(65535);
        sendMessage_unchecked(new HostLookupMessage(id, name, nonce, maxWait));
        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) DataFormatException(net.i2p.data.DataFormatException) HostLookupMessage(net.i2p.data.i2cp.HostLookupMessage) I2PSessionException(net.i2p.client.I2PSessionException) SessionId(net.i2p.data.i2cp.SessionId)

Example 37 with I2PSessionException

use of net.i2p.client.I2PSessionException 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)

Example 38 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class I2PSessionImpl method removeSubsession.

/**
 *  @since 0.9.21
 */
public void removeSubsession(I2PSession session) {
    if (!(session instanceof SubSession))
        return;
    synchronized (_subsessionLock) {
        _subsessions.remove(session);
        SessionId id = ((SubSession) session).getSessionId();
        if (id != null)
            _subsessionMap.remove(id);
        // / tell the subsession
        try {
            // doesn't really throw
            session.destroySession();
        } catch (I2PSessionException ise) {
        }
    }
    // do we need this here? subsession.destroySession() calls primary
    Destination d = session.getMyDestination();
    if (d != null)
        _context.keyRing().remove(d.calculateHash());
}
Also used : Destination(net.i2p.data.Destination) I2PSessionException(net.i2p.client.I2PSessionException) SessionId(net.i2p.data.i2cp.SessionId)

Example 39 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class I2PSessionImpl2 method sendBestEffort.

/**
 * TODO - Don't need to save MessageState since actuallyWait is false...
 * But for now just use sendNoEffort() instead.
 *
 * @param flags to be passed to the router
 * @since 0.8.4
 */
protected boolean sendBestEffort(Destination dest, byte[] payload, long expires, int flags) throws I2PSessionException {
    long nonce = _sendMessageNonce.incrementAndGet();
    MessageState state = new MessageState(_context, nonce, getPrefix());
    // since this is 'best effort', all we're waiting for is a status update
    // saying that the router received it - in theory, that should come back
    // immediately, but in practice can take up to a second (though usually
    // much quicker).  setting this to false will short-circuit that delay
    // true;
    boolean actuallyWait = false;
    if (actuallyWait)
        _sendingStates.put(Long.valueOf(nonce), state);
    _producer.sendMessage(this, dest, nonce, payload, expires, flags);
    if (actuallyWait) {
        try {
            state.waitForAccept(_context.clock().now() + getTimeout());
        } catch (InterruptedException ie) {
            throw new I2PSessionException("interrupted");
        } finally {
            _sendingStates.remove(Long.valueOf(nonce));
        }
    }
    boolean found = !actuallyWait || state.wasAccepted();
    if (found) {
        if (_log.shouldLog(Log.INFO))
            _log.info(getPrefix() + "Message sent after " + state.getElapsed() + "ms with " + payload.length + " bytes");
    } else {
        if (_log.shouldLog(Log.INFO))
            _log.info(getPrefix() + "Message send failed after " + state.getElapsed() + "ms with " + payload.length + " bytes");
        // disconnect();
        return false;
    }
    return found;
}
Also used : I2PSessionException(net.i2p.client.I2PSessionException)

Example 40 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class MessageStatusMessageHandler method handleMessage.

public void handleMessage(I2CPMessage message, I2PSessionImpl session) {
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Handle message " + message);
    MessageStatusMessage msg = (MessageStatusMessage) message;
    int status = msg.getStatus();
    long id = msg.getMessageId();
    switch(status) {
        case MessageStatusMessage.STATUS_AVAILABLE:
            ReceiveMessageBeginMessage m = new ReceiveMessageBeginMessage();
            m.setMessageId(id);
            m.setSessionId(msg.getSessionId());
            try {
                session.sendMessage(m);
            } catch (I2PSessionException ise) {
                _log.error("Error asking for the message", ise);
            }
            return;
        case MessageStatusMessage.STATUS_SEND_ACCEPTED:
            session.receiveStatus((int) id, msg.getNonce(), status);
            // noop
            return;
        default:
            if (msg.isSuccessful()) {
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("Message delivery succeeded for message " + id);
            } else {
                if (_log.shouldLog(Log.INFO))
                    _log.info("Message delivery FAILED (" + status + ") for message " + id);
            }
            // if (!skipStatus)
            session.receiveStatus((int) id, msg.getNonce(), status);
            return;
    }
}
Also used : I2PSessionException(net.i2p.client.I2PSessionException) MessageStatusMessage(net.i2p.data.i2cp.MessageStatusMessage) ReceiveMessageBeginMessage(net.i2p.data.i2cp.ReceiveMessageBeginMessage)

Aggregations

I2PSessionException (net.i2p.client.I2PSessionException)44 IOException (java.io.IOException)18 DataFormatException (net.i2p.data.DataFormatException)15 Properties (java.util.Properties)13 I2PSession (net.i2p.client.I2PSession)11 Destination (net.i2p.data.Destination)11 SessionId (net.i2p.data.i2cp.SessionId)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InterruptedIOException (java.io.InterruptedIOException)5 I2PException (net.i2p.I2PException)5 File (java.io.File)4 GeneralSecurityException (java.security.GeneralSecurityException)4 I2PClient (net.i2p.client.I2PClient)4 FileInputStream (java.io.FileInputStream)3 UnknownHostException (java.net.UnknownHostException)3 Payload (net.i2p.data.Payload)3 Log (net.i2p.util.Log)3 BufferedInputStream (java.io.BufferedInputStream)2 DataInputStream (java.io.DataInputStream)2