When cancelling, assume nothing is instantiated

This commit is contained in:
ameba23
2021-04-15 11:40:07 +02:00
parent b901974488
commit 20df10d7a8
3 changed files with 28 additions and 21 deletions

View File

@@ -9,8 +9,8 @@ import org.briarproject.bramble.api.transport.StreamReaderFactory;
import org.briarproject.bramble.api.transport.StreamWriterFactory;
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@@ -60,16 +60,20 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
@Override
public void cancel() {
cancelled = true;
try {
socket.close();
} catch (IOException e) {
// The reason here is OTHER rather than NO_CONNECTION because
// the socket could fail to close because it is already closed
observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.OTHER));
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// The reason here is OTHER rather than NO_CONNECTION because
// the socket could fail to close because it is already closed
observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.OTHER));
}
}
if (observer != null) {
observer.onStateChanged(
new CustodianTask.State.Failure(State.Failure.Reason.OTHER));
}
observer.onStateChanged(
new CustodianTask.State.Failure(State.Failure.Reason.OTHER));
}
@Override
@@ -146,7 +150,7 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
private void receiveAck() {
try {
InputStream inputStream = socket.getInputStream();
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
// InputStream inputStream = streamReaderFactory
// .createContactExchangeStreamReader(socket.getInputStream(),
// sharedSecret);

View File

@@ -60,9 +60,8 @@ public class ReturnShardTaskImpl {
return message;
}
byte[] read(InputStream inputStream, int length)
byte[] read(DataInputStream dis, int length)
throws IOException {
DataInputStream dis = new DataInputStream(inputStream);
byte[] output = new byte[length];
dis.readFully(output);
return output;

View File

@@ -9,6 +9,7 @@ import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -28,7 +29,6 @@ import static java.util.logging.Logger.getLogger;
public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
implements SecretOwnerTask {
// private final Executor ioExecutor;
private boolean cancelled = false;
private InetSocketAddress socketAddress;
private ClientHelper clientHelper;
@@ -43,9 +43,8 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
@Inject
SecretOwnerTaskImpl(AuthenticatedCipher cipher, CryptoComponent crypto,
@IoExecutor Executor ioExecutor, ClientHelper clientHelper) {
ClientHelper clientHelper) {
super(cipher, crypto);
// this.ioExecutor = ioExecutor;
this.clientHelper = clientHelper;
// this.streamReaderFactory = streamReaderFactory;
// this.streamWriterFactory = streamWriterFactory;
@@ -101,7 +100,7 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
LOG.info("Client connected");
observer.onStateChanged(new State.ReceivingShard());
InputStream inputStream = socket.getInputStream();
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
AgreementPublicKey remotePublicKey =
new AgreementPublicKey(
@@ -156,11 +155,16 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
public void cancel() {
cancelled = true;
LOG.info("Cancel called, failing...");
try {
serverSocket.close();
} catch (IOException e) {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
observer.onStateChanged(new State.Failure());
}
}
if (observer != null) {
observer.onStateChanged(new State.Failure());
}
observer.onStateChanged(new State.Failure());
}
}