mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 17:19:39 +02:00
Add PingManager.pingAsync(Jid, long)
and SmackFuture API.
This commit is contained in:
parent
cb971f2e10
commit
5f900d3713
5 changed files with 351 additions and 2 deletions
170
smack-core/src/main/java/org/jivesoftware/smack/SmackFuture.java
Normal file
170
smack-core/src/main/java/org/jivesoftware/smack/SmackFuture.java
Normal file
|
@ -0,0 +1,170 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Florian Schmaus
|
||||
*
|
||||
* 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.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.packet.Stanza;
|
||||
|
||||
public abstract class SmackFuture<V> implements Future<V> {
|
||||
|
||||
private boolean cancelled;
|
||||
|
||||
private V result;
|
||||
|
||||
protected Exception exception;
|
||||
|
||||
private SuccessCallback<V> successCallback;
|
||||
|
||||
private ExceptionCallback exceptionCallback;
|
||||
|
||||
@Override
|
||||
public synchronized final boolean cancel(boolean mayInterruptIfRunning) {
|
||||
if (isDone()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cancelled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized final boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized final boolean isDone() {
|
||||
return result != null;
|
||||
}
|
||||
|
||||
public void onSuccessOrError(SuccessCallback<V> successCallback, ExceptionCallback exceptionCallback) {
|
||||
this.successCallback = successCallback;
|
||||
this.exceptionCallback = exceptionCallback;
|
||||
|
||||
maybeInvokeCallbacks();
|
||||
}
|
||||
|
||||
public void onSuccess(SuccessCallback<V> successCallback) {
|
||||
onSuccessOrError(successCallback, null);
|
||||
}
|
||||
|
||||
public void onError(ExceptionCallback exceptionCallback) {
|
||||
onSuccessOrError(null, exceptionCallback);
|
||||
}
|
||||
|
||||
private final V getResultOrThrow() throws ExecutionException {
|
||||
assert (result != null || exception != null);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new ExecutionException(exception);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized final V get() throws InterruptedException, ExecutionException {
|
||||
while (result == null && exception == null) {
|
||||
wait();
|
||||
}
|
||||
|
||||
return getResultOrThrow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized final V get(long timeout, TimeUnit unit)
|
||||
throws InterruptedException, ExecutionException, TimeoutException {
|
||||
final long deadline = System.currentTimeMillis() + unit.toMillis(timeout);
|
||||
while (result != null && exception != null) {
|
||||
final long waitTimeRemaining = deadline - System.currentTimeMillis();
|
||||
if (waitTimeRemaining > 0) {
|
||||
wait(waitTimeRemaining);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null || exception == null) {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
|
||||
return getResultOrThrow();
|
||||
}
|
||||
|
||||
protected final synchronized void maybeInvokeCallbacks() {
|
||||
if (result != null && successCallback != null) {
|
||||
successCallback.onSuccess(result);
|
||||
} else if (exception != null && exceptionCallback != null) {
|
||||
exceptionCallback.processException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks if the given exception is <b>not</b> fatal. If this method returns <code>false</code>, then
|
||||
* the future will automatically set the given exception as failure reason and notify potential waiting threads.
|
||||
*
|
||||
* @param exception the exception to check.
|
||||
* @return <code>true</code> if the exception is not fatal, <code>false</code> otherwise.
|
||||
*/
|
||||
protected abstract boolean isNonFatalException(Exception exception);
|
||||
|
||||
protected abstract void handleStanza(Stanza stanza) throws NotConnectedException, InterruptedException;
|
||||
|
||||
protected final void setResult(V result) {
|
||||
assert (Thread.holdsLock(this));
|
||||
|
||||
this.result = result;
|
||||
this.notifyAll();
|
||||
|
||||
maybeInvokeCallbacks();
|
||||
}
|
||||
|
||||
public static abstract class InternalSmackFuture<V> extends SmackFuture<V> implements StanzaListener, ExceptionCallback {
|
||||
|
||||
@Override
|
||||
public synchronized final void processException(Exception exception) {
|
||||
if (!isNonFatalException(exception)) {
|
||||
this.exception = exception;
|
||||
this.notifyAll();
|
||||
|
||||
maybeInvokeCallbacks();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for {@link #handleStanza(Stanza)}. Note that this method is <code>synchronized</code>.
|
||||
*/
|
||||
@Override
|
||||
public synchronized final void processStanza(Stanza stanza) throws NotConnectedException, InterruptedException {
|
||||
handleStanza(stanza);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple version of InternalSmackFuture which implements {@link #isNonFatalException(Exception)} as always returning <code>false</code> method.
|
||||
*
|
||||
* @param <V>
|
||||
*/
|
||||
public static abstract class SimpleInternalSmackFuture<V> extends InternalSmackFuture<V> {
|
||||
@Override
|
||||
protected boolean isNonFatalException(Exception exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Florian Schmaus
|
||||
*
|
||||
* 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;
|
||||
|
||||
public interface SuccessCallback<T> {
|
||||
|
||||
public void onSuccess(T result);
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Florian Schmaus
|
||||
*
|
||||
* 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 static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.SmackFuture.InternalSmackFuture;
|
||||
import org.jivesoftware.smack.SmackFuture.SimpleInternalSmackFuture;
|
||||
import org.jivesoftware.smack.packet.Stanza;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SmackFutureTest {
|
||||
|
||||
@Test
|
||||
public void simpleSmackFutureSuccessTest() throws NotConnectedException, InterruptedException, ExecutionException {
|
||||
InternalSmackFuture<Boolean> future = new SimpleInternalSmackFuture<Boolean>() {
|
||||
@Override
|
||||
protected void handleStanza(Stanza stanza) throws NotConnectedException, InterruptedException {
|
||||
setResult(true);
|
||||
}
|
||||
};
|
||||
|
||||
future.processStanza(null);
|
||||
|
||||
assertTrue(future.get());
|
||||
}
|
||||
|
||||
@Test(expected = TimeoutException.class)
|
||||
public void simpleSmackFutureTimeoutTest() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
InternalSmackFuture<Boolean> future = new SimpleInternalSmackFuture<Boolean>() {
|
||||
@Override
|
||||
protected void handleStanza(Stanza stanza) throws NotConnectedException, InterruptedException {
|
||||
}
|
||||
};
|
||||
|
||||
future.get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue