Throw exception if address storage fails

This commit is contained in:
Manfred Karrer 2014-11-21 01:21:45 +01:00
parent ef7c2305fc
commit c3f87609a8
3 changed files with 62 additions and 21 deletions

View file

@ -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<BaseFuture>() {
@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());

View file

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

View file

@ -45,6 +45,7 @@
<logger name="io.bitsquare.gui.CachedViewCB" level="WARN"/>
<logger name="io.bitsquare.gui.util.Profiler" level="WARN"/>
<logger name="io.bitsquare.persistence.Persistence" level="WARN"/>
<logger name="io.bitsquare.locale.BSResources" level="OFF"/>
<logger name="org.bitcoinj.core.BitcoinSerializer" level="WARN"/>