From 6197f6200fbbf88dbfabf0788ccdaa054aeab2e5 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 17 Mar 2014 19:33:17 +0100 Subject: [PATCH] Move duplicate sendPacket() code into XMPPConnection --- .../jivesoftware/smack/BOSHConnection.java | 26 ++++--------------- .../jivesoftware/smack/XMPPConnection.java | 22 +++++++++++++--- .../jivesoftware/smack/DummyConnection.java | 10 +------ .../org/jivesoftware/smack/PacketWriter.java | 8 ------ .../org/jivesoftware/smack/TCPConnection.java | 8 +----- 5 files changed, 26 insertions(+), 48 deletions(-) diff --git a/bosh/src/main/java/org/jivesoftware/smack/BOSHConnection.java b/bosh/src/main/java/org/jivesoftware/smack/BOSHConnection.java index db3192316..b1b0af67b 100644 --- a/bosh/src/main/java/org/jivesoftware/smack/BOSHConnection.java +++ b/bosh/src/main/java/org/jivesoftware/smack/BOSHConnection.java @@ -21,6 +21,8 @@ import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; import java.io.Writer; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.security.sasl.SaslException; @@ -54,6 +56,7 @@ import org.igniterealtime.jbosh.ComposableBody; * @author Guenther Niess */ public class BOSHConnection extends XMPPConnection { + private static final Logger LOGGER = Logger.getLogger(BOSHConnection.class.getName()); /** * The XMPP Over Bosh namespace. @@ -332,31 +335,14 @@ public class BOSHConnection extends XMPPConnection { callConnectionAuthenticatedListener(); } - public void sendPacket(Packet packet) { - if (!isConnected()) { - throw new IllegalStateException("Not connected to server."); - } - if (packet == null) { - throw new NullPointerException("Packet is null."); - } + void sendPacketInternal(Packet packet) { if (!done) { - // Invoke interceptors for the new packet that is about to be sent. - // Interceptors - // may modify the content of the packet. - firePacketInterceptors(packet); - try { send(ComposableBody.builder().setPayloadXML(packet.toXML()) .build()); } catch (BOSHException e) { - e.printStackTrace(); - return; + LOGGER.log(Level.SEVERE, "BOSHException in sendPacketInternal", e); } - - // Process packet writer listeners. Note that we're using the - // sending - // thread so it's expected that listeners are fast. - firePacketSendingListeners(packet); } } @@ -550,8 +536,6 @@ public class BOSHConnection extends XMPPConnection { protected void notifyConnectionError(Exception e) { // Closes the connection temporary. A reconnection is possible shutdown(new Presence(Presence.Type.unavailable)); - // Print the stack trace to help catch the problem - e.printStackTrace(); callConnectionClosedOnErrorListener(e); } diff --git a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index 098a2009c..58c2549a0 100644 --- a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -335,6 +335,8 @@ public abstract class XMPPConnection { */ public abstract boolean isSecureConnection(); + abstract void sendPacketInternal(Packet packet); + /** * Returns if the reconnection mechanism is allowed to be used. By default * reconnection is allowed. @@ -447,7 +449,21 @@ public abstract class XMPPConnection { * * @param packet the packet to send. */ - public abstract void sendPacket(Packet packet); + public void sendPacket(Packet packet) { + if (!isConnected()) { + throw new IllegalStateException("Not connected to server."); + } + if (packet == null) { + throw new NullPointerException("Packet is null."); + } + // Invoke interceptors for the new packet that is about to be sent. Interceptors may modify + // the content of the packet. + firePacketInterceptors(packet); + sendPacketInternal(packet); + // Process packet writer listeners. Note that we're using the sending thread so it's + // expected that listeners are fast. + firePacketSendingListeners(packet); + } /** * Returns an account manager instance for this connection. @@ -771,7 +787,7 @@ public abstract class XMPPConnection { * * @param packet the packet to process. */ - protected void firePacketSendingListeners(Packet packet) { + private void firePacketSendingListeners(Packet packet) { // Notify the listeners of the new sent packet for (ListenerWrapper listenerWrapper : sendListeners.values()) { listenerWrapper.notifyListener(packet); @@ -824,7 +840,7 @@ public abstract class XMPPConnection { * * @param packet the packet that is going to be sent to the server */ - protected void firePacketInterceptors(Packet packet) { + private void firePacketInterceptors(Packet packet) { if (packet != null) { for (InterceptorWrapper interceptorWrapper : interceptors.values()) { interceptorWrapper.notifyListener(packet); diff --git a/core/src/test/java/org/jivesoftware/smack/DummyConnection.java b/core/src/test/java/org/jivesoftware/smack/DummyConnection.java index a299733aa..68d5e82e4 100644 --- a/core/src/test/java/org/jivesoftware/smack/DummyConnection.java +++ b/core/src/test/java/org/jivesoftware/smack/DummyConnection.java @@ -185,19 +185,11 @@ public class DummyConnection extends XMPPConnection { } @Override - public void sendPacket(Packet packet) { - if (!isConnected()) { - throw new IllegalStateException("Not connected to server."); - } - if (packet == null) { - throw new NullPointerException("Packet is null."); - } - firePacketInterceptors(packet); + void sendPacketInternal(Packet packet) { if (DEBUG_ENABLED) { System.out.println("[SEND]: " + packet.toXML()); } queue.add(packet); - firePacketSendingListeners(packet); } /** diff --git a/tcp/src/main/java/org/jivesoftware/smack/PacketWriter.java b/tcp/src/main/java/org/jivesoftware/smack/PacketWriter.java index 687244910..c4e108fa3 100644 --- a/tcp/src/main/java/org/jivesoftware/smack/PacketWriter.java +++ b/tcp/src/main/java/org/jivesoftware/smack/PacketWriter.java @@ -80,10 +80,6 @@ class PacketWriter { */ public void sendPacket(Packet packet) { if (!done) { - // Invoke interceptors for the new packet that is about to be sent. Interceptors - // may modify the content of the packet. - connection.firePacketInterceptors(packet); - try { queue.put(packet); } @@ -94,10 +90,6 @@ class PacketWriter { synchronized (queue) { queue.notifyAll(); } - - // Process packet writer listeners. Note that we're using the sending - // thread so it's expected that listeners are fast. - connection.firePacketSendingListeners(packet); } } diff --git a/tcp/src/main/java/org/jivesoftware/smack/TCPConnection.java b/tcp/src/main/java/org/jivesoftware/smack/TCPConnection.java index 46aff9ebe..9f9827cf2 100644 --- a/tcp/src/main/java/org/jivesoftware/smack/TCPConnection.java +++ b/tcp/src/main/java/org/jivesoftware/smack/TCPConnection.java @@ -414,13 +414,7 @@ public class TCPConnection extends XMPPConnection { wasAuthenticated = false; } - public void sendPacket(Packet packet) { - if (!isConnected()) { - throw new IllegalStateException("Not connected to server."); - } - if (packet == null) { - throw new NullPointerException("Packet is null."); - } + void sendPacketInternal(Packet packet) { packetWriter.sendPacket(packet); }