diff --git a/src/com/google/bitcoin/core/AbstractPeerEventListener.java b/src/com/google/bitcoin/core/AbstractPeerEventListener.java index 80ac6748..c0608f21 100644 --- a/src/com/google/bitcoin/core/AbstractPeerEventListener.java +++ b/src/com/google/bitcoin/core/AbstractPeerEventListener.java @@ -30,4 +30,10 @@ public class AbstractPeerEventListener extends Object implements PeerEventListen public void onChainDownloadStarted(Peer peer, int blocksLeft) { } + + public void onPeerConnected(Peer peer, int peerCount) { + } + + public void onPeerDisconnected(Peer peer, int peerCount) { + } } diff --git a/src/com/google/bitcoin/core/PeerEventListener.java b/src/com/google/bitcoin/core/PeerEventListener.java index 7f6ecc87..91126c4b 100644 --- a/src/com/google/bitcoin/core/PeerEventListener.java +++ b/src/com/google/bitcoin/core/PeerEventListener.java @@ -45,4 +45,20 @@ public interface PeerEventListener { * @param blocksLeft the number of blocks left to download */ public void onChainDownloadStarted(Peer peer, int blocksLeft); + + /** + * Called when a peer is connected + * + * @param peer + * @param peerCount the total number of connected peers + */ + public void onPeerConnected(Peer peer, int peerCount); + + /** + * Called when a peer is disconnected + * + * @param peer + * @param peerCount the total number of connected peers + */ + public void onPeerDisconnected(Peer peer, int peerCount); } diff --git a/src/com/google/bitcoin/core/PeerGroup.java b/src/com/google/bitcoin/core/PeerGroup.java index 5e27e702..e0449859 100644 --- a/src/com/google/bitcoin/core/PeerGroup.java +++ b/src/com/google/bitcoin/core/PeerGroup.java @@ -78,6 +78,9 @@ public class PeerGroup { // Callback for events related to chain download private PeerEventListener downloadListener; + // Callbacks for events related to peer connection/disconnection + private Set peerEventListeners; + private NetworkParameters params; private BlockStore blockStore; private BlockChain chain; @@ -93,12 +96,27 @@ public class PeerGroup { inactives = new LinkedBlockingQueue(); peers = Collections.synchronizedSet(new HashSet()); + + peerEventListeners = Collections.synchronizedSet(new HashSet()); + peerPool = new ThreadPoolExecutor(CORE_THREADS, DEFAULT_CONNECTIONS, THREAD_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, new LinkedBlockingQueue(1), new PeerGroupThreadFactory()); } + /** + * Callbacks to the listener are performed in the connection thread. The callback + * should not perform time consuming tasks. + */ + public void addEventListener(PeerEventListener listener) { + peerEventListeners.add(listener); + } + + public boolean removeEventListener(PeerEventListener listener) { + return peerEventListeners.remove(listener); + } + /** * Depending on the environment, this should normally be between 1 and 10, default is 4. * @@ -305,6 +323,13 @@ public class PeerGroup { protected synchronized void handleNewPeer(Peer peer) { if (downloadListener != null && downloadPeer == null) startBlockChainDownloadFromPeer(peer); + synchronized (peerEventListeners) { + for (PeerEventListener listener : peerEventListeners) { + synchronized (listener) { + listener.onPeerConnected(peer, peers.size()); + } + } + } } protected synchronized void handlePeerDeath(Peer peer) { @@ -316,6 +341,14 @@ public class PeerGroup { } } } + + synchronized (peerEventListeners) { + for (PeerEventListener listener : peerEventListeners) { + synchronized (listener) { + listener.onPeerDisconnected(peer, peers.size()); + } + } + } } private synchronized void startBlockChainDownloadFromPeer(Peer peer) { diff --git a/tests/com/google/bitcoin/core/PeerGroupTest.java b/tests/com/google/bitcoin/core/PeerGroupTest.java new file mode 100644 index 00000000..2b251222 --- /dev/null +++ b/tests/com/google/bitcoin/core/PeerGroupTest.java @@ -0,0 +1,49 @@ +/** + * Copyright 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.bitcoin.core; + +import static org.junit.Assert.assertTrue; + +import com.google.bitcoin.store.BlockStore; +import com.google.bitcoin.store.MemoryBlockStore; + +import org.junit.Before; +import org.junit.Test; + +public class PeerGroupTest { + static final NetworkParameters params = NetworkParameters.unitTests(); + + private Wallet wallet; + private BlockStore blockStore; + private PeerGroup peerGroup; + + @Before + public void setUp() throws Exception { + wallet = new Wallet(params); + blockStore = new MemoryBlockStore(params); + BlockChain chain = new BlockChain(params, wallet, blockStore); + peerGroup = new PeerGroup(blockStore, params, chain); + } + + @Test + public void listener() throws Exception { + AbstractPeerEventListener listener = new AbstractPeerEventListener() { + }; + peerGroup.addEventListener(listener); + assertTrue(peerGroup.removeEventListener(listener)); + } +}