Search in sources :

Example 31 with OutNetMessage

use of net.i2p.router.OutNetMessage in project i2p.i2p by i2p.

the class OutboundMessageRegistry method unregisterPending.

/**
 *  @param msg may be be null
 */
@SuppressWarnings("unchecked")
public void unregisterPending(OutNetMessage msg) {
    if (msg == null)
        return;
    MessageSelector sel = msg.getReplySelector();
    boolean stillActive = false;
    synchronized (_selectorToMessage) {
        Object old = _selectorToMessage.remove(sel);
        if (old != null) {
            if (old instanceof List) {
                List<OutNetMessage> l = (List<OutNetMessage>) old;
                l.remove(msg);
                if (!l.isEmpty()) {
                    _selectorToMessage.put(sel, l);
                    stillActive = true;
                }
            }
        }
    }
    if (!stillActive)
        synchronized (_selectors) {
            _selectors.remove(sel);
        }
    _activeMessages.remove(msg);
}
Also used : OutNetMessage(net.i2p.router.OutNetMessage) ArrayList(java.util.ArrayList) List(java.util.List) MessageSelector(net.i2p.router.MessageSelector)

Example 32 with OutNetMessage

use of net.i2p.router.OutNetMessage in project i2p.i2p by i2p.

the class StoreJob method sendDirect.

/**
 * Send directly,
 * with the reply to come back directly.
 */
private void sendDirect(DatabaseStoreMessage msg, RouterInfo peer, long expiration) {
    long token = 1 + getContext().random().nextLong(I2NPMessage.MAX_ID_VALUE);
    msg.setReplyToken(token);
    msg.setReplyGateway(getContext().routerHash());
    _state.addPending(peer.getIdentity().getHash());
    SendSuccessJob onReply = new SendSuccessJob(getContext(), peer);
    FailedJob onFail = new FailedJob(getContext(), peer, getContext().clock().now());
    StoreMessageSelector selector = new StoreMessageSelector(getContext(), getJobId(), peer, token, expiration);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug(getJobId() + ": sending store directly to " + peer.getIdentity().getHash());
    OutNetMessage m = new OutNetMessage(getContext(), msg, expiration, STORE_PRIORITY, peer);
    m.setOnFailedReplyJob(onFail);
    m.setOnFailedSendJob(onFail);
    m.setOnReplyJob(onReply);
    m.setReplySelector(selector);
    getContext().messageRegistry().registerPending(m);
    getContext().commSystem().processMessage(m);
}
Also used : OutNetMessage(net.i2p.router.OutNetMessage)

Example 33 with OutNetMessage

use of net.i2p.router.OutNetMessage in project i2p.i2p by i2p.

the class UDPFlooder method run.

public void run() {
    long nextSend = _context.clock().now();
    while (_alive) {
        try {
            synchronized (_peers) {
                if (_peers.isEmpty())
                    _peers.wait();
            }
        } catch (InterruptedException ie) {
        }
        long now = _context.clock().now();
        if (now >= nextSend) {
            // peers always grows, so this is fairly safe
            for (int i = 0; i < _peers.size(); i++) {
                PeerState peer = _peers.get(i);
                DataMessage m = new DataMessage(_context);
                // new byte[4096];
                byte[] data = _floodData;
                // _context.random().nextBytes(data);
                m.setData(data);
                m.setMessageExpiration(_context.clock().now() + 10 * 1000);
                m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
                if (true) {
                    RouterInfo to = _context.netDb().lookupRouterInfoLocally(peer.getRemotePeer());
                    if (to == null)
                        continue;
                    OutNetMessage msg = new OutNetMessage(_context, m, m.getMessageExpiration(), 500, to);
                    // warning, getStatLog() can be null
                    // _context.statManager().getStatLog().addData(peer.getRemotePeer().toBase64().substring(0,6), "udp.floodDataSent", 1, 0);
                    _transport.send(msg);
                } else {
                    _transport.send(m, peer);
                }
            }
            nextSend = now + calcFloodDelay();
        }
        long delay = nextSend - now;
        if (delay > 0) {
            if (delay > 10 * 1000) {
                long fd = calcFloodDelay();
                if (delay > fd) {
                    nextSend = now + fd;
                    delay = fd;
                }
            }
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ie) {
            }
        }
    }
}
Also used : OutNetMessage(net.i2p.router.OutNetMessage) DataMessage(net.i2p.data.i2np.DataMessage) RouterInfo(net.i2p.data.router.RouterInfo)

Example 34 with OutNetMessage

use of net.i2p.router.OutNetMessage in project i2p.i2p by i2p.

the class SendMessageDirectJob method send.

private void send() {
    if (_sent) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Not resending!", new Exception("blah"));
        return;
    }
    _sent = true;
    Hash to = _router.getIdentity().getHash();
    Hash us = getContext().routerHash();
    if (us.equals(to)) {
        if (_selector != null) {
            OutNetMessage outM = new OutNetMessage(getContext(), _message, _expiration, _priority, _router);
            outM.setOnFailedReplyJob(_onFail);
            outM.setOnFailedSendJob(_onFail);
            outM.setOnReplyJob(_onSuccess);
            outM.setOnSendJob(_onSend);
            outM.setReplySelector(_selector);
            getContext().messageRegistry().registerPending(outM);
        }
        if (_onSend != null)
            getContext().jobQueue().addJob(_onSend);
        getContext().inNetMessagePool().add(_message, _router.getIdentity(), null);
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Adding " + _message.getClass().getName() + " to inbound message pool as it was destined for ourselves");
    // _log.debug("debug", _createdBy);
    } else {
        OutNetMessage msg = new OutNetMessage(getContext(), _message, _expiration, _priority, _router);
        msg.setOnFailedReplyJob(_onFail);
        msg.setOnFailedSendJob(_onFail);
        msg.setOnReplyJob(_onSuccess);
        msg.setOnSendJob(_onSend);
        msg.setReplySelector(_selector);
        getContext().outNetMessagePool().add(msg);
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Adding " + _message.getClass().getName() + " to outbound message pool targeting " + _router.getIdentity().getHash().toBase64());
    // _log.debug("Message pooled: " + _message);
    }
}
Also used : OutNetMessage(net.i2p.router.OutNetMessage) Hash(net.i2p.data.Hash)

Example 35 with OutNetMessage

use of net.i2p.router.OutNetMessage in project i2p.i2p by i2p.

the class FloodfillRouterInfoFloodJob method runJob.

public void runJob() {
    FloodfillPeerSelector sel = (FloodfillPeerSelector) _facade.getPeerSelector();
    DatabaseStoreMessage dsm;
    OutNetMessage outMsg;
    RouterInfo nextPeerInfo;
    List<Hash> peers = sel.selectFloodfillParticipants(getContext().routerHash(), FLOOD_PEERS, null);
    for (Hash ri : peers) {
        // Iterate through list of nearby (ff) peers
        dsm = new DatabaseStoreMessage(getContext());
        dsm.setMessageExpiration(getContext().clock().now() + 10 * 1000);
        dsm.setEntry(getContext().router().getRouterInfo());
        nextPeerInfo = getContext().netDb().lookupRouterInfoLocally(ri);
        if (nextPeerInfo == null) {
            continue;
        }
        outMsg = new OutNetMessage(getContext(), dsm, getContext().clock().now() + 10 * 1000, OutNetMessage.PRIORITY_MY_NETDB_STORE, nextPeerInfo);
        // Whoosh!
        getContext().outNetMessagePool().add(outMsg);
        if (_log.shouldLog(Log.DEBUG)) {
            _log.logAlways(Log.DEBUG, "Sending our RI to: " + nextPeerInfo.getHash());
        }
    }
}
Also used : OutNetMessage(net.i2p.router.OutNetMessage) RouterInfo(net.i2p.data.router.RouterInfo) DatabaseStoreMessage(net.i2p.data.i2np.DatabaseStoreMessage) Hash(net.i2p.data.Hash)

Aggregations

OutNetMessage (net.i2p.router.OutNetMessage)36 ArrayList (java.util.ArrayList)9 Hash (net.i2p.data.Hash)9 RouterInfo (net.i2p.data.router.RouterInfo)9 DatabaseStoreMessage (net.i2p.data.i2np.DatabaseStoreMessage)5 List (java.util.List)4 RouterIdentity (net.i2p.data.router.RouterIdentity)4 I2NPMessage (net.i2p.data.i2np.I2NPMessage)3 RouterAddress (net.i2p.data.router.RouterAddress)3 MessageSelector (net.i2p.router.MessageSelector)3 IOException (java.io.IOException)2 TunnelGatewayMessage (net.i2p.data.i2np.TunnelGatewayMessage)2 Job (net.i2p.router.Job)2 TunnelInfo (net.i2p.router.TunnelInfo)2 TunnelManagerFacade (net.i2p.router.TunnelManagerFacade)2 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 Map (java.util.Map)1