Class AbstractXMPPConnection

  • All Implemented Interfaces:
    XMPPConnection
    Direct Known Subclasses:
    ModularXmppClientToServerConnection

    public abstract class AbstractXMPPConnection
    extends java.lang.Object
    implements XMPPConnection
    This abstract class is commonly used as super class for XMPP connection mechanisms like TCP and BOSH. Hence it provides the methods for connection state management, like connect(), login() and disconnect() (which are deliberately not provided by the XMPPConnection interface).

    Note: The default entry point to Smack's documentation is XMPPConnection. If you are getting started with Smack, then head over to XMPPConnection and the come back here.

    Parsing Exceptions

    In case a Smack parser (Provider) throws those exceptions are handled over to the ParsingExceptionCallback. A common cause for a provider throwing is illegal input, for example a non-numeric String where only Integers are allowed. Smack's default behavior follows the "fail-hard per default" principle leading to a termination of the connection on parsing exceptions. This default was chosen to make users eventually aware that they should configure their own callback and handle those exceptions to prevent the disconnect. Handle a parsing exception could be as simple as using a non-throwing no-op callback, which would cause the faulty stream element to be taken out of the stream, i.e., Smack behaves like that element was never received.

    If the parsing exception is because Smack received illegal input, then please consider informing the authors of the originating entity about that. If it was thrown because of an bug in a Smack parser, then please consider filling a bug with Smack.

    Managing the parsing exception callback

    The "fail-hard per default" behavior is achieved by using the ExceptionThrowingCallbackWithHint as default parsing exception callback. You can change the behavior using setParsingExceptionCallback(ParsingExceptionCallback) to set a new callback. Use SmackConfiguration.setDefaultParsingExceptionCallback(ParsingExceptionCallback) to set the default callback.

    • Field Detail

      • SMACK_REACTOR

        protected static final SmackReactor SMACK_REACTOR
      • connectionListeners

        protected final java.util.Set<ConnectionListener> connectionListeners
        A collection of ConnectionListeners which listen for connection closing and reconnection events.
      • outgoingStreamXmlEnvironment

        protected XmlEnvironment outgoingStreamXmlEnvironment
      • connectionLock

        protected final java.util.concurrent.locks.Lock connectionLock
      • streamFeatures

        protected final java.util.Map<javax.xml.namespace.QName,​FullyQualifiedElement> streamFeatures
      • user

        protected org.jxmpp.jid.EntityFullJid user
        The full JID of the authenticated user, as returned by the resource binding response of the server.

        It is important that we don't infer the user from the login() arguments and the configurations service name, as, for example, when SASL External is used, the username is not given to login but taken from the 'external' certificate.

      • connected

        protected boolean connected
      • streamId

        protected java.lang.String streamId
        The stream ID, see RFC 6120 ยง 4.7.3
      • debugger

        protected final SmackDebugger debugger
        The SmackDebugger allows to log and debug XML traffic.
      • reader

        protected java.io.Reader reader
        The Reader which is used for the debugger.
      • writer

        protected java.io.Writer writer
        The Writer which is used for the debugger.
      • currentSmackException

        protected SmackException currentSmackException
      • currentXmppException

        protected XMPPException currentXmppException
      • tlsHandled

        protected boolean tlsHandled
      • lastFeaturesReceived

        protected boolean lastFeaturesReceived
        Set to true if the last features stanza from the server has been parsed. A XMPP connection handshake can invoke multiple features stanzas, e.g. when TLS is activated a second feature stanza is send by the server. This is set to true once the last feature stanza has been parsed.
      • saslFeatureReceived

        protected boolean saslFeatureReceived
        Set to true if the SASL feature has been received.
      • closingStreamReceived

        protected boolean closingStreamReceived
        A synchronization point which is successful if this connection has received the closing stream element from the remote end-point, i.e. the server.
      • connectionCounterValue

        protected final int connectionCounterValue
        A number to uniquely identify connections that are created. This is distinct from the connection ID, which is a value sent by the server once a connection is made.
      • config

        protected final ConnectionConfiguration config
        Holds the initial configuration used while creating the connection.
      • host

        protected java.lang.String host
        The used host to establish the connection to
      • port

        protected UInt16 port
        The used port to establish the connection to
      • authenticated

        protected boolean authenticated
        Flag that indicates if the user is currently authenticated with the server.
      • authenticatedConnectionInitiallyEstablishedTimestamp

        protected long authenticatedConnectionInitiallyEstablishedTimestamp
      • wasAuthenticated

        protected boolean wasAuthenticated
        Flag that indicates if the user was authenticated with the server when the connection to the server was closed (abruptly or not).
    • Constructor Detail

      • AbstractXMPPConnection

        protected AbstractXMPPConnection​(ConnectionConfiguration configuration)
        Create a new XMPPConnection to an XMPP server.
        Parameters:
        configuration - The configuration which is used to establish the connection.
    • Method Detail

      • getConfiguration

        public ConnectionConfiguration getConfiguration()
        Get the connection configuration used by this connection.
        Returns:
        the connection configuration.
      • getXMPPServiceDomain

        public org.jxmpp.jid.DomainBareJid getXMPPServiceDomain()
        Description copied from interface: XMPPConnection
        Returns the XMPP Domain of the service provided by the XMPP server and used for this connection. After authenticating with the server the returned value may be different.
        Specified by:
        getXMPPServiceDomain in interface XMPPConnection
        Returns:
        the XMPP domain of this XMPP session.
      • getHost

        public java.lang.String getHost()
        Description copied from interface: XMPPConnection
        Returns the host name of the server where the XMPP server is running. This would be the IP address of the server or a name that may be resolved by a DNS server.
        Specified by:
        getHost in interface XMPPConnection
        Returns:
        the host name of the server where the XMPP server is running or null if not yet connected.
      • getPort

        public int getPort()
        Description copied from interface: XMPPConnection
        Returns the port number of the XMPP server for this connection. The default port for normal connections is 5222.
        Specified by:
        getPort in interface XMPPConnection
        Returns:
        the port number of the XMPP server or 0 if not yet connected.
      • isSecureConnection

        public abstract boolean isSecureConnection()
        Description copied from interface: XMPPConnection
        Returns true if the connection to the server has successfully negotiated encryption.
        Specified by:
        isSecureConnection in interface XMPPConnection
        Returns:
        true if a secure connection to the server.
      • trySendStanza

        public boolean trySendStanza​(Stanza stanza)
                              throws SmackException.NotConnectedException
        Description copied from interface: XMPPConnection
        Try to send the given stanza. Returns true if the stanza was successfully put into the outgoing stanza queue, otherwise, if 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 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 XMPPConnection.sendStanza(Stanza) and will possibly block until the stanza could be scheduled for sending.

        Specified by:
        trySendStanza in interface XMPPConnection
        Parameters:
        stanza - the stanza to send.
        Returns:
        true if the stanza was successfully scheduled to be send, false otherwise.
        Throws:
        SmackException.NotConnectedException - if the connection is not connected.
      • trySendStanza

        public boolean trySendStanza​(Stanza stanza,
                                     long timeout,
                                     java.util.concurrent.TimeUnit unit)
                              throws SmackException.NotConnectedException,
                                     java.lang.InterruptedException
        Description copied from interface: XMPPConnection
        Try to send the given stanza. Returns true if the stanza was successfully put into the outgoing stanza queue within the given timeout period, otherwise, if 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 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 XMPPConnection.sendStanza(Stanza) and will possibly block until the stanza could be scheduled for sending.

        Specified by:
        trySendStanza in interface XMPPConnection
        Parameters:
        stanza - the stanza to send.
        timeout - how long to wait before giving up, in units of unit.
        unit - a TimeUnit determining how to interpret the timeout parameter.
        Returns:
        true if the stanza was successfully scheduled to be send, false otherwise.
        Throws:
        SmackException.NotConnectedException - if the connection is not connected.
        java.lang.InterruptedException - if the calling thread was interrupted.
      • sendNonza

        public abstract void sendNonza​(Nonza element)
                                throws SmackException.NotConnectedException,
                                       java.lang.InterruptedException
        Description copied from interface: XMPPConnection
        Send a Nonza.

        This method is not meant for end-user usage! It allows sending plain stream elements, which should not be done by a user manually. Doing so may result in a unstable or unusable connection. Certain Smack APIs use this method to send plain stream elements.

        Specified by:
        sendNonza in interface XMPPConnection
        Parameters:
        element - the Nonza to send.
        Throws:
        SmackException.NotConnectedException - if the XMPP connection is not connected.
        java.lang.InterruptedException - if the calling thread was interrupted.
      • isUsingCompression

        public abstract boolean isUsingCompression()
        Description copied from interface: XMPPConnection
        Returns true if network traffic is being compressed. When using stream compression network traffic can be reduced up to 90%. Therefore, stream compression is ideal when using a slow speed network connection. However, the server will need to use more CPU time in order to un/compress network data so under high load the server performance might be affected.
        Specified by:
        isUsingCompression in interface XMPPConnection
        Returns:
        true if network traffic is being compressed.
      • initState

        protected void initState()
      • connect

        public AbstractXMPPConnection connect()
                                       throws SmackException,
                                              java.io.IOException,
                                              XMPPException,
                                              java.lang.InterruptedException
        Establishes a connection to the XMPP server. It basically creates and maintains a connection to the server.

        Listeners will be preserved from a previous connection.

        Returns:
        a reference to this object, to chain connect() with login().
        Throws:
        XMPPException - if an error occurs on the XMPP protocol level.
        SmackException - if an error occurs somewhere else besides XMPP protocol level.
        java.io.IOException - if an I/O error occurred.
        java.lang.InterruptedException - if the calling thread was interrupted.
      • connectInternal

        protected abstract void connectInternal()
                                         throws SmackException,
                                                java.io.IOException,
                                                XMPPException,
                                                java.lang.InterruptedException
        Abstract method that concrete subclasses of XMPPConnection need to implement to perform their way of XMPP connection establishment. Implementations are required to perform an automatic login if the previous connection state was logged (authenticated).
        Throws:
        SmackException - if Smack detected an exceptional situation.
        java.io.IOException - if an I/O error occurred.
        XMPPException - if an XMPP protocol error was received.
        java.lang.InterruptedException - if the calling thread was interrupted.
      • login

        public void login​(java.lang.CharSequence username,
                          java.lang.String password)
                   throws XMPPException,
                          SmackException,
                          java.io.IOException,
                          java.lang.InterruptedException
        Same as login(CharSequence, String, Resourcepart), but takes the resource from the connection configuration.
        Parameters:
        username - TODO javadoc me please
        password - TODO javadoc me please
        Throws:
        XMPPException - if an XMPP protocol error was received.
        SmackException - if Smack detected an exceptional situation.
        java.io.IOException - if an I/O error occurred.
        java.lang.InterruptedException - if the calling thread was interrupted.
        See Also:
        login()
      • login

        public void login​(java.lang.CharSequence username,
                          java.lang.String password,
                          org.jxmpp.jid.parts.Resourcepart resource)
                   throws XMPPException,
                          SmackException,
                          java.io.IOException,
                          java.lang.InterruptedException
        Login with the given username (authorization identity). You may omit the password if a callback handler is used. If resource is null, then the server will generate one.
        Parameters:
        username - TODO javadoc me please
        password - TODO javadoc me please
        resource - TODO javadoc me please
        Throws:
        XMPPException - if an XMPP protocol error was received.
        SmackException - if Smack detected an exceptional situation.
        java.io.IOException - if an I/O error occurred.
        java.lang.InterruptedException - if the calling thread was interrupted.
        See Also:
        login()
      • loginInternal

        protected abstract void loginInternal​(java.lang.String username,
                                              java.lang.String password,
                                              org.jxmpp.jid.parts.Resourcepart resource)
                                       throws XMPPException,
                                              SmackException,
                                              java.io.IOException,
                                              java.lang.InterruptedException
        Throws:
        XMPPException
        SmackException
        java.io.IOException
        java.lang.InterruptedException
      • isConnected

        public final boolean isConnected()
        Description copied from interface: XMPPConnection
        Returns true if currently connected to the XMPP server.
        Specified by:
        isConnected in interface XMPPConnection
        Returns:
        true if connected.
      • isAuthenticated

        public final boolean isAuthenticated()
        Description copied from interface: XMPPConnection
        Returns true if currently authenticated by successfully calling the login method.
        Specified by:
        isAuthenticated in interface XMPPConnection
        Returns:
        true if authenticated.
      • getUser

        public final org.jxmpp.jid.EntityFullJid getUser()
        Description copied from interface: XMPPConnection
        Returns the full XMPP address of the user that is logged in to the connection or null if not logged in yet. An XMPP address is in the form username@server/resource.
        Specified by:
        getUser in interface XMPPConnection
        Returns:
        the full XMPP address of the user logged in.
      • getStreamId

        public java.lang.String getStreamId()
        Description copied from interface: XMPPConnection
        Returns the stream ID for this connection, which is the value set by the server when opening an XMPP stream. This value will be null if not connected to the server.
        Specified by:
        getStreamId in interface XMPPConnection
        Returns:
        the ID of this connection returned from the XMPP server or null if not connected to the server.
        See Also:
        RFC 6120 ยง 4.7.3. id
      • hasCurrentConnectionException

        protected final boolean hasCurrentConnectionException()
      • setCurrentConnectionExceptionAndNotify

        protected final void setCurrentConnectionExceptionAndNotify​(java.lang.Exception exception)
      • notifyWaitingThreads

        protected final void notifyWaitingThreads()
      • waitFor

        protected final boolean waitFor​(Supplier<java.lang.Boolean> condition)
                                 throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • waitForConditionOrConnectionException

        protected final boolean waitForConditionOrConnectionException​(Supplier<java.lang.Boolean> condition)
                                                               throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • waitForConditionOrThrowConnectionException

        protected final void waitForConditionOrThrowConnectionException​(Supplier<java.lang.Boolean> condition,
                                                                        java.lang.String waitFor)
                                                                 throws java.lang.InterruptedException,
                                                                        SmackException,
                                                                        XMPPException
        Throws:
        java.lang.InterruptedException
        SmackException
        XMPPException
      • bindResourceAndEstablishSession

        protected org.jxmpp.jid.parts.Resourcepart bindResourceAndEstablishSession​(org.jxmpp.jid.parts.Resourcepart resource)
                                                                            throws SmackException,
                                                                                   java.lang.InterruptedException,
                                                                                   XMPPException
        Throws:
        SmackException
        java.lang.InterruptedException
        XMPPException
      • isAnonymous

        public final boolean isAnonymous()
        Description copied from interface: XMPPConnection
        Returns true if currently authenticated anonymously.
        Specified by:
        isAnonymous in interface XMPPConnection
        Returns:
        true if authenticated anonymously.
      • getUsedSaslMechansism

        public final java.lang.String getUsedSaslMechansism()
        Get the name of the SASL mechanism that was used to authenticate this connection. This returns the name of mechanism which was used the last time this connection was authenticated, and will return null if this connection was not authenticated before.
        Returns:
        the name of the used SASL mechanism.
        Since:
        4.2
      • getConnectionLock

        protected java.util.concurrent.locks.Lock getConnectionLock()
      • afterSaslAuthenticationSuccess

        protected void afterSaslAuthenticationSuccess()
                                               throws SmackException.NotConnectedException,
                                                      java.lang.InterruptedException,
                                                      SmackException.SmackWrappedException
        Hook for subclasses right after successful SASL authentication. RFC 6120 ยง 6.4.6. specifies a that the initiating entity, needs to initiate a new stream in this case. But some transports, like BOSH, requires a special handling.

        Note that we can not reset XMPPTCPConnection's parser here, because this method is invoked by the thread calling login(), but the parser reset has to be done within the reader thread.

        Throws:
        SmackException.NotConnectedException - if the XMPP connection is not connected.
        java.lang.InterruptedException - if the calling thread was interrupted.
        SmackException.SmackWrappedException - in case of an exception.
      • isSaslAuthenticated

        protected final boolean isSaslAuthenticated()
      • disconnect

        public void disconnect()
        Closes the connection by setting presence to unavailable then closing the connection to the XMPP server. The XMPPConnection can still be used for connecting to the server again.
      • disconnect

        public void disconnect​(Presence unavailablePresence)
                        throws SmackException.NotConnectedException
        Closes the connection. A custom unavailable presence is sent to the server, followed by closing the stream. The XMPPConnection can still be used for connecting to the server again. A custom unavailable presence is useful for communicating offline presence information such as "On vacation". Typically, just the status text of the presence stanza is set with online information, but most XMPP servers will deliver the full presence stanza with whatever data is set.
        Parameters:
        unavailablePresence - the optional presence stanza to send during shutdown.
        Throws:
        SmackException.NotConnectedException - if the XMPP connection is not connected.
      • notifyConnectionError

        protected final void notifyConnectionError​(java.lang.Exception exception)
        Sends out a notification that there was an error with the connection and closes the connection.
        Parameters:
        exception - the exception that causes the connection close event.
      • instantShutdown

        public abstract void instantShutdown()
        Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza.
      • shutdown

        protected abstract void shutdown()
        Shuts the current connection down.
      • waitForClosingStreamTagFromServer

        protected final boolean waitForClosingStreamTagFromServer()
      • addConnectionListener

        public void addConnectionListener​(ConnectionListener connectionListener)
        Description copied from interface: XMPPConnection
        Adds a connection listener to this connection that will be notified when the connection closes or fails.
        Specified by:
        addConnectionListener in interface XMPPConnection
        Parameters:
        connectionListener - a connection listener.
      • createStanzaCollectorAndSend

        public StanzaCollector createStanzaCollectorAndSend​(IQ packet)
                                                     throws SmackException.NotConnectedException,
                                                            java.lang.InterruptedException
        Description copied from interface: XMPPConnection
        Creates a new stanza collector collecting IQ responses that are replies to the IQ request. Does also send the request IQ. The stanza filter for the collector is an IQReplyFilter, guaranteeing that stanza id and JID in the 'from' address have expected values.
        Specified by:
        createStanzaCollectorAndSend in interface XMPPConnection
        Parameters:
        packet - the IQ request to filter responses from
        Returns:
        a new stanza collector.
        Throws:
        SmackException.NotConnectedException - if the XMPP connection is not connected.
        java.lang.InterruptedException - if the calling thread was interrupted.
      • createStanzaCollectorAndSend

        public StanzaCollector createStanzaCollectorAndSend​(StanzaFilter packetFilter,
                                                            Stanza packet)
                                                     throws SmackException.NotConnectedException,
                                                            java.lang.InterruptedException
        Description copied from interface: XMPPConnection
        Creates a new stanza collector for this connection. A stanza filter determines which stanzas will be accumulated by the collector. A StanzaCollector is more suitable to use than a StanzaListener when you need to wait for a specific result.
        Specified by:
        createStanzaCollectorAndSend in interface XMPPConnection
        Parameters:
        packetFilter - the stanza filter to use.
        packet - the stanza to send right after the collector got created
        Returns:
        a new stanza collector.
        Throws:
        SmackException.NotConnectedException - if the XMPP connection is not connected.
        java.lang.InterruptedException - if the calling thread was interrupted.
      • createStanzaCollector

        public StanzaCollector createStanzaCollector​(StanzaFilter packetFilter)
        Description copied from interface: XMPPConnection
        Creates a new stanza collector for this connection. A stanza filter determines which stanzas will be accumulated by the collector. A StanzaCollector is more suitable to use than a StanzaListener when you need to wait for a specific result.

        Note: If you send a Stanza right after using this method, then consider using XMPPConnection.createStanzaCollectorAndSend(StanzaFilter, Stanza) instead. Otherwise make sure cancel the StanzaCollector in every case, e.g. even if an exception is thrown, or otherwise you may leak the StanzaCollector.

        Specified by:
        createStanzaCollector in interface XMPPConnection
        Parameters:
        packetFilter - the stanza filter to use.
        Returns:
        a new stanza collector.
      • removeStanzaCollector

        public void removeStanzaCollector​(StanzaCollector collector)
        Description copied from interface: XMPPConnection
        Remove a stanza collector of this connection.
        Specified by:
        removeStanzaCollector in interface XMPPConnection
        Parameters:
        collector - a stanza collectors which was created for this connection.
      • addStanzaListener

        public final void addStanzaListener​(StanzaListener stanzaListener,
                                            StanzaFilter stanzaFilter)
        Description copied from interface: XMPPConnection
        Registers a stanza listener with this connection. The listener will be invoked when a (matching) incoming stanza is received. The stanza filter determines which stanzas will be delivered to the listener. It is guaranteed that the same listener will not be invoked concurrently and the the order of invocation will reflect the order in which the stanzas have been received. If the same stanza listener is added again with a different filter, only the new filter will be used.
        Specified by:
        addStanzaListener in interface XMPPConnection
        Parameters:
        stanzaListener - the stanza listener to notify of new received stanzas.
        stanzaFilter - the stanza filter to use.
      • removeStanzaListener

        public final boolean removeStanzaListener​(StanzaListener stanzaListener)
        Description copied from interface: XMPPConnection
        Removes a stanza listener for received stanzas from this connection.
        Specified by:
        removeStanzaListener in interface XMPPConnection
        Parameters:
        stanzaListener - the stanza listener to remove.
        Returns:
        true if the stanza listener was removed.
      • addSyncStanzaListener

        public void addSyncStanzaListener​(StanzaListener packetListener,
                                          StanzaFilter packetFilter)
        Description copied from interface: XMPPConnection
        Registers a synchronous stanza listener with this connection. A stanza listener will be invoked only when an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener. If the same stanza listener is added again with a different filter, only the new filter will be used.

        Important: This stanza listeners will be called in the same single thread that processes all incoming stanzas. Only use this kind of stanza filter if it does not perform any XMPP activity that waits for a response. Consider using XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter) when possible, i.e. when the invocation order doesn't have to be the same as the order of the arriving stanzas. If the order of the arriving stanzas, consider using a StanzaCollector when possible.

        Specified by:
        addSyncStanzaListener in interface XMPPConnection
        Parameters:
        packetListener - the stanza listener to notify of new received stanzas.
        packetFilter - the stanza filter to use.
        See Also:
        XMPPConnection.addStanzaInterceptor(StanzaListener, StanzaFilter)
      • removeSyncStanzaListener

        public boolean removeSyncStanzaListener​(StanzaListener packetListener)
        Description copied from interface: XMPPConnection
        Removes a stanza listener for received stanzas from this connection.
        Specified by:
        removeSyncStanzaListener in interface XMPPConnection
        Parameters:
        packetListener - the stanza listener to remove.
        Returns:
        true if the stanza listener was removed
      • addAsyncStanzaListener

        public void addAsyncStanzaListener​(StanzaListener packetListener,
                                           StanzaFilter packetFilter)
        Description copied from interface: XMPPConnection
        Registers an asynchronous stanza listener with this connection. A stanza listener will be invoked only when an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener. If the same stanza listener is added again with a different filter, only the new filter will be used.

        Unlike XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter) stanza listeners added with this method will be invoked asynchronously in their own thread. Use this method if the order of the stanza listeners must not depend on the order how the stanzas where received.

        Specified by:
        addAsyncStanzaListener in interface XMPPConnection
        Parameters:
        packetListener - the stanza listener to notify of new received stanzas.
        packetFilter - the stanza filter to use.
        See Also:
        XMPPConnection.addStanzaInterceptor(StanzaListener, StanzaFilter)
      • removeAsyncStanzaListener

        public boolean removeAsyncStanzaListener​(StanzaListener packetListener)
        Description copied from interface: XMPPConnection
        Removes an asynchronous stanza listener for received stanzas from this connection.
        Specified by:
        removeAsyncStanzaListener in interface XMPPConnection
        Parameters:
        packetListener - the stanza listener to remove.
        Returns:
        true if the stanza listener was removed
      • addStanzaSendingListener

        public void addStanzaSendingListener​(StanzaListener packetListener,
                                             StanzaFilter packetFilter)
        Description copied from interface: XMPPConnection
        Registers a stanza listener with this connection. The listener will be notified of every stanza that this connection sends. A stanza filter determines which stanzas will be delivered to the listener. Note that the thread that writes stanzas will be used to invoke the listeners. Therefore, each stanza listener should complete all operations quickly or use a different thread for processing.
        Specified by:
        addStanzaSendingListener in interface XMPPConnection
        Parameters:
        packetListener - the stanza listener to notify of sent stanzas.
        packetFilter - the stanza filter to use.
      • removeStanzaSendingListener

        public void removeStanzaSendingListener​(StanzaListener packetListener)
        Description copied from interface: XMPPConnection
        Removes a stanza listener for sending stanzas from this connection.
        Specified by:
        removeStanzaSendingListener in interface XMPPConnection
        Parameters:
        packetListener - the stanza listener to remove.
      • firePacketSendingListeners

        protected void firePacketSendingListeners​(TopLevelStreamElement sendTopLevelStreamElement)
        Process all stanza listeners for sending stanzas.

        Compared to firePacketInterceptors(Stanza), the listeners will be invoked in a new thread.

        Parameters:
        sendTopLevelStreamElement - the top level stream element which just got send.
      • addStanzaInterceptor

        @Deprecated
        public void addStanzaInterceptor​(StanzaListener packetInterceptor,
                                         StanzaFilter packetFilter)
        Deprecated.
        Description copied from interface: XMPPConnection
        Registers a stanza interceptor with this connection. The interceptor will be invoked every time a stanza is about to be sent by this connection. Interceptors may modify the stanza to be sent. A stanza filter determines which stanzas will be delivered to the interceptor.

        NOTE: For a similar functionality on incoming stanzas, see XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter).

        Specified by:
        addStanzaInterceptor in interface XMPPConnection
        Parameters:
        packetInterceptor - the stanza interceptor to notify of stanzas about to be sent.
        packetFilter - the stanza filter to use.
      • removeStanzaInterceptor

        @Deprecated
        public void removeStanzaInterceptor​(StanzaListener packetInterceptor)
        Deprecated.
        Description copied from interface: XMPPConnection
        Removes a stanza interceptor.
        Specified by:
        removeStanzaInterceptor in interface XMPPConnection
        Parameters:
        packetInterceptor - the stanza interceptor to remove.
      • addMessageInterceptor

        public void addMessageInterceptor​(Consumer<MessageBuilder> messageInterceptor,
                                          Predicate<Message> messageFilter)
        Description copied from interface: XMPPConnection
        Registers a stanza interceptor with this connection. The interceptor will be invoked every time a stanza is about to be sent by this connection. Interceptors may modify the stanza to be sent. A stanza filter determines which stanzas will be delivered to the interceptor.

        NOTE: For a similar functionality on incoming stanzas, see XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter).

        Specified by:
        addMessageInterceptor in interface XMPPConnection
        Parameters:
        messageInterceptor - the stanza interceptor to notify of stanzas about to be sent.
        messageFilter - the stanza filter to use.
      • addPresenceInterceptor

        public void addPresenceInterceptor​(Consumer<PresenceBuilder> presenceInterceptor,
                                           Predicate<Presence> presenceFilter)
        Description copied from interface: XMPPConnection
        Registers a stanza interceptor with this connection. The interceptor will be invoked every time a stanza is about to be sent by this connection. Interceptors may modify the stanza to be sent. A stanza filter determines which stanzas will be delivered to the interceptor.

        NOTE: For a similar functionality on incoming stanzas, see XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter).

        Specified by:
        addPresenceInterceptor in interface XMPPConnection
        Parameters:
        presenceInterceptor - the stanza interceptor to notify of stanzas about to be sent.
        presenceFilter - the stanza filter to use.
      • initDebugger

        protected void initDebugger()
        Initialize the debugger. You can specify a customized SmackDebugger by setup the system property smack.debuggerClass to the implementation.
        Throws:
        java.lang.IllegalStateException - if the reader or writer isn't yet initialized.
        java.lang.IllegalArgumentException - if the SmackDebugger can't be loaded.
      • getReplyTimeout

        public long getReplyTimeout()
        Description copied from interface: XMPPConnection
        Returns the current value of the reply timeout in milliseconds for request for this XMPPConnection instance.
        Specified by:
        getReplyTimeout in interface XMPPConnection
        Returns:
        the reply timeout in milliseconds
      • setReplyTimeout

        public void setReplyTimeout​(long timeout)
        Description copied from interface: XMPPConnection
        Set the stanza reply timeout in milliseconds. In most cases, Smack will throw a SmackException.NoResponseException if no reply to a request was received within the timeout period.
        Specified by:
        setReplyTimeout in interface XMPPConnection
        Parameters:
        timeout - for a reply in milliseconds
      • setUnknownIqRequestReplyMode

        public void setUnknownIqRequestReplyMode​(SmackConfiguration.UnknownIqRequestReplyMode unknownIqRequestReplyMode)
        Set how Smack behaves when an unknown IQ request has been received.
        Parameters:
        unknownIqRequestReplyMode - reply mode.
      • parseAndProcessNonza

        protected final void parseAndProcessNonza​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                           throws java.io.IOException,
                                                  org.jivesoftware.smack.xml.XmlPullParserException,
                                                  SmackParsingException
        Throws:
        java.io.IOException
        org.jivesoftware.smack.xml.XmlPullParserException
        SmackParsingException
      • parseAndProcessStanza

        protected void parseAndProcessStanza​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                                             java.io.IOException,
                                             java.lang.InterruptedException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        java.lang.InterruptedException
      • processStanza

        protected void processStanza​(Stanza stanza)
                              throws java.lang.InterruptedException
        Processes a stanza after it's been fully parsed by looping through the installed stanza collectors and listeners and letting them examine the stanza to see if they are a match with the filter.
        Parameters:
        stanza - the stanza to process.
        Throws:
        java.lang.InterruptedException - if the calling thread was interrupted.
      • invokeStanzaCollectorsAndNotifyRecvListeners

        protected void invokeStanzaCollectorsAndNotifyRecvListeners​(Stanza packet)
        Invoke StanzaCollector.processStanza(Stanza) for every StanzaCollector with the given packet. Also notify the receive listeners with a matching stanza filter about the packet.

        This method will be invoked by the connections incoming processing thread which may be shared across multiple connections and thus it is important that no user code, e.g. in form of a callback, is invoked by this method. For the same reason, this method must not block for an extended period of time.

        Parameters:
        packet - the stanza to notify the StanzaCollectors and receive listeners about.
      • setWasAuthenticated

        protected void setWasAuthenticated()
        Sets whether the connection has already logged in the server. This method assures that the wasAuthenticated flag is never reset once it has ever been set.
      • callConnectionConnectingListener

        protected void callConnectionConnectingListener()
      • callConnectionConnectedListener

        protected void callConnectionConnectedListener()
      • callConnectionAuthenticatedListener

        protected void callConnectionAuthenticatedListener​(boolean resumed)
      • getConnectionCounter

        public int getConnectionCounter()
        Description copied from interface: XMPPConnection
        Get the connection counter of this XMPPConnection instance. Those can be used as ID to identify the connection, but beware that the ID may not be unique if you create more then 2*Integer.MAX_VALUE instances as the counter could wrap.
        Specified by:
        getConnectionCounter in interface XMPPConnection
        Returns:
        the connection counter of this XMPPConnection
      • setFromMode

        public void setFromMode​(XMPPConnection.FromMode fromMode)
        Description copied from interface: XMPPConnection
        Set the FromMode for this connection instance. Defines how the 'from' attribute of outgoing stanzas should be populated by Smack.
        Specified by:
        setFromMode in interface XMPPConnection
        Parameters:
        fromMode - TODO javadoc me please
      • parseFeatures

        protected final void parseFeatures​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                    throws org.jivesoftware.smack.xml.XmlPullParserException,
                                           java.io.IOException,
                                           SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • parseFeaturesAndNotify

        protected final void parseFeaturesAndNotify​(org.jivesoftware.smack.xml.XmlPullParser parser)
                                             throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getFeature

        public <F extends FullyQualifiedElement> F getFeature​(javax.xml.namespace.QName qname)
        Description copied from interface: XMPPConnection
        Get the feature stanza extensions for a given stream feature of the server, or null if the server doesn't support that feature.
        Specified by:
        getFeature in interface XMPPConnection
        Type Parameters:
        F - ExtensionElement type of the feature.
        Parameters:
        qname - the qualified name of the XML element of feature.
        Returns:
        a stanza extensions of the feature or null
      • hasFeature

        public boolean hasFeature​(javax.xml.namespace.QName qname)
        Description copied from interface: XMPPConnection
        Return true if the server supports the given stream feature.
        Specified by:
        hasFeature in interface XMPPConnection
        Parameters:
        qname - the qualified name of the XML element of feature.
        Returns:
        true if the server supports the stream feature.
      • sendIqRequestAsync

        public SmackFuture<IQ,​java.lang.Exception> sendIqRequestAsync​(IQ request)
        Description copied from interface: XMPPConnection
        Send an IQ request asynchronously. The connection's default reply timeout will be used.
        Specified by:
        sendIqRequestAsync in interface XMPPConnection
        Parameters:
        request - the IQ request to send.
        Returns:
        a SmackFuture for the response.
      • sendIqRequestAsync

        public SmackFuture<IQ,​java.lang.Exception> sendIqRequestAsync​(IQ request,
                                                                            long timeout)
        Description copied from interface: XMPPConnection
        Send an IQ request asynchronously.
        Specified by:
        sendIqRequestAsync in interface XMPPConnection
        Parameters:
        request - the IQ request to send.
        timeout - the reply timeout in milliseconds.
        Returns:
        a SmackFuture for the response.
      • sendAsync

        public <S extends StanzaSmackFuture<S,​java.lang.Exception> sendAsync​(S stanza,
                                                                                     StanzaFilter replyFilter)
        Description copied from interface: XMPPConnection
        Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter. The connection's default reply timeout will be used.
        Specified by:
        sendAsync in interface XMPPConnection
        Type Parameters:
        S - the type of the stanza to send.
        Parameters:
        stanza - the stanza to send.
        replyFilter - the filter used for the response stanza.
        Returns:
        a SmackFuture for the response.
      • sendAsync

        public <S extends StanzaSmackFuture<S,​java.lang.Exception> sendAsync​(S stanza,
                                                                                     StanzaFilter replyFilter,
                                                                                     long timeout)
        Description copied from interface: XMPPConnection
        Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter.
        Specified by:
        sendAsync in interface XMPPConnection
        Type Parameters:
        S - the type of the stanza to send.
        Parameters:
        stanza - the stanza to send.
        replyFilter - the filter used for the response stanza.
        timeout - the reply timeout in milliseconds.
        Returns:
        a SmackFuture for the response.
      • addOneTimeSyncCallback

        public void addOneTimeSyncCallback​(StanzaListener callback,
                                           StanzaFilter packetFilter)
        Description copied from interface: XMPPConnection
        Add a callback that is called exactly once and synchronously with the incoming stanza that matches the given stanza filter.
        Specified by:
        addOneTimeSyncCallback in interface XMPPConnection
        Parameters:
        callback - the callback invoked once the stanza filter matches a stanza.
        packetFilter - the filter to match stanzas or null to match all.
      • registerIQRequestHandler

        public IQRequestHandler registerIQRequestHandler​(IQRequestHandler iqRequestHandler)
        Description copied from interface: XMPPConnection
        Register an IQ request handler with this connection.

        IQ request handler process incoming IQ requests, i.e. incoming IQ stanzas of type 'get' or 'set', and return a result.

        Specified by:
        registerIQRequestHandler in interface XMPPConnection
        Parameters:
        iqRequestHandler - the IQ request handler to register.
        Returns:
        the previously registered IQ request handler or null.
      • unregisterIQRequestHandler

        public IQRequestHandler unregisterIQRequestHandler​(java.lang.String element,
                                                           java.lang.String namespace,
                                                           IQ.Type type)
        Description copied from interface: XMPPConnection
        Unregister an IQ request handler with this connection.
        Specified by:
        unregisterIQRequestHandler in interface XMPPConnection
        Parameters:
        element - the IQ element the IQ request handler is responsible for.
        namespace - the IQ namespace the IQ request handler is responsible for.
        type - the IQ type the IQ request handler is responsible for.
        Returns:
        the previously registered IQ request handler or null.
      • getLastStanzaReceived

        public long getLastStanzaReceived()
        Description copied from interface: XMPPConnection
        Returns the timestamp in milliseconds when the last stanza was received.
        Specified by:
        getLastStanzaReceived in interface XMPPConnection
        Returns:
        the timestamp in milliseconds
      • getAuthenticatedConnectionInitiallyEstablishedTimestamp

        public final long getAuthenticatedConnectionInitiallyEstablishedTimestamp()
        Get the timestamp when the connection was the first time authenticated, i.e., when the first successful login was performed. Note that this value is not reset on disconnect, so it represents the timestamp from the last authenticated connection. The value is also not reset on stream resumption.
        Returns:
        the timestamp or null.
        Since:
        4.3.3
      • setParsingExceptionCallback

        public void setParsingExceptionCallback​(ParsingExceptionCallback callback)
        Install a parsing exception callback, which will be invoked once an exception is encountered while parsing a stanza.
        Parameters:
        callback - the callback to install
      • getParsingExceptionCallback

        public ParsingExceptionCallback getParsingExceptionCallback()
        Get the current active parsing exception callback.
        Returns:
        the active exception callback or null if there is none
      • toString

        public final java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • asyncGoLimited

        protected void asyncGoLimited​(java.lang.Runnable runnable)
      • setMaxAsyncOperations

        public void setMaxAsyncOperations​(int maxAsyncOperations)
      • asyncGo

        protected static void asyncGo​(java.lang.Runnable runnable)
      • schedule

        protected static ScheduledAction schedule​(java.lang.Runnable runnable,
                                                  long delay,
                                                  java.util.concurrent.TimeUnit unit)
      • onStreamOpen

        protected void onStreamOpen​(org.jivesoftware.smack.xml.XmlPullParser parser)
      • getSmackTlsContext

        protected final SmackTlsContext getSmackTlsContext()