mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-04-20 23:56:30 -04:00
refactor p2pservice contructor
This commit is contained in:
parent
f9a31f4b8a
commit
cefcf5b3d8
@ -44,9 +44,6 @@ import java.util.concurrent.TimeUnit;
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* Represents our node in the P2P network
|
||||
*/
|
||||
public class P2PService implements SetupListener, MessageListener, ConnectionListener {
|
||||
private static final Logger log = LoggerFactory.getLogger(P2PService.class);
|
||||
|
||||
@ -92,9 +89,10 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||
@Named(ProgramArguments.TOR_DIR) File torDir,
|
||||
@Named(ProgramArguments.USE_LOCALHOST) boolean useLocalhost,
|
||||
@Named(ProgramArguments.NETWORK_ID) int networkId,
|
||||
@Named("storage.dir") File storageDir,
|
||||
@Nullable EncryptionService encryptionService,
|
||||
KeyRing keyRing,
|
||||
@Named("storage.dir") File storageDir) {
|
||||
@Nullable KeyRing keyRing
|
||||
) {
|
||||
Log.traceCall();
|
||||
this.seedNodesRepository = seedNodesRepository;
|
||||
this.port = port;
|
||||
@ -107,6 +105,16 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||
init(networkId, storageDir);
|
||||
}
|
||||
|
||||
// Used for seed node
|
||||
public P2PService(SeedNodesRepository seedNodesRepository,
|
||||
int port,
|
||||
File torDir,
|
||||
boolean useLocalhost,
|
||||
int networkId,
|
||||
File storageDir) {
|
||||
this(seedNodesRepository, port, torDir, useLocalhost, networkId, storageDir, null, null);
|
||||
}
|
||||
|
||||
private void init(int networkId, File storageDir) {
|
||||
Log.traceCall();
|
||||
|
||||
|
@ -22,8 +22,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
|
||||
// authentication example:
|
||||
// authentication protocol:
|
||||
// node2 -> node1 AuthenticationRequest
|
||||
// node1: close connection
|
||||
// node1 -> node2 AuthenticationResponse on new connection
|
||||
|
@ -23,7 +23,6 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
// Run in UserThread
|
||||
public class PeerGroup implements MessageListener, ConnectionListener {
|
||||
private static final Logger log = LoggerFactory.getLogger(PeerGroup.class);
|
||||
|
||||
@ -656,28 +655,31 @@ public class PeerGroup implements MessageListener, ConnectionListener {
|
||||
} else {
|
||||
newReportedPeers.remove(new ReportedPeer(getMyAddress(), new Date()));
|
||||
|
||||
//TODO if we have already peer, we mix date from old and new item
|
||||
//
|
||||
/* Map<Address, ReportedPeer> reportedPeersMap = new HashMap<>();
|
||||
// In case we have a peers already we adjust the lastActivityDate by adjusting the date to the mid
|
||||
// of the lastActivityDate of our already stored peer and the reported one
|
||||
Map<Address, ReportedPeer> reportedPeersMap = new HashMap<>();
|
||||
reportedPeers.stream().forEach(e -> reportedPeersMap.put(e.address, e));
|
||||
|
||||
HashSet<ReportedPeer> newAdjustedReportedPeers = new HashSet<>();
|
||||
Set<ReportedPeer> newAdjustedReportedPeers = new HashSet<>();
|
||||
newReportedPeers.stream()
|
||||
.forEach(e -> {
|
||||
if()
|
||||
long adjustedTime = (e.lastActivityDate.getTime() +
|
||||
reportedPeersMap.get(e.address).lastActivityDate.getTime()) / 2;
|
||||
newAdjustedReportedPeers.add(new ReportedPeer(e.address,
|
||||
new Date(adjustedTime)));
|
||||
});*/
|
||||
if (reportedPeersMap.containsKey(e.address)) {
|
||||
long adjustedTime = (e.lastActivityDate.getTime() +
|
||||
reportedPeersMap.get(e.address).lastActivityDate.getTime()) / 2;
|
||||
newAdjustedReportedPeers.add(new ReportedPeer(e.address,
|
||||
new Date(adjustedTime)));
|
||||
} else {
|
||||
newAdjustedReportedPeers.add(e);
|
||||
}
|
||||
});
|
||||
|
||||
this.reportedPeers.addAll(newReportedPeers);
|
||||
this.reportedPeers.addAll(newAdjustedReportedPeers);
|
||||
purgeReportedPeersIfExceeds();
|
||||
}
|
||||
|
||||
printReportedPeers();
|
||||
}
|
||||
|
||||
// TODO unit test
|
||||
private void purgeReportedPeersIfExceeds() {
|
||||
Log.traceCall();
|
||||
int size = reportedPeers.size();
|
||||
@ -685,9 +687,10 @@ public class PeerGroup implements MessageListener, ConnectionListener {
|
||||
log.trace("We have more then {} reported peers. size={}. " +
|
||||
"We remove random peers from the reported peers list.", MAX_REPORTED_PEERS, size);
|
||||
int diff = size - MAX_REPORTED_PEERS;
|
||||
List<ReportedPeer> list = new LinkedList<>(getReportedNotConnectedPeerAddresses());
|
||||
|
||||
//TODO sort and remove oldest
|
||||
List<ReportedPeer> list = new ArrayList<>(getReportedNotConnectedPeerAddresses());
|
||||
log.debug("Peers before sort " + list);
|
||||
list.sort((a, b) -> a.lastActivityDate.compareTo(b.lastActivityDate));
|
||||
log.debug("Peers after sort " + list);
|
||||
for (int i = 0; i < diff; i++) {
|
||||
ReportedPeer toRemove = getAndRemoveRandomReportedPeer(list);
|
||||
reportedPeers.remove(toRemove);
|
||||
|
@ -3,8 +3,6 @@ package io.bitsquare.p2p.seed;
|
||||
import io.bitsquare.app.Log;
|
||||
import io.bitsquare.app.Version;
|
||||
import io.bitsquare.common.UserThread;
|
||||
import io.bitsquare.common.crypto.KeyRing;
|
||||
import io.bitsquare.crypto.EncryptionService;
|
||||
import io.bitsquare.p2p.Address;
|
||||
import io.bitsquare.p2p.P2PService;
|
||||
import io.bitsquare.p2p.P2PServiceListener;
|
||||
@ -28,7 +26,7 @@ public class SeedNode {
|
||||
|
||||
private Address mySeedNodeAddress = new Address("localhost:8001");
|
||||
private boolean useLocalhost = false;
|
||||
private Set<Address> seedNodes;
|
||||
private Set<Address> progArgSeedNodes;
|
||||
private P2PService p2PService;
|
||||
private boolean stopped;
|
||||
private final String defaultUserDataDir;
|
||||
@ -80,13 +78,13 @@ public class SeedNode {
|
||||
checkArgument(arg4.contains(":") && arg4.split(":").length > 1 && arg4.split(":")[1].length() > 3,
|
||||
"Wrong program argument");
|
||||
List<String> list = Arrays.asList(arg4.split("|"));
|
||||
seedNodes = new HashSet<>();
|
||||
progArgSeedNodes = new HashSet<>();
|
||||
list.forEach(e -> {
|
||||
checkArgument(e.contains(":") && e.split(":").length == 2 && e.split(":")[1].length() == 4,
|
||||
"Wrong program argument");
|
||||
seedNodes.add(new Address(e));
|
||||
progArgSeedNodes.add(new Address(e));
|
||||
});
|
||||
seedNodes.remove(mySeedNodeAddress);
|
||||
progArgSeedNodes.remove(mySeedNodeAddress);
|
||||
} else if (args.length > 5) {
|
||||
log.error("Too many program arguments." +
|
||||
"\nProgram arguments: myAddress (incl. port) bitcoinNetworkId " +
|
||||
@ -100,23 +98,21 @@ public class SeedNode {
|
||||
}
|
||||
|
||||
public void createAndStartP2PService() {
|
||||
createAndStartP2PService(null, null, mySeedNodeAddress, useLocalhost, Version.NETWORK_ID, seedNodes, null);
|
||||
createAndStartP2PService(mySeedNodeAddress, useLocalhost, Version.NETWORK_ID, progArgSeedNodes, null);
|
||||
}
|
||||
|
||||
public void createAndStartP2PService(EncryptionService encryptionService,
|
||||
KeyRing keyRing,
|
||||
Address mySeedNodeAddress,
|
||||
public void createAndStartP2PService(Address mySeedNodeAddress,
|
||||
boolean useLocalhost,
|
||||
int networkId,
|
||||
@Nullable Set<Address> seedNodes,
|
||||
@Nullable Set<Address> progArgSeedNodes,
|
||||
@Nullable P2PServiceListener listener) {
|
||||
Log.traceCall();
|
||||
SeedNodesRepository seedNodesRepository = new SeedNodesRepository();
|
||||
if (seedNodes != null && !seedNodes.isEmpty()) {
|
||||
if (progArgSeedNodes != null && !progArgSeedNodes.isEmpty()) {
|
||||
if (useLocalhost)
|
||||
seedNodesRepository.setLocalhostSeedNodeAddresses(seedNodes);
|
||||
seedNodesRepository.setLocalhostSeedNodeAddresses(progArgSeedNodes);
|
||||
else
|
||||
seedNodesRepository.setTorSeedNodeAddresses(seedNodes);
|
||||
seedNodesRepository.setTorSeedNodeAddresses(progArgSeedNodes);
|
||||
}
|
||||
Path seedNodePath = Paths.get(defaultUserDataDir,
|
||||
"Bitsquare_seed_node_" + String.valueOf(mySeedNodeAddress.getFullAddress().replace(":", "_")));
|
||||
@ -128,8 +124,7 @@ public class SeedNode {
|
||||
if (torDir.mkdirs())
|
||||
log.info("Created torDir at " + torDir.getAbsolutePath());
|
||||
|
||||
p2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, torDir,
|
||||
useLocalhost, networkId, encryptionService, keyRing, storageDir);
|
||||
p2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, torDir, useLocalhost, networkId, storageDir);
|
||||
p2PService.removeMySeedNodeAddressFromList(mySeedNodeAddress);
|
||||
p2PService.start(listener);
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ public class P2PServiceTest {
|
||||
sleepTime = 1000;
|
||||
}
|
||||
|
||||
seedNode1 = TestUtils.getAndStartSeedNode(8001, encryptionService1, keyRing1, useLocalhost, seedNodes);
|
||||
seedNode1 = TestUtils.getAndStartSeedNode(8001, useLocalhost, seedNodes);
|
||||
p2PService1 = seedNode1.getP2PService();
|
||||
p2PService2 = TestUtils.getAndAuthenticateP2PService(8002, encryptionService2, keyRing2, useLocalhost, seedNodes);
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ public class TestUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static SeedNode getAndStartSeedNode(int port, EncryptionService encryptionService, KeyRing keyRing, boolean useLocalhost, Set<Address> seedNodes) throws InterruptedException {
|
||||
public static SeedNode getAndStartSeedNode(int port, boolean useLocalhost, Set<Address> seedNodes) throws InterruptedException {
|
||||
SeedNode seedNode;
|
||||
|
||||
if (useLocalhost) {
|
||||
@ -80,7 +80,7 @@ public class TestUtils {
|
||||
}
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
seedNode.createAndStartP2PService(encryptionService, keyRing, new Address("localhost", port), useLocalhost, 2,
|
||||
seedNode.createAndStartP2PService(new Address("localhost", port), useLocalhost, 2,
|
||||
seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
|
@ -80,7 +80,7 @@ public class PeerGroupTest {
|
||||
seedNodes.add(address);
|
||||
seedNode1 = new SeedNode("test_dummy_dir");
|
||||
latch = new CountDownLatch(2);
|
||||
seedNode1.createAndStartP2PService(null, null, address, useLocalhost, 2,
|
||||
seedNode1.createAndStartP2PService(address, useLocalhost, 2,
|
||||
seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
@ -125,7 +125,7 @@ public class PeerGroupTest {
|
||||
latch = new CountDownLatch(6);
|
||||
|
||||
seedNode1 = new SeedNode("test_dummy_dir");
|
||||
seedNode1.createAndStartP2PService(null, null, address1, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
seedNode1.createAndStartP2PService(address1, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
latch.countDown();
|
||||
@ -156,7 +156,7 @@ public class PeerGroupTest {
|
||||
Thread.sleep(500);
|
||||
|
||||
seedNode2 = new SeedNode("test_dummy_dir");
|
||||
seedNode2.createAndStartP2PService(null, null, address2, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
seedNode2.createAndStartP2PService(address2, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
latch.countDown();
|
||||
@ -386,7 +386,7 @@ public class PeerGroupTest {
|
||||
SeedNode seedNode = new SeedNode("test_dummy_dir");
|
||||
|
||||
latch = new CountDownLatch(1);
|
||||
seedNode.createAndStartP2PService(null, null, new Address("localhost", port), useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
seedNode.createAndStartP2PService(new Address("localhost", port), useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
latch.countDown();
|
||||
|
@ -64,7 +64,7 @@ public class ProtectedDataStorageTest {
|
||||
|
||||
storageSignatureKeyPair1 = keyRing1.getSignatureKeyPair();
|
||||
encryptionService1 = new EncryptionService(keyRing1);
|
||||
networkNode1 = TestUtils.getAndStartSeedNode(8001, encryptionService1, keyRing1, useClearNet, seedNodes).getP2PService().getNetworkNode();
|
||||
networkNode1 = TestUtils.getAndStartSeedNode(8001, useClearNet, seedNodes).getP2PService().getNetworkNode();
|
||||
peerGroup1 = new PeerGroup(networkNode1, seedNodes);
|
||||
dataStorage1 = new ProtectedExpirableDataStorage(peerGroup1, new File("dummy"));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user