Search in sources :

Example 1 with Message

use of io.bitsquare.p2p.Message in project bitsquare by bitsquare.

the class TorNetworkNodeTest method testTorNodeAfterBothReady.

//@Test
public void testTorNodeAfterBothReady() throws InterruptedException, IOException {
    latch = new CountDownLatch(2);
    int port = 9001;
    TorNetworkNode node1 = new TorNetworkNode(port, new File("torNode_" + port));
    node1.start(new SetupListener() {

        @Override
        public void onTorNodeReady() {
            log.debug("onReadyForSendingMessages");
        }

        @Override
        public void onHiddenServicePublished() {
            log.debug("onReadyForReceivingMessages");
            latch.countDown();
        }

        @Override
        public void onSetupFailed(Throwable throwable) {
        }
    });
    int port2 = 9002;
    TorNetworkNode node2 = new TorNetworkNode(port2, new File("torNode_" + port));
    node2.start(new SetupListener() {

        @Override
        public void onTorNodeReady() {
            log.debug("onReadyForSendingMessages");
        }

        @Override
        public void onHiddenServicePublished() {
            log.debug("onReadyForReceivingMessages");
            latch.countDown();
        }

        @Override
        public void onSetupFailed(Throwable throwable) {
        }
    });
    latch.await();
    latch = new CountDownLatch(2);
    node2.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, Connection connection) {
            log.debug("onMessage node2 " + message);
            latch.countDown();
        }
    });
    SettableFuture<Connection> future = node1.sendMessage(node2.getNodeAddress(), new MockPayload("msg1"));
    Futures.addCallback(future, new FutureCallback<Connection>() {

        @Override
        public void onSuccess(Connection connection) {
            log.debug("onSuccess ");
            latch.countDown();
        }

        @Override
        public void onFailure(@NotNull Throwable throwable) {
            log.debug("onFailure ");
        }
    });
    latch.await();
    latch = new CountDownLatch(2);
    node1.shutDown(() -> {
        latch.countDown();
    });
    node2.shutDown(() -> {
        latch.countDown();
    });
    latch.await();
}
Also used : Message(io.bitsquare.p2p.Message) CountDownLatch(java.util.concurrent.CountDownLatch) MockPayload(io.bitsquare.p2p.mocks.MockPayload) File(java.io.File)

Example 2 with Message

use of io.bitsquare.p2p.Message in project bitsquare by bitsquare.

the class OpenOfferManager method handleOfferAvailabilityRequest.

///////////////////////////////////////////////////////////////////////////////////////////
// Offer Availability
///////////////////////////////////////////////////////////////////////////////////////////
private void handleOfferAvailabilityRequest(OfferAvailabilityRequest message, NodeAddress sender) {
    log.trace("handleNewMessage: message = " + message.getClass().getSimpleName() + " from " + sender);
    if (!stopped) {
        try {
            nonEmptyStringOf(message.offerId);
            checkNotNull(message.getPubKeyRing());
        } catch (Throwable t) {
            log.warn("Invalid message " + message.toString());
            return;
        }
        Optional<OpenOffer> openOfferOptional = findOpenOffer(message.offerId);
        AvailabilityResult availabilityResult;
        if (openOfferOptional.isPresent()) {
            if (openOfferOptional.get().getState() == OpenOffer.State.AVAILABLE) {
                final Offer offer = openOfferOptional.get().getOffer();
                if (!preferences.getIgnoreTradersList().stream().filter(i -> i.equals(offer.getOffererNodeAddress().getHostNameWithoutPostFix())).findAny().isPresent()) {
                    availabilityResult = AvailabilityResult.AVAILABLE;
                    List<NodeAddress> acceptedArbitrators = user.getAcceptedArbitratorAddresses();
                    if (acceptedArbitrators != null && !acceptedArbitrators.isEmpty()) {
                        // We need to be backward compatible. takersTradePrice was not used before 0.4.9.
                        if (message.takersTradePrice > 0) {
                            // losses and therefore an outdated market price.
                            try {
                                offer.checkTradePriceTolerance(message.takersTradePrice);
                            } catch (TradePriceOutOfToleranceException e) {
                                log.warn("Trade price check failed because takers price is outside out tolerance.");
                                availabilityResult = AvailabilityResult.PRICE_OUT_OF_TOLERANCE;
                            } catch (MarketPriceNotAvailableException e) {
                                log.warn(e.getMessage());
                                availabilityResult = AvailabilityResult.MARKET_PRICE_NOT_AVAILABLE;
                            } catch (Throwable e) {
                                log.warn("Trade price check failed. " + e.getMessage());
                                availabilityResult = AvailabilityResult.UNKNOWN_FAILURE;
                            }
                        }
                    } else {
                        log.warn("acceptedArbitrators is null or empty: acceptedArbitrators=" + acceptedArbitrators);
                        availabilityResult = AvailabilityResult.NO_ARBITRATORS;
                    }
                } else {
                    availabilityResult = AvailabilityResult.USER_IGNORED;
                }
            } else {
                availabilityResult = AvailabilityResult.OFFER_TAKEN;
            }
        } else {
            log.warn("handleOfferAvailabilityRequest: openOffer not found. That should never happen.");
            availabilityResult = AvailabilityResult.OFFER_TAKEN;
        }
        try {
            p2PService.sendEncryptedDirectMessage(sender, message.getPubKeyRing(), new OfferAvailabilityResponse(message.offerId, availabilityResult), new SendDirectMessageListener() {

                @Override
                public void onArrived() {
                    log.trace("OfferAvailabilityResponse successfully arrived at peer");
                }

                @Override
                public void onFault() {
                    log.debug("Sending OfferAvailabilityResponse failed.");
                }
            });
        } catch (Throwable t) {
            t.printStackTrace();
            log.debug("Exception at handleRequestIsOfferAvailableMessage " + t.getMessage());
        }
    } else {
        log.debug("We have stopped already. We ignore that handleOfferAvailabilityRequest call.");
    }
}
Also used : OfferAvailabilityResponse(io.bitsquare.trade.protocol.availability.messages.OfferAvailabilityResponse) PriceFeedService(io.bitsquare.btc.pricefeed.PriceFeedService) P2PService(io.bitsquare.p2p.P2PService) TradableList(io.bitsquare.trade.TradableList) ErrorMessageHandler(io.bitsquare.common.handlers.ErrorMessageHandler) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Timer(io.bitsquare.common.Timer) AvailabilityResult(io.bitsquare.trade.protocol.availability.AvailabilityResult) TransactionResultHandler(io.bitsquare.trade.handlers.TransactionResultHandler) PlaceOfferModel(io.bitsquare.trade.protocol.placeoffer.PlaceOfferModel) ArrayList(java.util.ArrayList) DevFlags(io.bitsquare.app.DevFlags) User(io.bitsquare.user.User) WalletService(io.bitsquare.btc.WalletService) PeerManager(io.bitsquare.p2p.peers.PeerManager) KeyRing(io.bitsquare.common.crypto.KeyRing) TradeWalletService(io.bitsquare.btc.TradeWalletService) ResultHandler(io.bitsquare.common.handlers.ResultHandler) Named(javax.inject.Named) AddressEntry(io.bitsquare.btc.AddressEntry) Nullable(javax.annotation.Nullable) Log(io.bitsquare.app.Log) Logger(org.slf4j.Logger) Validator.nonEmptyStringOf(io.bitsquare.util.Validator.nonEmptyStringOf) UserThread(io.bitsquare.common.UserThread) OfferAvailabilityRequest(io.bitsquare.trade.protocol.availability.messages.OfferAvailabilityRequest) Message(io.bitsquare.p2p.Message) NodeAddress(io.bitsquare.p2p.NodeAddress) BootstrapListener(io.bitsquare.p2p.BootstrapListener) ClosedTradableManager(io.bitsquare.trade.closed.ClosedTradableManager) SendDirectMessageListener(io.bitsquare.p2p.messaging.SendDirectMessageListener) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) MarketPriceNotAvailableException(io.bitsquare.trade.exceptions.MarketPriceNotAvailableException) Preferences(io.bitsquare.user.Preferences) DecryptedDirectMessageListener(io.bitsquare.p2p.messaging.DecryptedDirectMessageListener) List(java.util.List) com.google.inject.internal.util.$Preconditions.checkNotNull(com.google.inject.internal.util.$Preconditions.checkNotNull) TradePriceOutOfToleranceException(io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException) PlaceOfferProtocol(io.bitsquare.trade.protocol.placeoffer.PlaceOfferProtocol) Optional(java.util.Optional) Storage(io.bitsquare.storage.Storage) DecryptedMsgWithPubKey(io.bitsquare.crypto.DecryptedMsgWithPubKey) ObservableList(javafx.collections.ObservableList) AvailabilityResult(io.bitsquare.trade.protocol.availability.AvailabilityResult) TradePriceOutOfToleranceException(io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException) OfferAvailabilityResponse(io.bitsquare.trade.protocol.availability.messages.OfferAvailabilityResponse) SendDirectMessageListener(io.bitsquare.p2p.messaging.SendDirectMessageListener) MarketPriceNotAvailableException(io.bitsquare.trade.exceptions.MarketPriceNotAvailableException) NodeAddress(io.bitsquare.p2p.NodeAddress)

Example 3 with Message

use of io.bitsquare.p2p.Message in project bitsquare by bitsquare.

the class PrivateNotificationManager method handleMessage.

private void handleMessage(DecryptedMsgWithPubKey decryptedMsgWithPubKey, NodeAddress senderNodeAddress) {
    this.decryptedMsgWithPubKey = decryptedMsgWithPubKey;
    Message message = decryptedMsgWithPubKey.message;
    if (message instanceof PrivateNotificationMessage) {
        PrivateNotificationMessage privateNotificationMessage = (PrivateNotificationMessage) message;
        log.trace("Received privateNotificationMessage: " + privateNotificationMessage);
        if (privateNotificationMessage.getSenderNodeAddress().equals(senderNodeAddress)) {
            final PrivateNotification privateNotification = privateNotificationMessage.privateNotification;
            if (verifySignature(privateNotification))
                privateNotificationMessageProperty.set(privateNotification);
        } else {
            log.warn("Peer address not matching for privateNotificationMessage");
        }
    }
}
Also used : Message(io.bitsquare.p2p.Message)

Example 4 with Message

use of io.bitsquare.p2p.Message in project bitsquare by bitsquare.

the class DisputeManager method applyMessages.

private void applyMessages() {
    decryptedDirectMessageWithPubKeys.forEach(decryptedMessageWithPubKey -> {
        Message message = decryptedMessageWithPubKey.message;
        log.debug("decryptedDirectMessageWithPubKeys.message " + message);
        if (message instanceof DisputeMessage)
            dispatchMessage((DisputeMessage) message);
    });
    decryptedDirectMessageWithPubKeys.clear();
    decryptedMailboxMessageWithPubKeys.forEach(decryptedMessageWithPubKey -> {
        Message message = decryptedMessageWithPubKey.message;
        log.debug("decryptedMessageWithPubKey.message " + message);
        if (message instanceof DisputeMessage) {
            dispatchMessage((DisputeMessage) message);
            p2PService.removeEntryFromMailbox(decryptedMessageWithPubKey);
        }
    });
    decryptedMailboxMessageWithPubKeys.clear();
}
Also used : Message(io.bitsquare.p2p.Message)

Example 5 with Message

use of io.bitsquare.p2p.Message in project bitsquare by bitsquare.

the class TorNetworkNodeTest method testTorNodeBeforeSecondReady.

@Test
public void testTorNodeBeforeSecondReady() throws InterruptedException, IOException {
    latch = new CountDownLatch(1);
    int port = 9001;
    TorNetworkNode node1 = new TorNetworkNode(port, new File("torNode_" + port));
    node1.start(new SetupListener() {

        @Override
        public void onTorNodeReady() {
            log.debug("onReadyForSendingMessages");
        }

        @Override
        public void onHiddenServicePublished() {
            log.debug("onReadyForReceivingMessages");
            latch.countDown();
        }

        @Override
        public void onSetupFailed(Throwable throwable) {
        }
    });
    latch.await();
    latch = new CountDownLatch(1);
    int port2 = 9002;
    TorNetworkNode node2 = new TorNetworkNode(port2, new File("torNode_" + port2));
    node2.start(new SetupListener() {

        @Override
        public void onTorNodeReady() {
            log.debug("onReadyForSendingMessages");
            latch.countDown();
        }

        @Override
        public void onHiddenServicePublished() {
            log.debug("onReadyForReceivingMessages");
        }

        @Override
        public void onSetupFailed(Throwable throwable) {
        }
    });
    latch.await();
    latch = new CountDownLatch(2);
    node1.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, Connection connection) {
            log.debug("onMessage node1 " + message);
            latch.countDown();
        }
    });
    SettableFuture<Connection> future = node2.sendMessage(node1.getNodeAddress(), new MockPayload("msg1"));
    Futures.addCallback(future, new FutureCallback<Connection>() {

        @Override
        public void onSuccess(Connection connection) {
            log.debug("onSuccess ");
            latch.countDown();
        }

        @Override
        public void onFailure(@NotNull Throwable throwable) {
            log.debug("onFailure ");
        }
    });
    latch.await();
    latch = new CountDownLatch(2);
    node1.shutDown(() -> {
        latch.countDown();
    });
    node2.shutDown(() -> {
        latch.countDown();
    });
    latch.await();
}
Also used : Message(io.bitsquare.p2p.Message) CountDownLatch(java.util.concurrent.CountDownLatch) MockPayload(io.bitsquare.p2p.mocks.MockPayload) File(java.io.File) Test(org.junit.Test)

Aggregations

Message (io.bitsquare.p2p.Message)5 File (java.io.File)3 MockPayload (io.bitsquare.p2p.mocks.MockPayload)2 Inject (com.google.inject.Inject)1 com.google.inject.internal.util.$Preconditions.checkNotNull (com.google.inject.internal.util.$Preconditions.checkNotNull)1 DevFlags (io.bitsquare.app.DevFlags)1 Log (io.bitsquare.app.Log)1 AddressEntry (io.bitsquare.btc.AddressEntry)1 TradeWalletService (io.bitsquare.btc.TradeWalletService)1 WalletService (io.bitsquare.btc.WalletService)1 PriceFeedService (io.bitsquare.btc.pricefeed.PriceFeedService)1 Timer (io.bitsquare.common.Timer)1 UserThread (io.bitsquare.common.UserThread)1 KeyRing (io.bitsquare.common.crypto.KeyRing)1 ErrorMessageHandler (io.bitsquare.common.handlers.ErrorMessageHandler)1 ResultHandler (io.bitsquare.common.handlers.ResultHandler)1 DecryptedMsgWithPubKey (io.bitsquare.crypto.DecryptedMsgWithPubKey)1 BootstrapListener (io.bitsquare.p2p.BootstrapListener)1 NodeAddress (io.bitsquare.p2p.NodeAddress)1 P2PService (io.bitsquare.p2p.P2PService)1