From aac4731f807b7a6c0f96914ac8a83b862462764d Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Wed, 5 Nov 2014 12:58:29 +0100 Subject: [PATCH] Replace AddOfferListener Result/Fault handlers This change begins the process of simplifying the trade protocol by eliminating extra callback layers such as AddOfferListener and task layers such as PublishOfferToDHT in favor of calling directly into Repository interfaces. --- .../msg/listeners/AddOfferListener.java | 24 ---------- .../io/bitsquare/offer/OfferRepository.java | 5 +- .../offer/TomP2POfferRepository.java | 17 ++++--- .../createoffer/CreateOfferCoordinator.java | 5 +- .../createoffer/tasks/PublishOfferToDHT.java | 46 ------------------- 5 files changed, 13 insertions(+), 84 deletions(-) delete mode 100644 src/main/java/io/bitsquare/msg/listeners/AddOfferListener.java delete mode 100644 src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/PublishOfferToDHT.java diff --git a/src/main/java/io/bitsquare/msg/listeners/AddOfferListener.java b/src/main/java/io/bitsquare/msg/listeners/AddOfferListener.java deleted file mode 100644 index 7a05c60141..0000000000 --- a/src/main/java/io/bitsquare/msg/listeners/AddOfferListener.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * This file is part of Bitsquare. - * - * Bitsquare is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bitsquare is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bitsquare. If not, see . - */ - -package io.bitsquare.msg.listeners; - -public interface AddOfferListener { - void onComplete(); - - void onFailed(String reason, Throwable throwable); -} diff --git a/src/main/java/io/bitsquare/offer/OfferRepository.java b/src/main/java/io/bitsquare/offer/OfferRepository.java index a7f42c987b..5ff36cea12 100644 --- a/src/main/java/io/bitsquare/offer/OfferRepository.java +++ b/src/main/java/io/bitsquare/offer/OfferRepository.java @@ -17,7 +17,8 @@ package io.bitsquare.offer; -import io.bitsquare.msg.listeners.AddOfferListener; +import io.bitsquare.trade.handlers.FaultHandler; +import io.bitsquare.trade.handlers.ResultHandler; import java.util.List; @@ -27,7 +28,7 @@ public interface OfferRepository { void getOffers(String fiatCode); - void addOffer(Offer offer, AddOfferListener addOfferListener); + void addOffer(Offer offer, ResultHandler resultHandler, FaultHandler faultHandler); void removeOffer(Offer offer); diff --git a/src/main/java/io/bitsquare/offer/TomP2POfferRepository.java b/src/main/java/io/bitsquare/offer/TomP2POfferRepository.java index 8bb753068c..b4270c6886 100644 --- a/src/main/java/io/bitsquare/offer/TomP2POfferRepository.java +++ b/src/main/java/io/bitsquare/offer/TomP2POfferRepository.java @@ -18,7 +18,8 @@ package io.bitsquare.offer; import io.bitsquare.msg.P2PNode; -import io.bitsquare.msg.listeners.AddOfferListener; +import io.bitsquare.trade.handlers.FaultHandler; +import io.bitsquare.trade.handlers.ResultHandler; import java.io.IOException; @@ -60,7 +61,7 @@ class TomP2POfferRepository implements OfferRepository { } @Override - public void addOffer(Offer offer, AddOfferListener addOfferListener) { + public void addOffer(Offer offer, ResultHandler resultHandler, FaultHandler faultHandler) { Number160 locationKey = Number160.createHash(offer.getCurrency().getCurrencyCode()); try { final Data offerData = new Data(offer); @@ -77,7 +78,7 @@ class TomP2POfferRepository implements OfferRepository { // deactivate it for the moment until the port forwarding bug is fixed // if (future.isSuccess()) { Platform.runLater(() -> { - addOfferListener.onComplete(); + resultHandler.onResult(); offerRepositoryListeners.stream().forEach(listener -> { try { Object offerDataObject = offerData.object(); @@ -98,17 +99,15 @@ class TomP2POfferRepository implements OfferRepository { } @Override - public void exceptionCaught(Throwable t) throws Exception { + public void exceptionCaught(Throwable ex) throws Exception { Platform.runLater(() -> { - addOfferListener.onFailed("Add offer to DHT failed with an exception.", t); - log.error("Add offer to DHT failed with an exception: " + t.getMessage()); + faultHandler.onFault("Failed to add offer to DHT", ex); }); } }); - } catch (IOException e) { + } catch (IOException ex) { Platform.runLater(() -> { - addOfferListener.onFailed("Add offer to DHT failed with an exception.", e); - log.error("Add offer to DHT failed with an exception: " + e.getMessage()); + faultHandler.onFault("Failed to add offer to DHT", ex); }); } } diff --git a/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java b/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java index d9e982c158..2b10909293 100644 --- a/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java +++ b/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java @@ -26,7 +26,6 @@ import io.bitsquare.trade.handlers.FaultHandler; import io.bitsquare.trade.handlers.TransactionResultHandler; import io.bitsquare.trade.protocol.createoffer.tasks.BroadCastOfferFeeTx; import io.bitsquare.trade.protocol.createoffer.tasks.CreateOfferFeeTx; -import io.bitsquare.trade.protocol.createoffer.tasks.PublishOfferToDHT; import io.bitsquare.trade.protocol.createoffer.tasks.VerifyOffer; import org.bitcoinj.core.Transaction; @@ -133,7 +132,7 @@ public class CreateOfferCoordinator { private void onOfferFeeTxBroadCasted() { model.setState(State.OFFER_FEE_BROAD_CASTED); - PublishOfferToDHT.run(this::onOfferPublishedToDHT, this::onFailed, offerRepository, offer); + offerRepository.addOffer(offer, this::onOfferPublishedToDHT, faultHandler); } private void onOfferPublishedToDHT() { @@ -163,7 +162,7 @@ public class CreateOfferCoordinator { case OFFER_FEE_BROAD_CASTED: // actually the only replay case here, tx publish was successful but storage to dht failed. // Republish the offer to DHT - PublishOfferToDHT.run(this::onOfferPublishedToDHT, this::onFailed, offerRepository, offer); + offerRepository.addOffer(offer, this::onOfferPublishedToDHT, faultHandler); break; case OFFER_PUBLISHED_TO_DHT: // should be impossible diff --git a/src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/PublishOfferToDHT.java b/src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/PublishOfferToDHT.java deleted file mode 100644 index 143a06afed..0000000000 --- a/src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/PublishOfferToDHT.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is part of Bitsquare. - * - * Bitsquare is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bitsquare is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bitsquare. If not, see . - */ - -package io.bitsquare.trade.protocol.createoffer.tasks; - -import io.bitsquare.msg.listeners.AddOfferListener; -import io.bitsquare.offer.Offer; -import io.bitsquare.offer.OfferRepository; -import io.bitsquare.trade.handlers.FaultHandler; -import io.bitsquare.trade.handlers.ResultHandler; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PublishOfferToDHT { - private static final Logger log = LoggerFactory.getLogger(PublishOfferToDHT.class); - - public static void run(ResultHandler resultHandler, FaultHandler faultHandler, OfferRepository offerRepository, - Offer offer) { - offerRepository.addOffer(offer, new AddOfferListener() { - @Override - public void onComplete() { - resultHandler.onResult(); - } - - @Override - public void onFailed(String reason, Throwable throwable) { - faultHandler.onFault("Publish offer to DHT failed.", throwable); - } - }); - } -}