mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
SMACK-341 Updated collectors to use concurrent classes.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13452 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
e0e92eca76
commit
d1e9d81769
4 changed files with 437 additions and 128 deletions
|
@ -20,11 +20,12 @@
|
|||
|
||||
package org.jivesoftware.smackx.muc;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.jivesoftware.smack.SmackConfiguration;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* A variant of the {@link org.jivesoftware.smack.PacketCollector} class
|
||||
* that does not force attachment to a <code>Connection</code>
|
||||
|
@ -41,14 +42,14 @@ class ConnectionDetachedPacketCollector {
|
|||
*/
|
||||
private int maxPackets = SmackConfiguration.getPacketCollectorSize();
|
||||
|
||||
private LinkedList<Packet> resultQueue;
|
||||
private ArrayBlockingQueue<Packet> resultQueue;
|
||||
|
||||
/**
|
||||
* Creates a new packet collector. If the packet filter is <tt>null</tt>, then
|
||||
* all packets will match this collector.
|
||||
*/
|
||||
public ConnectionDetachedPacketCollector() {
|
||||
this.resultQueue = new LinkedList<Packet>();
|
||||
this(SmackConfiguration.getPacketCollectorSize());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -56,8 +57,7 @@ class ConnectionDetachedPacketCollector {
|
|||
* all packets will match this collector.
|
||||
*/
|
||||
public ConnectionDetachedPacketCollector(int maxSize) {
|
||||
this.resultQueue = new LinkedList<Packet>();
|
||||
maxPackets = maxSize;
|
||||
this.resultQueue = new ArrayBlockingQueue<Packet>(maxSize);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,13 +68,8 @@ class ConnectionDetachedPacketCollector {
|
|||
* @return the next packet result, or <tt>null</tt> if there are no more
|
||||
* results.
|
||||
*/
|
||||
public synchronized Packet pollResult() {
|
||||
if (resultQueue.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return resultQueue.removeLast();
|
||||
}
|
||||
public Packet pollResult() {
|
||||
return resultQueue.poll();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,17 +78,13 @@ class ConnectionDetachedPacketCollector {
|
|||
*
|
||||
* @return the next available packet.
|
||||
*/
|
||||
public synchronized Packet nextResult() {
|
||||
// Wait indefinitely until there is a result to return.
|
||||
while (resultQueue.isEmpty()) {
|
||||
try {
|
||||
wait();
|
||||
}
|
||||
catch (InterruptedException ie) {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
return resultQueue.removeLast();
|
||||
public Packet nextResult() {
|
||||
try {
|
||||
return resultQueue.take();
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -104,23 +95,13 @@ class ConnectionDetachedPacketCollector {
|
|||
* @param timeout the amount of time to wait for the next packet (in milleseconds).
|
||||
* @return the next available packet.
|
||||
*/
|
||||
public synchronized Packet nextResult(long timeout) {
|
||||
// Wait up to the specified amount of time for a result.
|
||||
if (resultQueue.isEmpty()) {
|
||||
try {
|
||||
wait(timeout);
|
||||
}
|
||||
catch (InterruptedException ie) {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
// If still no result, return null.
|
||||
if (resultQueue.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return resultQueue.removeLast();
|
||||
}
|
||||
public Packet nextResult(long timeout) {
|
||||
try {
|
||||
return resultQueue.poll(timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -129,17 +110,14 @@ class ConnectionDetachedPacketCollector {
|
|||
*
|
||||
* @param packet the packet to process.
|
||||
*/
|
||||
protected synchronized void processPacket(Packet packet) {
|
||||
protected void processPacket(Packet packet) {
|
||||
if (packet == null) {
|
||||
return;
|
||||
}
|
||||
// If the max number of packets has been reached, remove the oldest one.
|
||||
if (resultQueue.size() == maxPackets) {
|
||||
resultQueue.removeLast();
|
||||
}
|
||||
// Add the new packet.
|
||||
resultQueue.addFirst(packet);
|
||||
// Notify waiting threads a result is available.
|
||||
notifyAll();
|
||||
|
||||
while (!resultQueue.offer(packet)) {
|
||||
// Since we know the queue is full, this poll should never actually block.
|
||||
resultQueue.poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue