From b4917c1822de24496de53b7834354a15eace7064 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Wed, 13 Jul 2016 16:07:04 +0200 Subject: [PATCH] Add handling for mailbox messages after all is initialized --- .../io/bitsquare/gui/main/MainViewModel.java | 2 ++ .../java/io/bitsquare/p2p/P2PService.java | 26 ++++++++++++++++--- .../PrefixedSealedAndSignedMessage.java | 2 +- .../p2p/peers/getdata/RequestDataManager.java | 2 +- .../p2p/seed/SeedNodesRepository.java | 6 +++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java b/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java index 3bc037cf76..ec6973611e 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java +++ b/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java @@ -528,6 +528,8 @@ public class MainViewModel implements ViewModel { privateNotificationManager.privateNotificationProperty().addListener((observable, oldValue, newValue) -> displayPrivateNotification(newValue)); displayAlertIfPresent(alertManager.alertMessageProperty().get()); + p2PService.onAllServicesInitialized(); + setupBtcNumPeersWatcher(); setupP2PNumPeersWatcher(); updateBalance(); diff --git a/network/src/main/java/io/bitsquare/p2p/P2PService.java b/network/src/main/java/io/bitsquare/p2p/P2PService.java index 5f9bfebf8c..843fe61b37 100644 --- a/network/src/main/java/io/bitsquare/p2p/P2PService.java +++ b/network/src/main/java/io/bitsquare/p2p/P2PService.java @@ -65,6 +65,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis private final int maxConnections; private final File torDir; private Clock clock; + //TODO optional can be removed as seednode are created with those objects now private final Optional optionalEncryptionService; private final Optional optionalKeyRing; @@ -173,7 +174,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis BanList.setList(Arrays.asList(banList.replace(" ", "").split(",")).stream().map(NodeAddress::new).collect(Collectors.toList())); if (myAddress != null && !myAddress.isEmpty()) seedNodesRepository.setNodeAddressToExclude(new NodeAddress(myAddress)); - + networkNode = useLocalhost ? new LocalhostNetworkNode(port) : new TorNetworkNode(port, torDir); networkNode.addConnectionListener(this); networkNode.addMessageListener(this); @@ -221,6 +222,24 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis networkNode.start(useBridges, this); } + public void onAllServicesInitialized() { + if (networkNode.getNodeAddress() != null) { + p2PDataStorage.getMap().values().stream().forEach(protectedStorageEntry -> { + if (protectedStorageEntry instanceof ProtectedMailboxStorageEntry) + processProtectedMailboxStorageEntry((ProtectedMailboxStorageEntry) protectedStorageEntry); + }); + } else { + networkNode.nodeAddressProperty().addListener((observable, oldValue, newValue) -> { + if (newValue != null) { + p2PDataStorage.getMap().values().stream().forEach(protectedStorageEntry -> { + if (protectedStorageEntry instanceof ProtectedMailboxStorageEntry) + processProtectedMailboxStorageEntry((ProtectedMailboxStorageEntry) protectedStorageEntry); + }); + } + }); + } + } + public void shutDown(Runnable shutDownCompleteHandler) { Log.traceCall(); if (!shutDownInProgress) { @@ -510,8 +529,9 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis /////////////////////////////////////////////////////////////////////////////////////////// private void processProtectedMailboxStorageEntry(ProtectedMailboxStorageEntry protectedMailboxStorageEntry) { - // Seed nodes don't have set the encryptionService - if (optionalEncryptionService.isPresent()) { + final NodeAddress nodeAddress = networkNode.getNodeAddress(); + // Seed nodes don't receive mailbox messages + if (optionalEncryptionService.isPresent() && nodeAddress != null && !seedNodesRepository.isSeedNode(nodeAddress)) { Log.traceCall(); MailboxStoragePayload mailboxStoragePayload = protectedMailboxStorageEntry.getMailboxStoragePayload(); PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = mailboxStoragePayload.prefixedSealedAndSignedMessage; diff --git a/network/src/main/java/io/bitsquare/p2p/messaging/PrefixedSealedAndSignedMessage.java b/network/src/main/java/io/bitsquare/p2p/messaging/PrefixedSealedAndSignedMessage.java index e3698ded58..c0d848086c 100644 --- a/network/src/main/java/io/bitsquare/p2p/messaging/PrefixedSealedAndSignedMessage.java +++ b/network/src/main/java/io/bitsquare/p2p/messaging/PrefixedSealedAndSignedMessage.java @@ -44,7 +44,7 @@ public final class PrefixedSealedAndSignedMessage implements MailboxMessage, Sen @Override public String toString() { - return "SealedAndSignedMessage{" + + return "PrefixedSealedAndSignedMessage{" + "uid=" + uid + ", messageVersion=" + messageVersion + ", sealedAndSigned=" + sealedAndSigned + diff --git a/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataManager.java b/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataManager.java index 62a51153c4..3116c50271 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataManager.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/getdata/RequestDataManager.java @@ -106,7 +106,7 @@ public class RequestDataManager implements MessageListener, ConnectionListener, public void requestUpdateData() { Log.traceCall(); - checkArgument(nodeAddressOfPreliminaryDataRequest.isPresent(), "seedNodeOfPreliminaryDataRequest must be present"); + checkArgument(nodeAddressOfPreliminaryDataRequest.isPresent(), "nodeAddressOfPreliminaryDataRequest must be present"); dataUpdateRequested = true; List remainingNodeAddresses = new ArrayList<>(seedNodeAddresses); if (!remainingNodeAddresses.isEmpty()) { diff --git a/network/src/main/java/io/bitsquare/p2p/seed/SeedNodesRepository.java b/network/src/main/java/io/bitsquare/p2p/seed/SeedNodesRepository.java index 6c468c3ceb..fdcdcfa3ef 100644 --- a/network/src/main/java/io/bitsquare/p2p/seed/SeedNodesRepository.java +++ b/network/src/main/java/io/bitsquare/p2p/seed/SeedNodesRepository.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; public class SeedNodesRepository { private static final Logger log = LoggerFactory.getLogger(SeedNodesRepository.class); @@ -95,6 +96,11 @@ public class SeedNodesRepository { this.localhostSeedNodeAddresses = localhostSeedNodeAddresses; } + public boolean isSeedNode(NodeAddress nodeAddress) { + return Stream.concat(localhostSeedNodeAddresses.stream(), torSeedNodeAddresses.stream()) + .filter(e -> e.equals(nodeAddress)).findAny().isPresent(); + } + public void setNodeAddressToExclude(NodeAddress nodeAddress) { this.nodeAddressToExclude = nodeAddress; }