From a50bf41803e95bca0f7ba1d73d1f70c3fa901534 Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Fri, 6 May 2016 09:00:21 +0200 Subject: [PATCH 1/7] Remove unnecessary class reference --- network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java index 9649fc4b56..3f856f8588 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java @@ -346,7 +346,7 @@ public class PeerManager implements ConnectionListener { printNewReportedPeers(reportedPeersToAdd); // We check if the reported msg is not violating our rules - if (reportedPeersToAdd.size() <= (MAX_REPORTED_PEERS + PeerManager.MAX_CONNECTIONS_ABSOLUTE + 10)) { + if (reportedPeersToAdd.size() <= (MAX_REPORTED_PEERS + MAX_CONNECTIONS_ABSOLUTE + 10)) { reportedPeers.addAll(reportedPeersToAdd); purgeReportedPeersIfExceeds(); From f0aec0d55ced352b54d69f23f1634d51a2d25afa Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Fri, 6 May 2016 09:39:47 +0200 Subject: [PATCH 2/7] Add internal ``P2PService`` constructor with (unused) maximum connections argument The value of ``P2PService.MAX_CONNECTIONS_DEFAULT`` is taken from the static block invocation of ``PeerManager.setMaxConnections()``. --- .../java/io/bitsquare/p2p/P2PService.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/network/src/main/java/io/bitsquare/p2p/P2PService.java b/network/src/main/java/io/bitsquare/p2p/P2PService.java index 48268effeb..990ccf3c4f 100644 --- a/network/src/main/java/io/bitsquare/p2p/P2PService.java +++ b/network/src/main/java/io/bitsquare/p2p/P2PService.java @@ -55,9 +55,11 @@ import static com.google.common.base.Preconditions.checkNotNull; public class P2PService implements SetupListener, MessageListener, ConnectionListener, RequestDataManager.Listener, HashMapChangedListener { private static final Logger log = LoggerFactory.getLogger(P2PService.class); + public static final int MAX_CONNECTIONS_DEFAULT = 12; private final SeedNodesRepository seedNodesRepository; private final int port; + private final int maxConnections; private final File torDir; private Clock clock; private final Optional optionalEncryptionService; @@ -104,8 +106,32 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis Clock clock, @Nullable EncryptionService encryptionService, @Nullable KeyRing keyRing) { + this( + seedNodesRepository, + port, MAX_CONNECTIONS_DEFAULT, + torDir, + useLocalhost, + networkId, + storageDir, + clock, + encryptionService, + keyRing + ); + } + + @VisibleForTesting + public P2PService(SeedNodesRepository seedNodesRepository, + int port, int maxConnections, + File torDir, + boolean useLocalhost, + int networkId, + File storageDir, + Clock clock, + @Nullable EncryptionService encryptionService, + @Nullable KeyRing keyRing) { this.seedNodesRepository = seedNodesRepository; this.port = port; + this.maxConnections = maxConnections; this.torDir = torDir; this.clock = clock; From dc90a950cbbef95a58d25cc2ceaedf2d06ae6b5b Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Fri, 6 May 2016 10:07:28 +0200 Subject: [PATCH 3/7] Pass max connections to ``SeedNode.createAndStartP2PService()`` Still keep calls to ``PeerManager.setMaxConnections()`` until it is retired. --- .../main/java/io/bitsquare/p2p/seed/SeedNode.java | 13 +++++++------ .../test/java/io/bitsquare/p2p/PeerServiceTest.java | 11 ++++++----- .../src/test/java/io/bitsquare/p2p/TestUtils.java | 2 +- .../io/bitsquare/p2p/routing/PeerManagerTest.java | 11 ++++++----- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java b/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java index 389fa7fb91..1f081dea08 100644 --- a/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java +++ b/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java @@ -30,6 +30,7 @@ public class SeedNode { public static final int MAX_CONNECTIONS_DEFAULT = 50; private NodeAddress mySeedNodeAddress = new NodeAddress("localhost:8001"); + private int maxConnections = MAX_CONNECTIONS_DEFAULT; // we keep default a higher connection size for seed nodes private boolean useLocalhost = false; private Set progArgSeedNodes; private P2PService seedNodeP2PService; @@ -67,13 +68,10 @@ public class SeedNode { Version.setBtcNetworkId(networkId); if (args.length > 2) { String arg2 = args[2]; - int maxConnections = Integer.parseInt(arg2); + maxConnections = Integer.parseInt(arg2); log.info("From processArgs: maxConnections=" + maxConnections); checkArgument(maxConnections < MAX_CONNECTIONS_LIMIT, "maxConnections seems to be a bit too high..."); PeerManager.setMaxConnections(maxConnections); - } else { - // we keep default a higher connection size for seed nodes - PeerManager.setMaxConnections(MAX_CONNECTIONS_DEFAULT); } if (args.length > 3) { String arg3 = args[3]; @@ -107,11 +105,13 @@ public class SeedNode { } public void createAndStartP2PService(boolean useDetailedLogging) { - createAndStartP2PService(mySeedNodeAddress, useLocalhost, Version.getBtcNetworkId(), useDetailedLogging, progArgSeedNodes, null); + createAndStartP2PService(mySeedNodeAddress, maxConnections, useLocalhost, + Version.getBtcNetworkId(), useDetailedLogging, progArgSeedNodes, null); } @VisibleForTesting public void createAndStartP2PService(NodeAddress mySeedNodeAddress, + int maxConnections, boolean useLocalhost, int networkId, boolean useDetailedLogging, @@ -144,7 +144,8 @@ public class SeedNode { log.info("Created torDir at " + torDir.getAbsolutePath()); seedNodesRepository.setNodeAddressToExclude(mySeedNodeAddress); - seedNodeP2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, torDir, useLocalhost, networkId, storageDir, new Clock(), null, null); + seedNodeP2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, maxConnections, + torDir, useLocalhost, networkId, storageDir, new Clock(), null, null); seedNodeP2PService.start(listener); } diff --git a/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java b/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java index 8723561751..068295185c 100644 --- a/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java +++ b/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java @@ -23,6 +23,7 @@ import java.util.concurrent.CountDownLatch; // Run it once then lookup for onion address at: tor/hiddenservice/hostname and use that for the NodeAddress param. public class PeerServiceTest { private static final Logger log = LoggerFactory.getLogger(PeerServiceTest.class); + public static final int MAX_CONNECTIONS = 100; final boolean useLocalhost = true; private CountDownLatch latch; @@ -37,7 +38,7 @@ public class PeerServiceTest { public void setup() throws InterruptedException { LocalhostNetworkNode.setSimulateTorDelayTorNode(50); LocalhostNetworkNode.setSimulateTorDelayHiddenService(8); - PeerManager.setMaxConnections(100); + PeerManager.setMaxConnections(MAX_CONNECTIONS); if (useLocalhost) { seedNodeAddresses.add(new NodeAddress("localhost:8001")); @@ -127,7 +128,7 @@ public class PeerServiceTest { /* latch = new CountDownLatch(2); - seedNode.createAndStartP2PService(nodeAddress, useLocalhost, 2, true, + seedNode.createAndStartP2PService(nodeAddress, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodeAddresses, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { @@ -181,7 +182,7 @@ public class PeerServiceTest { latch = new CountDownLatch(6); seedNode1 = new SeedNode("test_dummy_dir"); - seedNode1.createAndStartP2PService(nodeAddress1, useLocalhost, 2, true, seedNodeAddresses, new P2PServiceListener() { + seedNode1.createAndStartP2PService(nodeAddress1, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodeAddresses, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { latch.countDown(); @@ -219,7 +220,7 @@ public class PeerServiceTest { Thread.sleep(500); seedNode2 = new SeedNode("test_dummy_dir"); - seedNode2.createAndStartP2PService(nodeAddress2, useLocalhost, 2, true, seedNodeAddresses, new P2PServiceListener() { + seedNode2.createAndStartP2PService(nodeAddress2, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodeAddresses, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { latch.countDown(); @@ -456,7 +457,7 @@ public class PeerServiceTest { SeedNode seedNode = new SeedNode("test_dummy_dir"); latch = new CountDownLatch(1); - seedNode.createAndStartP2PService(new NodeAddress("localhost", port), useLocalhost, 2, true, seedNodeAddresses, new P2PServiceListener() { + seedNode.createAndStartP2PService(new NodeAddress("localhost", port), MAX_CONNECTIONS, useLocalhost, 2, true, seedNodeAddresses, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { latch.countDown(); diff --git a/network/src/test/java/io/bitsquare/p2p/TestUtils.java b/network/src/test/java/io/bitsquare/p2p/TestUtils.java index b5b40cdd95..25a6fdcc01 100644 --- a/network/src/test/java/io/bitsquare/p2p/TestUtils.java +++ b/network/src/test/java/io/bitsquare/p2p/TestUtils.java @@ -82,7 +82,7 @@ public class TestUtils { } CountDownLatch latch = new CountDownLatch(1); - seedNode.createAndStartP2PService(new NodeAddress("localhost", port), useLocalhost, 2, true, + seedNode.createAndStartP2PService(new NodeAddress("localhost", port), SeedNode.MAX_CONNECTIONS_DEFAULT, useLocalhost, 2, true, seedNodes, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { diff --git a/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java b/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java index 96ec47e6ca..aefde4cd6f 100644 --- a/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java +++ b/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java @@ -26,6 +26,7 @@ import java.util.concurrent.CountDownLatch; @Ignore public class PeerManagerTest { private static final Logger log = LoggerFactory.getLogger(PeerManagerTest.class); + public static final int MAX_CONNECTIONS = 100; final boolean useLocalhost = true; private CountDownLatch latch; @@ -37,7 +38,7 @@ public class PeerManagerTest { public void setup() throws InterruptedException { LocalhostNetworkNode.setSimulateTorDelayTorNode(50); LocalhostNetworkNode.setSimulateTorDelayHiddenService(8); - PeerManager.setMaxConnections(100); + PeerManager.setMaxConnections(MAX_CONNECTIONS); seedNodes = new HashSet<>(); if (useLocalhost) { @@ -84,7 +85,7 @@ public class PeerManagerTest { seedNodes.add(nodeAddress); seedNode1 = new SeedNode("test_dummy_dir"); latch = new CountDownLatch(2); - seedNode1.createAndStartP2PService(nodeAddress, useLocalhost, 2, true, + seedNode1.createAndStartP2PService(nodeAddress, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodes, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { @@ -136,7 +137,7 @@ public class PeerManagerTest { latch = new CountDownLatch(6); seedNode1 = new SeedNode("test_dummy_dir"); - seedNode1.createAndStartP2PService(nodeAddress1, useLocalhost, 2, true, seedNodes, new P2PServiceListener() { + seedNode1.createAndStartP2PService(nodeAddress1, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodes, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { latch.countDown(); @@ -174,7 +175,7 @@ public class PeerManagerTest { Thread.sleep(500); seedNode2 = new SeedNode("test_dummy_dir"); - seedNode2.createAndStartP2PService(nodeAddress2, useLocalhost, 2, true, seedNodes, new P2PServiceListener() { + seedNode2.createAndStartP2PService(nodeAddress2, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodes, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { latch.countDown(); @@ -411,7 +412,7 @@ public class PeerManagerTest { SeedNode seedNode = new SeedNode("test_dummy_dir"); latch = new CountDownLatch(1); - seedNode.createAndStartP2PService(new NodeAddress("localhost", port), useLocalhost, 2, true, seedNodes, new P2PServiceListener() { + seedNode.createAndStartP2PService(new NodeAddress("localhost", port), MAX_CONNECTIONS, useLocalhost, 2, true, seedNodes, new P2PServiceListener() { @Override public void onRequestingDataCompleted() { latch.countDown(); From fd37b8999b97eebfea5316ff575be14a9d7d8b7b Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Fri, 6 May 2016 10:25:00 +0200 Subject: [PATCH 4/7] Do pass max connections to ``PeerManager``, new constructor signature Remove calls to now instance private ``PeerManager.setMaxConnections()``. Static fields regarding connection limits are now private instance fields. --- .../java/io/bitsquare/p2p/P2PService.java | 2 +- .../io/bitsquare/p2p/peers/PeerManager.java | 20 +++++++++---------- .../java/io/bitsquare/p2p/seed/SeedNode.java | 1 - .../io/bitsquare/p2p/PeerServiceTest.java | 1 - .../p2p/routing/PeerManagerTest.java | 1 - 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/P2PService.java b/network/src/main/java/io/bitsquare/p2p/P2PService.java index 990ccf3c4f..45e63c82b1 100644 --- a/network/src/main/java/io/bitsquare/p2p/P2PService.java +++ b/network/src/main/java/io/bitsquare/p2p/P2PService.java @@ -150,7 +150,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis networkNode.addMessageListener(this); Set seedNodeAddresses = seedNodesRepository.getSeedNodeAddresses(useLocalhost, networkId); - peerManager = new PeerManager(networkNode, seedNodeAddresses, storageDir, clock); + peerManager = new PeerManager(networkNode, maxConnections, seedNodeAddresses, storageDir, clock); broadcaster = new Broadcaster(networkNode, peerManager); diff --git a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java index 3f856f8588..bf3a066280 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java @@ -28,17 +28,17 @@ public class PeerManager implements ConnectionListener { // Use a long delay as the bootstrapping peer might need a while until it knows its onion address private static final long REMOVE_ANONYMOUS_PEER_SEC = Timer.STRESS_TEST ? 10 : 120; - private static int MAX_CONNECTIONS; - private static int MIN_CONNECTIONS; - private static int MAX_CONNECTIONS_PEER; - private static int MAX_CONNECTIONS_NON_DIRECT; + private int MAX_CONNECTIONS; + private int MIN_CONNECTIONS; + private int MAX_CONNECTIONS_PEER; + private int MAX_CONNECTIONS_NON_DIRECT; - private static int MAX_CONNECTIONS_ABSOLUTE; + private int MAX_CONNECTIONS_ABSOLUTE; private final boolean printReportedPeersDetails = true; private boolean lostAllConnections; - public static void setMaxConnections(int maxConnections) { + private void setMaxConnections(int maxConnections) { MAX_CONNECTIONS = maxConnections; MIN_CONNECTIONS = Math.max(1, maxConnections - 4); MAX_CONNECTIONS_PEER = MAX_CONNECTIONS + 4; @@ -46,10 +46,6 @@ public class PeerManager implements ConnectionListener { MAX_CONNECTIONS_ABSOLUTE = MAX_CONNECTIONS + 18; } - static { - setMaxConnections(12); - } - private static final int MAX_REPORTED_PEERS = 1000; private static final int MAX_PERSISTED_PEERS = 500; private static final long MAX_AGE = TimeUnit.DAYS.toMillis(14); // max age for reported peers is 14 days @@ -90,8 +86,10 @@ public class PeerManager implements ConnectionListener { // Constructor /////////////////////////////////////////////////////////////////////////////////////////// - public PeerManager(NetworkNode networkNode, Set seedNodeAddresses, File storageDir, Clock clock) { + public PeerManager(NetworkNode networkNode, int maxConnections, Set seedNodeAddresses, + File storageDir, Clock clock) { this.networkNode = networkNode; + setMaxConnections(maxConnections); this.clock = clock; // seedNodeAddresses can be empty (in case there is only 1 seed node, the seed node starting up has no other seed nodes) this.seedNodeAddresses = new HashSet<>(seedNodeAddresses); diff --git a/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java b/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java index 1f081dea08..300b46482c 100644 --- a/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java +++ b/network/src/main/java/io/bitsquare/p2p/seed/SeedNode.java @@ -71,7 +71,6 @@ public class SeedNode { maxConnections = Integer.parseInt(arg2); log.info("From processArgs: maxConnections=" + maxConnections); checkArgument(maxConnections < MAX_CONNECTIONS_LIMIT, "maxConnections seems to be a bit too high..."); - PeerManager.setMaxConnections(maxConnections); } if (args.length > 3) { String arg3 = args[3]; diff --git a/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java b/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java index 068295185c..06e600691c 100644 --- a/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java +++ b/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java @@ -38,7 +38,6 @@ public class PeerServiceTest { public void setup() throws InterruptedException { LocalhostNetworkNode.setSimulateTorDelayTorNode(50); LocalhostNetworkNode.setSimulateTorDelayHiddenService(8); - PeerManager.setMaxConnections(MAX_CONNECTIONS); if (useLocalhost) { seedNodeAddresses.add(new NodeAddress("localhost:8001")); diff --git a/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java b/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java index aefde4cd6f..c790d7814c 100644 --- a/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java +++ b/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java @@ -38,7 +38,6 @@ public class PeerManagerTest { public void setup() throws InterruptedException { LocalhostNetworkNode.setSimulateTorDelayTorNode(50); LocalhostNetworkNode.setSimulateTorDelayHiddenService(8); - PeerManager.setMaxConnections(MAX_CONNECTIONS); seedNodes = new HashSet<>(); if (useLocalhost) { From f0b0b69b24b25281ff04ab10ea18aa3262b2bbb4 Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Fri, 6 May 2016 10:26:29 +0200 Subject: [PATCH 5/7] Change access of max connections constant to private in tests --- network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java | 2 +- .../src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java b/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java index 06e600691c..fb0fe75710 100644 --- a/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java +++ b/network/src/test/java/io/bitsquare/p2p/PeerServiceTest.java @@ -23,7 +23,7 @@ import java.util.concurrent.CountDownLatch; // Run it once then lookup for onion address at: tor/hiddenservice/hostname and use that for the NodeAddress param. public class PeerServiceTest { private static final Logger log = LoggerFactory.getLogger(PeerServiceTest.class); - public static final int MAX_CONNECTIONS = 100; + private static final int MAX_CONNECTIONS = 100; final boolean useLocalhost = true; private CountDownLatch latch; diff --git a/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java b/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java index c790d7814c..f157d7d259 100644 --- a/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java +++ b/network/src/test/java/io/bitsquare/p2p/routing/PeerManagerTest.java @@ -26,7 +26,7 @@ import java.util.concurrent.CountDownLatch; @Ignore public class PeerManagerTest { private static final Logger log = LoggerFactory.getLogger(PeerManagerTest.class); - public static final int MAX_CONNECTIONS = 100; + private static final int MAX_CONNECTIONS = 100; final boolean useLocalhost = true; private CountDownLatch latch; From 3aac3a3865639195e724ab51b7662cc596ee82ab Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Fri, 6 May 2016 10:48:59 +0200 Subject: [PATCH 6/7] Rearrange connection limit lines in ``PeerManager`` --- .../io/bitsquare/p2p/peers/PeerManager.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java index bf3a066280..d0b6009692 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java @@ -28,16 +28,20 @@ public class PeerManager implements ConnectionListener { // Use a long delay as the bootstrapping peer might need a while until it knows its onion address private static final long REMOVE_ANONYMOUS_PEER_SEC = Timer.STRESS_TEST ? 10 : 120; + private static final int MAX_REPORTED_PEERS = 1000; + private static final int MAX_PERSISTED_PEERS = 500; + private static final long MAX_AGE = TimeUnit.DAYS.toMillis(14); // max age for reported peers is 14 days + + private final boolean printReportedPeersDetails = true; + private boolean lostAllConnections; + private int MAX_CONNECTIONS; private int MIN_CONNECTIONS; private int MAX_CONNECTIONS_PEER; private int MAX_CONNECTIONS_NON_DIRECT; - - private int MAX_CONNECTIONS_ABSOLUTE; - private final boolean printReportedPeersDetails = true; - private boolean lostAllConnections; + // Modify this to change the relationships between connection limits. private void setMaxConnections(int maxConnections) { MAX_CONNECTIONS = maxConnections; MIN_CONNECTIONS = Math.max(1, maxConnections - 4); @@ -46,10 +50,6 @@ public class PeerManager implements ConnectionListener { MAX_CONNECTIONS_ABSOLUTE = MAX_CONNECTIONS + 18; } - private static final int MAX_REPORTED_PEERS = 1000; - private static final int MAX_PERSISTED_PEERS = 500; - private static final long MAX_AGE = TimeUnit.DAYS.toMillis(14); // max age for reported peers is 14 days - /////////////////////////////////////////////////////////////////////////////////////////// // Listener @@ -88,8 +88,8 @@ public class PeerManager implements ConnectionListener { public PeerManager(NetworkNode networkNode, int maxConnections, Set seedNodeAddresses, File storageDir, Clock clock) { - this.networkNode = networkNode; setMaxConnections(maxConnections); + this.networkNode = networkNode; this.clock = clock; // seedNodeAddresses can be empty (in case there is only 1 seed node, the seed node starting up has no other seed nodes) this.seedNodeAddresses = new HashSet<>(seedNodeAddresses); From a1f98f2495700156434ac5cf0908694ad20cd916 Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Fri, 6 May 2016 10:56:28 +0200 Subject: [PATCH 7/7] Rename former constant-like fields to camel case in ``PeerManager`` --- .../io/bitsquare/p2p/peers/PeerManager.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java index d0b6009692..9f38a75735 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/PeerManager.java @@ -35,19 +35,19 @@ public class PeerManager implements ConnectionListener { private final boolean printReportedPeersDetails = true; private boolean lostAllConnections; - private int MAX_CONNECTIONS; - private int MIN_CONNECTIONS; - private int MAX_CONNECTIONS_PEER; - private int MAX_CONNECTIONS_NON_DIRECT; - private int MAX_CONNECTIONS_ABSOLUTE; + private int maxConnections; + private int minConnections; + private int maxConnectionsPeer; + private int maxConnectionsNonDirect; + private int maxConnectionsAbsolute; // Modify this to change the relationships between connection limits. - private void setMaxConnections(int maxConnections) { - MAX_CONNECTIONS = maxConnections; - MIN_CONNECTIONS = Math.max(1, maxConnections - 4); - MAX_CONNECTIONS_PEER = MAX_CONNECTIONS + 4; - MAX_CONNECTIONS_NON_DIRECT = MAX_CONNECTIONS + 8; - MAX_CONNECTIONS_ABSOLUTE = MAX_CONNECTIONS + 18; + private void setConnectionLimits(int maxConnections) { + this.maxConnections = maxConnections; + minConnections = Math.max(1, maxConnections - 4); + maxConnectionsPeer = maxConnections + 4; + maxConnectionsNonDirect = maxConnections + 8; + maxConnectionsAbsolute = maxConnections + 18; } @@ -88,7 +88,7 @@ public class PeerManager implements ConnectionListener { public PeerManager(NetworkNode networkNode, int maxConnections, Set seedNodeAddresses, File storageDir, Clock clock) { - setMaxConnections(maxConnections); + setConnectionLimits(maxConnections); this.networkNode = networkNode; this.clock = clock; // seedNodeAddresses can be empty (in case there is only 1 seed node, the seed node starting up has no other seed nodes) @@ -137,7 +137,7 @@ public class PeerManager implements ConnectionListener { /////////////////////////////////////////////////////////////////////////////////////////// public int getMaxConnections() { - return MAX_CONNECTIONS_ABSOLUTE; + return maxConnectionsAbsolute; } public void addListener(Listener listener) { @@ -197,7 +197,7 @@ public class PeerManager implements ConnectionListener { removeSuperfluousSeedNodes(); removeTooOldReportedPeers(); removeTooOldPersistedPeers(); - checkMaxConnections(MAX_CONNECTIONS); + checkMaxConnections(maxConnections); } else { log.warn("We have stopped already. We ignore that checkMaxConnectionsTimer.run call."); } @@ -221,8 +221,8 @@ public class PeerManager implements ConnectionListener { if (candidates.size() == 0) { log.info("No candidates found. We check if we exceed our " + - "MAX_CONNECTIONS_PEER limit of {}", MAX_CONNECTIONS_PEER); - if (size > MAX_CONNECTIONS_PEER) { + "maxConnectionsPeer limit of {}", maxConnectionsPeer); + if (size > maxConnectionsPeer) { log.info("Lets try to remove ANY connection of type PEER."); candidates = allConnections.stream() .filter(e -> e.getPeerType() == Connection.PeerType.PEER) @@ -230,8 +230,8 @@ public class PeerManager implements ConnectionListener { if (candidates.size() == 0) { log.info("No candidates found. We check if we exceed our " + - "MAX_CONNECTIONS_NON_DIRECT limit of {}", MAX_CONNECTIONS_NON_DIRECT); - if (size > MAX_CONNECTIONS_NON_DIRECT) { + "maxConnectionsNonDirect limit of {}", maxConnectionsNonDirect); + if (size > maxConnectionsNonDirect) { log.info("Lets try to remove any connection which is not of type DIRECT_MSG_PEER."); candidates = allConnections.stream() .filter(e -> e.getPeerType() != Connection.PeerType.DIRECT_MSG_PEER) @@ -239,8 +239,8 @@ public class PeerManager implements ConnectionListener { if (candidates.size() == 0) { log.info("No candidates found. We check if we exceed our " + - "MAX_CONNECTIONS_ABSOLUTE limit of {}", MAX_CONNECTIONS_ABSOLUTE); - if (size > MAX_CONNECTIONS_ABSOLUTE) { + "maxConnectionsAbsolute limit of {}", maxConnectionsAbsolute); + if (size > maxConnectionsAbsolute) { log.info("Lets try to remove any connection."); candidates = allConnections.stream().collect(Collectors.toList()); } @@ -286,7 +286,7 @@ public class PeerManager implements ConnectionListener { private void removeSuperfluousSeedNodes() { Log.traceCall(); - if (networkNode.getConfirmedConnections().size() > MAX_CONNECTIONS) { + if (networkNode.getConfirmedConnections().size() > maxConnections) { Set connections = networkNode.getConfirmedConnections(); if (hasSufficientConnections()) { List candidates = connections.stream() @@ -344,7 +344,7 @@ public class PeerManager implements ConnectionListener { printNewReportedPeers(reportedPeersToAdd); // We check if the reported msg is not violating our rules - if (reportedPeersToAdd.size() <= (MAX_REPORTED_PEERS + MAX_CONNECTIONS_ABSOLUTE + 10)) { + if (reportedPeersToAdd.size() <= (MAX_REPORTED_PEERS + maxConnectionsAbsolute + 10)) { reportedPeers.addAll(reportedPeersToAdd); purgeReportedPeersIfExceeds(); @@ -365,7 +365,7 @@ public class PeerManager implements ConnectionListener { private void purgeReportedPeersIfExceeds() { Log.traceCall(); int size = reportedPeers.size(); - int limit = MAX_REPORTED_PEERS - MAX_CONNECTIONS_ABSOLUTE; + int limit = MAX_REPORTED_PEERS - maxConnectionsAbsolute; if (size > limit) { log.trace("We have already {} reported peers which exceeds our limit of {}." + "We remove random peers from the reported peers list.", size, limit); @@ -468,7 +468,7 @@ public class PeerManager implements ConnectionListener { /////////////////////////////////////////////////////////////////////////////////////////// public boolean hasSufficientConnections() { - return networkNode.getNodeAddressesOfConfirmedConnections().size() >= MIN_CONNECTIONS; + return networkNode.getNodeAddressesOfConfirmedConnections().size() >= minConnections; } public boolean isSeedNode(Peer reportedPeer) {