From ad6be5887c28410438f0b4606a816cdbf9cc9b1f Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Sat, 27 Aug 2005 02:29:04 +0000 Subject: [PATCH] 1. Added support for TLS. SMACK-76 2. Added support for SASL. SMACK-24 git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2732 b35dd754-fafc-0310-a699-88a17e54d16e --- .../org/jivesoftware/smack/PacketReader.java | 134 +++++++++++++++--- .../org/jivesoftware/smack/PacketWriter.java | 33 +++-- 2 files changed, 142 insertions(+), 25 deletions(-) diff --git a/source/org/jivesoftware/smack/PacketReader.java b/source/org/jivesoftware/smack/PacketReader.java index dfd2cf598..157602c47 100644 --- a/source/org/jivesoftware/smack/PacketReader.java +++ b/source/org/jivesoftware/smack/PacketReader.java @@ -20,17 +20,16 @@ package org.jivesoftware.smack; -import org.xmlpull.v1.*; +import org.jivesoftware.smack.filter.PacketFilter; +import org.jivesoftware.smack.packet.*; +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smack.provider.ProviderManager; +import org.jivesoftware.smack.util.PacketParserUtils; import org.xmlpull.mxp1.MXParser; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; import java.util.*; -import java.util.List; - -import org.jivesoftware.smack.packet.*; -import org.jivesoftware.smack.packet.XMPPError; -import org.jivesoftware.smack.filter.PacketFilter; -import org.jivesoftware.smack.util.*; -import org.jivesoftware.smack.provider.*; /** * Listens for XML traffic from the XMPP server and parses it into packet objects. @@ -97,8 +96,7 @@ class PacketReader { * @return a new packet collector. */ public PacketCollector createPacketCollector(PacketFilter packetFilter) { - PacketCollector packetCollector = new PacketCollector(this, packetFilter); - return packetCollector; + return new PacketCollector(this, packetFilter); } /** @@ -158,7 +156,8 @@ class PacketReader { if (waitTime <= 0) { break; } - connectionIDLock.wait(waitTime); + // Wait 3 times the standard time since TLS may take a while + connectionIDLock.wait(waitTime * 3); long now = System.currentTimeMillis(); waitTime -= now - start; start = now; @@ -217,6 +216,15 @@ class PacketReader { } } + /** + * Resets the parser using the latest connection's reader. Reseting the parser is necessary + * when the plain connection has been secured or when a new opening stream element is going + * to be sent by the server. + */ + private void resetParser() throws XmlPullParserException { + parser.setInput(connection.reader); + } + /** * Process listeners. */ @@ -274,19 +282,51 @@ class PacketReader { // Get the connection id. for (int i=0; i"); - writer.write(stream.toString()); - writer.flush(); - stream = null; + openStream(); // Write out packets from the queue. while (!done) { Packet packet = nextPacket(); @@ -273,6 +270,24 @@ class PacketWriter { } } + /** + * Sends to the server a new stream element. This operation may be requested several times + * so we need to encapsulate the logic in one place. This message will be sent while doing + * TLS, SASL and resource binding. + * + * @throws IOException If an error occurs while sending the stanza to the server. + */ + void openStream() throws IOException { + StringBuffer stream = new StringBuffer(); + stream.append(""); + writer.write(stream.toString()); + writer.flush(); + } + /** * A wrapper class to associate a packet filter with a listener. */ @@ -282,7 +297,7 @@ class PacketWriter { private PacketFilter packetFilter; public ListenerWrapper(PacketListener packetListener, - PacketFilter packetFilter) + PacketFilter packetFilter) { this.packetListener = packetListener; this.packetFilter = packetFilter;