From c3f87609a806ee4fe8927c975684077fb3d368af Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Fri, 21 Nov 2014 01:21:45 +0100 Subject: [PATCH] Throw exception if address storage fails --- .../io/bitsquare/msg/tomp2p/TomP2PNode.java | 44 ++++++++++--------- .../bitsquare/network/NetworkException.java | 38 ++++++++++++++++ src/main/resources/logback.xml | 1 + 3 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 src/main/java/io/bitsquare/network/NetworkException.java diff --git a/src/main/java/io/bitsquare/msg/tomp2p/TomP2PNode.java b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PNode.java index 9be6bc40d5..812a549b63 100644 --- a/src/main/java/io/bitsquare/msg/tomp2p/TomP2PNode.java +++ b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PNode.java @@ -23,6 +23,7 @@ import io.bitsquare.msg.listeners.BootstrapListener; import io.bitsquare.network.BootstrapState; import io.bitsquare.network.ClientNode; import io.bitsquare.network.ConnectionType; +import io.bitsquare.network.NetworkException; import io.bitsquare.network.Node; import io.bitsquare.network.tomp2p.TomP2PPeer; @@ -126,7 +127,11 @@ public class TomP2PNode implements ClientNode { public void onSuccess(@Nullable PeerDHT peerDHT) { if (peerDHT != null) { TomP2PNode.this.peerDHT = peerDHT; - setup(); + try { + setup(); + } catch (NetworkException e) { + Platform.runLater(() -> bootstrapListener.onFailed(e)); + } Platform.runLater(bootstrapListener::onCompleted); } else { @@ -144,10 +149,10 @@ public class TomP2PNode implements ClientNode { }); } - private void setup() { + private void setup() throws NetworkException { setupTimerForIPCheck(); setupReplyHandler(); - storeAddressAfterBootstrap(); + storeAddress(); } public void shutDown() { @@ -322,27 +327,20 @@ public class TomP2PNode implements ClientNode { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { - if (storedPeerAddress != null) { - if (peerDHT != null && !storedPeerAddress.equals(peerDHT.peerAddress())) { - try { - storeAddress(); - } catch (IOException e) { - e.printStackTrace(); - log.error(e.toString()); - } + if (storedPeerAddress != null && peerDHT != null + && !storedPeerAddress.equals(peerDHT.peerAddress())) + try { + storeAddress(); + } catch (NetworkException e) { + e.printStackTrace(); } - } - else { - log.error("storedPeerAddress is null. That should not happen. " + - "Seems there is a problem with DHT storage."); - } } }, checkIfIPChangedPeriod, checkIfIPChangedPeriod); } - private void storeAddressAfterBootstrap() { + private void storeAddress() throws NetworkException { try { - FuturePut futurePut = storeAddress(); + FuturePut futurePut = saveAddress(); futurePut.addListener(new BaseFutureListener() { @Override public void operationComplete(BaseFuture future) throws Exception { @@ -352,21 +350,25 @@ public class TomP2PNode implements ClientNode { } else { log.error("storedPeerAddress not successful"); + throw new NetworkException("Storing address was not successful. Reason: " + + future.failedReason()); } } @Override public void exceptionCaught(Throwable t) throws Exception { - log.error("Error at storedPeerAddress " + t.toString()); + log.error("Exception at storedPeerAddress " + t.toString()); + throw new NetworkException("Exception at storeAddress.", t); } }); } catch (IOException e) { e.printStackTrace(); - log.error("Error at storePeerAddress " + e.toString()); + log.error("Exception at storePeerAddress " + e.toString()); + throw new NetworkException("Exception at storeAddress.", e); } } - private FuturePut storeAddress() throws IOException { + private FuturePut saveAddress() throws IOException { Number160 locationKey = Utils.makeSHAHash(keyPair.getPublic().getEncoded()); Data data = new Data(new TomP2PPeer(peerDHT.peerAddress())); log.debug("storePeerAddress " + peerDHT.peerAddress().toString()); diff --git a/src/main/java/io/bitsquare/network/NetworkException.java b/src/main/java/io/bitsquare/network/NetworkException.java new file mode 100644 index 0000000000..b16ef3aa4c --- /dev/null +++ b/src/main/java/io/bitsquare/network/NetworkException.java @@ -0,0 +1,38 @@ +/* + * 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.network; + +import java.io.IOException; + +@SuppressWarnings("serializable") +public class NetworkException extends IOException { + + public NetworkException(Throwable cause) { + super(cause); + } + + public NetworkException(String message) { + super(message); + } + + public NetworkException(String message, Throwable cause) { + super(message, cause); + } + +} + diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index e47a692b95..d4f47c177e 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -45,6 +45,7 @@ +