public abstract class Connection extends Object
// Create a connection to the igniterealtime.org XMPP server.
Connection con = new XMPPConnection("igniterealtime.org");
// Connect to the server
con.connect();
// Most servers require you to login before performing other tasks.
con.login("jsmith", "mypass");
// Start a new conversation with John Doe and send him a message.
Chat chat = connection.getChatManager().createChat("jdoe@igniterealtime.org", new MessageListener() {
public void processMessage(Chat chat, Message message) {
// Print out any messages we get back to standard out.
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Howdy!");
// Disconnect from the server
con.disconnect();
Connections can be reused between connections. This means that an Connection
may be connected, disconnected and then connected again. Listeners of the
Connection will be retained accross connections.
If a connected Connection gets disconnected abruptly then it will try to reconnect again. To stop the reconnection process, use
disconnect().
Once stopped you can use connect() to manually connect to the
server.XMPPConnection| Modifier and Type | Class and Description |
|---|---|
protected static class |
Connection.InterceptorWrapper
A wrapper class to associate a packet filter with an interceptor.
|
protected static class |
Connection.ListenerWrapper
A wrapper class to associate a packet filter with a listener.
|
| Modifier and Type | Field and Description |
|---|---|
protected Collection<PacketCollector> |
collectors
A collection of PacketCollectors which collects packets for a specified
filter and perform blocking and polling operations on the result queue.
|
protected XMPPInputOutputStream |
compressionHandler |
protected static List<XMPPInputOutputStream> |
compressionHandlers |
protected ConnectionConfiguration |
config
Holds the initial configuration used while creating the connection.
|
protected int |
connectionCounterValue
A number to uniquely identify connections that are created.
|
protected Collection<ConnectionListener> |
connectionListeners
A collection of ConnectionListeners which listen for connection closing
and reconnection events.
|
static boolean |
DEBUG_ENABLED
Value that indicates whether debugging is enabled.
|
protected SmackDebugger |
debugger
The SmackDebugger allows to log and debug XML traffic.
|
protected Map<PacketInterceptor,Connection.InterceptorWrapper> |
interceptors
List of PacketInterceptors that will be notified when a new packet is
about to be sent to the server.
|
protected Reader |
reader
The Reader which is used for the debugger.
|
protected Map<PacketListener,Connection.ListenerWrapper> |
recvListeners
List of PacketListeners that will be notified when a new packet was
received.
|
protected SASLAuthentication |
saslAuthentication
The SASLAuthentication manager that is responsible for authenticating
with the server.
|
protected Map<PacketListener,Connection.ListenerWrapper> |
sendListeners
List of PacketListeners that will be notified when a new packet was sent.
|
protected Writer |
writer
The Writer which is used for the debugger.
|
| Modifier | Constructor and Description |
|---|---|
protected |
Connection(ConnectionConfiguration configuration)
Create a new Connection to a XMPP server.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
addConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
Adds a new listener that will be notified when new Connections are
created.
|
void |
addConnectionListener(ConnectionListener connectionListener)
Adds a connection listener to this connection that will be notified when
the connection closes or fails.
|
void |
addPacketInterceptor(PacketInterceptor packetInterceptor,
PacketFilter packetFilter)
Registers a packet interceptor with this connection.
|
void |
addPacketListener(PacketListener packetListener,
PacketFilter packetFilter)
Registers a packet listener with this connection.
|
void |
addPacketSendingListener(PacketListener packetListener,
PacketFilter packetFilter)
Registers a packet listener with this connection.
|
abstract void |
connect()
Establishes a connection to the XMPP server and performs an automatic
login only if the previous connection state was logged (authenticated).
|
PacketCollector |
createPacketCollector(PacketFilter packetFilter)
Creates a new packet collector for this connection.
|
void |
disconnect()
Closes the connection by setting presence to unavailable then closing the
connection to the XMPP server.
|
abstract void |
disconnect(org.xmpp.packet.Presence unavailablePresence)
Closes the connection.
|
protected void |
firePacketInterceptors(org.xmpp.packet.Packet packet)
Process interceptors.
|
protected void |
firePacketSendingListeners(org.xmpp.packet.Packet packet)
Process all packet listeners for sending packets.
|
ChatManager |
getChatManager()
Returns a chat manager instance for this connection.
|
protected ConnectionConfiguration |
getConfiguration()
Returns the configuration used to connect to the server.
|
protected static Collection<ConnectionCreationListener> |
getConnectionCreationListeners()
Get the collection of listeners that are interested in connection
creation events.
|
abstract String |
getConnectionID()
Returns the connection ID for this connection, which is the value set by
the server when opening a XMPP stream.
|
protected Collection<ConnectionListener> |
getConnectionListeners()
Get the collection of listeners that are interested in connection events.
|
String |
getHost()
Returns the host name of the server where the XMPP server is running.
|
protected Collection<PacketCollector> |
getPacketCollectors()
Get the collection of all packet collectors for this connection.
|
protected Map<PacketInterceptor,Connection.InterceptorWrapper> |
getPacketInterceptors()
Get a map of all packet interceptors for sending packets of this
connection.
|
protected Map<PacketListener,Connection.ListenerWrapper> |
getPacketListeners()
Get a map of all packet listeners for received packets of this
connection.
|
protected Map<PacketListener,Connection.ListenerWrapper> |
getPacketSendingListeners()
Get a map of all packet listeners for sending packets of this connection.
|
int |
getPort()
Returns the port number of the XMPP server for this connection.
|
abstract UserRoster |
getRoster()
Returns the roster for the user.
|
SASLAuthentication |
getSASLAuthentication()
Returns the SASLAuthentication manager that is responsible for
authenticating with the server.
|
String |
getServiceCapsNode()
Retrieve the servers Entity Caps node
Connection holds this information in order to avoid a dependency to
smackx where EntityCapsManager lives from smack.
|
String |
getServiceName()
Returns the name of the service provided by the XMPP server for this
connection.
|
abstract String |
getUser()
Returns the full XMPP address of the user that is logged in to the
connection or null if not logged in yet.
|
protected void |
initDebugger()
Initialize the
debugger. |
abstract boolean |
isAnonymous()
Returns true if currently authenticated anonymously.
|
abstract boolean |
isAuthenticated()
Returns true if currently authenticated by successfully calling the login
method.
|
abstract boolean |
isConnected()
Returns true if currently connected to the XMPP server.
|
protected boolean |
isReconnectionAllowed()
Returns if the reconnection mechanism is allowed to be used.
|
boolean |
isRosterVersioningSupported()
Returns true if the server supports roster versioning as defined in
XEP-0237.
|
abstract boolean |
isSecureConnection()
Returns true if the connection to the server has successfully negotiated
encryption.
|
abstract boolean |
isUsingCompression()
Returns true if network traffic is being compressed.
|
void |
login(String username,
String password)
Logs in to the server using the strongest authentication mode supported
by the server, then sets presence to available.
|
abstract void |
login(String username,
String password,
String resource)
Logs in to the server using the strongest authentication mode supported
by the server, then sets presence to available.
|
abstract void |
loginAnonymously()
Logs in to the server anonymously.
|
static void |
removeConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
Removes a listener that was interested in connection creation events.
|
void |
removeConnectionListener(ConnectionListener connectionListener)
Removes a connection listener from this connection.
|
protected void |
removePacketCollector(PacketCollector collector)
Remove a packet collector of this connection.
|
void |
removePacketInterceptor(PacketInterceptor packetInterceptor)
Removes a packet interceptor.
|
void |
removePacketListener(PacketListener packetListener)
Removes a packet listener for received packets from this connection.
|
void |
removePacketSendingListener(PacketListener packetListener)
Removes a packet listener for sending packets from this connection.
|
abstract void |
sendPacket(org.xmpp.packet.Packet packet)
Sends the specified packet to the server.
|
protected void |
setRosterVersioningSupported()
Indicates that the server supports roster versioning as defined in
XEP-0237.
|
protected void |
setServiceCapsNode(String node)
Set the servers Entity Caps node
Connection holds this information in order to avoid a dependency to
smackx where EntityCapsManager lives from smack.
|
protected static final List<XMPPInputOutputStream> compressionHandlers
public static boolean DEBUG_ENABLED
protected final Collection<ConnectionListener> connectionListeners
protected final Collection<PacketCollector> collectors
protected final Map<PacketListener,Connection.ListenerWrapper> recvListeners
protected final Map<PacketListener,Connection.ListenerWrapper> sendListeners
protected final Map<PacketInterceptor,Connection.InterceptorWrapper> interceptors
protected SmackDebugger debugger
protected Reader reader
protected Writer writer
protected SASLAuthentication saslAuthentication
protected final int connectionCounterValue
protected final ConnectionConfiguration config
protected XMPPInputOutputStream compressionHandler
protected Connection(ConnectionConfiguration configuration)
configuration - The configuration which is used to establish the connection.protected ConnectionConfiguration getConfiguration()
public String getServiceName()
public String getHost()
public int getPort()
public abstract String getUser()
public abstract String getConnectionID()
public abstract boolean isConnected()
public abstract boolean isAuthenticated()
public abstract boolean isAnonymous()
public abstract boolean isSecureConnection()
protected boolean isReconnectionAllowed()
public abstract boolean isUsingCompression()
public abstract void connect()
throws XMPPException
Listeners will be preserved from a previous connection if the reconnection occurs after an abrupt termination.
XMPPException - if an error occurs while trying to establish the connection.public void login(String username, String password) throws XMPPException
Before logging in (i.e. authenticate) to the server the connection must
be connected.
It is possible to log in without sending an initial available presence by
using ConnectionConfiguration.setSendPresence(boolean). If this
connection is not interested in loading its roster upon login then use
ConnectionConfiguration.setRosterLoadedAtLogin(boolean). Finally,
if you want to not pass a password and instead use a more advanced
mechanism while using SASL then you may be interested in using
ConnectionConfiguration.setCallbackHandler(javax.security.auth.callback.CallbackHandler)
. For more advanced login settings see ConnectionConfiguration.
username - the username.password - the password or null if using a CallbackHandler.XMPPException - if an error occurs.public abstract void login(String username, String password, String resource) throws XMPPException
Before logging in (i.e. authenticate) to the server the connection must
be connected.
It is possible to log in without sending an initial available presence by
using ConnectionConfiguration.setSendPresence(boolean). If this
connection is not interested in loading its roster upon login then use
ConnectionConfiguration.setRosterLoadedAtLogin(boolean). Finally,
if you want to not pass a password and instead use a more advanced
mechanism while using SASL then you may be interested in using
ConnectionConfiguration.setCallbackHandler(javax.security.auth.callback.CallbackHandler)
. For more advanced login settings see ConnectionConfiguration.
username - the username.password - the password or null if using a CallbackHandler.resource - the resource.XMPPException - if an error occurs.IllegalStateException - if not connected to the server, or already logged in to the
serrver.public abstract void loginAnonymously()
throws XMPPException
XMPPException - if an error occurs or anonymous logins are not supported by
the server.IllegalStateException - if not connected to the server, or already logged in to the
serrver.public abstract void sendPacket(org.xmpp.packet.Packet packet)
packet - the packet to send.public ChatManager getChatManager()
public abstract UserRoster getRoster()
This method will never return null, instead if the user has
not yet logged into the server or is logged in anonymously all modifying
methods of the returned roster object like
UserRoster.createEntry(String, String, String[]),
UserRoster.removeEntry(RosterEntry) , etc. except adding or
removing RosterListeners will throw an IllegalStateException.
public SASLAuthentication getSASLAuthentication()
public void disconnect()
This method cleans up all resources used by the connection. Therefore, the roster, listeners and other stateful objects cannot be re-used by simply calling connect() on this connection again. This is unlike the behavior during unexpected disconnects (and subsequent connections). In that case, all state is preserved to allow for more seamless error recovery.
public abstract void disconnect(org.xmpp.packet.Presence unavailablePresence)
This method cleans up all resources used by the connection. Therefore, the roster, listeners and other stateful objects cannot be re-used by simply calling connect() on this connection again. This is unlike the behavior during unexpected disconnects (and subsequent connections). In that case, all state is preserved to allow for more seamless error recovery.
unavailablePresence - the presence packet to send during shutdown.public static void addConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
connectionCreationListener - a listener interested on new connections.public static void removeConnectionCreationListener(ConnectionCreationListener connectionCreationListener)
connectionCreationListener - a listener interested on new connections.protected static Collection<ConnectionCreationListener> getConnectionCreationListeners()
public void addConnectionListener(ConnectionListener connectionListener)
connectionListener - a connection listener.public void removeConnectionListener(ConnectionListener connectionListener)
connectionListener - a connection listener.protected Collection<ConnectionListener> getConnectionListeners()
public PacketCollector createPacketCollector(PacketFilter packetFilter)
PacketListener
when you need to wait for a specific result.packetFilter - the packet filter to use.protected void removePacketCollector(PacketCollector collector)
collector - a packet collectors which was created for this connection.protected Collection<PacketCollector> getPacketCollectors()
public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter)
NOTE: If you want get a similar callback for outgoing packets, see
addPacketInterceptor(PacketInterceptor, PacketFilter).
packetListener - the packet listener to notify of new received packets.packetFilter - the packet filter to use.public void removePacketListener(PacketListener packetListener)
packetListener - the packet listener to remove.protected Map<PacketListener,Connection.ListenerWrapper> getPacketListeners()
public void addPacketSendingListener(PacketListener packetListener, PacketFilter packetFilter)
packetListener - the packet listener to notify of sent packets.packetFilter - the packet filter to use.public void removePacketSendingListener(PacketListener packetListener)
packetListener - the packet listener to remove.protected Map<PacketListener,Connection.ListenerWrapper> getPacketSendingListeners()
protected void firePacketSendingListeners(org.xmpp.packet.Packet packet)
packet - the packet to process.public void addPacketInterceptor(PacketInterceptor packetInterceptor, PacketFilter packetFilter)
NOTE: For a similar functionality on incoming packets, see
addPacketListener(PacketListener, PacketFilter).
packetInterceptor - the packet interceptor to notify of packets about to be sent.packetFilter - the packet filter to use.public void removePacketInterceptor(PacketInterceptor packetInterceptor)
packetInterceptor - the packet interceptor to remove.protected Map<PacketInterceptor,Connection.InterceptorWrapper> getPacketInterceptors()
protected void firePacketInterceptors(org.xmpp.packet.Packet packet)
packet - the packet that is going to be sent to the serverprotected void initDebugger()
debugger. You can specify a customized
SmackDebugger by setup the system property
smack.debuggerClass to the implementation.IllegalStateException - if the reader or writer isn't yet initialized.IllegalArgumentException - if the SmackDebugger can't be loaded.protected void setServiceCapsNode(String node)
node - public String getServiceCapsNode()
public boolean isRosterVersioningSupported()
protected void setRosterVersioningSupported()
Copyright © 2014. All Rights Reserved.