From da5434505b558f6ce8c77356a084d73c609717c6 Mon Sep 17 00:00:00 2001 From: rcollier Date: Sat, 18 Jun 2011 18:18:03 +0000 Subject: [PATCH] SMACK-129 - Added a property in smack-config file for the default packet collector size and set it much lower than the previous default. Also made the max size for packet collectors configurable from the constructor. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_2_0@12509 b35dd754-fafc-0310-a699-88a17e54d16e --- build/resources/META-INF/smack-config.xml | 3 ++ .../jivesoftware/smack/PacketCollector.java | 22 ++++++++-- .../smack/SmackConfiguration.java | 41 +++++++++++++++---- .../ConnectionDetachedPacketCollector.java | 14 ++++++- .../jivesoftware/smack/SmackConfigTest.java | 32 +++++++++++++++ 5 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 test-unit/org/jivesoftware/smack/SmackConfigTest.java diff --git a/build/resources/META-INF/smack-config.xml b/build/resources/META-INF/smack-config.xml index 34c5422ce..85b468c00 100644 --- a/build/resources/META-INF/smack-config.xml +++ b/build/resources/META-INF/smack-config.xml @@ -28,4 +28,7 @@ 7777 + + 10000 + diff --git a/source/org/jivesoftware/smack/PacketCollector.java b/source/org/jivesoftware/smack/PacketCollector.java index 3ef49aa12..317e940b2 100644 --- a/source/org/jivesoftware/smack/PacketCollector.java +++ b/source/org/jivesoftware/smack/PacketCollector.java @@ -32,8 +32,9 @@ import java.util.LinkedList; * use than a {@link PacketListener} when you need to wait for a specific * result.

* - * Each packet collector will queue up to 2^16 packets for processing before - * older packets are automatically dropped. + * 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()}. * * @see Connection#createPacketCollector(PacketFilter) * @author Matt Tucker @@ -45,7 +46,7 @@ public class PacketCollector { * reached, older packets will be automatically dropped from the queue as * new packets are added. */ - private static final int MAX_PACKETS = 65536; + private int maxPackets = SmackConfiguration.getPacketCollectorSize(); private PacketFilter packetFilter; private LinkedList resultQueue; @@ -65,6 +66,19 @@ public class PacketCollector { this.resultQueue = new LinkedList(); } + /** + * Creates a new packet collector. If the packet filter is null, 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) { + this(conection, packetFilter); + maxPackets = maxSize; + } + /** * Explicitly cancels the packet collector so that no more results are * queued up. Once a packet collector has been cancelled, it cannot be @@ -180,7 +194,7 @@ public class PacketCollector { } if (packetFilter == null || packetFilter.accept(packet)) { // If the max number of packets has been reached, remove the oldest one. - if (resultQueue.size() == MAX_PACKETS) { + if (resultQueue.size() == maxPackets) { resultQueue.removeLast(); } // Add the new packet. diff --git a/source/org/jivesoftware/smack/SmackConfiguration.java b/source/org/jivesoftware/smack/SmackConfiguration.java index 0e09d23f8..200ad39c3 100644 --- a/source/org/jivesoftware/smack/SmackConfiguration.java +++ b/source/org/jivesoftware/smack/SmackConfiguration.java @@ -51,7 +51,8 @@ public final class SmackConfiguration { private static Vector defaultMechs = new Vector(); private static boolean localSocks5ProxyEnabled = true; - private static int localSocks5ProxyPort = 7777; + private static int localSocks5ProxyPort = 7778; + private static int packetCollectorSize = 5000; private SmackConfiguration() { } @@ -85,20 +86,22 @@ public final class SmackConfiguration { parseClassToLoad(parser); } else if (parser.getName().equals("packetReplyTimeout")) { - packetReplyTimeout = - parseIntProperty(parser, packetReplyTimeout); + packetReplyTimeout = parseIntProperty(parser, packetReplyTimeout); } else if (parser.getName().equals("keepAliveInterval")) { keepAliveInterval = parseIntProperty(parser, keepAliveInterval); } else if (parser.getName().equals("mechName")) { defaultMechs.add(parser.nextText()); - } else if (parser.getName().equals("localSocks5ProxyEnabled")) { - localSocks5ProxyEnabled = Boolean.parseBoolean(parser - .nextText()); - } else if (parser.getName().equals("localSocks5ProxyPort")) { - localSocks5ProxyPort = parseIntProperty(parser, - localSocks5ProxyPort); + } + else if (parser.getName().equals("localSocks5ProxyEnabled")) { + localSocks5ProxyEnabled = Boolean.parseBoolean(parser.nextText()); + } + else if (parser.getName().equals("localSocks5ProxyPort")) { + localSocks5ProxyPort = parseIntProperty(parser, localSocks5ProxyPort); + } + else if (parser.getName().equals("packetCollectorSize")) { + packetCollectorSize = parseIntProperty(parser, packetCollectorSize); } } eventType = parser.next(); @@ -184,6 +187,26 @@ public final class SmackConfiguration { keepAliveInterval = interval; } + /** + * Gets the default max size of a packet collector before it will delete + * the older packets. + * + * @return The number of packets to queue before deleting older packets. + */ + public static int getPacketCollectorSize() { + return packetCollectorSize; + } + + /** + * Sets the default max size of a packet collector before it will delete + * the older packets. + * + * @param The number of packets to queue before deleting older packets. + */ + public static void setPacketCollectorSize(int collectorSize) { + packetCollectorSize = collectorSize; + } + /** * Add a SASL mechanism to the list to be used. * diff --git a/source/org/jivesoftware/smackx/muc/ConnectionDetachedPacketCollector.java b/source/org/jivesoftware/smackx/muc/ConnectionDetachedPacketCollector.java index 95c4f6610..2a719f629 100644 --- a/source/org/jivesoftware/smackx/muc/ConnectionDetachedPacketCollector.java +++ b/source/org/jivesoftware/smackx/muc/ConnectionDetachedPacketCollector.java @@ -20,6 +20,7 @@ package org.jivesoftware.smackx.muc; +import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.packet.Packet; import java.util.LinkedList; @@ -38,7 +39,7 @@ class ConnectionDetachedPacketCollector { * reached, older packets will be automatically dropped from the queue as * new packets are added. */ - private static final int MAX_PACKETS = 65536; + private int maxPackets = SmackConfiguration.getPacketCollectorSize(); private LinkedList resultQueue; @@ -50,6 +51,15 @@ class ConnectionDetachedPacketCollector { this.resultQueue = new LinkedList(); } + /** + * Creates a new packet collector. If the packet filter is null, then + * all packets will match this collector. + */ + public ConnectionDetachedPacketCollector(int maxSize) { + this.resultQueue = new LinkedList(); + maxPackets = maxSize; + } + /** * Polls to see if a packet is currently available and returns it, or * immediately returns null if no packets are currently in the @@ -124,7 +134,7 @@ class ConnectionDetachedPacketCollector { return; } // If the max number of packets has been reached, remove the oldest one. - if (resultQueue.size() == MAX_PACKETS) { + if (resultQueue.size() == maxPackets) { resultQueue.removeLast(); } // Add the new packet. diff --git a/test-unit/org/jivesoftware/smack/SmackConfigTest.java b/test-unit/org/jivesoftware/smack/SmackConfigTest.java new file mode 100644 index 000000000..9d7d15d2c --- /dev/null +++ b/test-unit/org/jivesoftware/smack/SmackConfigTest.java @@ -0,0 +1,32 @@ +package org.jivesoftware.smack; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SmackConfigTest +{ + @Test + public void validatePacketCollectorSize() + { + assertEquals(10000, SmackConfiguration.getPacketCollectorSize()); + } + + @Test + public void validateKeepAliveInterval() + { + assertEquals(30000, SmackConfiguration.getKeepAliveInterval()); + } + + @Test + public void validateLocalSocks5ProxyPort() + { + assertEquals(7777, SmackConfiguration.getLocalSocks5ProxyPort()); + } + + @Test + public void validateIsLocalSocks5Proxy() + { + assertTrue(SmackConfiguration.isLocalSocks5ProxyEnabled()); + } +}