1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 14:22:05 +01:00

Fail sync points on exception in XMPPTCPConnection

This commit is contained in:
Florian Schmaus 2019-02-07 20:43:45 +01:00
parent 5c8e830157
commit 09bffb8dca
4 changed files with 68 additions and 14 deletions

View file

@ -235,7 +235,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* stanza is send by the server. This is set to true once the last feature stanza has been * stanza is send by the server. This is set to true once the last feature stanza has been
* parsed. * parsed.
*/ */
protected final SynchronizationPoint<Exception> lastFeaturesReceived = new SynchronizationPoint<Exception>( protected final SynchronizationPoint<SmackException> lastFeaturesReceived = new SynchronizationPoint<>(
AbstractXMPPConnection.this, "last stream features received from server"); AbstractXMPPConnection.this, "last stream features received from server");
/** /**
@ -550,7 +550,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
// - the servers last features stanza has been parsed // - the servers last features stanza has been parsed
// - the timeout occurs // - the timeout occurs
LOGGER.finer("Waiting for last features to be received before continuing with resource binding"); LOGGER.finer("Waiting for last features to be received before continuing with resource binding");
lastFeaturesReceived.checkIfSuccessOrWait(); lastFeaturesReceived.checkIfSuccessOrWaitOrThrow();
if (!hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) { if (!hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) {

View file

@ -334,4 +334,16 @@ public class SmackException extends Exception {
super("Resource binding was not offered by server"); super("Resource binding was not offered by server");
} }
} }
public static class SmackWrappedException extends SmackException {
/**
*
*/
private static final long serialVersionUID = 1L;
public SmackWrappedException(Exception exception) {
super(exception);
}
}
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014-2015 Florian Schmaus * Copyright © 2014-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import java.util.concurrent.locks.Lock;
import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.SmackWrappedException;
import org.jivesoftware.smack.packet.Nonza; import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.TopLevelStreamElement; import org.jivesoftware.smack.packet.TopLevelStreamElement;
@ -37,6 +38,7 @@ public class SynchronizationPoint<E extends Exception> {
// same memory synchronization effects as synchronization block enter and leave. // same memory synchronization effects as synchronization block enter and leave.
private State state; private State state;
private E failureException; private E failureException;
private SmackWrappedException smackWrappedExcpetion;
/** /**
* Construct a new synchronization point for the given connection. * Construct a new synchronization point for the given connection.
@ -59,6 +61,7 @@ public class SynchronizationPoint<E extends Exception> {
connectionLock.lock(); connectionLock.lock();
state = State.Initial; state = State.Initial;
failureException = null; failureException = null;
smackWrappedExcpetion = null;
connectionLock.unlock(); connectionLock.unlock();
} }
@ -71,7 +74,7 @@ public class SynchronizationPoint<E extends Exception> {
* @throws InterruptedException if the connection is interrupted. * @throws InterruptedException if the connection is interrupted.
* @return <code>null</code> if synchronization point was successful, or the failure Exception. * @return <code>null</code> if synchronization point was successful, or the failure Exception.
*/ */
public E sendAndWaitForResponse(TopLevelStreamElement request) throws NoResponseException, public Exception sendAndWaitForResponse(TopLevelStreamElement request) throws NoResponseException,
NotConnectedException, InterruptedException { NotConnectedException, InterruptedException {
assert (state == State.Initial); assert (state == State.Initial);
connectionLock.lock(); connectionLock.lock();
@ -103,15 +106,14 @@ public class SynchronizationPoint<E extends Exception> {
* @throws NoResponseException if no response was received. * @throws NoResponseException if no response was received.
* @throws NotConnectedException if the connection is not connected. * @throws NotConnectedException if the connection is not connected.
* @throws InterruptedException if the connection is interrupted. * @throws InterruptedException if the connection is interrupted.
* @throws SmackWrappedException in case of a wrapped exception;
*/ */
public void sendAndWaitForResponseOrThrow(Nonza request) throws E, NoResponseException, public void sendAndWaitForResponseOrThrow(Nonza request) throws E, NoResponseException,
NotConnectedException, InterruptedException { NotConnectedException, InterruptedException, SmackWrappedException {
sendAndWaitForResponse(request); sendAndWaitForResponse(request);
switch (state) { switch (state) {
case Failure: case Failure:
if (failureException != null) { throwException();
throw failureException;
}
break; break;
default: default:
// Success, do nothing // Success, do nothing
@ -123,11 +125,12 @@ public class SynchronizationPoint<E extends Exception> {
* @throws NoResponseException if there was no response marking the synchronization point as success or failed. * @throws NoResponseException if there was no response marking the synchronization point as success or failed.
* @throws E if there was a failure * @throws E if there was a failure
* @throws InterruptedException if the connection is interrupted. * @throws InterruptedException if the connection is interrupted.
* @throws SmackWrappedException in case of a wrapped exception;
*/ */
public void checkIfSuccessOrWaitOrThrow() throws NoResponseException, E, InterruptedException { public void checkIfSuccessOrWaitOrThrow() throws NoResponseException, E, InterruptedException, SmackWrappedException {
checkIfSuccessOrWait(); checkIfSuccessOrWait();
if (state == State.Failure) { if (state == State.Failure) {
throw failureException; throwException();
} }
} }
@ -137,7 +140,7 @@ public class SynchronizationPoint<E extends Exception> {
* @throws InterruptedException * @throws InterruptedException
* @return <code>null</code> if synchronization point was successful, or the failure Exception. * @return <code>null</code> if synchronization point was successful, or the failure Exception.
*/ */
public E checkIfSuccessOrWait() throws NoResponseException, InterruptedException { public Exception checkIfSuccessOrWait() throws NoResponseException, InterruptedException {
connectionLock.lock(); connectionLock.lock();
try { try {
switch (state) { switch (state) {
@ -145,7 +148,7 @@ public class SynchronizationPoint<E extends Exception> {
case Success: case Success:
return null; return null;
case Failure: case Failure:
return failureException; return getException();
default: default:
// Do nothing // Do nothing
break; break;
@ -198,6 +201,24 @@ public class SynchronizationPoint<E extends Exception> {
} }
} }
/**
* Report this synchronization point as failed because of the given exception. The {@code failureException} must be set.
*
* @param exception the exception causing this synchronization point to fail.
*/
public void reportGenericFailure(SmackWrappedException exception) {
assert exception != null;
connectionLock.lock();
try {
state = State.Failure;
this.smackWrappedExcpetion = exception;
condition.signalAll();
}
finally {
connectionLock.unlock();
}
}
/** /**
* Check if this synchronization point was successful. * Check if this synchronization point was successful.
* *
@ -256,6 +277,20 @@ public class SynchronizationPoint<E extends Exception> {
} }
} }
private Exception getException() {
if (failureException != null) {
return failureException;
}
return smackWrappedExcpetion;
}
private void throwException() throws E, SmackWrappedException {
if (failureException != null) {
throw failureException;
}
throw smackWrappedExcpetion;
}
/** /**
* Check for a response and throw a {@link NoResponseException} if there was none. * Check for a response and throw a {@link NoResponseException} if there was none.
* <p> * <p>
@ -264,7 +299,7 @@ public class SynchronizationPoint<E extends Exception> {
* @return <code>true</code> if synchronization point was successful, <code>false</code> on failure. * @return <code>true</code> if synchronization point was successful, <code>false</code> on failure.
* @throws NoResponseException * @throws NoResponseException
*/ */
private E checkForResponse() throws NoResponseException { private Exception checkForResponse() throws NoResponseException {
switch (state) { switch (state) {
case Initial: case Initial:
case NoResponse: case NoResponse:
@ -273,7 +308,7 @@ public class SynchronizationPoint<E extends Exception> {
case Success: case Success:
return null; return null;
case Failure: case Failure:
return failureException; return getException();
default: default:
throw new AssertionError("Unknown state " + state); throw new AssertionError("Unknown state " + state);
} }

View file

@ -84,6 +84,7 @@ import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.NotLoggedInException; import org.jivesoftware.smack.SmackException.NotLoggedInException;
import org.jivesoftware.smack.SmackException.SecurityRequiredByServerException; import org.jivesoftware.smack.SmackException.SecurityRequiredByServerException;
import org.jivesoftware.smack.SmackException.SmackWrappedException;
import org.jivesoftware.smack.SmackFuture; import org.jivesoftware.smack.SmackFuture;
import org.jivesoftware.smack.StanzaListener; import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.SynchronizationPoint; import org.jivesoftware.smack.SynchronizationPoint;
@ -937,6 +938,12 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
if ((packetReader == null || packetReader.done) && if ((packetReader == null || packetReader.done) &&
(packetWriter == null || packetWriter.done())) return; (packetWriter == null || packetWriter.done())) return;
SmackWrappedException smackWrappedException = new SmackWrappedException(e);
tlsHandled.reportGenericFailure(smackWrappedException);
saslFeatureReceived.reportGenericFailure(smackWrappedException);
maybeCompressFeaturesReceived.reportGenericFailure(smackWrappedException);
lastFeaturesReceived.reportGenericFailure(smackWrappedException);
// Closes the connection temporary. A reconnection is possible // Closes the connection temporary. A reconnection is possible
// Note that a connection listener of XMPPTCPConnection will drop the SM state in // Note that a connection listener of XMPPTCPConnection will drop the SM state in
// case the Exception is a StreamErrorException. // case the Exception is a StreamErrorException.