2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2013-02-07 04:42:33 +01:00
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
2006-07-10 18:51:56 +02:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a mechanism to collect packets into a result queue that pass a
|
|
|
|
* specified filter. The collector lets you perform blocking and polling
|
|
|
|
* operations on the result queue. So, a PacketCollector is more suitable to
|
|
|
|
* use than a {@link PacketListener} when you need to wait for a specific
|
2003-04-15 15:52:38 +02:00
|
|
|
* result.<p>
|
|
|
|
*
|
2011-06-18 20:18:03 +02:00
|
|
|
* Each packet collector will queue up a configured number of packets for processing before
|
|
|
|
* older packets are automatically dropped. The default number is retrieved by
|
|
|
|
* {@link SmackConfiguration#getPacketCollectorSize()}.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2010-02-09 12:55:56 +01:00
|
|
|
* @see Connection#createPacketCollector(PacketFilter)
|
2003-01-13 17:58:47 +01:00
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
|
|
|
public class PacketCollector {
|
|
|
|
|
|
|
|
private PacketFilter packetFilter;
|
2013-02-07 04:42:33 +01:00
|
|
|
private ArrayBlockingQueue<Packet> resultQueue;
|
|
|
|
private Connection connection;
|
2003-01-13 17:58:47 +01:00
|
|
|
private boolean cancelled = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new packet collector. If the packet filter is <tt>null</tt>, then
|
|
|
|
* all packets will match this collector.
|
|
|
|
*
|
2010-02-09 12:55:56 +01:00
|
|
|
* @param conection the connection the collector is tied to.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @param packetFilter determines which packets will be returned by this collector.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
protected PacketCollector(Connection conection, PacketFilter packetFilter) {
|
2013-02-07 04:42:33 +01:00
|
|
|
this(conection, packetFilter, SmackConfiguration.getPacketCollectorSize());
|
2003-01-13 17:58:47 +01: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.
|
|
|
|
*
|
|
|
|
* @param conection the connection the collector is tied to.
|
|
|
|
* @param packetFilter determines which packets will be returned by this collector.
|
|
|
|
* @param maxSize the maximum number of packets that will be stored in the collector.
|
|
|
|
*/
|
|
|
|
protected PacketCollector(Connection conection, PacketFilter packetFilter, int maxSize) {
|
2013-02-07 04:42:33 +01:00
|
|
|
this.connection = conection;
|
|
|
|
this.packetFilter = packetFilter;
|
|
|
|
this.resultQueue = new ArrayBlockingQueue<Packet>(maxSize);
|
2011-06-18 20:18:03 +02:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Explicitly cancels the packet collector so that no more results are
|
|
|
|
* queued up. Once a packet collector has been cancelled, it cannot be
|
|
|
|
* re-enabled. Instead, a new packet collector must be created.
|
|
|
|
*/
|
|
|
|
public void cancel() {
|
|
|
|
// If the packet collector has already been cancelled, do nothing.
|
2005-09-05 22:00:45 +02:00
|
|
|
if (!cancelled) {
|
2003-01-13 17:58:47 +01:00
|
|
|
cancelled = true;
|
2013-02-07 04:42:33 +01:00
|
|
|
connection.removePacketCollector(this);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the packet filter associated with this packet collector. The packet
|
|
|
|
* filter is used to determine what packets are queued as results.
|
|
|
|
*
|
|
|
|
* @return the packet filter.
|
|
|
|
*/
|
|
|
|
public PacketFilter getPacketFilter() {
|
|
|
|
return packetFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-17 08:12:57 +01:00
|
|
|
* Polls to see if a packet is currently available and returns it, or
|
2003-01-13 17:58:47 +01:00
|
|
|
* 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();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-17 08:12:57 +01: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);
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-17 08:12:57 +01: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);
|
|
|
|
}
|
2003-01-13 17:58:47 +01: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) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (packet == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-07 04:42:33 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
if (packetFilter == null || packetFilter.accept(packet)) {
|
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();
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|