mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-14 11:15:51 +00:00
PeerGroup connect/disconnect callback
This commit is contained in:
parent
1cf1147c87
commit
be531960d6
@ -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) {
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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<PeerEventListener> peerEventListeners;
|
||||
|
||||
private NetworkParameters params;
|
||||
private BlockStore blockStore;
|
||||
private BlockChain chain;
|
||||
@ -93,12 +96,27 @@ public class PeerGroup {
|
||||
inactives = new LinkedBlockingQueue<PeerAddress>();
|
||||
|
||||
peers = Collections.synchronizedSet(new HashSet<Peer>());
|
||||
|
||||
peerEventListeners = Collections.synchronizedSet(new HashSet<PeerEventListener>());
|
||||
|
||||
peerPool = new ThreadPoolExecutor(CORE_THREADS, DEFAULT_CONNECTIONS,
|
||||
THREAD_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<Runnable>(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) {
|
||||
|
49
tests/com/google/bitcoin/core/PeerGroupTest.java
Normal file
49
tests/com/google/bitcoin/core/PeerGroupTest.java
Normal file
@ -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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user