3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-15 19:55:51 +00:00

Fixes for r165 code review

This commit is contained in:
Miron Cuperman (devrandom) 2011-08-10 17:32:59 +00:00
parent 0c8638ae2e
commit 381cda1ff2

View File

@ -36,7 +36,7 @@ import java.util.concurrent.TimeoutException;
*/ */
public class Peer { public class Peer {
private static final Logger log = LoggerFactory.getLogger(Peer.class); private static final Logger log = LoggerFactory.getLogger(Peer.class);
private NetworkConnection conn; private NetworkConnection conn;
private final NetworkParameters params; private final NetworkParameters params;
// Whether the peer loop is supposed to be running or not. Set to false during shutdown so the peer loop // Whether the peer loop is supposed to be running or not. Set to false during shutdown so the peer loop
@ -95,7 +95,7 @@ public class Peer {
* *
* @throws PeerException when there is a temporary problem with the peer and we should retry later * @throws PeerException when there is a temporary problem with the peer and we should retry later
*/ */
public void connect() throws PeerException { public synchronized void connect() throws PeerException {
try { try {
conn = new NetworkConnection(address, params, bestHeight, 60000); conn = new NetworkConnection(address, params, bestHeight, 60000);
} catch (IOException ex) { } catch (IOException ex) {
@ -136,11 +136,11 @@ public class Peer {
} }
} }
} catch (IOException e) { } catch (IOException e) {
disconnect();
if (!running) { if (!running) {
// This exception was expected because we are tearing down the socket as part of quitting. // This exception was expected because we are tearing down the socket as part of quitting.
log.info("Shutting down peer loop"); log.info("Shutting down peer loop");
} else { } else {
disconnect();
throw new PeerException(e); throw new PeerException(e);
} }
} catch (ProtocolException e) { } catch (ProtocolException e) {
@ -398,10 +398,8 @@ public class Peer {
/** /**
* Terminates the network connection and stops the message handling loop. * Terminates the network connection and stops the message handling loop.
*/ */
public void disconnect() { public synchronized void disconnect() {
synchronized (this) { running = false;
running = false;
}
try { try {
// This is the correct way to stop an IO bound loop // This is the correct way to stop an IO bound loop
if (conn != null) if (conn != null)