synchronizing the initial xmpp login to prevent thread race condition on authentication
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Sat, 28 Jul 2007 17:41:32 +0000 (17:41 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Sat, 28 Jul 2007 17:41:32 +0000 (17:41 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1064 9efc2488-bf62-4759-914b-345cdb29e865

src/java/org/opensrf/net/xmpp/XMPPSession.java

index e8b639e..11e0c7d 100644 (file)
@@ -113,29 +113,33 @@ public class XMPPSession {
         thread.setDaemon(true);
         thread.start();
 
-        /* send the initial jabber message */
-        sendConnect();
-        reader.waitCoreEvent(10000);
+        synchronized(reader) {
+            /* send the initial jabber message */
+            sendConnect();
+            reader.waitCoreEvent(10000);
+        }
         if( reader.getXMPPStreamState() != XMPPReader.XMPPStreamState.CONNECT_RECV ) 
             throw new XMPPException("unable to connect to jabber server");
 
-        /* send the basic auth message */
-        sendBasicAuth(); /* XXX add support for other auth mechanisms */
-        reader.waitCoreEvent(10000);
-        if(!connected())
+        synchronized(reader) {
+            /* send the basic auth message */
+            sendBasicAuth(); 
+            reader.waitCoreEvent(10000);
+        }
+        if(!connected()) 
             throw new XMPPException("Authentication failed");
     }
 
     /** Sends the initial jabber message */
     private void sendConnect() {
-        writer.printf(JABBER_CONNECT, host);
         reader.setXMPPStreamState(XMPPReader.XMPPStreamState.CONNECT_SENT);
+        writer.printf(JABBER_CONNECT, host);
     }
 
     /** Send the basic auth message */
     private void sendBasicAuth() {
-        writer.printf(JABBER_BASIC_AUTH, username, password, resource);
         reader.setXMPPStreamState(XMPPReader.XMPPStreamState.AUTH_SENT);
+        writer.printf(JABBER_BASIC_AUTH, username, password, resource);
     }