2006-07-18 07:04:47 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision: 2779 $
|
|
|
|
* $Date: 2005-09-05 17:00:45 -0300 (Mon, 05 Sep 2005) $
|
|
|
|
*
|
|
|
|
* Copyright 2003-2006 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smackx.muc;
|
|
|
|
|
2013-02-07 04:42:33 +01:00
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2011-06-18 20:18:03 +02:00
|
|
|
import org.jivesoftware.smack.SmackConfiguration;
|
2006-07-18 07:04:47 +02:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A variant of the {@link org.jivesoftware.smack.PacketCollector} class
|
2010-02-09 12:55:56 +01:00
|
|
|
* that does not force attachment to a <code>Connection</code>
|
2006-07-18 07:04:47 +02:00
|
|
|
* on creation and no filter is required. Used to collect message
|
|
|
|
* packets targeted to a group chat room.
|
|
|
|
*
|
|
|
|
* @author Larry Kirschner
|
|
|
|
*/
|
|
|
|
class ConnectionDetachedPacketCollector {
|
|
|
|
/**
|
|
|
|
* Max number of packets that any one collector can hold. After the max is
|
|
|
|
* reached, older packets will be automatically dropped from the queue as
|
|
|
|
* new packets are added.
|
|
|
|
*/
|
2011-06-18 20:18:03 +02:00
|
|
|
private int maxPackets = SmackConfiguration.getPacketCollectorSize();
|
2006-07-18 07:04:47 +02:00
|
|
|
|
2013-02-07 04:42:33 +01:00
|
|
|
private ArrayBlockingQueue<Packet> resultQueue;
|
2006-07-18 07:04:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new packet collector. If the packet filter is <tt>null</tt>, then
|
|
|
|
* all packets will match this collector.
|
|
|
|
*/
|
|
|
|
public ConnectionDetachedPacketCollector() {
|
2013-02-07 04:42:33 +01:00
|
|
|
this(SmackConfiguration.getPacketCollectorSize());
|
2006-07-18 07:04:47 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 20:18:03 +02:00
|
|
|
/**
|
|
|
|
* Creates a new packet collector. If the packet filter is <tt>null</tt>, then
|
|
|
|
* all packets will match this collector.
|
|
|
|
*/
|
|
|
|
public ConnectionDetachedPacketCollector(int maxSize) {
|
2013-02-07 04:42:33 +01:00
|
|
|
this.resultQueue = new ArrayBlockingQueue<Packet>(maxSize);
|
2011-06-18 20:18:03 +02:00
|
|
|
}
|
|
|
|
|
2006-07-18 07:04:47 +02:00
|
|
|
/**
|
|
|
|
* Polls to see if a packet is currently available and returns it, or
|
|
|
|
* immediately returns <tt>null</tt> if no packets are currently in the
|
|
|
|
* result queue.
|
|
|
|
*
|
|
|
|
* @return the next packet result, or <tt>null</tt> if there are no more
|
|
|
|
* results.
|
|
|
|
*/
|
2013-02-07 04:42:33 +01:00
|
|
|
public Packet pollResult() {
|
|
|
|
return resultQueue.poll();
|
2006-07-18 07:04:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next available packet. The method call will block (not return)
|
|
|
|
* until a packet is available.
|
|
|
|
*
|
|
|
|
* @return the next available packet.
|
|
|
|
*/
|
2013-02-07 04:42:33 +01:00
|
|
|
public Packet nextResult() {
|
|
|
|
try {
|
|
|
|
return resultQueue.take();
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2006-07-18 07:04:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next available packet. The method call will block (not return)
|
|
|
|
* until a packet is available or the <tt>timeout</tt> has elapased. If the
|
|
|
|
* timeout elapses without a result, <tt>null</tt> will be returned.
|
|
|
|
*
|
|
|
|
* @param timeout the amount of time to wait for the next packet (in milleseconds).
|
|
|
|
* @return the next available packet.
|
|
|
|
*/
|
2013-02-07 04:42:33 +01:00
|
|
|
public Packet nextResult(long timeout) {
|
|
|
|
try {
|
|
|
|
return resultQueue.poll(timeout, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2006-07-18 07:04:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a packet to see if it meets the criteria for this packet collector.
|
|
|
|
* If so, the packet is added to the result queue.
|
|
|
|
*
|
|
|
|
* @param packet the packet to process.
|
|
|
|
*/
|
2013-02-07 04:42:33 +01:00
|
|
|
protected void processPacket(Packet packet) {
|
2006-07-18 07:04:47 +02:00
|
|
|
if (packet == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-07 04:42:33 +01:00
|
|
|
|
|
|
|
while (!resultQueue.offer(packet)) {
|
|
|
|
// Since we know the queue is full, this poll should never actually block.
|
|
|
|
resultQueue.poll();
|
|
|
|
}
|
2006-07-18 07:04:47 +02:00
|
|
|
}
|
|
|
|
}
|