1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 23:42:06 +01:00

Code cleanup and refactoring.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@5383 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2006-09-15 21:51:08 +00:00 committed by matt
parent edd7df6799
commit 6be93f4a4e
7 changed files with 78 additions and 77 deletions

View file

@ -5,7 +5,7 @@ import org.jivesoftware.smack.packet.PrivacyItem;
import java.util.List; import java.util.List;
/** /**
* A Privacy List is a read only class used to represent a set of allowed or blocked communications. * A privacy list represents a list of contacts that is a read only class used to represent a set of allowed or blocked communications.
* Basically it can:<ul> * Basically it can:<ul>
* *
* <li>Handle many {@link org.jivesoftware.smack.packet.PrivacyItem}.</li> * <li>Handle many {@link org.jivesoftware.smack.packet.PrivacyItem}.</li>

View file

@ -35,7 +35,7 @@ public class PrivacyListManager {
// Create a new PrivacyListManager on every established connection. In the init() // Create a new PrivacyListManager on every established connection. In the init()
// method of PrivacyListManager, we'll add a listener that will delete the // method of PrivacyListManager, we'll add a listener that will delete the
// instance when the connection is closed. // instance when the connection is closed.
XMPPConnection.addConnectionListener(new ConnectionEstablishedListener() { XMPPConnection.addConnectionEstablishedListener(new ConnectionEstablishedListener() {
public void connectionEstablished(XMPPConnection connection) { public void connectionEstablished(XMPPConnection connection) {
new PrivacyListManager(connection); new PrivacyListManager(connection);
} }

View file

@ -4,11 +4,11 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* Handles the reconnection process. Every time a connection is broken, it automatically * Handles the automatic reconnection process. Every time a connection is dropped without
* tries to reconnect it. The reconnection is been executed when the first connection * the application explicitly closing it, the manager automatically tries to reconnect to
* error is detected.<p> * the server.<p>
* *
* The reconnection mechanism will try to reconnect periodically in this way: * The reconnection mechanism will try to reconnect periodically:
* <ol> * <ol>
* <li>First it will try 6 times every 10 seconds. * <li>First it will try 6 times every 10 seconds.
* <li>Then it will try 10 times every 1 minute. * <li>Then it will try 10 times every 1 minute.
@ -35,7 +35,7 @@ public class ReconnectionManager implements ConnectionListener {
// Create a new PrivacyListManager on every established connection. In the init() // Create a new PrivacyListManager on every established connection. In the init()
// method of PrivacyListManager, we'll add a listener that will delete the // method of PrivacyListManager, we'll add a listener that will delete the
// instance when the connection is closed. // instance when the connection is closed.
XMPPConnection.addConnectionListener(new ConnectionEstablishedListener() { XMPPConnection.addConnectionEstablishedListener(new ConnectionEstablishedListener() {
public void connectionEstablished(XMPPConnection connection) { public void connectionEstablished(XMPPConnection connection) {
connection.addConnectionListener(new ReconnectionManager(connection)); connection.addConnectionListener(new ReconnectionManager(connection));
} }
@ -48,7 +48,9 @@ public class ReconnectionManager implements ConnectionListener {
/** /**
* Returns if the reconnection mechanism is allowed to use. * Returns true if the reconnection mechanism is enabled.
*
* @return true if automatic reconnections are allowed.
*/ */
private boolean isReconnectionAllowed() { private boolean isReconnectionAllowed() {
return !done && !connection.isConnected() return !done && !connection.isConnected()
@ -103,15 +105,16 @@ public class ReconnectionManager implements ConnectionListener {
private int firstReconnectionPeriod = 7; // 6 attempts private int firstReconnectionPeriod = 7; // 6 attempts
private int secondReconnectionPeriod = 10 + firstReconnectionPeriod; // 16 attempts private int secondReconnectionPeriod = 10 + firstReconnectionPeriod; // 16 attempts
private int firstReconnectionTime = 10; // 10 seconds private int firstReconnectionTime = 10; // 10 seconds
private int secondReconnectionTime = 1 * 60; // 1 minute private int secondReconnectionTime = 60; // 1 minute
private int lastReconnectionTime = private int lastReconnectionTime =
getSecondBetweenReconnection(); // user defined in seconds getSecondBetweenReconnection(); // user defined in seconds
private int remainingSeconds = 0; // The seconds remaining to a reconnection private int remainingSeconds = 0; // The seconds remaining to a reconnection
private int notificationPeriod = 1000; // 1 second private int notificationPeriod = 1000; // 1 second
/** /**
* Answer the time it should wait until the next reconnection * Returns the amount of time until the next reconnection attempt.
* attempt *
* @return the amount of time until the next reconnection attempt.
*/ */
private int timeDelay() { private int timeDelay() {
if (attempts > secondReconnectionPeriod) { if (attempts > secondReconnectionPeriod) {
@ -173,6 +176,8 @@ public class ReconnectionManager implements ConnectionListener {
/** /**
* Fires listeners when a reconnection attempt has failed. * Fires listeners when a reconnection attempt has failed.
*
* @param exception the exception that occured.
*/ */
protected void notifyReconnectionFailed(Exception exception) { protected void notifyReconnectionFailed(Exception exception) {
List<ConnectionListener> listenersCopy; List<ConnectionListener> listenersCopy;
@ -190,6 +195,8 @@ public class ReconnectionManager implements ConnectionListener {
/** /**
* Fires listeners when The XMPPConnection will retry a reconnection. Expressed in seconds. * Fires listeners when The XMPPConnection will retry a reconnection. Expressed in seconds.
*
* @param seconds the number of seconds that a reconnection will be attempted in.
*/ */
protected void notifyAttemptToReconnectIn(int seconds) { protected void notifyAttemptToReconnectIn(int seconds) {
List<ConnectionListener> listenersCopy; List<ConnectionListener> listenersCopy;

View file

@ -39,11 +39,11 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* Creates a connection to a XMPP server. A simple use of this API might * Creates a connection to a XMPP server. A simple use of this API might
@ -64,10 +64,10 @@ import java.util.concurrent.ConcurrentHashMap;
* *
* XMPPConnections can be reused between connections. This means that an XMPPConnection * XMPPConnections can be reused between connections. This means that an XMPPConnection
* may be connected, disconnected and then connected again. Listeners of the XMPPConnection * may be connected, disconnected and then connected again. Listeners of the XMPPConnection
* will remain accross connections.<p> * will be retained accross connections.<p>
* *
* If a connected XMPPConnection gets disconnected abruptly then it will try to reconnect * If a connected XMPPConnection gets disconnected abruptly then it will try to reconnect
* again. To stop the reconnection process just use {@link #disconnect()}. Once stopped * again. To stop the reconnection process, use {@link #disconnect()}. Once stopped
* you can use {@link #connect()} to manually connect to the server. * you can use {@link #connect()} to manually connect to the server.
* *
* @author Matt Tucker * @author Matt Tucker
@ -90,7 +90,7 @@ public class XMPPConnection {
public static boolean DEBUG_ENABLED = false; public static boolean DEBUG_ENABLED = false;
private final static List<ConnectionEstablishedListener> connectionEstablishedListeners = private final static List<ConnectionEstablishedListener> connectionEstablishedListeners =
new ArrayList<ConnectionEstablishedListener>(); new CopyOnWriteArrayList<ConnectionEstablishedListener>();
static { static {
// Use try block since we may not have permission to get a system // Use try block since we may not have permission to get a system
@ -267,33 +267,6 @@ public class XMPPConnection {
init(config, socketFactory); init(config, socketFactory);
} }
private void connectUsingConfiguration(ConnectionConfiguration config) throws XMPPException {
this.host = config.getHost();
this.port = config.getPort();
try {
if (config.getSocketFactory() == null) {
this.socket = new Socket(host, port);
}
else {
this.socket = config.getSocketFactory().createSocket(host, port);
}
}
catch (UnknownHostException uhe) {
String errorMessage = "Could not connect to " + host + ":" + port + ".";
throw new XMPPException(errorMessage, new XMPPError(
XMPPError.Condition.remote_server_timeout, errorMessage),
uhe);
}
catch (IOException ioe) {
String errorMessage = "XMPPError connecting to " + host + ":"
+ port + ".";
throw new XMPPException(errorMessage, new XMPPError(
XMPPError.Condition.remote_server_error, errorMessage), ioe);
}
this.serviceName = config.getServiceName();
initConnection();
}
/** /**
* Package-private default constructor. This constructor is only intended * Package-private default constructor. This constructor is only intended
* for unit testing. Normal classes extending XMPPConnection should override * for unit testing. Normal classes extending XMPPConnection should override
@ -845,28 +818,54 @@ public class XMPPConnection {
* *
* @param connectionEstablishedListener a listener interested on connection established events. * @param connectionEstablishedListener a listener interested on connection established events.
*/ */
public static void addConnectionListener(ConnectionEstablishedListener connectionEstablishedListener) { public static void addConnectionEstablishedListener(ConnectionEstablishedListener connectionEstablishedListener) {
synchronized (connectionEstablishedListeners) {
if (!connectionEstablishedListeners.contains(connectionEstablishedListener)) { if (!connectionEstablishedListeners.contains(connectionEstablishedListener)) {
connectionEstablishedListeners.add(connectionEstablishedListener); connectionEstablishedListeners.add(connectionEstablishedListener);
} }
} }
}
/** /**
* Removes a listener on new established connections. * Removes a listener on new established connections.
* *
* @param connectionEstablishedListener a listener interested on connection established events. * @param connectionEstablishedListener a listener interested on connection established events.
*/ */
public static void removeConnectionListener(ConnectionEstablishedListener connectionEstablishedListener) { public static void removeConnectionEstablishedListener(ConnectionEstablishedListener connectionEstablishedListener) {
synchronized (connectionEstablishedListeners) {
connectionEstablishedListeners.remove(connectionEstablishedListener); connectionEstablishedListeners.remove(connectionEstablishedListener);
} }
private void connectUsingConfiguration(ConnectionConfiguration config) throws XMPPException {
this.host = config.getHost();
this.port = config.getPort();
try {
if (config.getSocketFactory() == null) {
this.socket = new Socket(host, port);
}
else {
this.socket = config.getSocketFactory().createSocket(host, port);
}
}
catch (UnknownHostException uhe) {
String errorMessage = "Could not connect to " + host + ":" + port + ".";
throw new XMPPException(errorMessage, new XMPPError(
XMPPError.Condition.remote_server_timeout, errorMessage),
uhe);
}
catch (IOException ioe) {
String errorMessage = "XMPPError connecting to " + host + ":"
+ port + ".";
throw new XMPPException(errorMessage, new XMPPError(
XMPPError.Condition.remote_server_error, errorMessage), ioe);
}
this.serviceName = config.getServiceName();
initConnection();
} }
/** /**
* Initializes the connection configuration. This method is only executed * Initializes the connection configuration. This method is only executed
* when the XMPPConnection is created. * when the XMPPConnection is created.
*
* @param config the connection configuration options.
* @param socketFactory a factory used for creating sockets to the XMPP server.
*/ */
private void init(ConnectionConfiguration config, SocketFactory socketFactory) { private void init(ConnectionConfiguration config, SocketFactory socketFactory) {
try { try {
@ -925,8 +924,10 @@ public class XMPPConnection {
if (isFirstInitialization) { if (isFirstInitialization) {
// Notify that a new connection has been established // Notify listeners that a new connection has been established
connectionEstablished(this); for (ConnectionEstablishedListener listener : connectionEstablishedListeners) {
listener.connectionEstablished(this);
}
// Add a listener for all message packets so that we can deliver errant // Add a listener for all message packets so that we can deliver errant
// messages to the best Chat instance available. // messages to the best Chat instance available.
@ -1110,20 +1111,6 @@ public class XMPPConnection {
} }
} }
/**
* Fires listeners on connection established events.
*/
private static void connectionEstablished(XMPPConnection connection) {
ConnectionEstablishedListener[] listeners;
synchronized (connectionEstablishedListeners) {
listeners = new ConnectionEstablishedListener[connectionEstablishedListeners.size()];
connectionEstablishedListeners.toArray(listeners);
}
for (ConnectionEstablishedListener listener : listeners) {
listener.connectionEstablished(connection);
}
}
/*********************************************** /***********************************************
* TLS code below * TLS code below
**********************************************/ **********************************************/
@ -1180,6 +1167,8 @@ public class XMPPConnection {
* The server has indicated that TLS negotiation can start. We now need to secure the * The server has indicated that TLS negotiation can start. We now need to secure the
* existing plain connection and perform a handshake. This method won't return until the * existing plain connection and perform a handshake. This method won't return until the
* connection has finished the handshake or an error occured while securing the connection. * connection has finished the handshake or an error occured while securing the connection.
*
* @throws Exception if an exception occurs.
*/ */
void proceedTLSReceived() throws Exception { void proceedTLSReceived() throws Exception {
SSLContext context = SSLContext.getInstance("TLS"); SSLContext context = SSLContext.getInstance("TLS");
@ -1300,6 +1289,8 @@ public class XMPPConnection {
/** /**
* Start using stream compression since the server has acknowledged stream compression. * Start using stream compression since the server has acknowledged stream compression.
*
* @throws Exception if there is an exception starting stream compression.
*/ */
void startStreamCompression() throws Exception { void startStreamCompression() throws Exception {
// Secure the plain connection // Secure the plain connection
@ -1317,8 +1308,11 @@ public class XMPPConnection {
} }
} }
/**
* Notifies the XMPP connection that stream compression was denied so that
* the connection process can proceed.
*/
void streamCompressionDenied() { void streamCompressionDenied() {
// Notify that compression has been denied
synchronized (this) { synchronized (this) {
this.notify(); this.notify();
} }
@ -1358,9 +1352,9 @@ public class XMPPConnection {
} }
/** /**
* Sets if the connection has already logged in the server. * Sets whether the connection has already logged in the server.
* *
* @param wasAuthenticated * @param wasAuthenticated true if the connection has already been authenticated.
*/ */
private void setWasAuthenticated(boolean wasAuthenticated) { private void setWasAuthenticated(boolean wasAuthenticated) {
if (!this.wasAuthenticated) { if (!this.wasAuthenticated) {

View file

@ -75,7 +75,7 @@ public class LastActivityManager {
// Enable the LastActivity support on every established connection // Enable the LastActivity support on every established connection
static { static {
XMPPConnection.addConnectionListener(new ConnectionEstablishedListener() { XMPPConnection.addConnectionEstablishedListener(new ConnectionEstablishedListener() {
public void connectionEstablished(XMPPConnection connection) { public void connectionEstablished(XMPPConnection connection) {
new LastActivityManager(connection); new LastActivityManager(connection);
} }

View file

@ -59,7 +59,7 @@ public class ServiceDiscoveryManager {
// Create a new ServiceDiscoveryManager on every established connection // Create a new ServiceDiscoveryManager on every established connection
static { static {
XMPPConnection.addConnectionListener(new ConnectionEstablishedListener() { XMPPConnection.addConnectionEstablishedListener(new ConnectionEstablishedListener() {
public void connectionEstablished(XMPPConnection connection) { public void connectionEstablished(XMPPConnection connection) {
new ServiceDiscoveryManager(connection); new ServiceDiscoveryManager(connection);
} }

View file

@ -76,7 +76,7 @@ public class MultiUserChat {
private List<PacketListener> connectionListeners = new ArrayList<PacketListener>(); private List<PacketListener> connectionListeners = new ArrayList<PacketListener>();
static { static {
XMPPConnection.addConnectionListener(new ConnectionEstablishedListener() { XMPPConnection.addConnectionEstablishedListener(new ConnectionEstablishedListener() {
public void connectionEstablished(final XMPPConnection connection) { public void connectionEstablished(final XMPPConnection connection) {
// Set on every established connection that this client supports the Multi-User // Set on every established connection that this client supports the Multi-User
// Chat protocol. This information will be used when another client tries to // Chat protocol. This information will be used when another client tries to