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.
This commit is contained in:
Chris Beams 2014-11-05 12:58:29 +01:00
parent 40807b07f8
commit aac4731f80
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
5 changed files with 13 additions and 84 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.msg.listeners;
public interface AddOfferListener {
void onComplete();
void onFailed(String reason, Throwable throwable);
}

View File

@ -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);

View File

@ -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);
});
}
}

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
});
}
}