Java Code Examples for org.bitcoinj.utils.ListenerRegistration

The following examples show how to use org.bitcoinj.utils.ListenerRegistration. These examples are extracted from open source projects. 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 Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void versionHandshakeComplete() {
    log.debug("{}: Handshake complete.", this);
    setTimeoutEnabled(false);
    for (final ListenerRegistration<PeerConnectedEventListener> registration : connectedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onPeerConnected(Peer.this, 1);
            }
        });
    }
    // We check min version after onPeerConnected as channel.close() will
    // call onPeerDisconnected, and we should probably call onPeerConnected first.
    final int version = vMinProtocolVersion;
    if (vPeerVersionMessage.clientVersion < version) {
        log.warn("Connected to a peer speaking protocol version {} but need {}, closing",
                vPeerVersionMessage.clientVersion, version);
        close();
    }
}
 
Example 2
Source Project: bcm-android   Source File: Peer.java    License: 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 Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Starts an asynchronous download of the block chain. The chain download is deemed to be complete once we've
 * downloaded the same number of blocks that the peer advertised having in its version handshake message.
 */
public void startBlockChainDownload() {
    setDownloadData(true);
    // TODO: peer might still have blocks that we don't have, and even have a heavier
    // chain even if the chain block count is lower.
    final int blocksLeft = getPeerBlockHeightDifference();
    if (blocksLeft >= 0) {
        for (final ListenerRegistration<ChainDownloadStartedEventListener> registration : chainDownloadStartedEventListeners) {
            registration.executor.execute(new Runnable() {
                @Override
                public void run() {
                    registration.listener.onChainDownloadStarted(Peer.this, blocksLeft);
                }
            });
        }
        // When we just want as many blocks as possible, we can set the target hash to zero.
        lock.lock();
        try {
            blockChainDownloadLocked(Sha256Hash.ZERO_HASH);
        } finally {
            lock.unlock();
        }
    }
}
 
Example 4
Source Project: green_android   Source File: Peer.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void versionHandshakeComplete() {
    log.debug("{}: Handshake complete.", this);
    setTimeoutEnabled(false);
    for (final ListenerRegistration<PeerConnectedEventListener> registration : connectedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onPeerConnected(Peer.this, 1);
            }
        });
    }
    // We check min version after onPeerConnected as channel.close() will
    // call onPeerDisconnected, and we should probably call onPeerConnected first.
    final int version = vMinProtocolVersion;
    if (vPeerVersionMessage.clientVersion < version) {
        log.warn("Connected to a peer speaking protocol version {} but need {}, closing",
                vPeerVersionMessage.clientVersion, version);
        close();
    }
}
 
Example 5
Source Project: green_android   Source File: Peer.java    License: 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 6
Source Project: green_android   Source File: Peer.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Starts an asynchronous download of the block chain. The chain download is deemed to be complete once we've
 * downloaded the same number of blocks that the peer advertised having in its version handshake message.
 */
public void startBlockChainDownload() {
    setDownloadData(true);
    // TODO: peer might still have blocks that we don't have, and even have a heavier
    // chain even if the chain block count is lower.
    final int blocksLeft = getPeerBlockHeightDifference();
    if (blocksLeft >= 0) {
        for (final ListenerRegistration<ChainDownloadStartedEventListener> registration : chainDownloadStartedEventListeners) {
            registration.executor.execute(new Runnable() {
                @Override
                public void run() {
                    registration.listener.onChainDownloadStarted(Peer.this, blocksLeft);
                }
            });
        }
        // When we just want as many blocks as possible, we can set the target hash to zero.
        lock.lock();
        try {
            blockChainDownloadLocked(Sha256Hash.ZERO_HASH);
        } finally {
            lock.unlock();
        }
    }
}
 
Example 7
Source Project: java-stratum   Source File: ElectrumMultiWallet.java    License: 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 8
Source Project: GreenBits   Source File: Peer.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void versionHandshakeComplete() {
    log.debug("{}: Handshake complete.", this);
    setTimeoutEnabled(false);
    for (final ListenerRegistration<PeerConnectedEventListener> registration : connectedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onPeerConnected(Peer.this, 1);
            }
        });
    }
    // We check min version after onPeerConnected as channel.close() will
    // call onPeerDisconnected, and we should probably call onPeerConnected first.
    final int version = vMinProtocolVersion;
    if (vPeerVersionMessage.clientVersion < version) {
        log.warn("Connected to a peer speaking protocol version {} but need {}, closing",
                vPeerVersionMessage.clientVersion, version);
        close();
    }
}
 
Example 9
Source Project: GreenBits   Source File: Peer.java    License: 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 10
Source Project: GreenBits   Source File: Peer.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Starts an asynchronous download of the block chain. The chain download is deemed to be complete once we've
 * downloaded the same number of blocks that the peer advertised having in its version handshake message.
 */
public void startBlockChainDownload() {
    setDownloadData(true);
    // TODO: peer might still have blocks that we don't have, and even have a heavier
    // chain even if the chain block count is lower.
    final int blocksLeft = getPeerBlockHeightDifference();
    if (blocksLeft >= 0) {
        for (final ListenerRegistration<ChainDownloadStartedEventListener> registration : chainDownloadStartedEventListeners) {
            registration.executor.execute(new Runnable() {
                @Override
                public void run() {
                    registration.listener.onChainDownloadStarted(Peer.this, blocksLeft);
                }
            });
        }
        // When we just want as many blocks as possible, we can set the target hash to zero.
        lock.lock();
        try {
            blockChainDownloadLocked(Sha256Hash.ZERO_HASH);
        } finally {
            lock.unlock();
        }
    }
}
 
Example 11
Source Project: bcm-android   Source File: BasicKeyChain.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void queueOnKeysAdded(final List<ECKey> keys) {
    checkState(lock.isHeldByCurrentThread());
    for (final ListenerRegistration<KeyChainEventListener> registration : listeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onKeysAdded(keys);
            }
        });
    }
}
 
Example 12
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void connectionClosed() {
    for (final ListenerRegistration<PeerDisconnectedEventListener> registration : disconnectedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onPeerDisconnected(Peer.this, 0);
            }
        });
    }
}
 
Example 13
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void invokeOnBlocksDownloaded(final Block block, @Nullable final FilteredBlock fb) {
    // It is possible for the peer block height difference to be negative when blocks have been solved and broadcast
    // since the time we first connected to the peer. However, it's weird and unexpected to receive a callback
    // with negative "blocks left" in this case, so we clamp to zero so the API user doesn't have to think about it.
    final int blocksLeft = Math.max(0, (int) vPeerVersionMessage.bestHeight - checkNotNull(blockChain).getBestChainHeight());
    for (final ListenerRegistration<BlocksDownloadedEventListener> registration : blocksDownloadedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onBlocksDownloaded(Peer.this, block, fb, blocksLeft);
            }
        });
    }
}
 
Example 14
Source Project: green_android   Source File: BasicKeyChain.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void queueOnKeysAdded(final List<ECKey> keys) {
    checkState(lock.isHeldByCurrentThread());
    for (final ListenerRegistration<KeyChainEventListener> registration : listeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onKeysAdded(keys);
            }
        });
    }
}
 
Example 15
Source Project: green_android   Source File: Peer.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void connectionClosed() {
    for (final ListenerRegistration<PeerDisconnectedEventListener> registration : disconnectedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onPeerDisconnected(Peer.this, 0);
            }
        });
    }
}
 
Example 16
Source Project: green_android   Source File: Peer.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void invokeOnBlocksDownloaded(final Block block, @Nullable final FilteredBlock fb) {
    // It is possible for the peer block height difference to be negative when blocks have been solved and broadcast
    // since the time we first connected to the peer. However, it's weird and unexpected to receive a callback
    // with negative "blocks left" in this case, so we clamp to zero so the API user doesn't have to think about it.
    final int blocksLeft = Math.max(0, (int) vPeerVersionMessage.bestHeight - checkNotNull(blockChain).getBestChainHeight());
    for (final ListenerRegistration<BlocksDownloadedEventListener> registration : blocksDownloadedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onBlocksDownloaded(Peer.this, block, fb, blocksLeft);
            }
        });
    }
}
 
Example 17
Source Project: java-stratum   Source File: ElectrumMultiWallet.java    License: 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 18
Source Project: GreenBits   Source File: BasicKeyChain.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void queueOnKeysAdded(final List<ECKey> keys) {
    checkState(lock.isHeldByCurrentThread());
    for (final ListenerRegistration<KeyChainEventListener> registration : listeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onKeysAdded(keys);
            }
        });
    }
}
 
Example 19
Source Project: GreenBits   Source File: Peer.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void connectionClosed() {
    for (final ListenerRegistration<PeerDisconnectedEventListener> registration : disconnectedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onPeerDisconnected(Peer.this, 0);
            }
        });
    }
}
 
Example 20
Source Project: GreenBits   Source File: Peer.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void invokeOnBlocksDownloaded(final Block block, @Nullable final FilteredBlock fb) {
    // It is possible for the peer block height difference to be negative when blocks have been solved and broadcast
    // since the time we first connected to the peer. However, it's weird and unexpected to receive a callback
    // with negative "blocks left" in this case, so we clamp to zero so the API user doesn't have to think about it.
    final int blocksLeft = Math.max(0, (int) vPeerVersionMessage.bestHeight - checkNotNull(blockChain).getBestChainHeight());
    for (final ListenerRegistration<BlocksDownloadedEventListener> registration : blocksDownloadedEventListeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onBlocksDownloaded(Peer.this, block, fb, blocksLeft);
            }
        });
    }
}
 
Example 21
Source Project: bcm-android   Source File: BasicKeyChain.java    License: GNU General Public License v3.0 4 votes vote down vote up
public List<ListenerRegistration<KeyChainEventListener>> getListeners() {
    return new ArrayList<>(listeners);
}
 
Example 22
Source Project: bcm-android   Source File: BasicKeyChain.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public void addEventListener(KeyChainEventListener listener, Executor executor) {
    listeners.add(new ListenerRegistration<>(listener, executor));
}
 
Example 23
Source Project: bcm-android   Source File: BasicKeyChain.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean removeEventListener(KeyChainEventListener listener) {
    return ListenerRegistration.removeFromList(listener, listeners);
}
 
Example 24
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers a listener that is invoked when new blocks are downloaded.
 */
public void addBlocksDownloadedEventListener(Executor executor, BlocksDownloadedEventListener listener) {
    blocksDownloadedEventListeners.add(new ListenerRegistration(listener, executor));
}
 
Example 25
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers a listener that is invoked when a blockchain downloaded starts.
 */
public void addChainDownloadStartedEventListener(Executor executor, ChainDownloadStartedEventListener listener) {
    chainDownloadStartedEventListeners.add(new ListenerRegistration(listener, executor));
}
 
Example 26
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers a listener that is invoked when a peer is connected.
 */
public void addConnectedEventListener(Executor executor, PeerConnectedEventListener listener) {
    connectedEventListeners.add(new ListenerRegistration(listener, executor));
}
 
Example 27
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers a listener that is invoked when a peer is disconnected.
 */
public void addDisconnectedEventListener(Executor executor, PeerDisconnectedEventListener listener) {
    disconnectedEventListeners.add(new ListenerRegistration(listener, executor));
}
 
Example 28
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers a listener that is called when messages are received.
 */
public void addGetDataEventListener(Executor executor, GetDataEventListener listener) {
    getDataEventListeners.add(new ListenerRegistration<>(listener, executor));
}
 
Example 29
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers a listener that is called when a transaction is broadcast across the network
 */
public void addOnTransactionBroadcastListener(Executor executor, OnTransactionBroadcastListener listener) {
    onTransactionEventListeners.add(new ListenerRegistration<>(listener, executor));
}
 
Example 30
Source Project: bcm-android   Source File: Peer.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers a listener that is called immediately before a message is received
 */
public void addPreMessageReceivedEventListener(Executor executor, PreMessageReceivedEventListener listener) {
    preMessageReceivedEventListeners.add(new ListenerRegistration<>(listener, executor));
}