1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 16:22:06 +01:00

Changed flushing type of jzlib to fix hang in client.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7613 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Derek DeMoro 2007-03-20 22:09:15 +00:00 committed by derek
parent 9627fef139
commit 93766ee788

View file

@ -32,9 +32,9 @@ import javax.net.ssl.SSLSocket;
import java.io.*; import java.io.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.net.InetAddress;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
@ -52,7 +52,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* con.login("jsmith", "mypass"); * con.login("jsmith", "mypass");
* // Start a new conversation with John Doe and send him a message. * // Start a new conversation with John Doe and send him a message.
* Chat chat = connection.getChatManager().createChat("jdoe@igniterealtime.org"</font>, new MessageListener() { * Chat chat = connection.getChatManager().createChat("jdoe@igniterealtime.org"</font>, new MessageListener() {
* * <p/>
* public void processMessage(Chat chat, Message message) { * public void processMessage(Chat chat, Message message) {
* // Print out any messages we get back to standard out. * // Print out any messages we get back to standard out.
* System.out.println(<font color="green">"Received message: "</font> + message); * System.out.println(<font color="green">"Received message: "</font> + message);
@ -62,11 +62,11 @@ import java.util.concurrent.atomic.AtomicInteger;
* // Disconnect from the server * // Disconnect from the server
* con.disconnect(); * con.disconnect();
* </pre> * </pre>
* * <p/>
* 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 be retained accross connections.<p> * will be retained accross connections.<p>
* * <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, 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.
@ -79,11 +79,11 @@ public class XMPPConnection {
* Value that indicates whether debugging is enabled. When enabled, a debug * Value that indicates whether debugging is enabled. When enabled, a debug
* window will apear for each new connection that will contain the following * window will apear for each new connection that will contain the following
* information:<ul> * information:<ul>
* <li> Client Traffic -- raw XML traffic generated by Smack and sent to the server. * <li> Client Traffic -- raw XML traffic generated by Smack and sent to the server.
* <li> Server Traffic -- raw XML traffic sent by the server to the client. * <li> Server Traffic -- raw XML traffic sent by the server to the client.
* <li> Interpreted Packets -- shows XML packets from the server as parsed by Smack. * <li> Interpreted Packets -- shows XML packets from the server as parsed by Smack.
* </ul> * </ul>
* * <p/>
* Debugging can be enabled by setting this field to true, or by setting the Java system * Debugging can be enabled by setting this field to true, or by setting the Java system
* property <tt>smack.debugEnabled</tt> to true. The system property can be set on the * property <tt>smack.debugEnabled</tt> to true. The system property can be set on the
* command line such as "java SomeApp -Dsmack.debugEnabled=true". * 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. // Ensure the SmackConfiguration class is loaded by calling a method in it.
SmackConfiguration.getVersion(); SmackConfiguration.getVersion();
} }
private SmackDebugger debugger = null; private SmackDebugger debugger = null;
/** /**
@ -174,11 +175,11 @@ public class XMPPConnection {
* <tt>serviceName</tt> with the default port of 5222. Encrypted connections (TLS) * <tt>serviceName</tt> with the default port of 5222. Encrypted connections (TLS)
* will be used if available, stream compression is disabled, and standard SASL * will be used if available, stream compression is disabled, and standard SASL
* mechanisms will be used for authentication.<p> * mechanisms will be used for authentication.<p>
* * <p/>
* This is the simplest constructor for connecting to an XMPP server. Alternatively, * This is the simplest constructor for connecting to an XMPP server. Alternatively,
* you can get fine-grained control over connection settings using the * you can get fine-grained control over connection settings using the
* {@link #XMPPConnection(ConnectionConfiguration)} constructor.<p> * {@link #XMPPConnection(ConnectionConfiguration)} constructor.<p>
* * <p/>
* Note that XMPPConnection constructors do not establish a connection to the server * Note that XMPPConnection constructors do not establish a connection to the server
* and you must call {@link #connect()}. * and you must call {@link #connect()}.
* *
@ -195,11 +196,11 @@ public class XMPPConnection {
/** /**
* Creates a new XMPP connection using the specified connection configuration.<p> * Creates a new XMPP connection using the specified connection configuration.<p>
* * <p/>
* Manually specifying connection configuration information is suitable for * Manually specifying connection configuration information is suitable for
* advanced users of the API. In many cases, using the * advanced users of the API. In many cases, using the
* {@link #XMPPConnection(String)} constructor is a better approach.<p> * {@link #XMPPConnection(String)} constructor is a better approach.<p>
* * <p/>
* Note that XMPPConnection constructors do not establish a connection to the server * Note that XMPPConnection constructors do not establish a connection to the server
* and you must call {@link #connect()}. * and you must call {@link #connect()}.
* *
@ -215,7 +216,7 @@ public class XMPPConnection {
* will be null. This value will be <tt>null</tt> if not connected to the server. * will be null. This value will be <tt>null</tt> if not connected to the server.
* *
* @return the ID of this connection returned from the XMPP server or <tt>null</tt> if * @return the ID of this connection returned from the XMPP server or <tt>null</tt> if
* not connected to the server. * not connected to the server.
*/ */
public String getConnectionID() { public String getConnectionID() {
if (!isConnected()) { if (!isConnected()) {
@ -291,13 +292,12 @@ public class XMPPConnection {
* @param username the username. * @param username the username.
* @param password the password. * @param password the password.
* @param resource the resource. * @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 * @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) public synchronized void login(String username, String password, String resource)
throws XMPPException throws XMPPException {
{
login(username, password, resource, true); 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 * 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 * (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.<p> * response from the server, or if an error occurs, a XMPPException will be thrown.<p>
* * <p/>
* Before logging in (i.e. authenticate) to the server the connection must be connected. * 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 * For compatibility and easiness of use the connection will automatically connect to the
* server if not already connected. * server if not already connected.
* *
* @param username the username. * @param username the username.
* @param password the password. * @param password the password.
* @param resource the resource. * @param resource the resource.
* @param sendPresence if <tt>true</tt> an available presence will be sent automatically * @param sendPresence if <tt>true</tt> an available presence will be sent automatically
* after login is completed. * after login is completed.
* @throws XMPPException if an error occurs. * @throws XMPPException if an error occurs.
* @throws IllegalStateException if not connected to the server, or already logged in * @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, public synchronized void login(String username, String password, String resource,
boolean sendPresence) throws XMPPException boolean sendPresence) throws XMPPException {
{
if (!isConnected()) { if (!isConnected()) {
throw new IllegalStateException("Not connected to server."); 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" * does succeed, your XMPP address will likely be in the form "server/123ABC" (where "123ABC"
* is a random value generated by the server). * 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 * @throws IllegalStateException if not connected to the server, or already logged in
* to the serrver. * to the serrver.
*/ */
public synchronized void loginAnonymously() throws XMPPException { public synchronized void loginAnonymously() throws XMPPException {
if (!isConnected()) { if (!isConnected()) {
@ -508,7 +507,7 @@ public class XMPPConnection {
* @return a chat manager instance for this connection. * @return a chat manager instance for this connection.
*/ */
public synchronized ChatManager getChatManager() { public synchronized ChatManager getChatManager() {
if(this.chatManager == null) { if (this.chatManager == null) {
this.chatManager = new ChatManager(this); this.chatManager = new ChatManager(this);
} }
return this.chatManager; return this.chatManager;
@ -592,14 +591,18 @@ public class XMPPConnection {
} }
// Close down the readers and writers. // Close down the readers and writers.
if (reader != null) if (reader != null) {
{ try {
try { reader.close(); } catch (Throwable ignore) { /* ignore */ } reader.close();
}
catch (Throwable ignore) { /* ignore */ }
reader = null; reader = null;
} }
if (writer != null) if (writer != null) {
{ try {
try { writer.close(); } catch (Throwable ignore) { /* ignore */ } writer.close();
}
catch (Throwable ignore) { /* ignore */ }
writer = null; writer = null;
} }
@ -617,7 +620,7 @@ public class XMPPConnection {
* Closes the connection by setting presence to unavailable then closing the stream to * 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 * the XMPP server. The XMPPConnection can still be used for connecting to the server
* again.<p> * again.<p>
* * <p/>
* This method cleans up all resources used by the connection. Therefore, the roster, * 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() * 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 * 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 * 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 * packet is set with online information, but most XMPP servers will deliver the full
* presence packet with whatever data is set.<p> * presence packet with whatever data is set.<p>
* * <p/>
* This method cleans up all resources used by the connection. Therefore, the roster, * 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() * 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 * 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 * (and subsequent connections). In that case, all state is preserved to allow for
* more seamless error recovery. * 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) { public void disconnect(Presence unavailablePresence) {
// If not connected, ignore this request. // 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. * 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 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) { public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
if (!isConnected()) { if (!isConnected()) {
@ -713,7 +716,7 @@ public class XMPPConnection {
* thread for processing. * thread for processing.
* *
* @param packetListener the packet listener to notify of sent packets. * @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) { public void addPacketWriterListener(PacketListener packetListener, PacketFilter packetFilter) {
if (!isConnected()) { if (!isConnected()) {
@ -738,10 +741,10 @@ public class XMPPConnection {
* will be delivered to the interceptor. * will be delivered to the interceptor.
* *
* @param packetInterceptor the packet interceptor to notify of packets about to be sent. * @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, public void addPacketWriterInterceptor(PacketInterceptor packetInterceptor,
PacketFilter packetFilter) { PacketFilter packetFilter) {
if (!isConnected()) { if (!isConnected()) {
throw new IllegalStateException("Not connected to server."); throw new IllegalStateException("Not connected to server.");
} }
@ -964,7 +967,7 @@ public class XMPPConnection {
zoClass.getConstructor(OutputStream.class, Integer.TYPE); zoClass.getConstructor(OutputStream.class, Integer.TYPE);
Object out = constructor.newInstance(socket.getOutputStream(), 9); Object out = constructor.newInstance(socket.getOutputStream(), 9);
Method method = zoClass.getMethod("setFlushMode", Integer.TYPE); Method method = zoClass.getMethod("setFlushMode", Integer.TYPE);
method.invoke(out, 1); method.invoke(out, 2);
writer = writer =
new BufferedWriter(new OutputStreamWriter((OutputStream) out, "UTF-8")); new BufferedWriter(new OutputStreamWriter((OutputStream) out, "UTF-8"));
@ -972,7 +975,7 @@ public class XMPPConnection {
constructor = ziClass.getConstructor(InputStream.class); constructor = ziClass.getConstructor(InputStream.class);
Object in = constructor.newInstance(socket.getInputStream()); Object in = constructor.newInstance(socket.getInputStream());
method = ziClass.getMethod("setFlushMode", Integer.TYPE); method = ziClass.getMethod("setFlushMode", Integer.TYPE);
method.invoke(in, 1); method.invoke(in, 2);
reader = new BufferedReader(new InputStreamReader((InputStream) in, "UTF-8")); reader = new BufferedReader(new InputStreamReader((InputStream) in, "UTF-8"));
} }
catch (Exception e) { catch (Exception e) {
@ -1093,13 +1096,12 @@ public class XMPPConnection {
*/ */
void startTLSReceived(boolean required) { void startTLSReceived(boolean required) {
if (required && configuration.getSecurityMode() == if (required && configuration.getSecurityMode() ==
ConnectionConfiguration.SecurityMode.disabled) ConnectionConfiguration.SecurityMode.disabled) {
{
packetReader.notifyConnectionError(new IllegalStateException( packetReader.notifyConnectionError(new IllegalStateException(
"TLS required by server but not allowed by connection configuration")); "TLS required by server but not allowed by connection configuration"));
return; return;
} }
if (configuration.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) { if (configuration.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
// Do not secure the connection using TLS since TLS was disabled // Do not secure the connection using TLS since TLS was disabled
return; return;
@ -1170,7 +1172,7 @@ public class XMPPConnection {
* traffic can be reduced up to 90%. Therefore, stream compression is ideal when using a slow * 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 * 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.<p> * un/compress network data so under high load the server performance might be affected.<p>
* * <p/>
* Note: to use stream compression the smackx.jar file has to be present in the classpath. * Note: to use stream compression the smackx.jar file has to be present in the classpath.
* *
* @return true if network traffic is being compressed. * @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 * 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 * 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.<p> * un/compress network data so under high load the server performance might be affected.<p>
* * <p/>
* Stream compression has to have been previously offered by the server. Currently only the * 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 * zlib method is supported by the client. Stream compression negotiation has to be done
* before authentication took place.<p> * before authentication took place.<p>
* * <p/>
* Note: to use stream compression the smackx.jar file has to be present in the classpath. * Note: to use stream compression the smackx.jar file has to be present in the classpath.
* *
* @return true if stream compression negotiation was successful. * @return true if stream compression negotiation was successful.
@ -1267,19 +1269,20 @@ public class XMPPConnection {
this.notify(); this.notify();
} }
} }
/** /**
* Establishes a connection to the XMPP server and performs an automatic login * Establishes a connection to the XMPP server and performs an automatic login
* only if the previous connection state was logged (authenticated). It basically * only if the previous connection state was logged (authenticated). It basically
* creates and maintains a socket connection to the server.<p> * creates and maintains a socket connection to the server.<p>
* * <p/>
* Listeners will be preserved from a previous connection if the reconnection * Listeners will be preserved from a previous connection if the reconnection
* occurs after an abrupt termination. * occurs after an abrupt termination.
* *
* @throws XMPPException if an error occurs while trying to establish the connection. * @throws XMPPException if an error occurs while trying to establish the connection.
* Two possible errors can occur which will be wrapped by an XMPPException -- * Two possible errors can occur which will be wrapped by an XMPPException --
* UnknownHostException (XMPP error code 504), and IOException (XMPP error code * UnknownHostException (XMPP error code 504), and IOException (XMPP error code
* 502). The error codes and wrapped exceptions can be used to present more * 502). The error codes and wrapped exceptions can be used to present more
* appropiate error messages to end-users. * appropiate error messages to end-users.
*/ */
public void connect() throws XMPPException { public void connect() throws XMPPException {
// Stablishes the connection, readers and writers // Stablishes the connection, readers and writers
@ -1292,11 +1295,13 @@ public class XMPPConnection {
if (isAnonymous()) { if (isAnonymous()) {
// Make the anonymous login // Make the anonymous login
loginAnonymously(); loginAnonymously();
} else { }
else {
login(getConfiguration().getUsername(), getConfiguration().getPassword(), login(getConfiguration().getUsername(), getConfiguration().getPassword(),
getConfiguration().getResource(), getConfiguration().isSendPresence()); getConfiguration().getResource(), getConfiguration().isSendPresence());
} }
} catch (XMPPException e) { }
catch (XMPPException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }