From 41dad8658accf38931a60c0c1125b324f86fbfef Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Sat, 20 Dec 2003 12:20:30 +0000 Subject: [PATCH] Allows reply timeout to be set git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2189 b35dd754-fafc-0310-a699-88a17e54d16e --- build/resources/META-INF/smack.configuration | 44 ++++++++++---- .../jivesoftware/smack/AccountManager.java | 18 ++++-- source/org/jivesoftware/smack/GroupChat.java | 16 +++++- source/org/jivesoftware/smack/Roster.java | 14 ++++- .../smack/SmackConfiguration.java | 57 ++++++++++++++++--- .../jivesoftware/smack/XMPPConnection.java | 22 +++++-- 6 files changed, 139 insertions(+), 32 deletions(-) diff --git a/build/resources/META-INF/smack.configuration b/build/resources/META-INF/smack.configuration index f9d761d96..1071abf1e 100644 --- a/build/resources/META-INF/smack.configuration +++ b/build/resources/META-INF/smack.configuration @@ -1,14 +1,38 @@ - + - - - - - org.jivesoftware.smackx.ServiceDiscoveryManager - org.jivesoftware.smackx.XHTMLManager - - + + - 1.3.0 + 1.3.0 + + + + org.jivesoftware.smackx.ServiceDiscoveryManager + org.jivesoftware.smackx.XHTMLManager + + + + + 5000 + org.jivesoftware.smack.AccountManager + + + + + 5000 + org.jivesoftware.smack.GroupChat + + + + + 5000 + org.jivesoftware.smack.Roster + + + + + 5000 + org.jivesoftware.smack.XMPPConnection + \ No newline at end of file diff --git a/source/org/jivesoftware/smack/AccountManager.java b/source/org/jivesoftware/smack/AccountManager.java index 0618aacc3..1e06a2bf0 100644 --- a/source/org/jivesoftware/smack/AccountManager.java +++ b/source/org/jivesoftware/smack/AccountManager.java @@ -67,6 +67,16 @@ import java.util.*; */ public class AccountManager { + /** + * Value that indicates the number of milliseconds to wait for a response from + * the server. + * + * The reply timeout value can be assigned by setting this field to the required + * timeout, or by modifying the smack.configuration file that holds the default value + * to use. + */ + public static int REPLY_TIMEOUT = 5000; + private XMPPConnection connection; private Registration info = null; @@ -207,7 +217,7 @@ public class AccountManager { new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); - IQ result = (IQ)collector.nextResult(5000); + IQ result = (IQ)collector.nextResult(REPLY_TIMEOUT); if (result == null) { throw new XMPPException("No response from server."); } @@ -234,7 +244,7 @@ public class AccountManager { new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); - IQ result = (IQ)collector.nextResult(5000); + IQ result = (IQ)collector.nextResult(REPLY_TIMEOUT); if (result == null) { throw new XMPPException("No response from server."); } @@ -266,7 +276,7 @@ public class AccountManager { new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); - IQ result = (IQ)collector.nextResult(5000); + IQ result = (IQ)collector.nextResult(REPLY_TIMEOUT); if (result == null) { throw new XMPPException("No response from server."); } @@ -287,7 +297,7 @@ public class AccountManager { new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); - IQ result = (IQ)collector.nextResult(5000); + IQ result = (IQ)collector.nextResult(REPLY_TIMEOUT); if (result == null) { throw new XMPPException("No response from server."); } diff --git a/source/org/jivesoftware/smack/GroupChat.java b/source/org/jivesoftware/smack/GroupChat.java index b4cd4fcf3..0d19eaf17 100644 --- a/source/org/jivesoftware/smack/GroupChat.java +++ b/source/org/jivesoftware/smack/GroupChat.java @@ -69,6 +69,16 @@ import java.util.*; */ public class GroupChat { + /** + * Value that indicates the number of milliseconds to wait for a response from + * the server. + * + * The reply timeout value can be assigned by setting this field to the required + * timeout, or by modifying the smack.configuration file that holds the default value + * to use. + */ + public static int REPLY_TIMEOUT = 5000; + private XMPPConnection connection; private String room; private String nickname = null; @@ -151,7 +161,7 @@ public class GroupChat { * nickname. */ public synchronized void join(String nickname) throws XMPPException { - join(nickname, 5000); + join(nickname, REPLY_TIMEOUT); } /** @@ -186,8 +196,8 @@ public class GroupChat { PacketCollector response = connection.createPacketCollector(responseFilter); // Send join packet. connection.sendPacket(joinPresence); - // Wait up to five seconds for a reply. - Presence presence = (Presence)response.nextResult(5000); + // Wait up to a certain number of seconds for a reply. + Presence presence = (Presence)response.nextResult(timeout); if (presence == null) { throw new XMPPException("No response from server."); } diff --git a/source/org/jivesoftware/smack/Roster.java b/source/org/jivesoftware/smack/Roster.java index 331bfdb0a..e2934acbb 100644 --- a/source/org/jivesoftware/smack/Roster.java +++ b/source/org/jivesoftware/smack/Roster.java @@ -96,6 +96,16 @@ public class Roster { */ public static final int SUBSCRIPTION_MANUAL = 2; + /** + * Value that indicates the number of milliseconds to wait for a response from + * the server. + * + * The reply timeout value can be assigned by setting this field to the required + * timeout, or by modifying the smack.configuration file that holds the default value + * to use. + */ + public static int REPLY_TIMEOUT = 5000; + private XMPPConnection connection; private Map groups; private List entries; @@ -237,11 +247,11 @@ public class Roster { } } rosterPacket.addRosterItem(item); - // Wait up to 5 seconds for a reply from the server. + // Wait up to a certain number of seconds for a reply from the server. PacketCollector collector = connection.createPacketCollector( new PacketIDFilter(rosterPacket.getPacketID())); connection.sendPacket(rosterPacket); - IQ response = (IQ)collector.nextResult(5000); + IQ response = (IQ)collector.nextResult(REPLY_TIMEOUT); if (response == null) { throw new XMPPException("No response from the server."); } diff --git a/source/org/jivesoftware/smack/SmackConfiguration.java b/source/org/jivesoftware/smack/SmackConfiguration.java index 4639d7528..151b949b9 100644 --- a/source/org/jivesoftware/smack/SmackConfiguration.java +++ b/source/org/jivesoftware/smack/SmackConfiguration.java @@ -53,6 +53,7 @@ package org.jivesoftware.smack; import java.io.*; +import java.lang.reflect.*; import java.net.*; import java.util.*; @@ -104,18 +105,15 @@ public final class SmackConfiguration { do { if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("className")) { - String className = parser.nextText(); // Attempt to load the class so that the class can get initialized - try { - Class provider = Class.forName(className); - } - catch (ClassNotFoundException cnfe) { - cnfe.printStackTrace(); - } + parseClassToLoad(parser); } else if (parser.getName().equals("versionNumber")) { versionNumber = parser.nextText(); } + else if (parser.getName().equals("replyTimeout")) { + parseReplyTimeout(parser); + } } eventType = parser.next(); } @@ -135,6 +133,51 @@ public final class SmackConfiguration { } } + private static void parseClassToLoad(XmlPullParser parser) throws Exception { + String className = parser.nextText(); + // Attempt to load the class so that the class can get initialized + try { + Class.forName(className); + } + catch (ClassNotFoundException cnfe) { + cnfe.printStackTrace(); + } + } + + private static void parseReplyTimeout(XmlPullParser parser) throws Exception { + boolean done = false; + String timeout = null; + String className = null; + // Parse the timeout value to set + while (!done) { + int eventType = parser.next(); + if (eventType == XmlPullParser.START_TAG) { + String elementName = parser.getName(); + if (elementName.equals("value")) { + timeout = parser.nextText(); + } + else if (elementName.equals("className")) { + className = parser.nextText(); + } + } + else if (eventType == XmlPullParser.END_TAG) { + if (parser.getName().equals("replyTimeout")) { + done = true; + } + } + } + + // Set the reply timeout value + try { + Class classToConfigure = Class.forName(className); + Field field = classToConfigure.getDeclaredField("REPLY_TIMEOUT"); + field.set(null, new Integer(timeout)); + } + catch (Exception e) { + e.printStackTrace(); + } + } + /** * Returns the current Smack release version. The version number value * gets loaded from the smack.configuration file at system startup. diff --git a/source/org/jivesoftware/smack/XMPPConnection.java b/source/org/jivesoftware/smack/XMPPConnection.java index dccac17ba..e73d78365 100644 --- a/source/org/jivesoftware/smack/XMPPConnection.java +++ b/source/org/jivesoftware/smack/XMPPConnection.java @@ -93,6 +93,16 @@ public class XMPPConnection { */ public static boolean DEBUG_ENABLED = false; + /** + * Value that indicates the number of milliseconds to wait for a response from + * the server. + * + * The reply timeout value can be assigned by setting this field to the required + * timeout, or by modifying the smack.configuration file that holds the default value + * to use. + */ + public static int REPLY_TIMEOUT = 5000; + private static List connectionEstablishedListeners = new ArrayList(); static { // Use try block since we may not have permission to get a system @@ -269,8 +279,8 @@ public class XMPPConnection { packetReader.createPacketCollector(new PacketIDFilter(discoveryAuth.getPacketID())); // Send the packet packetWriter.sendPacket(discoveryAuth); - // Wait up to five seconds for a response from the server. - IQ response = (IQ) collector.nextResult(5000); + // Wait up to a certain number of seconds for a response from the server. + IQ response = (IQ) collector.nextResult(REPLY_TIMEOUT); if (response == null) { throw new XMPPException("No response from the server."); } @@ -302,8 +312,8 @@ public class XMPPConnection { collector = packetReader.createPacketCollector(new PacketIDFilter(auth.getPacketID())); // Send the packet. packetWriter.sendPacket(auth); - // Wait up to five seconds for a response from the server. - response = (IQ) collector.nextResult(5000); + // Wait up to a certain number of seconds for a response from the server. + response = (IQ) collector.nextResult(REPLY_TIMEOUT); if (response == null) { throw new XMPPException("Authentication failed."); } @@ -366,8 +376,8 @@ public class XMPPConnection { packetReader.createPacketCollector(new PacketIDFilter(auth.getPacketID())); // Send the packet. packetWriter.sendPacket(auth); - // Wait up to five seconds for a response from the server. - IQ response = (IQ) collector.nextResult(5000); + // Wait up to a certain number of seconds for a response from the server. + IQ response = (IQ) collector.nextResult(REPLY_TIMEOUT); if (response == null) { throw new XMPPException("Anonymous login failed."); }