From 84f282befe7b8b87ed90bafc3c8770e76f045536 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 16 May 2018 08:09:09 +0200 Subject: [PATCH] Add XMPPConnection trySendStanza() --- .../smack/AbstractXMPPConnection.java | 23 +++++++++++ .../jivesoftware/smack/XMPPConnection.java | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java index ad576d00b..71a48fedb 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -343,6 +343,29 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { protected abstract void sendStanzaInternal(Stanza packet) throws NotConnectedException, InterruptedException; + @Override + public boolean trySendStanza(Stanza stanza) throws NotConnectedException { + // Default implementation which falls back to sendStanza() as mentioned in the methods javadoc. May be + // overwritten by subclasses. + try { + sendStanza(stanza); + } catch (InterruptedException e) { + LOGGER.log(Level.FINER, + "Thread blocked in fallback implementation of trySendStanza(Stanza) was interrupted", e); + return false; + } + return true; + } + + @Override + public boolean trySendStanza(Stanza stanza, long timeout, TimeUnit unit) + throws NotConnectedException, InterruptedException { + // Default implementation which falls back to sendStanza() as mentioned in the methods javadoc. May be + // overwritten by subclasses. + sendStanza(stanza); + return true; + } + @Override public abstract void sendNonza(Nonza element) throws NotConnectedException, InterruptedException; diff --git a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index 09767042e..3e6395938 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -16,6 +16,8 @@ */ package org.jivesoftware.smack; +import java.util.concurrent.TimeUnit; + import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; @@ -163,6 +165,44 @@ public interface XMPPConnection { * */ void sendStanza(Stanza stanza) throws NotConnectedException, InterruptedException; + /** + * Try to send the given stanza. Returns {@code true} if the stanza was successfully put into the outgoing stanza + * queue, otherwise, if {@code false} is returned, the stanza could not be scheduled for sending (for example + * because the outgoing element queue is full). Note that this means that the stanza possibly was not put onto the + * wire, even if {@code true} is returned, it just has been successfully scheduled for sending. + *

+ * Note: Implementations are not required to provide that functionality. In that case this method is mapped + * to {@link #sendStanza(Stanza)} and will possibly block until the stanza could be scheduled for sending. + *

+ * + * @param stanza the stanza to send. + * @return {@code true} if the stanza was successfully scheduled to be send, {@code false} otherwise. + * @throws NotConnectedException if the connection is not connected. + * @since 4.4 + */ + boolean trySendStanza(Stanza stanza) throws NotConnectedException; + + /** + * Try to send the given stanza. Returns {@code true} if the stanza was successfully put into the outgoing stanza + * queue within the given timeout period, otherwise, if {@code false} is returned, the stanza could not be scheduled + * for sending (for example because the outgoing element queue is full). Note that this means that the stanza + * possibly was not put onto the wire, even if {@code true} is returned, it just has been successfully scheduled for + * sending. + *

+ * Note: Implementations are not required to provide that functionality. In that case this method is mapped + * to {@link #sendStanza(Stanza)} and will possibly block until the stanza could be scheduled for sending. + *

+ * + * @param stanza the stanza to send. + * @param timeout how long to wait before giving up, in units of {@code unit}. + * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter. + * @return {@code true} if the stanza was successfully scheduled to be send, {@code false} otherwise. + * @throws NotConnectedException if the connection is not connected. + * @throws InterruptedException if the calling thread was interrupted. + * @since 4.4 + */ + boolean trySendStanza(Stanza stanza, long timeout, TimeUnit unit) throws NotConnectedException, InterruptedException; + /** * Send a Nonza. *