Search in sources :

Example 1 with I2PDatagramMaker

use of net.i2p.client.datagram.I2PDatagramMaker in project i2p.i2p by i2p.

the class KRPC method sendMessage.

/**
 *  Lowest-level send message call.
 *  @param repliable true for all but announce
 *  @return success
 */
private boolean sendMessage(Destination dest, int toPort, Map<String, Object> map, boolean repliable) {
    if (_session.isClosed()) {
        // Don't allow DHT to open a closed session
        if (_log.shouldLog(Log.WARN))
            _log.warn("Not sending message, session is closed");
        return false;
    }
    if (dest.calculateHash().equals(_myNodeInfo.getHash()))
        throw new IllegalArgumentException("don't send to ourselves");
    byte[] payload = BEncoder.bencode(map);
    if (_log.shouldLog(Log.DEBUG)) {
        ByteArrayInputStream bais = new ByteArrayInputStream(payload);
        try {
            _log.debug("Sending to: " + dest.calculateHash() + ' ' + BDecoder.bdecode(bais).toString());
        } catch (IOException ioe) {
        }
    }
    // Always send query port, peer will increment for unsigned replies
    int fromPort = _qPort;
    if (repliable) {
        I2PDatagramMaker dgMaker = new I2PDatagramMaker(_session);
        payload = dgMaker.makeI2PDatagram(payload);
        if (payload == null) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("DGM fail");
            return false;
        }
    }
    SendMessageOptions opts = new SendMessageOptions();
    opts.setDate(_context.clock().now() + 60 * 1000);
    opts.setTagsToSend(SEND_CRYPTO_TAGS);
    opts.setTagThreshold(LOW_CRYPTO_TAGS);
    if (!repliable)
        opts.setSendLeaseSet(false);
    try {
        boolean success = _session.sendMessage(dest, payload, 0, payload.length, repliable ? I2PSession.PROTO_DATAGRAM : I2PSession.PROTO_DATAGRAM_RAW, fromPort, toPort, opts);
        if (success) {
            _txPkts.incrementAndGet();
            _txBytes.addAndGet(payload.length);
        } else {
            if (_log.shouldLog(Log.WARN))
                _log.warn("sendMessage fail");
        }
        return success;
    } catch (I2PSessionException ise) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("sendMessage fail", ise);
        return false;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) I2PDatagramMaker(net.i2p.client.datagram.I2PDatagramMaker) SendMessageOptions(net.i2p.client.SendMessageOptions) I2PSessionException(net.i2p.client.I2PSessionException) IOException(java.io.IOException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 I2PSessionException (net.i2p.client.I2PSessionException)1 SendMessageOptions (net.i2p.client.SendMessageOptions)1 I2PDatagramMaker (net.i2p.client.datagram.I2PDatagramMaker)1