org.jivesoftware.smack
Class DummyConnection

java.lang.Object
  extended by org.jivesoftware.smack.Connection
      extended by org.jivesoftware.smack.DummyConnection
Direct Known Subclasses:
ThreadedDummyConnection

public class DummyConnection
extends Connection

A dummy implementation of Connection, intended to be used during unit tests. Instances store any packets that are delivered to be send using the sendPacket(Packet) method in a blocking queue. The content of this queue can be inspected using getSentPacket(). Typically these queues are used to retrieve a message that was generated by the client. Packets that should be processed by the client to simulate a received stanza can be delivered using the processPacket(Packet) method. It invokes the registered packet interceptors and listeners.

Author:
Guenther Niess
See Also:
Connection

Field Summary
 
Fields inherited from class org.jivesoftware.smack.Connection
DEBUG_ENABLED
 
Constructor Summary
DummyConnection()
           
DummyConnection(ConnectionConfiguration configuration)
           
 
Method Summary
 void connect()
          Establishes a connection to the XMPP server and performs an automatic login only if the previous connection state was logged (authenticated).
 void disconnect(Presence unavailablePresence)
          Closes the connection.
 java.lang.String getConnectionID()
          Returns the connection ID for this connection, which is the value set by the server when opening a XMPP stream.
 int getNumberOfSentPackets()
          Returns the number of packets that's sent through sendPacket(Packet) and that has not been returned by getSentPacket().
 Roster getRoster()
          Returns the roster for the user.
 Packet getSentPacket()
          Returns the first packet that's sent through sendPacket(Packet) and that has not been returned by earlier calls to this method.
 java.lang.String getUser()
          Returns the full XMPP address of the user that is logged in to the connection or null if not logged in yet.
 boolean isAnonymous()
          Returns true if currently authenticated anonymously.
 boolean isAuthenticated()
          Returns true if currently authenticated by successfully calling the login method.
 boolean isConnected()
          Returns true if currently connected to the XMPP server.
 boolean isSecureConnection()
          Returns true if the connection to the server has successfully negotiated encryption.
 boolean isUsingCompression()
          Returns true if network traffic is being compressed.
 void login(java.lang.String username, java.lang.String password, java.lang.String resource)
          Logs in to the server using the strongest authentication mode supported by the server, then sets presence to available.
 void loginAnonymously()
          Logs in to the server anonymously.
 void processPacket(Packet packet)
          Processes a packet through the installed packet collectors and listeners and letting them examine the packet to see if they are a match with the filter.
 void sendPacket(Packet packet)
          Sends the specified packet to the server.
 
Methods inherited from class org.jivesoftware.smack.Connection
addConnectionCreationListener, addConnectionListener, addPacketInterceptor, addPacketListener, addPacketSendingListener, createPacketCollector, disconnect, getAccountManager, getChatManager, getHost, getPort, getSASLAuthentication, getServiceName, login, removeConnectionCreationListener, removeConnectionListener, removePacketInterceptor, removePacketListener, removePacketSendingListener
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DummyConnection

public DummyConnection()

DummyConnection

public DummyConnection(ConnectionConfiguration configuration)
Method Detail

connect

public void connect()
             throws XMPPException
Description copied from class: Connection
Establishes a connection to the XMPP server and performs an automatic login only if the previous connection state was logged (authenticated). It basically creates and maintains a connection to the server.

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

Specified by:
connect in class Connection
Throws:
XMPPException - if an error occurs while trying to establish the connection.

disconnect

public void disconnect(Presence unavailablePresence)
Description copied from class: Connection
Closes the connection. A custom unavailable presence is sent to the server, followed by closing the stream. The Connection can still be used for connecting to the server again. A custom unavilable presence is useful for communicating offline presence information such as "On vacation". Typically, just the status text of the presence packet is set with online information, but most XMPP servers will deliver the full presence packet with whatever data is set.

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.

Specified by:
disconnect in class Connection
Parameters:
unavailablePresence - the presence packet to send during shutdown.

getConnectionID

public java.lang.String getConnectionID()
Description copied from class: Connection
Returns the connection ID for this connection, which is the value set by the server when opening a XMPP stream. If the server does not set a connection ID, this value will be null. This value will be null if not connected to the server.

Specified by:
getConnectionID in class Connection
Returns:
the ID of this connection returned from the XMPP server or null if not connected to the server.

getRoster

public Roster getRoster()
Description copied from class: Connection
Returns the roster for the user.

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 Roster.createEntry(String, String, String[]), Roster.removeEntry(RosterEntry) , etc. except adding or removing RosterListeners will throw an IllegalStateException.

Specified by:
getRoster in class Connection
Returns:
the user's roster.

getUser

public java.lang.String getUser()
Description copied from class: Connection
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 class Connection
Returns:
the full XMPP address of the user logged in.

isAnonymous

public boolean isAnonymous()
Description copied from class: Connection
Returns true if currently authenticated anonymously.

Specified by:
isAnonymous in class Connection
Returns:
true if authenticated anonymously.

isAuthenticated

public boolean isAuthenticated()
Description copied from class: Connection
Returns true if currently authenticated by successfully calling the login method.

Specified by:
isAuthenticated in class Connection
Returns:
true if authenticated.

isConnected

public boolean isConnected()
Description copied from class: Connection
Returns true if currently connected to the XMPP server.

Specified by:
isConnected in class Connection
Returns:
true if connected.

isSecureConnection

public boolean isSecureConnection()
Description copied from class: Connection
Returns true if the connection to the server has successfully negotiated encryption.

Specified by:
isSecureConnection in class Connection
Returns:
true if a secure connection to the server.

isUsingCompression

public boolean isUsingCompression()
Description copied from class: Connection
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 class Connection
Returns:
true if network traffic is being compressed.

login

public void login(java.lang.String username,
                  java.lang.String password,
                  java.lang.String resource)
           throws XMPPException
Description copied from class: Connection
Logs in to the server using the strongest authentication mode supported by the server, then sets presence to available. If the server supports SASL authentication then the user will be authenticated using SASL if not Non-SASL authentication will be tried. If more than five seconds (default timeout) elapses in each step of the authentication process without a response from the server, or if an error occurs, a XMPPException will be thrown.

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.

Specified by:
login in class Connection
Parameters:
username - the username.
password - the password or null if using a CallbackHandler.
resource - the resource.
Throws:
XMPPException - if an error occurs.

loginAnonymously

public void loginAnonymously()
                      throws XMPPException
Description copied from class: Connection
Logs in to the server anonymously. Very few servers are configured to support anonymous authentication, so it's fairly likely logging in anonymously will fail. If anonymous login does succeed, your XMPP address will likely be in the form "123ABC@server/789XYZ" or "server/123ABC" (where "123ABC" and "789XYZ" is a random value generated by the server).

Specified by:
loginAnonymously in class Connection
Throws:
XMPPException - if an error occurs or anonymous logins are not supported by the server.

sendPacket

public void sendPacket(Packet packet)
Description copied from class: Connection
Sends the specified packet to the server.

Specified by:
sendPacket in class Connection
Parameters:
packet - the packet to send.

getNumberOfSentPackets

public int getNumberOfSentPackets()
Returns the number of packets that's sent through sendPacket(Packet) and that has not been returned by getSentPacket().

Returns:
the number of packets which are in the queue.

getSentPacket

public Packet getSentPacket()
                     throws java.lang.InterruptedException
Returns the first packet that's sent through sendPacket(Packet) and that has not been returned by earlier calls to this method. This method will block for up to two seconds if no packets have been sent yet.

Returns:
a sent packet.
Throws:
java.lang.InterruptedException

processPacket

public void processPacket(Packet packet)
Processes a packet through the installed packet collectors and listeners and letting them examine the packet to see if they are a match with the filter.

Parameters:
packet - the packet to process.