Java Code Examples for org.bitcoinj.utils.Threading#SAME_THREAD

The following examples show how to use org.bitcoinj.utils.Threading#SAME_THREAD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Peer.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
protected void processGetData(GetDataMessage getdata) {
    log.info("{}: Received getdata message: {}", getAddress(), getdata.toString());
    ArrayList<Message> items = new ArrayList<>();
    for (ListenerRegistration<GetDataEventListener> registration : getDataEventListeners) {
        if (registration.executor != Threading.SAME_THREAD)
            continue;
        List<Message> listenerItems = registration.listener.getData(this, getdata);
        if (listenerItems == null)
            continue;
        items.addAll(listenerItems);
    }
    if (items.isEmpty()) {
        return;
    }
    log.info("{}: Sending {} items gathered from listeners to peer", getAddress(), items.size());
    for (Message item : items) {
        sendMessage(item);
    }
}
 
Example 2
Source File: Peer.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
protected void processGetData(GetDataMessage getdata) {
    log.info("{}: Received getdata message: {}", getAddress(), getdata.toString());
    ArrayList<Message> items = new ArrayList<>();
    for (ListenerRegistration<GetDataEventListener> registration : getDataEventListeners) {
        if (registration.executor != Threading.SAME_THREAD) continue;
        List<Message> listenerItems = registration.listener.getData(this, getdata);
        if (listenerItems == null) continue;
        items.addAll(listenerItems);
    }
    if (items.isEmpty()) {
        return;
    }
    log.info("{}: Sending {} items gathered from listeners to peer", getAddress(), items.size());
    for (Message item : items) {
        sendMessage(item);
    }
}
 
Example 3
Source File: ElectrumMultiWallet.java    From java-stratum with Apache License 2.0 6 votes vote down vote up
private void notifyHeight(final long height) {
    final boolean isSynced = isChainSynced && isHistorySynced;
    log.info("notify height {} {} {}", isChainSynced, isHistorySynced, height);
    for (final ListenerRegistration<MultiWalletEventListener> registration : eventListeners) {
        if (registration.executor == Threading.SAME_THREAD) {
            registration.listener.onSyncState(this, isSynced, height);
        } else {
            registration.executor.execute(new Runnable() {
                @Override
                public void run() {
                    registration.listener.onSyncState(ElectrumMultiWallet.this, isSynced, height);
                }
            });
        }
    }
}
 
Example 4
Source File: Peer.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
protected void processGetData(GetDataMessage getdata) {
    log.info("{}: Received getdata message: {}", getAddress(), getdata.toString());
    ArrayList<Message> items = new ArrayList<>();
    for (ListenerRegistration<GetDataEventListener> registration : getDataEventListeners) {
        if (registration.executor != Threading.SAME_THREAD) continue;
        List<Message> listenerItems = registration.listener.getData(this, getdata);
        if (listenerItems == null) continue;
        items.addAll(listenerItems);
    }
    if (items.isEmpty()) {
        return;
    }
    log.info("{}: Sending {} items gathered from listeners to peer", getAddress(), items.size());
    for (Message item : items) {
        sendMessage(item);
    }
}
 
Example 5
Source File: ElectrumMultiWallet.java    From java-stratum with Apache License 2.0 5 votes vote down vote up
private void notifyTransaction(final Transaction tx, final boolean isNewCoin) {
    for (final ListenerRegistration<MultiWalletEventListener> registration : eventListeners) {
        if (registration.executor == Threading.SAME_THREAD) {
            registration.listener.onTransaction(this, tx, isNewCoin);
        } else {
            registration.executor.execute(new Runnable() {
                @Override
                public void run() {
                    registration.listener.onTransaction(ElectrumMultiWallet.this, tx, isNewCoin);
                }
            });
        }
    }
}
 
Example 6
Source File: Peer.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void processMessage(Message m) throws Exception {
    // Allow event listeners to filter the message stream. Listeners are allowed to drop messages by
    // returning null.
    for (ListenerRegistration<PreMessageReceivedEventListener> registration : preMessageReceivedEventListeners) {
        // Skip any listeners that are supposed to run in another thread as we don't want to block waiting
        // for it, which might cause circular deadlock.
        if (registration.executor == Threading.SAME_THREAD) {
            m = registration.listener.onPreMessageReceived(this, m);
            if (m == null)
                break;
        }
    }
    if (m == null)
        return;

    // If we are in the middle of receiving transactions as part of a filtered block push from the remote node,
    // and we receive something that's not a transaction, then we're done.
    if (currentFilteredBlock != null && !(m instanceof Transaction)) {
        endFilteredBlock(currentFilteredBlock);
        currentFilteredBlock = null;
    }

    // No further communication is possible until version handshake is complete.
    if (!(m instanceof VersionMessage || m instanceof VersionAck
            || (versionHandshakeFuture.isDone() && !versionHandshakeFuture.isCancelled())))
        throw new ProtocolException(
                "Received " + m.getClass().getSimpleName() + " before version handshake is complete.");

    if (m instanceof Ping) {
        processPing((Ping) m);
    } else if (m instanceof Pong) {
        processPong((Pong) m);
    } else if (m instanceof NotFoundMessage) {
        // This is sent to us when we did a getdata on some transactions that aren't in the peers memory pool.
        // Because NotFoundMessage is a subclass of InventoryMessage, the test for it must come before the next.
        processNotFoundMessage((NotFoundMessage) m);
    } else if (m instanceof InventoryMessage) {
        processInv((InventoryMessage) m);
    } else if (m instanceof Block) {
        processBlock((Block) m);
    } else if (m instanceof FilteredBlock) {
        startFilteredBlock((FilteredBlock) m);
    } else if (m instanceof Transaction) {
        processTransaction((Transaction) m);
    } else if (m instanceof GetDataMessage) {
        processGetData((GetDataMessage) m);
    } else if (m instanceof AddressMessage) {
        // We don't care about addresses of the network right now. But in future,
        // we should save them in the wallet so we don't put too much load on the seed nodes and can
        // properly explore the network.
        processAddressMessage((AddressMessage) m);
    } else if (m instanceof HeadersMessage) {
        processHeaders((HeadersMessage) m);
    } else if (m instanceof AlertMessage) {
        processAlert((AlertMessage) m);
    } else if (m instanceof VersionMessage) {
        processVersionMessage((VersionMessage) m);
    } else if (m instanceof VersionAck) {
        processVersionAck((VersionAck) m);
    } else if (m instanceof UTXOsMessage) {
        processUTXOMessage((UTXOsMessage) m);
    } else if (m instanceof RejectMessage) {
        log.error("{} {}: Received {}", this, getPeerVersionMessage().subVer, m);
    } else {
        log.warn("{}: Received unhandled message: {}", this, m);
    }
}
 
Example 7
Source File: Peer.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void processMessage(Message m) throws Exception {
    // Allow event listeners to filter the message stream. Listeners are allowed to drop messages by
    // returning null.
    for (ListenerRegistration<PreMessageReceivedEventListener> registration : preMessageReceivedEventListeners) {
        // Skip any listeners that are supposed to run in another thread as we don't want to block waiting
        // for it, which might cause circular deadlock.
        if (registration.executor == Threading.SAME_THREAD) {
            m = registration.listener.onPreMessageReceived(this, m);
            if (m == null) break;
        }
    }
    if (m == null) return;

    // If we are in the middle of receiving transactions as part of a filtered block push from the remote node,
    // and we receive something that's not a transaction, then we're done.
    if (currentFilteredBlock != null && !(m instanceof Transaction)) {
        endFilteredBlock(currentFilteredBlock);
        currentFilteredBlock = null;
    }

    // No further communication is possible until version handshake is complete.
    if (!(m instanceof VersionMessage || m instanceof VersionAck
            || (versionHandshakeFuture.isDone() && !versionHandshakeFuture.isCancelled())))
        throw new ProtocolException(
                "Received " + m.getClass().getSimpleName() + " before version handshake is complete.");

    if (m instanceof Ping) {
        processPing((Ping) m);
    } else if (m instanceof Pong) {
        processPong((Pong) m);
    } else if (m instanceof NotFoundMessage) {
        // This is sent to us when we did a getdata on some transactions that aren't in the peers memory pool.
        // Because NotFoundMessage is a subclass of InventoryMessage, the test for it must come before the next.
        processNotFoundMessage((NotFoundMessage) m);
    } else if (m instanceof InventoryMessage) {
        processInv((InventoryMessage) m);
    } else if (m instanceof Block) {
        processBlock((Block) m);
    } else if (m instanceof FilteredBlock) {
        startFilteredBlock((FilteredBlock) m);
    } else if (m instanceof Transaction) {
        processTransaction((Transaction) m);
    } else if (m instanceof GetDataMessage) {
        processGetData((GetDataMessage) m);
    } else if (m instanceof AddressMessage) {
        // We don't care about addresses of the network right now. But in future,
        // we should save them in the wallet so we don't put too much load on the seed nodes and can
        // properly explore the network.
        processAddressMessage((AddressMessage) m);
    } else if (m instanceof HeadersMessage) {
        processHeaders((HeadersMessage) m);
    } else if (m instanceof AlertMessage) {
        processAlert((AlertMessage) m);
    } else if (m instanceof VersionMessage) {
        processVersionMessage((VersionMessage) m);
    } else if (m instanceof VersionAck) {
        processVersionAck((VersionAck) m);
    } else if (m instanceof UTXOsMessage) {
        processUTXOMessage((UTXOsMessage) m);
    } else if (m instanceof RejectMessage) {
        log.error("{} {}: Received {}", this, getPeerVersionMessage().subVer, m);
    } else {
        log.warn("{}: Received unhandled message: {}", this, m);
    }
}
 
Example 8
Source File: Peer.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void processMessage(Message m) throws Exception {
    // Allow event listeners to filter the message stream. Listeners are allowed to drop messages by
    // returning null.
    for (ListenerRegistration<PreMessageReceivedEventListener> registration : preMessageReceivedEventListeners) {
        // Skip any listeners that are supposed to run in another thread as we don't want to block waiting
        // for it, which might cause circular deadlock.
        if (registration.executor == Threading.SAME_THREAD) {
            m = registration.listener.onPreMessageReceived(this, m);
            if (m == null) break;
        }
    }
    if (m == null) return;

    // If we are in the middle of receiving transactions as part of a filtered block push from the remote node,
    // and we receive something that's not a transaction, then we're done.
    if (currentFilteredBlock != null && !(m instanceof Transaction)) {
        endFilteredBlock(currentFilteredBlock);
        currentFilteredBlock = null;
    }

    // No further communication is possible until version handshake is complete.
    if (!(m instanceof VersionMessage || m instanceof VersionAck
            || (versionHandshakeFuture.isDone() && !versionHandshakeFuture.isCancelled())))
        throw new ProtocolException(
                "Received " + m.getClass().getSimpleName() + " before version handshake is complete.");

    if (m instanceof Ping) {
        processPing((Ping) m);
    } else if (m instanceof Pong) {
        processPong((Pong) m);
    } else if (m instanceof NotFoundMessage) {
        // This is sent to us when we did a getdata on some transactions that aren't in the peers memory pool.
        // Because NotFoundMessage is a subclass of InventoryMessage, the test for it must come before the next.
        processNotFoundMessage((NotFoundMessage) m);
    } else if (m instanceof InventoryMessage) {
        processInv((InventoryMessage) m);
    } else if (m instanceof Block) {
        processBlock((Block) m);
    } else if (m instanceof FilteredBlock) {
        startFilteredBlock((FilteredBlock) m);
    } else if (m instanceof Transaction) {
        processTransaction((Transaction) m);
    } else if (m instanceof GetDataMessage) {
        processGetData((GetDataMessage) m);
    } else if (m instanceof AddressMessage) {
        // We don't care about addresses of the network right now. But in future,
        // we should save them in the wallet so we don't put too much load on the seed nodes and can
        // properly explore the network.
        processAddressMessage((AddressMessage) m);
    } else if (m instanceof HeadersMessage) {
        processHeaders((HeadersMessage) m);
    } else if (m instanceof AlertMessage) {
        processAlert((AlertMessage) m);
    } else if (m instanceof VersionMessage) {
        processVersionMessage((VersionMessage) m);
    } else if (m instanceof VersionAck) {
        processVersionAck((VersionAck) m);
    } else if (m instanceof UTXOsMessage) {
        processUTXOMessage((UTXOsMessage) m);
    } else if (m instanceof RejectMessage) {
        log.error("{} {}: Received {}", this, getPeerVersionMessage().subVer, m);
    } else {
        log.warn("{}: Received unhandled message: {}", this, m);
    }
}