From d843cc9a77f306eab39b361df0f653f4acee93bd Mon Sep 17 00:00:00 2001 From: Matt Tucker Date: Thu, 18 Sep 2003 22:35:42 +0000 Subject: [PATCH] Packet writer errors will now notify listeners of errors (SMACK-85) git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2092 b35dd754-fafc-0310-a699-88a17e54d16e --- .../org/jivesoftware/smack/PacketReader.java | 36 ++++++++++++------- .../org/jivesoftware/smack/PacketWriter.java | 6 ++-- .../jivesoftware/smack/XMPPConnection.java | 4 +-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/source/org/jivesoftware/smack/PacketReader.java b/source/org/jivesoftware/smack/PacketReader.java index 0dec348cd..73b9a10a5 100644 --- a/source/org/jivesoftware/smack/PacketReader.java +++ b/source/org/jivesoftware/smack/PacketReader.java @@ -212,6 +212,24 @@ class PacketReader { done = true; } + /** + * Sends out a notification that there was an error with the connection + * and closes the connection. + * + * @param e the exception that causes the connection close event. + */ + void notifyConnectionError(Exception e) { + done = true; + connection.close(); + // Notify connection listeners of the error. + synchronized (connectionListeners) { + for (Iterator i=connectionListeners.iterator(); i.hasNext(); ) { + ConnectionListener listener = (ConnectionListener)i.next(); + listener.connectionClosedOnError(e); + } + } + } + /** * Process listeners. */ @@ -238,7 +256,8 @@ class PacketReader { if (!processedPacket) { try { Thread.sleep(100); - } catch (InterruptedException ie) { } + } + catch (InterruptedException ie) { } } } } @@ -289,18 +308,9 @@ class PacketReader { } catch (Exception e) { if (!done) { - // Set done to true since we want to do our own notification of connection closing. - done = true; - connection.close(); - // An exception occurred while parsing. Notify connection listeners of - // the error and close the connection. - synchronized (connectionListeners) { - for (Iterator i=connectionListeners.iterator(); i.hasNext(); ) { - ConnectionListener listener = (ConnectionListener)i.next(); - listener.connectionClosedOnError(e); - } - } - + // Close the connection and notify connection listeners of the + // error. + notifyConnectionError(e); } } } diff --git a/source/org/jivesoftware/smack/PacketWriter.java b/source/org/jivesoftware/smack/PacketWriter.java index d524373c6..75a132fd7 100644 --- a/source/org/jivesoftware/smack/PacketWriter.java +++ b/source/org/jivesoftware/smack/PacketWriter.java @@ -53,6 +53,7 @@ package org.jivesoftware.smack; import java.util.LinkedList; +import java.util.Iterator; import java.io.*; import org.jivesoftware.smack.packet.Packet; @@ -145,7 +146,6 @@ class PacketWriter { stream.append(" to=\"" + connection.getHost() + "\""); stream.append(" xmlns=\"jabber:client\""); stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\">"); -// stream.append("xmlns:sasl=\"http://www.iana.org/assignments/sasl-mechanisms\" "); writer.write(stream.toString()); writer.flush(); stream = null; @@ -170,8 +170,8 @@ class PacketWriter { } catch (IOException ioe){ if (!done) { - ioe.printStackTrace(); - connection.close(); + done = true; + connection.packetReader.notifyConnectionError(ioe); } } } diff --git a/source/org/jivesoftware/smack/XMPPConnection.java b/source/org/jivesoftware/smack/XMPPConnection.java index 3dacd50b9..eafcceb6e 100644 --- a/source/org/jivesoftware/smack/XMPPConnection.java +++ b/source/org/jivesoftware/smack/XMPPConnection.java @@ -117,8 +117,8 @@ public class XMPPConnection { private boolean authenticated = false; private boolean anonymous = false; - private PacketWriter packetWriter; - private PacketReader packetReader; + PacketWriter packetWriter; + PacketReader packetReader; Roster roster = null; private AccountManager accountManager = null;