Class XMPPTCPConnection

java.lang.Object
org.jivesoftware.smack.AbstractXMPPConnection
org.jivesoftware.smack.tcp.XMPPTCPConnection
All Implemented Interfaces:
org.jivesoftware.smack.XMPPConnection

public class XMPPTCPConnection extends org.jivesoftware.smack.AbstractXMPPConnection
Creates a socket connection to an XMPP server. This is the default connection to an XMPP server and is specified in the XMPP Core (RFC 6120).
See Also:
  • XMPPConnection
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected class 
     
    protected class 
     

    Nested classes/interfaces inherited from class org.jivesoftware.smack.AbstractXMPPConnection

    org.jivesoftware.smack.AbstractXMPPConnection.InterceptorWrapper, org.jivesoftware.smack.AbstractXMPPConnection.ListenerWrapper, org.jivesoftware.smack.AbstractXMPPConnection.SyncPointState

    Nested classes/interfaces inherited from interface org.jivesoftware.smack.XMPPConnection

    org.jivesoftware.smack.XMPPConnection.FromMode
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Protected access level because of unit test purposes
    Protected access level because of unit test purposes

    Fields inherited from class org.jivesoftware.smack.AbstractXMPPConnection

    ASYNC_BUT_ORDERED, authenticated, authenticatedConnectionInitiallyEstablishedTimestamp, closingStreamReceived, compressionHandler, connected, connectionCounterValue, connectionListeners, connectionLock, debugger, host, lastFeaturesReceived, outgoingStreamXmlEnvironment, port, reader, saslFeatureReceived, SMACK_REACTOR, streamFeatures, streamId, tlsHandled, user, wasAuthenticated, writer
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new XMPP connection over TCP.
    XMPPTCPConnection(CharSequence username, String password, String serviceName)
    Creates a new XMPP connection over TCP.
    Creates a new XMPP connection over TCP (optionally using proxies).
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    addRequestAckPredicate(org.jivesoftware.smack.filter.StanzaFilter predicate)
    Add a predicate for Stream Management acknowledgment requests.
    void
    addStanzaAcknowledgedListener(org.jivesoftware.smack.StanzaListener listener)
    Add a Stanza acknowledged listener.
    void
    addStanzaDroppedListener(org.jivesoftware.smack.StanzaListener listener)
    Add a Stanza dropped listener.
    org.jivesoftware.smack.StanzaListener
    addStanzaIdAcknowledgedListener(String id, org.jivesoftware.smack.StanzaListener listener)
    Add a new Stanza ID acknowledged listener for the given ID.
    protected void
     
    protected void
    afterSuccessfulLogin(boolean resumed)
     
    protected void
    Establishes a connection to the XMPP server.
    Returns the local address currently in use for this connection.
    int
    Get the maximum resumption time in seconds after which a managed stream can be resumed.
    void
     
    boolean
    Returns true if the connection is disconnected by a Stream resumption via Stream Management is possible.
    boolean
     
    boolean
    Returns true if Stream Management is supported by the server.
    boolean
    Returns true if Stream Management was successfully negotiated with the server.
    boolean
    Returns true if the stream is resumable.
    boolean
     
    protected void
    loginInternal(String username, String password, org.jxmpp.jid.parts.Resourcepart resource)
     
    void
    Remove all predicates for Stream Management acknowledgment requests.
    void
    Remove all stanza acknowledged listeners.
    void
    Removes all Stanza ID acknowledged listeners.
    boolean
    removeRequestAckPredicate(org.jivesoftware.smack.filter.StanzaFilter predicate)
    Remove the given predicate for Stream Management acknowledgment request.
    boolean
    removeStanzaAcknowledgedListener(org.jivesoftware.smack.StanzaListener listener)
    Remove the given Stanza acknowledged listener.
    boolean
    removeStanzaDroppedListener(org.jivesoftware.smack.StanzaListener listener)
    Remove the given Stanza dropped listener.
    org.jivesoftware.smack.StanzaListener
    Remove the Stanza ID acknowledged listener for the given ID.
    void
    Send an unconditional Stream Management acknowledgement request to the server.
    protected void
    sendInternal(org.jivesoftware.smack.packet.TopLevelStreamElement element)
     
    protected void
    sendNonBlockingInternal(org.jivesoftware.smack.packet.TopLevelStreamElement element)
     
    void
    Send a unconditional Stream Management acknowledgment to the server.
    void
    Set the bundle and defer callback used for this connection.
    static void
    Set the default bundle and defer callback used for new connections.
    void
    setPreferredResumptionTime(int resumptionTime)
    Set the preferred resumption time in seconds.
    void
    setUseStreamManagement(boolean useSm)
    Set if Stream Management should be used if supported by the server.
    static void
    setUseStreamManagementDefault(boolean useSmDefault)
    Set if Stream Management should be used by default for new connections.
    static void
    setUseStreamManagementResumptiodDefault(boolean useSmResumptionDefault)
    void
    setUseStreamManagementResumption(boolean useSmResumption)
    Set if Stream Management resumption should be used if supported by the server.
    static void
    setUseStreamManagementResumptionDefault(boolean useSmResumptionDefault)
    Set if Stream Management resumption should be used by default for new connections.
    protected void
    setWriter(Writer writer)
    For unit testing purposes
    protected void
    Shuts the current connection down.
    boolean
    Returns true if the stream was successfully resumed with help of Stream Management.
    protected void
     
    protected void
     
    protected void
     

    Methods inherited from class org.jivesoftware.smack.AbstractXMPPConnection

    addAsyncStanzaListener, addConnectionListener, addMessageInterceptor, addOneTimeSyncCallback, addPresenceInterceptor, addStanzaListener, addStanzaSendingListener, addStreamFeature, addSyncStanzaListener, afterSaslAuthenticationSuccess, asyncGo, asyncGoLimited, authenticate, bindResourceAndEstablishSession, buildNonzaCallback, callConnectionAuthenticatedListener, callConnectionConnectedListener, callConnectionConnectingListener, connect, createStanzaCollector, createStanzaCollector, createStanzaCollectorAndSend, createStanzaCollectorAndSend, disconnect, disconnect, firePacketSendingListeners, getAuthenticatedConnectionInitiallyEstablishedTimestamp, getConfiguration, getConnectionCounter, getConnectionLock, getFeature, getFromMode, getHost, getJxmppContext, getLastStanzaReceived, getParsingExceptionCallback, getPort, getReactor, getReplyTimeout, getSmackTlsContext, getStanzaFactory, getStreamId, getStreamOpen, getUsedSaslMechansism, getUser, getXMPPServiceDomain, hasCurrentConnectionException, hasFeature, initDebugger, initState, invokeStanzaCollectorsAndNotifyRecvListeners, isAnonymous, isAuthenticated, isConnected, isSaslAuthenticated, login, login, login, notifyConnectionError, notifyWaitingThreads, onStreamOpen, parseAndProcessNonza, parseAndProcessStanza, parseFeatures, parseFeaturesAndNotify, processStanza, registerIQRequestHandler, removeAsyncStanzaListener, removeConnectionListener, removeMessageInterceptor, removePresenceInterceptor, removeStanzaCollector, removeStanzaListener, removeStanzaSendingListener, removeSyncStanzaListener, schedule, sendAndWaitForResponse, sendAsync, sendAsync, sendIqRequestAndWaitForResponse, sendIqRequestAsync, sendIqRequestAsync, sendNonza, sendNonzaNonBlocking, sendStanza, sendStanzaNonBlocking, sendStreamOpen, setCurrentConnectionExceptionAndNotify, setFromMode, setMaxAsyncOperations, setParsingExceptionCallback, setReplyTimeout, setUnknownIqRequestReplyMode, setWasAuthenticated, throwNotConnectedExceptionIfAppropriate, toString, trySendStanza, trySendStanza, unregisterIQRequestHandler, unregisterIQRequestHandler, updateOutgoingStreamXmlEnvironmentOnStreamOpen, waitFor, waitForClosingStreamTagFromServer, waitForConditionOrThrowConnectionException

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.jivesoftware.smack.XMPPConnection

    getFeature, hasFeature
  • Field Details

  • Constructor Details

    • XMPPTCPConnection

      public XMPPTCPConnection(XMPPTCPConnectionConfiguration config)
      Creates a new XMPP connection over TCP (optionally using proxies).

      Note that XMPPTCPConnection constructors do not establish a connection to the server and you must call AbstractXMPPConnection.connect().

      Parameters:
      config - the connection configuration.
    • XMPPTCPConnection

      public XMPPTCPConnection(CharSequence jid, String password) throws org.jxmpp.stringprep.XmppStringprepException
      Creates a new XMPP connection over TCP.

      Note that jid must be the bare JID, e.g. "user@example.org". More fine-grained control over the connection settings is available using the XMPPTCPConnection(XMPPTCPConnectionConfiguration) constructor.

      Parameters:
      jid - the bare JID used by the client.
      password - the password or authentication token.
      Throws:
      org.jxmpp.stringprep.XmppStringprepException - if the provided string is invalid.
    • XMPPTCPConnection

      public XMPPTCPConnection(CharSequence username, String password, String serviceName) throws org.jxmpp.stringprep.XmppStringprepException
      Creates a new XMPP connection over TCP.

      This is the simplest constructor for connecting to an XMPP server. Alternatively, you can get fine-grained control over connection settings using the XMPPTCPConnection(XMPPTCPConnectionConfiguration) constructor.

      Parameters:
      username - TODO javadoc me please
      password - TODO javadoc me please
      serviceName - TODO javadoc me please
      Throws:
      org.jxmpp.stringprep.XmppStringprepException - if the provided string is invalid.
  • Method Details

    • throwNotConnectedExceptionIfAppropriate

      protected void throwNotConnectedExceptionIfAppropriate() throws org.jivesoftware.smack.SmackException.NotConnectedException
      Overrides:
      throwNotConnectedExceptionIfAppropriate in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.SmackException.NotConnectedException
    • throwAlreadyConnectedExceptionIfAppropriate

      protected void throwAlreadyConnectedExceptionIfAppropriate() throws org.jivesoftware.smack.SmackException.AlreadyConnectedException
      Overrides:
      throwAlreadyConnectedExceptionIfAppropriate in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.SmackException.AlreadyConnectedException
    • throwAlreadyLoggedInExceptionIfAppropriate

      protected void throwAlreadyLoggedInExceptionIfAppropriate() throws org.jivesoftware.smack.SmackException.AlreadyLoggedInException
      Overrides:
      throwAlreadyLoggedInExceptionIfAppropriate in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.SmackException.AlreadyLoggedInException
    • afterSuccessfulLogin

      protected void afterSuccessfulLogin(boolean resumed) throws org.jivesoftware.smack.SmackException.NotConnectedException, InterruptedException
      Overrides:
      afterSuccessfulLogin in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.SmackException.NotConnectedException
      InterruptedException
    • loginInternal

      protected void loginInternal(String username, String password, org.jxmpp.jid.parts.Resourcepart resource) throws org.jivesoftware.smack.XMPPException, org.jivesoftware.smack.SmackException, IOException, InterruptedException
      Specified by:
      loginInternal in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.XMPPException
      org.jivesoftware.smack.SmackException
      IOException
      InterruptedException
    • isSecureConnection

      public boolean isSecureConnection()
      Specified by:
      isSecureConnection in interface org.jivesoftware.smack.XMPPConnection
      Specified by:
      isSecureConnection in class org.jivesoftware.smack.AbstractXMPPConnection
    • shutdown

      protected void shutdown()
      Shuts the current connection down. After this method returns, the connection must be ready for re-use by connect.
      Specified by:
      shutdown in class org.jivesoftware.smack.AbstractXMPPConnection
    • instantShutdown

      public void instantShutdown()
      Specified by:
      instantShutdown in class org.jivesoftware.smack.AbstractXMPPConnection
    • sendInternal

      protected void sendInternal(org.jivesoftware.smack.packet.TopLevelStreamElement element) throws org.jivesoftware.smack.SmackException.NotConnectedException, InterruptedException
      Specified by:
      sendInternal in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.SmackException.NotConnectedException
      InterruptedException
    • sendNonBlockingInternal

      protected void sendNonBlockingInternal(org.jivesoftware.smack.packet.TopLevelStreamElement element) throws org.jivesoftware.smack.SmackException.NotConnectedException, org.jivesoftware.smack.SmackException.OutgoingQueueFullException
      Specified by:
      sendNonBlockingInternal in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.SmackException.NotConnectedException
      org.jivesoftware.smack.SmackException.OutgoingQueueFullException
    • isUsingCompression

      public boolean isUsingCompression()
      Specified by:
      isUsingCompression in interface org.jivesoftware.smack.XMPPConnection
      Specified by:
      isUsingCompression in class org.jivesoftware.smack.AbstractXMPPConnection
    • connectInternal

      protected void connectInternal() throws org.jivesoftware.smack.SmackException, IOException, org.jivesoftware.smack.XMPPException, InterruptedException
      Establishes a connection to the XMPP server. It basically creates and maintains a socket connection to the server.

      Listeners will be preserved from a previous connection if the reconnection occurs after an abrupt termination.

      Specified by:
      connectInternal in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.XMPPException - if an error occurs while trying to establish the connection.
      org.jivesoftware.smack.SmackException - if Smack detected an exceptional situation.
      IOException - if an I/O error occurred.
      InterruptedException - if the calling thread was interrupted.
    • setWriter

      protected void setWriter(Writer writer)
      For unit testing purposes
      Parameters:
      writer - TODO javadoc me please
    • afterFeaturesReceived

      protected void afterFeaturesReceived() throws org.jivesoftware.smack.SmackException.NotConnectedException, InterruptedException, org.jivesoftware.smack.SmackException.SecurityRequiredByServerException
      Overrides:
      afterFeaturesReceived in class org.jivesoftware.smack.AbstractXMPPConnection
      Throws:
      org.jivesoftware.smack.SmackException.NotConnectedException
      InterruptedException
      org.jivesoftware.smack.SmackException.SecurityRequiredByServerException
    • setUseStreamManagementDefault

      public static void setUseStreamManagementDefault(boolean useSmDefault)
      Set if Stream Management should be used by default for new connections.
      Parameters:
      useSmDefault - true to use Stream Management for new connections.
    • setUseStreamManagementResumptiodDefault

      @Deprecated public static void setUseStreamManagementResumptiodDefault(boolean useSmResumptionDefault)
      Set if Stream Management resumption should be used by default for new connections.
      Parameters:
      useSmResumptionDefault - true to use Stream Management resumption for new connections.
    • setUseStreamManagementResumptionDefault

      public static void setUseStreamManagementResumptionDefault(boolean useSmResumptionDefault)
      Set if Stream Management resumption should be used by default for new connections.
      Parameters:
      useSmResumptionDefault - true to use Stream Management resumption for new connections.
    • setUseStreamManagement

      public void setUseStreamManagement(boolean useSm)
      Set if Stream Management should be used if supported by the server.
      Parameters:
      useSm - true to use Stream Management.
    • setUseStreamManagementResumption

      public void setUseStreamManagementResumption(boolean useSmResumption)
      Set if Stream Management resumption should be used if supported by the server.
      Parameters:
      useSmResumption - true to use Stream Management resumption.
    • setPreferredResumptionTime

      public void setPreferredResumptionTime(int resumptionTime)
      Set the preferred resumption time in seconds.
      Parameters:
      resumptionTime - the preferred resumption time in seconds
    • addRequestAckPredicate

      public boolean addRequestAckPredicate(org.jivesoftware.smack.filter.StanzaFilter predicate)
      Add a predicate for Stream Management acknowledgment requests.

      Those predicates are used to determine when a Stream Management acknowledgement request is send to the server. Some pre-defined predicates are found in the org.jivesoftware.smack.sm.predicates package.

      If not predicate is configured, the Predicate.forMessagesOrAfter5Stanzas() will be used.

      Parameters:
      predicate - the predicate to add.
      Returns:
      if the predicate was not already active.
    • removeRequestAckPredicate

      public boolean removeRequestAckPredicate(org.jivesoftware.smack.filter.StanzaFilter predicate)
      Remove the given predicate for Stream Management acknowledgment request.
      Parameters:
      predicate - the predicate to remove.
      Returns:
      true if the predicate was removed.
    • removeAllRequestAckPredicates

      public void removeAllRequestAckPredicates()
      Remove all predicates for Stream Management acknowledgment requests.
    • requestSmAcknowledgement

      public void requestSmAcknowledgement() throws org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException, org.jivesoftware.smack.SmackException.NotConnectedException, InterruptedException
      Send an unconditional Stream Management acknowledgement request to the server.
      Throws:
      org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException - if Stream Management is not enabled.
      org.jivesoftware.smack.SmackException.NotConnectedException - if the connection is not connected.
      InterruptedException - if the calling thread was interrupted.
    • sendSmAcknowledgement

      public void sendSmAcknowledgement() throws org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException, org.jivesoftware.smack.SmackException.NotConnectedException, InterruptedException
      Send a unconditional Stream Management acknowledgment to the server.

      See XEP-198: Stream Management ยง 4. Acks: "Either party MAY send an <a/> element at any time (e.g., after it has received a certain number of stanzas, or after a certain period of time), even if it has not received an <r/> element from the other party."

      Throws:
      org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException - if Stream Management is not enabled.
      org.jivesoftware.smack.SmackException.NotConnectedException - if the connection is not connected.
      InterruptedException - if the calling thread was interrupted.
    • addStanzaAcknowledgedListener

      public void addStanzaAcknowledgedListener(org.jivesoftware.smack.StanzaListener listener)
      Add a Stanza acknowledged listener.

      Those listeners will be invoked every time a Stanza has been acknowledged by the server. The will not get automatically removed. Consider using addStanzaIdAcknowledgedListener(String, StanzaListener) when possible.

      Parameters:
      listener - the listener to add.
    • removeStanzaAcknowledgedListener

      public boolean removeStanzaAcknowledgedListener(org.jivesoftware.smack.StanzaListener listener)
      Remove the given Stanza acknowledged listener.
      Parameters:
      listener - the listener.
      Returns:
      true if the listener was removed.
    • removeAllStanzaAcknowledgedListeners

      public void removeAllStanzaAcknowledgedListeners()
      Remove all stanza acknowledged listeners.
    • addStanzaDroppedListener

      public void addStanzaDroppedListener(org.jivesoftware.smack.StanzaListener listener)
      Add a Stanza dropped listener.

      Those listeners will be invoked every time a Stanza has been dropped due to a failed SM resume. They will not get automatically removed. If at least one StanzaDroppedListener is configured, no attempt will be made to retransmit the Stanzas.

      Parameters:
      listener - the listener to add.
      Since:
      4.3.3
    • removeStanzaDroppedListener

      public boolean removeStanzaDroppedListener(org.jivesoftware.smack.StanzaListener listener)
      Remove the given Stanza dropped listener.
      Parameters:
      listener - the listener.
      Returns:
      true if the listener was removed.
      Since:
      4.3.3
    • addStanzaIdAcknowledgedListener

      public org.jivesoftware.smack.StanzaListener addStanzaIdAcknowledgedListener(String id, org.jivesoftware.smack.StanzaListener listener) throws org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException
      Add a new Stanza ID acknowledged listener for the given ID.

      The listener will be invoked if the stanza with the given ID was acknowledged by the server. It will automatically be removed after the listener was run.

      Parameters:
      id - the stanza ID.
      listener - the listener to invoke.
      Returns:
      the previous listener for this stanza ID or null.
      Throws:
      org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException - if Stream Management is not enabled.
    • removeStanzaIdAcknowledgedListener

      public org.jivesoftware.smack.StanzaListener removeStanzaIdAcknowledgedListener(String id)
      Remove the Stanza ID acknowledged listener for the given ID.
      Parameters:
      id - the stanza ID.
      Returns:
      true if the listener was found and removed, false otherwise.
    • removeAllStanzaIdAcknowledgedListeners

      public void removeAllStanzaIdAcknowledgedListeners()
      Removes all Stanza ID acknowledged listeners.
    • isSmAvailable

      public boolean isSmAvailable()
      Returns true if Stream Management is supported by the server.
      Returns:
      true if Stream Management is supported by the server.
    • isSmEnabled

      public boolean isSmEnabled()
      Returns true if Stream Management was successfully negotiated with the server.
      Returns:
      true if Stream Management was negotiated.
    • streamWasResumed

      public boolean streamWasResumed()
      Returns true if the stream was successfully resumed with help of Stream Management.
      Returns:
      true if the stream was resumed.
    • isDisconnectedButSmResumptionPossible

      public boolean isDisconnectedButSmResumptionPossible()
      Returns true if the connection is disconnected by a Stream resumption via Stream Management is possible.
      Returns:
      true if disconnected but resumption possible.
    • isSmResumptionPossible

      public boolean isSmResumptionPossible()
      Returns true if the stream is resumable.
      Returns:
      true if the stream is resumable.
    • getMaxSmResumptionTime

      public int getMaxSmResumptionTime()
      Get the maximum resumption time in seconds after which a managed stream can be resumed.

      This method will return Integer.MAX_VALUE if neither the client nor the server specify a maximum resumption time. Be aware of integer overflows when using this value, e.g. do not add arbitrary values to it without checking for overflows before.

      Returns:
      the maximum resumption time in seconds or Integer.MAX_VALUE if none set.
    • setDefaultBundleAndDeferCallback

      public static void setDefaultBundleAndDeferCallback(BundleAndDeferCallback defaultBundleAndDeferCallback)
      Set the default bundle and defer callback used for new connections.
      Parameters:
      defaultBundleAndDeferCallback - TODO javadoc me please
      Since:
      4.1
      See Also:
    • setBundleandDeferCallback

      public void setBundleandDeferCallback(BundleAndDeferCallback bundleAndDeferCallback)
      Set the bundle and defer callback used for this connection.

      You can use null as argument to reset the callback. Outgoing stanzas will then no longer get deferred.

      Parameters:
      bundleAndDeferCallback - the callback or null.
      Since:
      4.1
      See Also:
    • getLocalAddress

      public InetAddress getLocalAddress()
      Returns the local address currently in use for this connection.
      Returns:
      the local address