1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 09:09:38 +02:00

Add XMPPConnection.sendStanza(Stanza)

and deprecate sendPacket().
This commit is contained in:
Florian Schmaus 2015-03-04 21:44:43 +01:00
parent 183af99ffb
commit ed4fa3390f
58 changed files with 183 additions and 167 deletions

View file

@ -54,7 +54,7 @@ public class IQTest extends SmackTestCase {
new StanzaTypeFilter(IQ.class));
PacketCollector collector = getConnection(0).createPacketCollector(filter);
// Send the iq packet with an invalid namespace
getConnection(0).sendPacket(iq);
getConnection(0).sendStanza(iq);
IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
@ -85,7 +85,7 @@ public class IQTest extends SmackTestCase {
PacketCollector collector = getConnection(0).createPacketCollector(
new PacketIDFilter(versionRequest.getStanzaId()));
getConnection(0).sendPacket(versionRequest);
getConnection(0).sendStanza(versionRequest);
// Wait up to 5 seconds for a result.
IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

View file

@ -42,11 +42,11 @@ public class MessageTest extends SmackTestCase {
* message?
*/
public void testDirectPresence() {
getConnection(1).sendPacket(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
Presence presence = new Presence(Presence.Type.available);
presence.setTo(getBareJID(1));
getConnection(0).sendPacket(presence);
getConnection(0).sendStanza(presence);
PacketCollector collector = getConnection(0)
.createPacketCollector(new MessageTypeFilter(Message.Type.chat));
@ -67,10 +67,10 @@ public class MessageTest extends SmackTestCase {
* the client becomes available again the offline messages are received.
*/
public void testOfflineMessage() {
getConnection(0).sendPacket(new Presence(Presence.Type.available));
getConnection(1).sendPacket(new Presence(Presence.Type.available));
getConnection(0).sendStanza(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Make user2 unavailable
getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
@ -86,7 +86,7 @@ public class MessageTest extends SmackTestCase {
// User2 becomes available again
getConnection(1).sendPacket(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available
Message message = (Message) collector.nextResult(2500);
@ -112,7 +112,7 @@ public class MessageTest extends SmackTestCase {
*/
/*public void testOfflineMessageInvalidXML() {
// Make user2 unavailable
getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
@ -128,7 +128,7 @@ public class MessageTest extends SmackTestCase {
// User2 becomes available again
getConnection(1).sendPacket(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available
Message message = (Message) collector.nextResult(2500);
@ -150,8 +150,8 @@ public class MessageTest extends SmackTestCase {
* connections are not being closed.
*/
public void testHugeMessage() {
getConnection(0).sendPacket(new Presence(Presence.Type.available));
getConnection(1).sendPacket(new Presence(Presence.Type.available));
getConnection(0).sendStanza(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.chat));
@ -165,7 +165,7 @@ public class MessageTest extends SmackTestCase {
msg.setBody(sb.toString());
// Send the first message
getConnection(0).sendPacket(msg);
getConnection(0).sendStanza(msg);
// Check that the connection that sent the message is still connected
assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
// Check that the message was received
@ -173,7 +173,7 @@ public class MessageTest extends SmackTestCase {
assertNotNull("No Message was received", rcv);
// Send the second message
getConnection(0).sendPacket(msg);
getConnection(0).sendStanza(msg);
// Check that the connection that sent the message is still connected
assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
// Check that the second message was received
@ -200,11 +200,11 @@ public class MessageTest extends SmackTestCase {
// Set this connection as highest priority
Presence presence = new Presence(Presence.Type.available);
presence.setPriority(10);
conn3.sendPacket(presence);
conn3.sendStanza(presence);
// Set this connection as highest priority
presence = new Presence(Presence.Type.available);
presence.setPriority(5);
getConnection(0).sendPacket(presence);
getConnection(0).sendStanza(presence);
// Let the server process the change in presences
Thread.sleep(200);
@ -249,11 +249,11 @@ public class MessageTest extends SmackTestCase {
// Set this connection as highest priority
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.away);
conn3.sendPacket(presence);
conn3.sendStanza(presence);
// Set this connection as highest priority
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
getConnection(0).sendPacket(presence);
getConnection(0).sendStanza(presence);
// Let the server process the change in presences
Thread.sleep(200);
@ -299,12 +299,12 @@ public class MessageTest extends SmackTestCase {
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setPriority(10);
conn3.sendPacket(presence);
conn3.sendStanza(presence);
// Set this connection as highest priority
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setPriority(10);
getConnection(0).sendPacket(presence);
getConnection(0).sendStanza(presence);
connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
@ -314,7 +314,7 @@ public class MessageTest extends SmackTestCase {
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setPriority(4);
getConnection(0).sendPacket(presence);
getConnection(0).sendStanza(presence);
// Let the server process the change in presences
@ -326,7 +326,7 @@ public class MessageTest extends SmackTestCase {
PacketCollector coll4 = conn4.createPacketCollector(new MessageTypeFilter(Message.Type.chat));
// Send a message from this resource to indicate most recent activity
conn3.sendPacket(new Message("admin@" + getServiceName()));
conn3.sendStanza(new Message("admin@" + getServiceName()));
// User1 sends a message to the bare JID of User0
Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
@ -362,7 +362,7 @@ public class MessageTest extends SmackTestCase {
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setPriority(-1);
getConnection(0).sendPacket(presence);
getConnection(0).sendStanza(presence);
// Let the server process the change in presences
Thread.sleep(200);
@ -383,7 +383,7 @@ public class MessageTest extends SmackTestCase {
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
presence.setPriority(1);
getConnection(0).sendPacket(presence);
getConnection(0).sendStanza(presence);
// Let the server process the change in presences
Thread.sleep(200);

View file

@ -84,7 +84,7 @@ public class PacketReaderTest extends SmackTestCase {
// Send the IQ and wait for the answer
PacketCollector collector = getConnection(0).createPacketCollector(
new PacketIDFilter(iqPacket.getStanzaId()));
getConnection(0).sendPacket(iqPacket);
getConnection(0).sendStanza(iqPacket);
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
if (response == null) {
fail("No response from the other user.");
@ -114,7 +114,7 @@ public class PacketReaderTest extends SmackTestCase {
Message msg = new Message(getConnection(0).getUser(), Message.Type.normal);
getConnection(1).sendPacket(msg);
getConnection(1).sendStanza(msg);
// Remove the listener
getConnection(0).removeAsyncPacketListener(listener);
@ -141,7 +141,7 @@ public class PacketReaderTest extends SmackTestCase {
Message message = new Message(packet.getFrom());
message.setFrom(getFullJID(1));
message.setBody("HELLO");
getConnection(1).sendPacket(message);
getConnection(1).sendStanza(message);
}
}, new StanzaTypeFilter(Message.class));
@ -149,7 +149,7 @@ public class PacketReaderTest extends SmackTestCase {
PacketCollector collector = getConnection(0).createPacketCollector(
new FromMatchesFilter(getFullJID(1)));
// User0 sends the regular message to user1
getConnection(0).sendPacket(packet);
getConnection(0).sendStanza(packet);
// Check that user0 got a reply from user1
assertNotNull("No message was received", collector.nextResult(1000));
@ -159,7 +159,7 @@ public class PacketReaderTest extends SmackTestCase {
packet.setTo(getFullJID(1));
packet.setBody("aloha");
packet.setError(new XMPPError(XMPPError.Condition.feature_not_implemented, null));
getConnection(0).sendPacket(packet);
getConnection(0).sendStanza(packet);
// Check that user0 got a reply from user1
assertNotNull("No message was received", collector.nextResult(1000));
}
@ -213,8 +213,8 @@ public class PacketReaderTest extends SmackTestCase {
for (int i = 0; i < 5; i++) {
getConnection(1).sendPacket(msg0);
getConnection(0).sendPacket(msg1);
getConnection(1).sendStanza(msg0);
getConnection(0).sendStanza(msg1);
}
try {
@ -236,8 +236,8 @@ public class PacketReaderTest extends SmackTestCase {
}
for (int i = 0; i < 10; i++) {
getConnection(0).sendPacket(msg1);
getConnection(1).sendPacket(msg0);
getConnection(0).sendStanza(msg1);
getConnection(1).sendStanza(msg0);
}
try {

View file

@ -46,9 +46,9 @@ public class PresenceTest extends SmackTestCase {
conn.connect();
conn.login(getUsername(1), getUsername(1), "OtherPlace");
// Change the presence priorities of User_1
getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 1,
getConnection(1).sendStanza(new Presence(Presence.Type.available, null, 1,
Presence.Mode.available));
conn.sendPacket(new Presence(Presence.Type.available, null, 2,
conn.sendStanza(new Presence(Presence.Type.available, null, 2,
Presence.Mode.available));
Thread.sleep(150);
// Create the chats between the participants
@ -64,9 +64,9 @@ public class PresenceTest extends SmackTestCase {
chat1.nextMessage(1000));*/
// Invert the presence priorities of User_1
getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 2,
getConnection(1).sendStanza(new Presence(Presence.Type.available, null, 2,
Presence.Mode.available));
conn.sendPacket(new Presence(Presence.Type.available, null, 1,
conn.sendStanza(new Presence(Presence.Type.available, null, 1,
Presence.Mode.available));
Thread.sleep(150);
@ -86,14 +86,14 @@ public class PresenceTest extends SmackTestCase {
/*assertNotNull("Resource with highest priority didn't receive the message",
chat1.nextMessage(2000));*/
getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 2,
getConnection(1).sendStanza(new Presence(Presence.Type.available, null, 2,
Presence.Mode.available));
// User_1 will log in again using another resource
conn = createConnection();
conn.connect();
conn.login(getUsername(1), getPassword(1), "OtherPlace");
conn.sendPacket(new Presence(Presence.Type.available, null, 1,
conn.sendStanza(new Presence(Presence.Type.available, null, 1,
Presence.Mode.available));
chat2 = conn.getChatManager().createChat(getBareJID(0), chat0.getThreadID(), null);
@ -106,9 +106,9 @@ public class PresenceTest extends SmackTestCase {
chat2.nextMessage(1000));*/
// Invert the presence priorities of User_1
getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 1,
getConnection(1).sendStanza(new Presence(Presence.Type.available, null, 1,
Presence.Mode.available));
conn.sendPacket(new Presence(Presence.Type.available, null, 2,
conn.sendStanza(new Presence(Presence.Type.available, null, 2,
Presence.Mode.available));
Thread.sleep(150);
@ -139,7 +139,7 @@ public class PresenceTest extends SmackTestCase {
*/
public void testNotAvailablePresence() throws XMPPException {
// Change the presence to unavailable of User_1
getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
// User_1 will log in again using another resource (that is going to be available)
XMPPTCPConnection conn = createConnection();

View file

@ -325,7 +325,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
@Override
public abstract boolean isSecureConnection();
protected abstract void sendPacketInternal(Stanza packet) throws NotConnectedException;
protected abstract void sendStanzaInternal(Stanza packet) throws NotConnectedException;
@Override
public abstract void send(PlainStreamElement element) throws NotConnectedException;
@ -538,7 +538,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
// eventually load the roster. And we should load the roster before we
// send the initial presence.
if (config.isSendPresence() && !resumed) {
sendPacket(new Presence(Presence.Type.available));
sendStanza(new Presence(Presence.Type.available));
}
}
@ -596,8 +596,14 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
}
@Deprecated
@Override
public void sendPacket(Stanza packet) throws NotConnectedException {
sendStanza(packet);
}
@Override
public void sendStanza(Stanza packet) throws NotConnectedException {
Objects.requireNonNull(packet, "Packet must not be null");
throwNotConnectedExceptionIfAppropriate();
@ -615,7 +621,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
// Invoke interceptors for the new packet that is about to be sent. Interceptors may modify
// the content of the packet.
firePacketInterceptors(packet);
sendPacketInternal(packet);
sendStanzaInternal(packet);
}
/**
@ -656,7 +662,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* @throws NotConnectedException
*/
public synchronized void disconnect(Presence unavailablePresence) throws NotConnectedException {
sendPacket(unavailablePresence);
sendStanza(unavailablePresence);
shutdown();
callConnectionClosedListener();
}
@ -694,7 +700,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
PacketCollector packetCollector = createPacketCollector(packetFilter);
try {
// Now we can send the packet as the collector has been created
sendPacket(packet);
sendStanza(packet);
}
catch (NotConnectedException | RuntimeException e) {
packetCollector.cancel();
@ -1023,7 +1029,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
ErrorIQ errorIQ = IQ.createErrorResponse(iq, new XMPPError(
XMPPError.Condition.feature_not_implemented));
try {
sendPacket(errorIQ);
sendStanza(errorIQ);
}
catch (NotConnectedException e) {
LOGGER.log(Level.WARNING, "NotConnectedException while sending error IQ to unkown IQ request", e);
@ -1052,7 +1058,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return;
}
try {
sendPacket(response);
sendStanza(response);
}
catch (NotConnectedException e) {
LOGGER.log(Level.WARNING, "NotConnectedException while sending response to IQ request", e);
@ -1433,7 +1439,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
}, timeout, TimeUnit.MILLISECONDS);
addAsyncStanzaListener(packetListener, replyFilter);
sendPacket(stanza);
sendStanza(stanza);
}
@Override

View file

@ -62,7 +62,7 @@ public class SynchronizationPoint<E extends Exception> {
try {
if (request != null) {
if (request instanceof Stanza) {
connection.sendPacket((Stanza) request);
connection.sendStanza((Stanza) request);
}
else if (request instanceof PlainStreamElement){
connection.send((PlainStreamElement) request);

View file

@ -158,9 +158,19 @@ public interface XMPPConnection {
*
* @param packet the packet to send.
* @throws NotConnectedException
* @deprecated use {@link #sendStanza(Stanza)} instead.
*/
@Deprecated
public void sendPacket(Stanza packet) throws NotConnectedException;
/**
* Sends the specified stanza to the server.
*
* @param stanza the stanza to send.
* @throws NotConnectedException if the connection is not connected.
*/
public void sendStanza(Stanza stanza) throws NotConnectedException;
/**
* Send a PlainStreamElement.
* <p>

View file

@ -33,7 +33,7 @@ import org.jivesoftware.smack.packet.TopLevelStreamElement;
* unit tests.
*
* Instances store any packets that are delivered to be send using the
* {@link #sendPacket(Stanza)} method in a blocking queue. The content of this queue
* {@link #sendStanza(Stanza)} method in a blocking queue. The content of this queue
* can be inspected using {@link #getSentPacket()}. Typically these queues are
* used to retrieve a message that was generated by the client.
*
@ -132,7 +132,7 @@ public class DummyConnection extends AbstractXMPPConnection {
}
@Override
protected void sendPacketInternal(Stanza packet) {
protected void sendStanzaInternal(Stanza packet) {
if (SmackConfiguration.DEBUG) {
System.out.println("[SEND]: " + packet.toXML());
}
@ -140,7 +140,7 @@ public class DummyConnection extends AbstractXMPPConnection {
}
/**
* Returns the number of packets that's sent through {@link #sendPacket(Stanza)} and
* Returns the number of packets that's sent through {@link #sendStanza(Stanza)} and
* that has not been returned by {@link #getSentPacket()}.
*
* @return the number of packets which are in the queue.
@ -150,7 +150,7 @@ public class DummyConnection extends AbstractXMPPConnection {
}
/**
* Returns the first packet that's sent through {@link #sendPacket(Stanza)}
* Returns the first packet that's sent through {@link #sendStanza(Stanza)}
* and that has not been returned by earlier calls to this method.
*
* @return a sent packet.
@ -160,7 +160,7 @@ public class DummyConnection extends AbstractXMPPConnection {
}
/**
* Returns the first packet that's sent through {@link #sendPacket(Stanza)}
* Returns the first packet that's sent through {@link #sendStanza(Stanza)}
* 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.

View file

@ -38,8 +38,8 @@ public class ThreadedDummyConnection extends DummyConnection {
private volatile boolean timeout = false;
@Override
public void sendPacket(Stanza packet) throws NotConnectedException {
super.sendPacket(packet);
public void sendStanza(Stanza packet) throws NotConnectedException {
super.sendStanza(packet);
if (packet instanceof IQ && !timeout) {
timeout = false;
@ -62,7 +62,7 @@ public class ThreadedDummyConnection extends DummyConnection {
}
/**
* Calling this method will cause the next sendPacket call with an IQ packet to timeout.
* Calling this method will cause the next sendStanza 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(Stanza)}.
*/