2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-02-07 20:43:45 +01:00
|
|
|
* Copyright © 2014-2019 Florian Schmaus
|
2014-09-11 09:49:16 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.locks.Condition;
|
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2019-02-07 20:43:45 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.SmackWrappedException;
|
2015-07-14 22:41:33 +02:00
|
|
|
import org.jivesoftware.smack.packet.Nonza;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
|
|
|
import org.jivesoftware.smack.packet.TopLevelStreamElement;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
public class SynchronizationPoint<E extends Exception> {
|
|
|
|
|
|
|
|
private final AbstractXMPPConnection connection;
|
|
|
|
private final Lock connectionLock;
|
|
|
|
private final Condition condition;
|
2015-06-02 17:21:33 +02:00
|
|
|
private final String waitFor;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
2014-12-17 10:29:48 +01:00
|
|
|
// Note that there is no need to make 'state' and 'failureException' volatile. Since 'lock' and 'unlock' have the
|
|
|
|
// same memory synchronization effects as synchronization block enter and leave.
|
2014-09-11 09:49:16 +02:00
|
|
|
private State state;
|
|
|
|
private E failureException;
|
2019-02-07 20:43:45 +01:00
|
|
|
private SmackWrappedException smackWrappedExcpetion;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
private volatile long waitStart;
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Construct a new synchronization point for the given connection.
|
|
|
|
*
|
|
|
|
* @param connection the connection of this synchronization point.
|
2015-06-02 17:21:33 +02:00
|
|
|
* @param waitFor a description of the event this synchronization point handles.
|
2015-04-06 17:37:45 +02:00
|
|
|
*/
|
2015-06-02 17:21:33 +02:00
|
|
|
public SynchronizationPoint(AbstractXMPPConnection connection, String waitFor) {
|
2014-09-11 09:49:16 +02:00
|
|
|
this.connection = connection;
|
|
|
|
this.connectionLock = connection.getConnectionLock();
|
|
|
|
this.condition = connection.getConnectionLock().newCondition();
|
2015-06-02 17:21:33 +02:00
|
|
|
this.waitFor = waitFor;
|
2014-09-11 09:49:16 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Initialize (or reset) this synchronization point.
|
|
|
|
*/
|
2018-10-31 16:06:31 +01:00
|
|
|
@SuppressWarnings("LockNotBeforeTry")
|
2014-09-11 09:49:16 +02:00
|
|
|
public void init() {
|
2014-12-17 10:29:48 +01:00
|
|
|
connectionLock.lock();
|
2014-09-11 09:49:16 +02:00
|
|
|
state = State.Initial;
|
|
|
|
failureException = null;
|
2019-02-07 20:43:45 +01:00
|
|
|
smackWrappedExcpetion = null;
|
2014-12-17 10:29:48 +01:00
|
|
|
connectionLock.unlock();
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Send the given top level stream element and wait for a response.
|
|
|
|
*
|
|
|
|
* @param request the plain stream element to send.
|
|
|
|
* @throws NoResponseException if no response was received.
|
|
|
|
* @throws NotConnectedException if the connection is not connected.
|
2017-12-23 20:21:19 +01:00
|
|
|
* @throws InterruptedException if the connection is interrupted.
|
2015-06-13 12:35:36 +02:00
|
|
|
* @return <code>null</code> if synchronization point was successful, or the failure Exception.
|
2015-04-06 17:37:45 +02:00
|
|
|
*/
|
2019-02-07 20:43:45 +01:00
|
|
|
public Exception sendAndWaitForResponse(TopLevelStreamElement request) throws NoResponseException,
|
2015-02-14 09:43:44 +01:00
|
|
|
NotConnectedException, InterruptedException {
|
2019-07-24 09:18:39 +02:00
|
|
|
assert state == State.Initial;
|
2014-09-11 09:49:16 +02:00
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
|
|
|
if (request != null) {
|
2015-02-05 11:17:27 +01:00
|
|
|
if (request instanceof Stanza) {
|
2015-03-04 21:44:43 +01:00
|
|
|
connection.sendStanza((Stanza) request);
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
2017-05-23 16:45:04 +02:00
|
|
|
else if (request instanceof Nonza) {
|
2015-07-14 22:41:33 +02:00
|
|
|
connection.sendNonza((Nonza) request);
|
2014-09-11 09:49:16 +02:00
|
|
|
} else {
|
|
|
|
throw new IllegalStateException("Unsupported element type");
|
|
|
|
}
|
|
|
|
state = State.RequestSent;
|
|
|
|
}
|
|
|
|
waitForConditionOrTimeout();
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
2015-05-27 17:36:04 +02:00
|
|
|
return checkForResponse();
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Send the given plain stream element and wait for a response.
|
|
|
|
*
|
|
|
|
* @param request the plain stream element to send.
|
|
|
|
* @throws E if an failure was reported.
|
|
|
|
* @throws NoResponseException if no response was received.
|
|
|
|
* @throws NotConnectedException if the connection is not connected.
|
2017-12-23 20:21:19 +01:00
|
|
|
* @throws InterruptedException if the connection is interrupted.
|
2019-02-07 20:43:45 +01:00
|
|
|
* @throws SmackWrappedException in case of a wrapped exception;
|
2015-04-06 17:37:45 +02:00
|
|
|
*/
|
2015-07-14 22:41:33 +02:00
|
|
|
public void sendAndWaitForResponseOrThrow(Nonza request) throws E, NoResponseException,
|
2019-02-07 20:43:45 +01:00
|
|
|
NotConnectedException, InterruptedException, SmackWrappedException {
|
2014-09-11 09:49:16 +02:00
|
|
|
sendAndWaitForResponse(request);
|
|
|
|
switch (state) {
|
|
|
|
case Failure:
|
2019-02-07 20:43:45 +01:00
|
|
|
throwException();
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Success, do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Check if this synchronization point is successful or wait the connections reply timeout.
|
|
|
|
* @throws NoResponseException if there was no response marking the synchronization point as success or failed.
|
|
|
|
* @throws E if there was a failure
|
2017-12-23 20:21:19 +01:00
|
|
|
* @throws InterruptedException if the connection is interrupted.
|
2019-02-07 20:43:45 +01:00
|
|
|
* @throws SmackWrappedException in case of a wrapped exception;
|
2015-04-06 17:37:45 +02:00
|
|
|
*/
|
2019-02-07 20:43:45 +01:00
|
|
|
public void checkIfSuccessOrWaitOrThrow() throws NoResponseException, E, InterruptedException, SmackWrappedException {
|
2014-09-11 09:49:16 +02:00
|
|
|
checkIfSuccessOrWait();
|
|
|
|
if (state == State.Failure) {
|
2019-02-07 20:43:45 +01:00
|
|
|
throwException();
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Check if this synchronization point is successful or wait the connections reply timeout.
|
|
|
|
* @throws NoResponseException if there was no response marking the synchronization point as success or failed.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2015-06-13 12:35:36 +02:00
|
|
|
* @return <code>null</code> if synchronization point was successful, or the failure Exception.
|
2015-04-06 17:37:45 +02:00
|
|
|
*/
|
2019-02-07 20:43:45 +01:00
|
|
|
public Exception checkIfSuccessOrWait() throws NoResponseException, InterruptedException {
|
2014-09-11 09:49:16 +02:00
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
2015-05-27 17:36:04 +02:00
|
|
|
switch (state) {
|
|
|
|
// Return immediately on success or failure
|
|
|
|
case Success:
|
2015-06-13 12:35:36 +02:00
|
|
|
return null;
|
2015-05-27 17:36:04 +02:00
|
|
|
case Failure:
|
2019-02-07 20:43:45 +01:00
|
|
|
return getException();
|
2015-05-27 17:36:04 +02:00
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
waitForConditionOrTimeout();
|
|
|
|
} finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
2015-05-27 17:36:04 +02:00
|
|
|
return checkForResponse();
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Report this synchronization point as successful.
|
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
public void reportSuccess() {
|
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
|
|
|
state = State.Success;
|
2015-04-06 17:38:51 +02:00
|
|
|
condition.signalAll();
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 08:52:48 +02:00
|
|
|
/**
|
2015-04-12 18:26:21 +02:00
|
|
|
* Deprecated.
|
2015-04-07 08:52:48 +02:00
|
|
|
* @deprecated use {@link #reportFailure(Exception)} instead.
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2014-09-11 09:49:16 +02:00
|
|
|
public void reportFailure() {
|
|
|
|
reportFailure(null);
|
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
2015-04-07 08:52:48 +02:00
|
|
|
* Report this synchronization point as failed because of the given exception. The {@code failureException} must be set.
|
2015-04-06 17:37:45 +02:00
|
|
|
*
|
|
|
|
* @param failureException the exception causing this synchronization point to fail.
|
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
public void reportFailure(E failureException) {
|
2015-04-07 08:52:48 +02:00
|
|
|
assert failureException != null;
|
2014-09-11 09:49:16 +02:00
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
|
|
|
state = State.Failure;
|
|
|
|
this.failureException = failureException;
|
2015-04-06 17:38:51 +02:00
|
|
|
condition.signalAll();
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:43:45 +01:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Check if this synchronization point was successful.
|
|
|
|
*
|
|
|
|
* @return true if the synchronization point was successful, false otherwise.
|
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
public boolean wasSuccessful() {
|
2014-12-17 10:29:48 +01:00
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
|
|
|
return state == State.Success;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 18:20:55 +01:00
|
|
|
public boolean isNotInInitialState() {
|
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
|
|
|
return state != State.Initial;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Check if this synchronization point has its request already sent.
|
|
|
|
*
|
|
|
|
* @return true if the request was already sent, false otherwise.
|
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
public boolean requestSent() {
|
2014-12-17 10:29:48 +01:00
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
|
|
|
return state == State.RequestSent;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 14:55:38 +01:00
|
|
|
public E getFailureException() {
|
|
|
|
connectionLock.lock();
|
|
|
|
try {
|
|
|
|
return failureException;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
connectionLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
public void resetTimeout() {
|
|
|
|
waitStart = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
2015-04-06 17:37:45 +02:00
|
|
|
/**
|
|
|
|
* Wait for the condition to become something else as {@link State#RequestSent} or {@link State#Initial}.
|
|
|
|
* {@link #reportSuccess()}, {@link #reportFailure()} and {@link #reportFailure(Exception)} will either set this
|
|
|
|
* synchronization point to {@link State#Success} or {@link State#Failure}. If none of them is set after the
|
|
|
|
* connections reply timeout, this method will set the state of {@link State#NoResponse}.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2015-04-06 17:37:45 +02:00
|
|
|
*/
|
2015-04-12 18:33:43 +02:00
|
|
|
private void waitForConditionOrTimeout() throws InterruptedException {
|
2019-02-04 08:59:39 +01:00
|
|
|
waitStart = System.currentTimeMillis();
|
2014-09-11 09:49:16 +02:00
|
|
|
while (state == State.RequestSent || state == State.Initial) {
|
2019-02-04 08:59:39 +01:00
|
|
|
long timeout = connection.getReplyTimeout();
|
|
|
|
long remainingWaitMillis = timeout - (System.currentTimeMillis() - waitStart);
|
|
|
|
long remainingWait = TimeUnit.MILLISECONDS.toNanos(remainingWaitMillis);
|
|
|
|
|
2015-04-12 18:33:43 +02:00
|
|
|
if (remainingWait <= 0) {
|
|
|
|
state = State.NoResponse;
|
|
|
|
break;
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
2019-02-04 08:59:39 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
condition.awaitNanos(remainingWait);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
state = State.Interrupted;
|
|
|
|
throw e;
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:43:45 +01:00
|
|
|
private Exception getException() {
|
|
|
|
if (failureException != null) {
|
|
|
|
return failureException;
|
|
|
|
}
|
|
|
|
return smackWrappedExcpetion;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void throwException() throws E, SmackWrappedException {
|
|
|
|
if (failureException != null) {
|
|
|
|
throw failureException;
|
|
|
|
}
|
|
|
|
throw smackWrappedExcpetion;
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
* Check for a response and throw a {@link NoResponseException} if there was none.
|
|
|
|
* <p>
|
|
|
|
* The exception is thrown, if state is one of 'Initial', 'NoResponse' or 'RequestSent'
|
|
|
|
* </p>
|
2015-06-13 12:35:36 +02:00
|
|
|
* @return <code>true</code> if synchronization point was successful, <code>false</code> on failure.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NoResponseException if there was no response from the remote entity.
|
2014-09-11 09:49:16 +02:00
|
|
|
*/
|
2019-02-07 20:43:45 +01:00
|
|
|
private Exception checkForResponse() throws NoResponseException {
|
2014-09-11 09:49:16 +02:00
|
|
|
switch (state) {
|
|
|
|
case Initial:
|
|
|
|
case NoResponse:
|
|
|
|
case RequestSent:
|
2015-06-02 17:21:33 +02:00
|
|
|
throw NoResponseException.newWith(connection, waitFor);
|
2015-05-27 17:36:04 +02:00
|
|
|
case Success:
|
2015-06-13 12:35:36 +02:00
|
|
|
return null;
|
2015-05-27 17:36:04 +02:00
|
|
|
case Failure:
|
2019-02-07 20:43:45 +01:00
|
|
|
return getException();
|
2014-09-11 09:49:16 +02:00
|
|
|
default:
|
2015-05-27 17:36:04 +02:00
|
|
|
throw new AssertionError("Unknown state " + state);
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum State {
|
|
|
|
Initial,
|
|
|
|
RequestSent,
|
|
|
|
NoResponse,
|
|
|
|
Success,
|
|
|
|
Failure,
|
2019-02-04 08:59:39 +01:00
|
|
|
Interrupted,
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
}
|