Class XMPPTCPConnection
- java.lang.Object
-
- org.jivesoftware.smack.AbstractXMPPConnection
-
- org.jivesoftware.smack.tcp.XMPPTCPConnection
-
- All Implemented Interfaces:
XMPPConnection
public class XMPPTCPConnection extends 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
XMPPTCPConnection.PacketReader
protected class
XMPPTCPConnection.PacketWriter
-
Nested classes/interfaces inherited from class org.jivesoftware.smack.AbstractXMPPConnection
AbstractXMPPConnection.InterceptorWrapper, AbstractXMPPConnection.ListenerWrapper, AbstractXMPPConnection.SyncPointState
-
Nested classes/interfaces inherited from interface org.jivesoftware.smack.XMPPConnection
XMPPConnection.FromMode
-
-
Field Summary
Fields Modifier and Type Field Description protected XMPPTCPConnection.PacketReader
packetReader
Protected access level because of unit test purposesprotected XMPPTCPConnection.PacketWriter
packetWriter
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, currentSmackException, currentXmppException, debugger, host, lastFeaturesReceived, outgoingStreamXmlEnvironment, port, reader, saslFeatureReceived, SMACK_REACTOR, streamFeatures, streamId, tlsHandled, user, wasAuthenticated, writer
-
-
Constructor Summary
Constructors Constructor Description XMPPTCPConnection(java.lang.CharSequence jid, java.lang.String password)
Creates a new XMPP connection over TCP.XMPPTCPConnection(java.lang.CharSequence username, java.lang.String password, java.lang.String serviceName)
Creates a new XMPP connection over TCP.XMPPTCPConnection(XMPPTCPConnectionConfiguration config)
Creates a new XMPP connection over TCP (optionally using proxies).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
addRequestAckPredicate(StanzaFilter predicate)
Add a predicate for Stream Management acknowledgment requests.void
addStanzaAcknowledgedListener(StanzaListener listener)
Add a Stanza acknowledged listener.void
addStanzaDroppedListener(StanzaListener listener)
Add a Stanza dropped listener.StanzaListener
addStanzaIdAcknowledgedListener(java.lang.String id, StanzaListener listener)
Add a new Stanza ID acknowledged listener for the given ID.protected void
afterFeaturesReceived()
protected void
afterSuccessfulLogin(boolean resumed)
protected void
connectInternal()
Establishes a connection to the XMPP server.int
getMaxSmResumptionTime()
Get the maximum resumption time in seconds after which a managed stream can be resumed.void
instantShutdown()
Performs an unclean disconnect and shutdown of the connection.boolean
isDisconnectedButSmResumptionPossible()
Returns true if the connection is disconnected by a Stream resumption via Stream Management is possible.boolean
isSecureConnection()
Returns true if the connection to the server has successfully negotiated encryption.boolean
isSmAvailable()
Returns true if Stream Management is supported by the server.boolean
isSmEnabled()
Returns true if Stream Management was successfully negotiated with the server.boolean
isSmResumptionPossible()
Returns true if the stream is resumable.boolean
isUsingCompression()
Returns true if network traffic is being compressed.protected void
loginInternal(java.lang.String username, java.lang.String password, Resourcepart resource)
void
removeAllRequestAckPredicates()
Remove all predicates for Stream Management acknowledgment requests.void
removeAllStanzaAcknowledgedListeners()
Remove all stanza acknowledged listeners.void
removeAllStanzaIdAcknowledgedListeners()
Removes all Stanza ID acknowledged listeners.boolean
removeRequestAckPredicate(StanzaFilter predicate)
Remove the given predicate for Stream Management acknowledgment request.boolean
removeStanzaAcknowledgedListener(StanzaListener listener)
Remove the given Stanza acknowledged listener.boolean
removeStanzaDroppedListener(StanzaListener listener)
Remove the given Stanza dropped listener.StanzaListener
removeStanzaIdAcknowledgedListener(java.lang.String id)
Remove the Stanza ID acknowledged listener for the given ID.void
requestSmAcknowledgement()
Send an unconditional Stream Management acknowledgement request to the server.void
sendNonza(Nonza element)
Send a Nonza.void
sendSmAcknowledgement()
Send a unconditional Stream Management acknowledgment to the server.protected void
sendStanzaInternal(Stanza packet)
void
setBundleandDeferCallback(BundleAndDeferCallback bundleAndDeferCallback)
Set the bundle and defer callback used for this connection.static void
setDefaultBundleAndDeferCallback(BundleAndDeferCallback defaultBundleAndDeferCallback)
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)
Deprecated.usesetUseStreamManagementResumptionDefault(boolean)
instead.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(java.io.Writer writer)
For unit testing purposesprotected void
shutdown()
Shuts the current connection down.boolean
streamWasResumed()
Returns true if the stream was successfully resumed with help of Stream Management.protected void
throwAlreadyConnectedExceptionIfAppropriate()
protected void
throwAlreadyLoggedInExceptionIfAppropriate()
protected void
throwNotConnectedExceptionIfAppropriate()
-
Methods inherited from class org.jivesoftware.smack.AbstractXMPPConnection
addAsyncStanzaListener, addConnectionListener, addMessageInterceptor, addOneTimeSyncCallback, addPresenceInterceptor, addStanzaInterceptor, 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, getLastStanzaReceived, getParsingExceptionCallback, getPort, getReactor, getReplyTimeout, getSmackTlsContext, getStanzaFactory, getStreamId, 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, removeStanzaInterceptor, removeStanzaListener, removeStanzaSendingListener, removeSyncStanzaListener, schedule, sendAndWaitForResponse, sendAsync, sendAsync, sendIqRequestAndWaitForResponse, sendIqRequestAsync, sendIqRequestAsync, sendStanza, sendStreamOpen, setCurrentConnectionExceptionAndNotify, setFromMode, setMaxAsyncOperations, setParsingExceptionCallback, setReplyTimeout, setUnknownIqRequestReplyMode, setWasAuthenticated, throwCurrentConnectionException, throwNotConnectedExceptionIfAppropriate, toString, trySendStanza, trySendStanza, unregisterIQRequestHandler, unregisterIQRequestHandler, 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, getFeature, hasFeature
-
-
-
-
Field Detail
-
packetWriter
protected final XMPPTCPConnection.PacketWriter packetWriter
Protected access level because of unit test purposes
-
packetReader
protected final XMPPTCPConnection.PacketReader packetReader
Protected access level because of unit test purposes
-
-
Constructor Detail
-
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(java.lang.CharSequence jid, java.lang.String password) throws 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 theXMPPTCPConnection(XMPPTCPConnectionConfiguration)
constructor.- Parameters:
jid
- the bare JID used by the client.password
- the password or authentication token.- Throws:
XmppStringprepException
- if the provided string is invalid.
-
XMPPTCPConnection
public XMPPTCPConnection(java.lang.CharSequence username, java.lang.String password, java.lang.String serviceName) throws 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 pleasepassword
- TODO javadoc me pleaseserviceName
- TODO javadoc me please- Throws:
XmppStringprepException
- if the provided string is invalid.
-
-
Method Detail
-
throwNotConnectedExceptionIfAppropriate
protected void throwNotConnectedExceptionIfAppropriate() throws SmackException.NotConnectedException
- Overrides:
throwNotConnectedExceptionIfAppropriate
in classAbstractXMPPConnection
- Throws:
SmackException.NotConnectedException
-
throwAlreadyConnectedExceptionIfAppropriate
protected void throwAlreadyConnectedExceptionIfAppropriate() throws SmackException.AlreadyConnectedException
- Overrides:
throwAlreadyConnectedExceptionIfAppropriate
in classAbstractXMPPConnection
- Throws:
SmackException.AlreadyConnectedException
-
throwAlreadyLoggedInExceptionIfAppropriate
protected void throwAlreadyLoggedInExceptionIfAppropriate() throws SmackException.AlreadyLoggedInException
- Overrides:
throwAlreadyLoggedInExceptionIfAppropriate
in classAbstractXMPPConnection
- Throws:
SmackException.AlreadyLoggedInException
-
afterSuccessfulLogin
protected void afterSuccessfulLogin(boolean resumed) throws SmackException.NotConnectedException, java.lang.InterruptedException
- Overrides:
afterSuccessfulLogin
in classAbstractXMPPConnection
- Throws:
SmackException.NotConnectedException
java.lang.InterruptedException
-
loginInternal
protected void loginInternal(java.lang.String username, java.lang.String password, Resourcepart resource) throws XMPPException, SmackException, java.io.IOException, java.lang.InterruptedException
- Specified by:
loginInternal
in classAbstractXMPPConnection
- Throws:
XMPPException
SmackException
java.io.IOException
java.lang.InterruptedException
-
isSecureConnection
public boolean isSecureConnection()
Description copied from interface:XMPPConnection
Returns true if the connection to the server has successfully negotiated encryption.- Specified by:
isSecureConnection
in interfaceXMPPConnection
- Specified by:
isSecureConnection
in classAbstractXMPPConnection
- Returns:
- true if a secure connection to the server.
-
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 classAbstractXMPPConnection
-
instantShutdown
public void instantShutdown()
Description copied from class:AbstractXMPPConnection
Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza.- Specified by:
instantShutdown
in classAbstractXMPPConnection
-
sendNonza
public 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 interfaceXMPPConnection
- Specified by:
sendNonza
in classAbstractXMPPConnection
- 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.
-
sendStanzaInternal
protected void sendStanzaInternal(Stanza packet) throws SmackException.NotConnectedException, java.lang.InterruptedException
- Specified by:
sendStanzaInternal
in classAbstractXMPPConnection
- Throws:
SmackException.NotConnectedException
java.lang.InterruptedException
-
isUsingCompression
public 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 interfaceXMPPConnection
- Specified by:
isUsingCompression
in classAbstractXMPPConnection
- Returns:
- true if network traffic is being compressed.
-
connectInternal
protected void connectInternal() throws SmackException, java.io.IOException, XMPPException, java.lang.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 classAbstractXMPPConnection
- Throws:
XMPPException
- if an error occurs while trying to establish the connection.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.
-
setWriter
protected void setWriter(java.io.Writer writer)
For unit testing purposes- Parameters:
writer
- TODO javadoc me please
-
afterFeaturesReceived
protected void afterFeaturesReceived() throws SmackException.NotConnectedException, java.lang.InterruptedException, SmackException.SecurityRequiredByServerException
- Overrides:
afterFeaturesReceived
in classAbstractXMPPConnection
- Throws:
SmackException.NotConnectedException
java.lang.InterruptedException
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)
Deprecated.usesetUseStreamManagementResumptionDefault(boolean)
instead.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(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(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 StreamManagementException.StreamManagementNotEnabledException, SmackException.NotConnectedException, java.lang.InterruptedException
Send an unconditional Stream Management acknowledgement request to the server.- Throws:
StreamManagementException.StreamManagementNotEnabledException
- if Stream Management is not enabled.SmackException.NotConnectedException
- if the connection is not connected.java.lang.InterruptedException
- if the calling thread was interrupted.
-
sendSmAcknowledgement
public void sendSmAcknowledgement() throws StreamManagementException.StreamManagementNotEnabledException, SmackException.NotConnectedException, java.lang.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:
StreamManagementException.StreamManagementNotEnabledException
- if Stream Management is not enabled.SmackException.NotConnectedException
- if the connection is not connected.java.lang.InterruptedException
- if the calling thread was interrupted.
-
addStanzaAcknowledgedListener
public void addStanzaAcknowledgedListener(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(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(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(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 StanzaListener addStanzaIdAcknowledgedListener(java.lang.String id, StanzaListener listener) throws 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:
StreamManagementException.StreamManagementNotEnabledException
- if Stream Management is not enabled.
-
removeStanzaIdAcknowledgedListener
public StanzaListener removeStanzaIdAcknowledgedListener(java.lang.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:
BundleAndDeferCallback
-
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 ornull
.- Since:
- 4.1
- See Also:
BundleAndDeferCallback
-
-