1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 18:48:00 +01:00

Add javadoc to SynchronizationPoint

This commit is contained in:
Florian Schmaus 2015-04-06 17:37:45 +02:00
parent 5597b1ffc0
commit 57fa631480

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014 Florian Schmaus * Copyright © 2014-2015 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.
@ -41,6 +41,11 @@ public class SynchronizationPoint<E extends Exception> {
private State state; private State state;
private E failureException; private E failureException;
/**
* Construct a new synchronization point for the given connection.
*
* @param connection the connection of this synchronization point.
*/
public SynchronizationPoint(AbstractXMPPConnection connection) { public SynchronizationPoint(AbstractXMPPConnection connection) {
this.connection = connection; this.connection = connection;
this.connectionLock = connection.getConnectionLock(); this.connectionLock = connection.getConnectionLock();
@ -48,6 +53,9 @@ public class SynchronizationPoint<E extends Exception> {
init(); init();
} }
/**
* Initialize (or reset) this synchronization point.
*/
public void init() { public void init() {
connectionLock.lock(); connectionLock.lock();
state = State.Initial; state = State.Initial;
@ -55,6 +63,13 @@ public class SynchronizationPoint<E extends Exception> {
connectionLock.unlock(); connectionLock.unlock();
} }
/**
* 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.
*/
public void sendAndWaitForResponse(TopLevelStreamElement request) throws NoResponseException, public void sendAndWaitForResponse(TopLevelStreamElement request) throws NoResponseException,
NotConnectedException { NotConnectedException {
assert (state == State.Initial); assert (state == State.Initial);
@ -79,6 +94,14 @@ public class SynchronizationPoint<E extends Exception> {
checkForResponse(); checkForResponse();
} }
/**
* 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.
*/
public void sendAndWaitForResponseOrThrow(PlainStreamElement request) throws E, NoResponseException, public void sendAndWaitForResponseOrThrow(PlainStreamElement request) throws E, NoResponseException,
NotConnectedException { NotConnectedException {
sendAndWaitForResponse(request); sendAndWaitForResponse(request);
@ -93,6 +116,11 @@ public class SynchronizationPoint<E extends Exception> {
} }
} }
/**
* 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
*/
public void checkIfSuccessOrWaitOrThrow() throws NoResponseException, E { public void checkIfSuccessOrWaitOrThrow() throws NoResponseException, E {
checkIfSuccessOrWait(); checkIfSuccessOrWait();
if (state == State.Failure) { if (state == State.Failure) {
@ -100,6 +128,10 @@ public class SynchronizationPoint<E extends Exception> {
} }
} }
/**
* 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.
*/
public void checkIfSuccessOrWait() throws NoResponseException { public void checkIfSuccessOrWait() throws NoResponseException {
connectionLock.lock(); connectionLock.lock();
try { try {
@ -114,6 +146,9 @@ public class SynchronizationPoint<E extends Exception> {
checkForResponse(); checkForResponse();
} }
/**
* Report this synchronization point as successful.
*/
public void reportSuccess() { public void reportSuccess() {
connectionLock.lock(); connectionLock.lock();
try { try {
@ -129,6 +164,11 @@ public class SynchronizationPoint<E extends Exception> {
reportFailure(null); reportFailure(null);
} }
/**
* Report this synchronization point as failed because of the given exception.
*
* @param failureException the exception causing this synchronization point to fail.
*/
public void reportFailure(E failureException) { public void reportFailure(E failureException) {
connectionLock.lock(); connectionLock.lock();
try { try {
@ -141,6 +181,11 @@ public class SynchronizationPoint<E extends Exception> {
} }
} }
/**
* Check if this synchronization point was successful.
*
* @return true if the synchronization point was successful, false otherwise.
*/
public boolean wasSuccessful() { public boolean wasSuccessful() {
connectionLock.lock(); connectionLock.lock();
try { try {
@ -151,6 +196,11 @@ public class SynchronizationPoint<E extends Exception> {
} }
} }
/**
* Check if this synchronization point has its request already sent.
*
* @return true if the request was already sent, false otherwise.
*/
public boolean requestSent() { public boolean requestSent() {
connectionLock.lock(); connectionLock.lock();
try { try {
@ -161,6 +211,12 @@ public class SynchronizationPoint<E extends Exception> {
} }
} }
/**
* 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}.
*/
private void waitForConditionOrTimeout() { private void waitForConditionOrTimeout() {
long remainingWait = TimeUnit.MILLISECONDS.toNanos(connection.getPacketReplyTimeout()); long remainingWait = TimeUnit.MILLISECONDS.toNanos(connection.getPacketReplyTimeout());
while (state == State.RequestSent || state == State.Initial) { while (state == State.RequestSent || state == State.Initial) {