Replaces lock object with Semaphore. SMACK-168

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@5359 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2006-09-13 17:58:28 +00:00 committed by gato
parent 0c71fe2624
commit e558ee8fa6
1 changed files with 16 additions and 26 deletions

View File

@ -31,6 +31,8 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/** /**
* Listens for XML traffic from the XMPP server and parses it into packet objects. * Listens for XML traffic from the XMPP server and parses it into packet objects.
@ -55,7 +57,7 @@ class PacketReader {
new ArrayList<ConnectionListener>(); new ArrayList<ConnectionListener>();
private String connectionID = null; private String connectionID = null;
private final Object connectionIDLock = new Object(); private Semaphore connectionSemaphore;
protected PacketReader(XMPPConnection connection) { protected PacketReader(XMPPConnection connection) {
this.connection = connection; this.connection = connection;
@ -135,31 +137,21 @@ class PacketReader {
* for more than five seconds. * for more than five seconds.
*/ */
public void startup() throws XMPPException { public void startup() throws XMPPException {
connectionSemaphore = new Semaphore(1);
readerThread.start(); readerThread.start();
listenerThread.start(); listenerThread.start();
// Wait for stream tag before returing. We'll wait a couple of seconds before // Wait for stream tag before returing. We'll wait a couple of seconds before
// giving up and throwing an error. // giving up and throwing an error.
try { try {
synchronized(connectionIDLock) { connectionSemaphore.acquire();
if (connectionID == null) {
// A waiting thread may be woken up before the wait time or a notify // A waiting thread may be woken up before the wait time or a notify
// (although this is a rare thing). Therefore, we continue waiting // (although this is a rare thing). Therefore, we continue waiting
// until either a connectionID has been set (and hence a notify was // until either a connectionID has been set (and hence a notify was
// made) or the total wait time has elapsed. // made) or the total wait time has elapsed.
long waitTime = SmackConfiguration.getPacketReplyTimeout(); int waitTime = SmackConfiguration.getPacketReplyTimeout();
long start = System.currentTimeMillis(); connectionSemaphore.tryAcquire(3 * waitTime, TimeUnit.MILLISECONDS);
while (connectionID == null && !done) {
if (waitTime <= 0) {
break;
}
// 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;
}
}
}
} }
catch (InterruptedException ie) { catch (InterruptedException ie) {
// Ignore. // Ignore.
@ -215,7 +207,7 @@ class PacketReader {
listener.connectionClosedOnError(e); listener.connectionClosedOnError(e);
} }
} }
// Make sure that the listenerThread is awake to shutdown properly // Make sure that the listenerThread is awake to shutdown properly
synchronized (listenerThread) { synchronized (listenerThread) {
listenerThread.notify(); listenerThread.notify();
@ -309,7 +301,7 @@ class PacketReader {
throw new XMPPException(parseStreamError(parser)); throw new XMPPException(parseStreamError(parser));
} }
else if (parser.getName().equals("features")) { else if (parser.getName().equals("features")) {
parseFeatures(parser); parseFeatures(parser);
} }
else if (parser.getName().equals("proceed")) { else if (parser.getName().equals("proceed")) {
// Secure the connection by negotiating TLS // Secure the connection by negotiating TLS
@ -390,9 +382,7 @@ class PacketReader {
* *
*/ */
private void releaseConnectionIDLock() { private void releaseConnectionIDLock() {
synchronized(connectionIDLock) { connectionSemaphore.release();
connectionIDLock.notifyAll();
}
} }
/** /**