1 /** 2 * $RCSfile$ 3 * $Revision: 2779 $ 4 * $Date: 2005-09-05 17:00:45 -0300 (Mon, 05 Sep 2005) $ 5 * 6 * Copyright 2003-2006 Jive Software. 7 * 8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package org.jivesoftware.smackx.muc; 22 23 import java.util.concurrent.ArrayBlockingQueue; 24 import java.util.concurrent.TimeUnit; 25 26 import org.jivesoftware.smack.SmackConfiguration; 27 import org.jivesoftware.smack.packet.Packet; 28 29 /** 30 * A variant of the {@link org.jivesoftware.smack.PacketCollector} class 31 * that does not force attachment to a <code>Connection</code> 32 * on creation and no filter is required. Used to collect message 33 * packets targeted to a group chat room. 34 * 35 * @author Larry Kirschner 36 */ 37 class ConnectionDetachedPacketCollector { 38 /** 39 * Max number of packets that any one collector can hold. After the max is 40 * reached, older packets will be automatically dropped from the queue as 41 * new packets are added. 42 */ 43 private int maxPackets = SmackConfiguration.getPacketCollectorSize(); 44 45 private ArrayBlockingQueue<Packet> resultQueue; 46 47 /** 48 * Creates a new packet collector. If the packet filter is <tt>null</tt>, then 49 * all packets will match this collector. 50 */ ConnectionDetachedPacketCollector()51 public ConnectionDetachedPacketCollector() { 52 this(SmackConfiguration.getPacketCollectorSize()); 53 } 54 55 /** 56 * Creates a new packet collector. If the packet filter is <tt>null</tt>, then 57 * all packets will match this collector. 58 */ ConnectionDetachedPacketCollector(int maxSize)59 public ConnectionDetachedPacketCollector(int maxSize) { 60 this.resultQueue = new ArrayBlockingQueue<Packet>(maxSize); 61 } 62 63 /** 64 * Polls to see if a packet is currently available and returns it, or 65 * immediately returns <tt>null</tt> if no packets are currently in the 66 * result queue. 67 * 68 * @return the next packet result, or <tt>null</tt> if there are no more 69 * results. 70 */ pollResult()71 public Packet pollResult() { 72 return resultQueue.poll(); 73 } 74 75 /** 76 * Returns the next available packet. The method call will block (not return) 77 * until a packet is available. 78 * 79 * @return the next available packet. 80 */ nextResult()81 public Packet nextResult() { 82 try { 83 return resultQueue.take(); 84 } 85 catch (InterruptedException e) { 86 throw new RuntimeException(e); 87 } 88 } 89 90 /** 91 * Returns the next available packet. The method call will block (not return) 92 * until a packet is available or the <tt>timeout</tt> has elapased. If the 93 * timeout elapses without a result, <tt>null</tt> will be returned. 94 * 95 * @param timeout the amount of time to wait for the next packet (in milleseconds). 96 * @return the next available packet. 97 */ nextResult(long timeout)98 public Packet nextResult(long timeout) { 99 try { 100 return resultQueue.poll(timeout, TimeUnit.MILLISECONDS); 101 } 102 catch (InterruptedException e) { 103 throw new RuntimeException(e); 104 } 105 } 106 107 /** 108 * Processes a packet to see if it meets the criteria for this packet collector. 109 * If so, the packet is added to the result queue. 110 * 111 * @param packet the packet to process. 112 */ processPacket(Packet packet)113 protected void processPacket(Packet packet) { 114 if (packet == null) { 115 return; 116 } 117 118 while (!resultQueue.offer(packet)) { 119 // Since we know the queue is full, this poll should never actually block. 120 resultQueue.poll(); 121 } 122 } 123 } 124