From f488c29c121efe6dc9c12a7843bcc17a7e3af59f Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Tue, 5 Feb 2013 15:14:34 +0100 Subject: [PATCH] Fix some races in PeerMonitor. --- .../com/google/bitcoin/examples/PeerMonitor.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java b/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java index 63f2b641..db85035a 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java +++ b/examples/src/main/java/com/google/bitcoin/examples/PeerMonitor.java @@ -22,6 +22,7 @@ import com.google.bitcoin.core.Peer; import com.google.bitcoin.core.PeerGroup; import com.google.bitcoin.discovery.DnsDiscovery; import com.google.bitcoin.utils.BriefLogFormatter; +import com.google.common.collect.Lists; import javax.swing.*; import javax.swing.event.ChangeEvent; @@ -139,8 +140,8 @@ public class PeerMonitor { public static final int PING_TIME = 4; public static final int LAST_PING_TIME = 5; - public List connectedPeers; - public List pendingPeers; + public List connectedPeers = Lists.newArrayList(); + public List pendingPeers = Lists.newArrayList(); public void updateFromPeerGroup() { connectedPeers = peerGroup.getConnectedPeers(); @@ -149,7 +150,7 @@ public class PeerMonitor { } public int getRowCount() { - return peerGroup.numConnectedPeers() + peerGroup.getPendingPeers().size(); + return connectedPeers.size() + pendingPeers.size(); } @Override @@ -183,12 +184,9 @@ public class PeerMonitor { } public Object getValueAt(int row, int col) { - // This is racy. A peer can be moving from pending to connected between these two lines. - List peers = peerGroup.getConnectedPeers(); - List pendingPeers = peerGroup.getPendingPeers(); - if (row >= peers.size()) { + if (row >= connectedPeers.size()) { // Peer that isn't connected yet. - Peer peer = pendingPeers.get(row - peers.size()); + Peer peer = pendingPeers.get(row - connectedPeers.size()); switch (col) { case IP_ADDRESS: return peer.getAddress().getAddr().getHostAddress(); @@ -202,7 +200,7 @@ public class PeerMonitor { return "(pending)"; } } - Peer peer = peers.get(row); + Peer peer = connectedPeers.get(row); switch (col) { case IP_ADDRESS: return peer.getAddress().getAddr().getHostAddress();