mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-10 18:59:41 +02:00
SMACK-412 Split the ping implementation to a server ping to replace keepalive and a simplified ping manager for manual pings of other entities.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13569 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
a55b54f20b
commit
aab1dcdabe
19 changed files with 749 additions and 651 deletions
|
@ -57,11 +57,15 @@ public class DummyConnection extends Connection {
|
|||
private final BlockingQueue<Packet> queue = new LinkedBlockingQueue<Packet>();
|
||||
|
||||
public DummyConnection() {
|
||||
super(new ConnectionConfiguration("example.com"));
|
||||
this(new ConnectionConfiguration("example.com"));
|
||||
}
|
||||
|
||||
public DummyConnection(ConnectionConfiguration configuration) {
|
||||
super(configuration);
|
||||
|
||||
for (ConnectionCreationListener listener : getConnectionCreationListeners()) {
|
||||
listener.connectionCreated(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -191,15 +195,27 @@ public class DummyConnection extends Connection {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the first packet that's sent through {@link #sendPacket(Packet)} and
|
||||
* that has not been returned by earlier calls to this method. This method
|
||||
* will block for up to two seconds if no packets have been sent yet.
|
||||
* Returns the first packet that's sent through {@link #sendPacket(Packet)}
|
||||
* and that has not been returned by earlier calls to this method.
|
||||
*
|
||||
* @return a sent packet.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public Packet getSentPacket() throws InterruptedException {
|
||||
return queue.poll(2, TimeUnit.SECONDS);
|
||||
return queue.poll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first packet that's sent through {@link #sendPacket(Packet)}
|
||||
* and that has not been returned by earlier calls to this method. This
|
||||
* method will block for up to the specified number of seconds if no packets
|
||||
* have been sent yet.
|
||||
*
|
||||
* @return a sent packet.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public Packet getSentPacket(int wait) throws InterruptedException {
|
||||
return queue.poll(wait, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,80 +17,85 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
|
||||
public class ThreadedDummyConnection extends DummyConnection
|
||||
{
|
||||
private BlockingQueue<IQ> replyQ = new ArrayBlockingQueue<IQ>(1);
|
||||
private BlockingQueue<Packet> messageQ = new LinkedBlockingQueue<Packet>(5);
|
||||
|
||||
@Override
|
||||
public void sendPacket(Packet packet)
|
||||
{
|
||||
super.sendPacket(packet);
|
||||
|
||||
if ((packet instanceof IQ) && !replyQ.isEmpty())
|
||||
{
|
||||
// Set reply packet to match one being sent. We haven't started the
|
||||
// other thread yet so this is still safe.
|
||||
IQ replyPacket = replyQ.peek();
|
||||
replyPacket.setPacketID(packet.getPacketID());
|
||||
replyPacket.setFrom(packet.getTo());
|
||||
replyPacket.setTo(packet.getFrom());
|
||||
replyPacket.setType(Type.RESULT);
|
||||
|
||||
new ProcessQueue(replyQ).start();
|
||||
}
|
||||
}
|
||||
|
||||
public void addMessage(Message msgToProcess)
|
||||
{
|
||||
messageQ.add(msgToProcess);
|
||||
}
|
||||
|
||||
public void addIQReply(IQ reply)
|
||||
{
|
||||
replyQ.add(reply);
|
||||
}
|
||||
|
||||
public void processMessages()
|
||||
{
|
||||
if (!messageQ.isEmpty())
|
||||
new ProcessQueue(messageQ).start();
|
||||
else
|
||||
System.out.println("No messages to process");
|
||||
}
|
||||
|
||||
class ProcessQueue extends Thread
|
||||
{
|
||||
private BlockingQueue<? extends Packet> processQ;
|
||||
|
||||
ProcessQueue(BlockingQueue<? extends Packet> queue)
|
||||
{
|
||||
processQ = queue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
processPacket(processQ.take());
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
|
||||
public class ThreadedDummyConnection extends DummyConnection {
|
||||
private BlockingQueue<IQ> replyQ = new ArrayBlockingQueue<IQ>(1);
|
||||
private BlockingQueue<Packet> messageQ = new LinkedBlockingQueue<Packet>(5);
|
||||
private volatile boolean timeout = false;
|
||||
|
||||
@Override
|
||||
public void sendPacket(Packet packet) {
|
||||
super.sendPacket(packet);
|
||||
|
||||
if (packet instanceof IQ && !timeout) {
|
||||
timeout = false;
|
||||
// Set reply packet to match one being sent. We haven't started the
|
||||
// other thread yet so this is still safe.
|
||||
IQ replyPacket = replyQ.peek();
|
||||
|
||||
// If no reply has been set via addIQReply, then we create a simple reply
|
||||
if (replyPacket == null) {
|
||||
replyPacket = IQ.createResultIQ((IQ) packet);
|
||||
replyQ.add(replyPacket);
|
||||
}
|
||||
replyPacket.setPacketID(packet.getPacketID());
|
||||
replyPacket.setFrom(packet.getTo());
|
||||
replyPacket.setTo(packet.getFrom());
|
||||
replyPacket.setType(Type.RESULT);
|
||||
|
||||
new ProcessQueue(replyQ).start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling this method will cause the next sendPacket call with an IQ packet to timeout.
|
||||
* This is accomplished by simply stopping the auto creating of the reply packet
|
||||
* or processing one that was entered via {@link #processPacket(Packet)}.
|
||||
*/
|
||||
public void setTimeout() {
|
||||
timeout = true;
|
||||
}
|
||||
|
||||
public void addMessage(Message msgToProcess) {
|
||||
messageQ.add(msgToProcess);
|
||||
}
|
||||
|
||||
public void addIQReply(IQ reply) {
|
||||
replyQ.add(reply);
|
||||
}
|
||||
|
||||
public void processMessages() {
|
||||
if (!messageQ.isEmpty())
|
||||
new ProcessQueue(messageQ).start();
|
||||
else
|
||||
System.out.println("No messages to process");
|
||||
}
|
||||
|
||||
class ProcessQueue extends Thread {
|
||||
private BlockingQueue<? extends Packet> processQ;
|
||||
|
||||
ProcessQueue(BlockingQueue<? extends Packet> queue) {
|
||||
processQ = queue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
processPacket(processQ.take());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
162
test-unit/org/jivesoftware/smack/ping/ServerPingTest.java
Normal file
162
test-unit/org/jivesoftware/smack/ping/ServerPingTest.java
Normal file
|
@ -0,0 +1,162 @@
|
|||
package org.jivesoftware.smack.ping;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.DummyConnection;
|
||||
import org.jivesoftware.smack.PacketInterceptor;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.SmackConfiguration;
|
||||
import org.jivesoftware.smack.TestUtils;
|
||||
import org.jivesoftware.smack.ThreadedDummyConnection;
|
||||
import org.jivesoftware.smack.filter.IQTypeFilter;
|
||||
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.ping.packet.Ping;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ServerPingTest {
|
||||
private static String TO = "juliet@capulet.lit/balcony";
|
||||
private static String ID = "s2c1";
|
||||
|
||||
private static Properties outputProperties = new Properties();
|
||||
{
|
||||
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
}
|
||||
|
||||
/*
|
||||
* Stanza copied from spec
|
||||
*/
|
||||
@Test
|
||||
public void validatePingStanzaXML() throws Exception {
|
||||
// @formatter:off
|
||||
String control = "<iq to='juliet@capulet.lit/balcony' id='s2c1' type='get'>"
|
||||
+ "<ping xmlns='urn:xmpp:ping'/>" + "</iq>";
|
||||
// @formatter:on
|
||||
|
||||
Ping ping = new Ping(TO);
|
||||
ping.setPacketID(ID);
|
||||
|
||||
assertXMLEqual(control, ping.toXML());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkProvider() throws Exception {
|
||||
// @formatter:off
|
||||
String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>"
|
||||
+ "<ping xmlns='urn:xmpp:ping'/>" + "</iq>";
|
||||
// @formatter:on
|
||||
DummyConnection con = new DummyConnection();
|
||||
IQ pingRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(control), con);
|
||||
|
||||
assertTrue(pingRequest instanceof Ping);
|
||||
|
||||
con.processPacket(pingRequest);
|
||||
|
||||
Packet pongPacket = con.getSentPacket();
|
||||
assertTrue(pongPacket instanceof IQ);
|
||||
|
||||
IQ pong = (IQ) pongPacket;
|
||||
assertEquals("juliet@capulet.lit/balcony", pong.getFrom());
|
||||
assertEquals("capulet.lit", pong.getTo());
|
||||
assertEquals("s2c1", pong.getPacketID());
|
||||
assertEquals(IQ.Type.RESULT, pong.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serverPingFailSingleConnection() throws Exception {
|
||||
DummyConnection connection = getConnection();
|
||||
CountDownLatch latch = new CountDownLatch(2);
|
||||
addInterceptor(connection, latch);
|
||||
addPingFailedListener(connection, latch);
|
||||
|
||||
// Time based testing kind of sucks, but this should be reliable on a DummyConnection since there
|
||||
// is no actual server involved. This will provide enough time to ping and wait for the lack of response.
|
||||
assertTrue(latch.await(getWaitTime(), TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serverPingSuccessfulSingleConnection() throws Exception {
|
||||
ThreadedDummyConnection connection = getThreadedConnection();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
connection.addPacketListener(new PacketListener() {
|
||||
@Override
|
||||
public void processPacket(Packet packet) {
|
||||
latch.countDown();
|
||||
}
|
||||
}, new IQTypeFilter(IQ.Type.RESULT));
|
||||
|
||||
// Time based testing kind of sucks, but this should be reliable on a DummyConnection since there
|
||||
// is no actual server involved. This will provide enough time to ping and wait for the lack of response.
|
||||
assertTrue(latch.await(getWaitTime(), TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serverPingFailMultipleConnection() throws Exception {
|
||||
CountDownLatch latch = new CountDownLatch(6);
|
||||
SmackConfiguration.setPacketReplyTimeout(15000);
|
||||
|
||||
DummyConnection con1 = getConnection();
|
||||
addInterceptor(con1, latch);
|
||||
addPingFailedListener(con1, latch);
|
||||
|
||||
DummyConnection con2 = getConnection();
|
||||
addInterceptor(con2, latch);
|
||||
addPingFailedListener(con2, latch);
|
||||
|
||||
DummyConnection con3 = getConnection();
|
||||
addInterceptor(con3, latch);
|
||||
addPingFailedListener(con2, latch);
|
||||
|
||||
assertTrue(latch.await(getWaitTime(), TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
private void addPingFailedListener(DummyConnection con, final CountDownLatch latch) {
|
||||
ServerPingManager manager = ServerPingManager.getInstanceFor(con);
|
||||
manager.addPingFailedListener(new PingFailedListener() {
|
||||
@Override
|
||||
public void pingFailed() {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private DummyConnection getConnection() {
|
||||
DummyConnection con = new DummyConnection();
|
||||
ServerPingManager mgr = ServerPingManager.getInstanceFor(con);
|
||||
mgr.setPingInterval(ServerPingManager.PING_MINIMUM);
|
||||
|
||||
return con;
|
||||
}
|
||||
|
||||
private ThreadedDummyConnection getThreadedConnection() {
|
||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
||||
ServerPingManager mgr = ServerPingManager.getInstanceFor(con);
|
||||
mgr.setPingInterval(ServerPingManager.PING_MINIMUM);
|
||||
|
||||
return con;
|
||||
}
|
||||
|
||||
private void addInterceptor(final Connection con, final CountDownLatch latch) {
|
||||
con.addPacketInterceptor(new PacketInterceptor() {
|
||||
@Override
|
||||
public void interceptPacket(Packet packet) {
|
||||
con.removePacketInterceptor(this);
|
||||
latch.countDown();
|
||||
}
|
||||
}, new PacketTypeFilter(Ping.class));
|
||||
}
|
||||
|
||||
private long getWaitTime() {
|
||||
return ServerPingManager.PING_MINIMUM + SmackConfiguration.getPacketReplyTimeout() + 3000;
|
||||
}
|
||||
}
|
|
@ -15,24 +15,127 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.ping;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.jivesoftware.smackx.ping.packet.Ping;
|
||||
import org.jivesoftware.smackx.ping.packet.Pong;
|
||||
import org.jivesoftware.smack.DummyConnection;
|
||||
import org.jivesoftware.smack.TestUtils;
|
||||
import org.jivesoftware.smack.ThreadedDummyConnection;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.ping.packet.Ping;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smackx.packet.DiscoverInfo;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PingPongTest {
|
||||
|
||||
@Test
|
||||
public void createPongfromPingTest() {
|
||||
Ping ping = new Ping("from@sender.local/resourceFrom", "to@receiver.local/resourceTo");
|
||||
public void checkSendingPing() throws Exception {
|
||||
DummyConnection con = new DummyConnection();
|
||||
PingManager pinger = new PingManager(con);
|
||||
pinger.ping("test@myserver.com");
|
||||
|
||||
// create a pong from a ping
|
||||
Pong pong = new Pong(ping);
|
||||
|
||||
assertEquals(pong.getFrom(), ping.getTo());
|
||||
assertEquals(pong.getTo(), ping.getFrom());
|
||||
assertEquals(pong.getPacketID(), ping.getPacketID());
|
||||
Packet sentPacket = con.getSentPacket();
|
||||
|
||||
assertTrue(sentPacket instanceof Ping);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkSuccessfulPing() throws Exception {
|
||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
||||
|
||||
PingManager pinger = new PingManager(con);
|
||||
|
||||
boolean pingSuccess = pinger.ping("test@myserver.com");
|
||||
|
||||
assertTrue(pingSuccess);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* DummyConnection will not reply so it will timeout.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void checkFailedPingOnTimeout() throws Exception {
|
||||
DummyConnection con = new DummyConnection();
|
||||
PingManager pinger = new PingManager(con);
|
||||
|
||||
boolean pingSuccess = pinger.ping("test@myserver.com");
|
||||
|
||||
assertFalse(pingSuccess);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* DummyConnection will not reply so it will timeout.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void checkFailedPingError() throws Exception {
|
||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
||||
//@formatter:off
|
||||
String reply =
|
||||
"<iq type='error' id='qrzSp-16' to='test@myserver.com'>" +
|
||||
"<ping xmlns='urn:xmpp:ping'/>" +
|
||||
"<error type='cancel'>" +
|
||||
"<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
|
||||
"</error>" +
|
||||
"</iq>";
|
||||
//@formatter:on
|
||||
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
|
||||
con.addIQReply(serviceUnavailable);
|
||||
|
||||
PingManager pinger = new PingManager(con);
|
||||
|
||||
boolean pingSuccess = pinger.ping("test@myserver.com");
|
||||
|
||||
assertFalse(pingSuccess);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkSuccessfulDiscoRequest() throws Exception {
|
||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
||||
DiscoverInfo info = new DiscoverInfo();
|
||||
info.addFeature(Ping.NAMESPACE);
|
||||
|
||||
//@formatter:off
|
||||
String reply =
|
||||
"<iq type='result' id='qrzSp-16' to='test@myserver.com'>" +
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc' name='Pidgin'/>" +
|
||||
"<feature var='urn:xmpp:ping'/>" +
|
||||
"</query></iq>";
|
||||
//@formatter:on
|
||||
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
|
||||
con.addIQReply(discoReply);
|
||||
|
||||
PingManager pinger = new PingManager(con);
|
||||
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
|
||||
|
||||
assertTrue(pingSupported);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkUnuccessfulDiscoRequest() throws Exception {
|
||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
||||
DiscoverInfo info = new DiscoverInfo();
|
||||
info.addFeature(Ping.NAMESPACE);
|
||||
|
||||
//@formatter:off
|
||||
String reply =
|
||||
"<iq type='result' id='qrzSp-16' to='test@myserver.com'>" +
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc' name='Pidgin'/>" +
|
||||
"<feature var='urn:xmpp:noping'/>" +
|
||||
"</query></iq>";
|
||||
//@formatter:on
|
||||
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
|
||||
con.addIQReply(discoReply);
|
||||
|
||||
PingManager pinger = new PingManager(con);
|
||||
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
|
||||
|
||||
assertFalse(pingSupported);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,7 +83,9 @@ public class ConfigureFormTest
|
|||
|
||||
Node node = mgr.getNode("princely_musings");
|
||||
|
||||
SmackConfiguration.setPacketReplyTimeout(100);
|
||||
SmackConfiguration.setPacketReplyTimeout(100);
|
||||
con.setTimeout();
|
||||
|
||||
node.getNodeConfiguration();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue