diff --git a/source/org/jivesoftware/smack/XMPPConnection.java b/source/org/jivesoftware/smack/XMPPConnection.java index d0cd0c2aa..4c67fdf4c 100644 --- a/source/org/jivesoftware/smack/XMPPConnection.java +++ b/source/org/jivesoftware/smack/XMPPConnection.java @@ -32,9 +32,9 @@ import javax.net.ssl.SSLSocket; import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; -import java.net.InetAddress; import java.util.Collection; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; @@ -52,7 +52,7 @@ import java.util.concurrent.atomic.AtomicInteger; * 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); @@ -62,11 +62,11 @@ import java.util.concurrent.atomic.AtomicInteger; * // Disconnect from the server * con.disconnect(); * - * + *

* XMPPConnections can be reused between connections. This means that an XMPPConnection * may be connected, disconnected and then connected again. Listeners of the XMPPConnection * will be retained accross connections.

- * + *

* If a connected XMPPConnection gets disconnected abruptly then it will try to reconnect * again. To stop the reconnection process, use {@link #disconnect()}. Once stopped * you can use {@link #connect()} to manually connect to the server. @@ -79,11 +79,11 @@ public class XMPPConnection { * Value that indicates whether debugging is enabled. When enabled, a debug * window will apear for each new connection that will contain the following * information:

- * + *

* Debugging can be enabled by setting this field to true, or by setting the Java system * property smack.debugEnabled to true. The system property can be set on the * command line such as "java SomeApp -Dsmack.debugEnabled=true". @@ -109,6 +109,7 @@ public class XMPPConnection { // Ensure the SmackConfiguration class is loaded by calling a method in it. SmackConfiguration.getVersion(); } + private SmackDebugger debugger = null; /** @@ -174,11 +175,11 @@ public class XMPPConnection { * serviceName with the default port of 5222. Encrypted connections (TLS) * will be used if available, stream compression is disabled, and standard SASL * mechanisms will be used for authentication.

- * + *

* This is the simplest constructor for connecting to an XMPP server. Alternatively, * you can get fine-grained control over connection settings using the * {@link #XMPPConnection(ConnectionConfiguration)} constructor.

- * + *

* Note that XMPPConnection constructors do not establish a connection to the server * and you must call {@link #connect()}. * @@ -195,11 +196,11 @@ public class XMPPConnection { /** * Creates a new XMPP connection using the specified connection configuration.

- * + *

* Manually specifying connection configuration information is suitable for * advanced users of the API. In many cases, using the * {@link #XMPPConnection(String)} constructor is a better approach.

- * + *

* Note that XMPPConnection constructors do not establish a connection to the server * and you must call {@link #connect()}. * @@ -215,7 +216,7 @@ public class XMPPConnection { * will be null. This value will be null if not connected to the server. * * @return the ID of this connection returned from the XMPP server or null if - * not connected to the server. + * not connected to the server. */ public String getConnectionID() { if (!isConnected()) { @@ -291,13 +292,12 @@ public class XMPPConnection { * @param username the username. * @param password the password. * @param resource the resource. - * @throws XMPPException if an error occurs. + * @throws XMPPException if an error occurs. * @throws IllegalStateException if not connected to the server, or already logged in - * to the serrver. + * to the serrver. */ public synchronized void login(String username, String password, String resource) - throws XMPPException - { + throws XMPPException { login(username, password, resource, true); } @@ -309,23 +309,22 @@ public class XMPPConnection { * is false, a presence packet must be sent manually later. 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. * For compatibility and easiness of use the connection will automatically connect to the * server if not already connected. * - * @param username the username. - * @param password the password. - * @param resource the resource. + * @param username the username. + * @param password the password. + * @param resource the resource. * @param sendPresence if true an available presence will be sent automatically - * after login is completed. - * @throws XMPPException if an error occurs. + * after login is completed. + * @throws XMPPException if an error occurs. * @throws IllegalStateException if not connected to the server, or already logged in - * to the serrver. + * to the serrver. */ public synchronized void login(String username, String password, String resource, - boolean sendPresence) throws XMPPException - { + boolean sendPresence) throws XMPPException { if (!isConnected()) { throw new IllegalStateException("Not connected to server."); } @@ -397,9 +396,9 @@ public class XMPPConnection { * does succeed, your XMPP address will likely be in the form "server/123ABC" (where "123ABC" * is a random value generated by the server). * - * @throws XMPPException if an error occurs or anonymous logins are not supported by the server. + * @throws XMPPException if an error occurs or anonymous logins are not supported by the server. * @throws IllegalStateException if not connected to the server, or already logged in - * to the serrver. + * to the serrver. */ public synchronized void loginAnonymously() throws XMPPException { if (!isConnected()) { @@ -508,7 +507,7 @@ public class XMPPConnection { * @return a chat manager instance for this connection. */ public synchronized ChatManager getChatManager() { - if(this.chatManager == null) { + if (this.chatManager == null) { this.chatManager = new ChatManager(this); } return this.chatManager; @@ -592,14 +591,18 @@ public class XMPPConnection { } // Close down the readers and writers. - if (reader != null) - { - try { reader.close(); } catch (Throwable ignore) { /* ignore */ } + if (reader != null) { + try { + reader.close(); + } + catch (Throwable ignore) { /* ignore */ } reader = null; } - if (writer != null) - { - try { writer.close(); } catch (Throwable ignore) { /* ignore */ } + if (writer != null) { + try { + writer.close(); + } + catch (Throwable ignore) { /* ignore */ } writer = null; } @@ -617,7 +620,7 @@ public class XMPPConnection { * Closes the connection by setting presence to unavailable then closing the stream to * the XMPP server. The XMPPConnection can still be used for connecting to the server * again.

- * + *

* 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 @@ -635,14 +638,14 @@ public class XMPPConnection { * 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. * - * @param unavailablePresence the presence packet to send during shutdown. + * @param unavailablePresence the presence packet to send during shutdown. */ public void disconnect(Presence unavailablePresence) { // If not connected, ignore this request. @@ -686,7 +689,7 @@ public class XMPPConnection { * is added again with a different filter, only the new filter will be used. * * @param packetListener the packet listener to notify of new packets. - * @param packetFilter the packet filter to use. + * @param packetFilter the packet filter to use. */ public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) { if (!isConnected()) { @@ -713,7 +716,7 @@ public class XMPPConnection { * thread for processing. * * @param packetListener the packet listener to notify of sent packets. - * @param packetFilter the packet filter to use. + * @param packetFilter the packet filter to use. */ public void addPacketWriterListener(PacketListener packetListener, PacketFilter packetFilter) { if (!isConnected()) { @@ -738,10 +741,10 @@ public class XMPPConnection { * will be delivered to the interceptor. * * @param packetInterceptor the packet interceptor to notify of packets about to be sent. - * @param packetFilter the packet filter to use. + * @param packetFilter the packet filter to use. */ public void addPacketWriterInterceptor(PacketInterceptor packetInterceptor, - PacketFilter packetFilter) { + PacketFilter packetFilter) { if (!isConnected()) { throw new IllegalStateException("Not connected to server."); } @@ -964,7 +967,7 @@ public class XMPPConnection { zoClass.getConstructor(OutputStream.class, Integer.TYPE); Object out = constructor.newInstance(socket.getOutputStream(), 9); Method method = zoClass.getMethod("setFlushMode", Integer.TYPE); - method.invoke(out, 1); + method.invoke(out, 2); writer = new BufferedWriter(new OutputStreamWriter((OutputStream) out, "UTF-8")); @@ -972,7 +975,7 @@ public class XMPPConnection { constructor = ziClass.getConstructor(InputStream.class); Object in = constructor.newInstance(socket.getInputStream()); method = ziClass.getMethod("setFlushMode", Integer.TYPE); - method.invoke(in, 1); + method.invoke(in, 2); reader = new BufferedReader(new InputStreamReader((InputStream) in, "UTF-8")); } catch (Exception e) { @@ -1093,13 +1096,12 @@ public class XMPPConnection { */ void startTLSReceived(boolean required) { if (required && configuration.getSecurityMode() == - ConnectionConfiguration.SecurityMode.disabled) - { + ConnectionConfiguration.SecurityMode.disabled) { packetReader.notifyConnectionError(new IllegalStateException( "TLS required by server but not allowed by connection configuration")); return; } - + if (configuration.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) { // Do not secure the connection using TLS since TLS was disabled return; @@ -1170,7 +1172,7 @@ public class XMPPConnection { * 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.

- * + *

* Note: to use stream compression the smackx.jar file has to be present in the classpath. * * @return true if network traffic is being compressed. @@ -1184,11 +1186,11 @@ public class XMPPConnection { * reduced up to 90%. Therefore, stream compression is ideal when using a slow speed network * connection. However, the server and the client will need to use more CPU time in order to * un/compress network data so under high load the server performance might be affected.

- * + *

* Stream compression has to have been previously offered by the server. Currently only the * zlib method is supported by the client. Stream compression negotiation has to be done * before authentication took place.

- * + *

* Note: to use stream compression the smackx.jar file has to be present in the classpath. * * @return true if stream compression negotiation was successful. @@ -1267,19 +1269,20 @@ public class XMPPConnection { this.notify(); } } + /** * 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 socket connection to the server.

- * + *

* Listeners will be preserved from a previous connection if the reconnection * occurs after an abrupt termination. * * @throws XMPPException if an error occurs while trying to establish the connection. - * Two possible errors can occur which will be wrapped by an XMPPException -- - * UnknownHostException (XMPP error code 504), and IOException (XMPP error code - * 502). The error codes and wrapped exceptions can be used to present more - * appropiate error messages to end-users. + * Two possible errors can occur which will be wrapped by an XMPPException -- + * UnknownHostException (XMPP error code 504), and IOException (XMPP error code + * 502). The error codes and wrapped exceptions can be used to present more + * appropiate error messages to end-users. */ public void connect() throws XMPPException { // Stablishes the connection, readers and writers @@ -1292,11 +1295,13 @@ public class XMPPConnection { if (isAnonymous()) { // Make the anonymous login loginAnonymously(); - } else { + } + else { login(getConfiguration().getUsername(), getConfiguration().getPassword(), getConfiguration().getResource(), getConfiguration().isSendPresence()); } - } catch (XMPPException e) { + } + catch (XMPPException e) { e.printStackTrace(); } }