1 20 21 package org.jivesoftware.smack; 22 23 import org.jivesoftware.smack.packet.Packet; 24 import org.jivesoftware.smack.filter.PacketFilter; 25 26 import java.util.LinkedList ; 27 28 41 public class PacketCollector { 42 43 48 private static final int MAX_PACKETS = 65536; 49 50 private PacketFilter packetFilter; 51 private LinkedList resultQueue; 52 private PacketReader packetReader; 53 private boolean cancelled = false; 54 55 62 protected PacketCollector(PacketReader packetReader, PacketFilter packetFilter) { 63 this.packetReader = packetReader; 64 this.packetFilter = packetFilter; 65 this.resultQueue = new LinkedList (); 66 synchronized (packetReader.collectors) { 68 packetReader.collectors.add(this); 69 } 70 } 71 72 77 public void cancel() { 78 if (cancelled) { 80 return; 81 } 82 else { 83 cancelled = true; 84 synchronized (packetReader.collectors) { 88 int index = packetReader.collectors.indexOf(this); 89 packetReader.collectors.set(index, null); 90 } 91 } 92 } 93 94 100 public PacketFilter getPacketFilter() { 101 return packetFilter; 102 } 103 104 112 public synchronized Packet pollResult() { 113 if (resultQueue.isEmpty()) { 114 return null; 115 } 116 else { 117 return (Packet)resultQueue.removeLast(); 118 } 119 } 120 121 127 public synchronized Packet nextResult() { 128 while (resultQueue.isEmpty()) { 130 try { 131 wait(); 132 } 133 catch (InterruptedException ie) { } 134 } 135 return (Packet)resultQueue.removeLast(); 136 } 137 138 146 public synchronized Packet nextResult(long timeout) { 147 if (resultQueue.isEmpty()) { 149 try { 150 wait(timeout); 151 } 152 catch (InterruptedException ie) { } 153 } 154 if (resultQueue.isEmpty()) { 156 return null; 157 } 158 else { 159 return (Packet)resultQueue.removeLast(); 160 } 161 } 162 163 169 protected synchronized void processPacket(Packet packet) { 170 if (packet == null) { 171 return; 172 } 173 if (packetFilter == null || packetFilter.accept(packet)) { 174 if (resultQueue.size() == MAX_PACKETS) { 176 resultQueue.removeLast(); 177 } 178 resultQueue.addFirst(packet); 180 notifyAll(); 182 } 183 } 184 } 185 | Popular Tags |