fixed some logic bugs with the body parsing, added some comments
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 9 May 2007 18:13:38 +0000 (18:13 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 9 May 2007 18:13:38 +0000 (18:13 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@878 9efc2488-bf62-4759-914b-345cdb29e865

src/java/org/opensrf/net/xmpp/XMPPReader.java
src/java/org/opensrf/test/TestXMPP.java

index 39ea0c2..9f5bdb4 100644 (file)
@@ -11,7 +11,7 @@ import java.util.Date;
 
 /**
  * Slim XMPP Stream reader.  This reader only understands enough XMPP
- * to handle logins and recv messages.
+ * to handle logins and recv messages.  It's implemented as a StAX parser.
  * @author Bill Erickson, Georgia Public Library Systems
  */
 public class XMPPReader implements Runnable {
@@ -68,7 +68,7 @@ public class XMPPReader implements Runnable {
      * @param inStream the inbound XML stream
      */
     public XMPPReader(InputStream inStream) {
-        msgQueue = new ConcurrentLinkedQueue();
+        msgQueue = new ConcurrentLinkedQueue<XMPPMessage>();
         this.inStream = inStream;
         resetBuffers();
         xmlState = XMLState.IN_NOTHING;
@@ -144,7 +144,8 @@ public class XMPPReader implements Runnable {
     }
 
 
-    /** Thread kickoff point */
+
+    /** Kickoff the thread */
     public void run() {
         read();
     }
@@ -157,9 +158,10 @@ public class XMPPReader implements Runnable {
     public void read() {
 
         try {
+
             XMLInputFactory factory = XMLInputFactory.newInstance();
 
-            /** disable as many features as possible to speed up the parsing */
+            /** disable as many unused features as possible to speed up the parsing */
             factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
             factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
             factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
@@ -227,19 +229,9 @@ public class XMPPReader implements Runnable {
 
         String name = reader.getName().toString();
 
-        if("stream:stream".equals(name)) {
-            setXMPPStreamState(XMPPStreamState.CONNECT_RECV);
-            return;
-        }
-
-        if("iq".equals(name)) {
-            if("result".equals(reader.getAttributeValue(null, "type")))
-                setXMPPStreamState(XMPPStreamState.CONNECTED);
-            return;
-        }
-
         if("message".equals(name)) {
             xmlState = XMLState.IN_BODY;
+
             /** add a special case for the opensrf "router_from" attribute */
             String rf = reader.getAttributeValue(null, "router_from");
             if( rf != null )
@@ -250,11 +242,27 @@ public class XMPPReader implements Runnable {
             return;
         }
 
+        if("body".equals(name)) {
+            xmlState = XMLState.IN_BODY;
+            return;
+        }
+
         if("thread".equals(name)) {
             xmlState = XMLState.IN_THREAD;
             return;
         }
 
+        if("stream:stream".equals(name)) {
+            setXMPPStreamState(XMPPStreamState.CONNECT_RECV);
+            return;
+        }
+
+        if("iq".equals(name)) {
+            if("result".equals(reader.getAttributeValue(null, "type")))
+                setXMPPStreamState(XMPPStreamState.CONNECTED);
+            return;
+        }
+
         if("status".equals(name)) {
             xmlState = XMLState.IN_STATUS;
             return;
index 8a34a2e..2fba67f 100644 (file)
@@ -6,6 +6,10 @@ import org.opensrf.net.xmpp.XMPPSession;
 
 public class TestXMPP {
 
+    /**
+     * Connects to the jabber server and waits for inbound messages.
+     * If a recipient is provided, a small message is sent to the recipient.
+     */
     public static void main(String args[]) throws Exception {
 
         String host;
@@ -23,7 +27,7 @@ public class TestXMPP {
             resource = args[4];
 
         } catch(ArrayIndexOutOfBoundsException e) {
-            System.err.println("usage: org.opensrf.test.TestXMPP <host> <port> <username> <password> <resource>");
+            System.err.println("usage: org.opensrf.test.TestXMPP <host> <port> <username> <password> <resource> [<recipient>]");
             return;
         }
 
@@ -33,7 +37,9 @@ public class TestXMPP {
         XMPPMessage msg;
 
         if( args.length == 6 ) {
+
             /** they specified a recipient */
+
             recipient = args[5];
             msg = new XMPPMessage();
             msg.setTo(recipient);
@@ -45,7 +51,7 @@ public class TestXMPP {
 
         while(true) {
             System.out.println("waiting for message...");
-            msg = session.recv(-1);
+            msg = session.recv(-1); /* wait forever for a message to arrive */
             System.out.println("got message: " + msg.toXML());
         }
     }