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

Improve NoResponseException messages

This commit is contained in:
Florian Schmaus 2015-06-02 17:21:33 +02:00
parent 2079ba6bd6
commit 9a00e09c0a
6 changed files with 37 additions and 21 deletions

View file

@ -194,13 +194,13 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* parsed. * parsed.
*/ */
protected final SynchronizationPoint<Exception> lastFeaturesReceived = new SynchronizationPoint<Exception>( protected final SynchronizationPoint<Exception> lastFeaturesReceived = new SynchronizationPoint<Exception>(
AbstractXMPPConnection.this); AbstractXMPPConnection.this, "last stream features received from server");
/** /**
* Set to success if the sasl feature has been received. * Set to success if the sasl feature has been received.
*/ */
protected final SynchronizationPoint<SmackException> saslFeatureReceived = new SynchronizationPoint<SmackException>( protected final SynchronizationPoint<SmackException> saslFeatureReceived = new SynchronizationPoint<SmackException>(
AbstractXMPPConnection.this); AbstractXMPPConnection.this, "SASL mechanisms stream feature from server");
/** /**
* The SASLAuthentication manager that is responsible for authenticating with the server. * The SASLAuthentication manager that is responsible for authenticating with the server.

View file

@ -207,7 +207,7 @@ public final class SASLAuthentication {
} }
if (!authenticationSuccessful) { if (!authenticationSuccessful) {
throw NoResponseException.newWith(connection); throw NoResponseException.newWith(connection, "successful SASL authentication");
} }
} }

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.
@ -68,6 +68,10 @@ public class SmackException extends Exception {
private final StanzaFilter filter; private final StanzaFilter filter;
private NoResponseException(String message) {
this(message, null);
}
private NoResponseException(String message, StanzaFilter filter) { private NoResponseException(String message, StanzaFilter filter) {
super(message); super(message);
this.filter = filter; this.filter = filter;
@ -82,8 +86,10 @@ public class SmackException extends Exception {
return filter; return filter;
} }
public static NoResponseException newWith(XMPPConnection connection) { public static NoResponseException newWith(XMPPConnection connection, String waitingFor) {
return newWith(connection, (StanzaFilter) null); final StringBuilder sb = getWaitingFor(connection);
sb.append(" While waiting for ").append(waitingFor);
return new NoResponseException(sb.toString());
} }
public static NoResponseException newWith(XMPPConnection connection, public static NoResponseException newWith(XMPPConnection connection,
@ -92,11 +98,8 @@ public class SmackException extends Exception {
} }
public static NoResponseException newWith(XMPPConnection connection, StanzaFilter filter) { public static NoResponseException newWith(XMPPConnection connection, StanzaFilter filter) {
final long replyTimeout = connection.getPacketReplyTimeout(); final StringBuilder sb = getWaitingFor(connection);
final StringBuilder sb = new StringBuilder(256); sb.append(" Waited for response using: ");
sb.append("No response received within reply timeout. Timeout was "
+ replyTimeout + "ms (~"
+ replyTimeout / 1000 + "s). Used filter: ");
if (filter != null) { if (filter != null) {
sb.append(filter.toString()); sb.append(filter.toString());
} }
@ -107,6 +110,14 @@ public class SmackException extends Exception {
return new NoResponseException(sb.toString(), filter); return new NoResponseException(sb.toString(), filter);
} }
private static StringBuilder getWaitingFor(XMPPConnection connection) {
final long replyTimeout = connection.getPacketReplyTimeout();
final StringBuilder sb = new StringBuilder(256);
sb.append("No response received within reply timeout. Timeout was "
+ replyTimeout + "ms (~"
+ replyTimeout / 1000 + "s).");
return sb;
}
} }
public static class NotLoggedInException extends SmackException { public static class NotLoggedInException extends SmackException {

View file

@ -31,6 +31,7 @@ public class SynchronizationPoint<E extends Exception> {
private final AbstractXMPPConnection connection; private final AbstractXMPPConnection connection;
private final Lock connectionLock; private final Lock connectionLock;
private final Condition condition; private final Condition condition;
private final String waitFor;
// Note that there is no need to make 'state' and 'failureException' volatile. Since 'lock' and 'unlock' have the // 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. // same memory synchronization effects as synchronization block enter and leave.
@ -41,11 +42,13 @@ public class SynchronizationPoint<E extends Exception> {
* Construct a new synchronization point for the given connection. * Construct a new synchronization point for the given connection.
* *
* @param connection the connection of this synchronization point. * @param connection the connection of this synchronization point.
* @param waitFor a description of the event this synchronization point handles.
*/ */
public SynchronizationPoint(AbstractXMPPConnection connection) { public SynchronizationPoint(AbstractXMPPConnection connection, String waitFor) {
this.connection = connection; this.connection = connection;
this.connectionLock = connection.getConnectionLock(); this.connectionLock = connection.getConnectionLock();
this.condition = connection.getConnectionLock().newCondition(); this.condition = connection.getConnectionLock().newCondition();
this.waitFor = waitFor;
init(); init();
} }
@ -253,7 +256,7 @@ public class SynchronizationPoint<E extends Exception> {
case Initial: case Initial:
case NoResponse: case NoResponse:
case RequestSent: case RequestSent:
throw NoResponseException.newWith(connection); throw NoResponseException.newWith(connection, waitFor);
case Success: case Success:
return true; return true;
case Failure: case Failure:

View file

@ -114,7 +114,7 @@ public abstract class StreamNegotiator {
} }
if (streamMethodInitiation == null) { if (streamMethodInitiation == null) {
throw NoResponseException.newWith(connection); throw NoResponseException.newWith(connection, "stream initiation");
} }
XMPPErrorException.ifHasErrorThenThrow(streamMethodInitiation); XMPPErrorException.ifHasErrorThenThrow(streamMethodInitiation);
return streamMethodInitiation; return streamMethodInitiation;

View file

@ -174,25 +174,27 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
*/ */
protected PacketReader packetReader; protected PacketReader packetReader;
private final SynchronizationPoint<Exception> initalOpenStreamSend = new SynchronizationPoint<Exception>(this); private final SynchronizationPoint<Exception> initalOpenStreamSend = new SynchronizationPoint<>(
this, "initial open stream element send to server");
/** /**
* *
*/ */
private final SynchronizationPoint<XMPPException> maybeCompressFeaturesReceived = new SynchronizationPoint<XMPPException>( private final SynchronizationPoint<XMPPException> maybeCompressFeaturesReceived = new SynchronizationPoint<XMPPException>(
this); this, "stream compression feature");
/** /**
* *
*/ */
private final SynchronizationPoint<XMPPException> compressSyncPoint = new SynchronizationPoint<XMPPException>( private final SynchronizationPoint<XMPPException> compressSyncPoint = new SynchronizationPoint<XMPPException>(
this); this, "stream compression");
/** /**
* A synchronization point which is successful if this connection has received the closing * A synchronization point which is successful if this connection has received the closing
* stream element from the remote end-point, i.e. the server. * stream element from the remote end-point, i.e. the server.
*/ */
private final SynchronizationPoint<Exception> closingStreamReceived = new SynchronizationPoint<Exception>(this); private final SynchronizationPoint<Exception> closingStreamReceived = new SynchronizationPoint<>(
this, "stream closing element received");
/** /**
* The default bundle and defer callback, used for new connections. * The default bundle and defer callback, used for new connections.
@ -221,10 +223,10 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
private String smSessionId; private String smSessionId;
private final SynchronizationPoint<XMPPException> smResumedSyncPoint = new SynchronizationPoint<XMPPException>( private final SynchronizationPoint<XMPPException> smResumedSyncPoint = new SynchronizationPoint<XMPPException>(
this); this, "stream resumed element");
private final SynchronizationPoint<XMPPException> smEnabledSyncPoint = new SynchronizationPoint<XMPPException>( private final SynchronizationPoint<XMPPException> smEnabledSyncPoint = new SynchronizationPoint<XMPPException>(
this); this, "stream enabled element");
/** /**
* The client's preferred maximum resumption time in seconds. * The client's preferred maximum resumption time in seconds.
@ -1169,7 +1171,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
* Needs to be protected for unit testing purposes. * Needs to be protected for unit testing purposes.
*/ */
protected SynchronizationPoint<NoResponseException> shutdownDone = new SynchronizationPoint<NoResponseException>( protected SynchronizationPoint<NoResponseException> shutdownDone = new SynchronizationPoint<NoResponseException>(
XMPPTCPConnection.this); XMPPTCPConnection.this, "shutdown completed");
/** /**
* If set, the stanza(/packet) writer is shut down * If set, the stanza(/packet) writer is shut down