Move duplicate sendPacket() code into XMPPConnection

This commit is contained in:
Florian Schmaus 2014-03-17 19:33:17 +01:00
parent c3f9ec4f94
commit 6197f6200f
5 changed files with 26 additions and 48 deletions

View File

@ -21,6 +21,8 @@ import java.io.IOException;
import java.io.PipedReader; import java.io.PipedReader;
import java.io.PipedWriter; import java.io.PipedWriter;
import java.io.Writer; import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.sasl.SaslException; import javax.security.sasl.SaslException;
@ -54,6 +56,7 @@ import org.igniterealtime.jbosh.ComposableBody;
* @author Guenther Niess * @author Guenther Niess
*/ */
public class BOSHConnection extends XMPPConnection { public class BOSHConnection extends XMPPConnection {
private static final Logger LOGGER = Logger.getLogger(BOSHConnection.class.getName());
/** /**
* The XMPP Over Bosh namespace. * The XMPP Over Bosh namespace.
@ -332,31 +335,14 @@ public class BOSHConnection extends XMPPConnection {
callConnectionAuthenticatedListener(); callConnectionAuthenticatedListener();
} }
public void sendPacket(Packet packet) { void sendPacketInternal(Packet packet) {
if (!isConnected()) {
throw new IllegalStateException("Not connected to server.");
}
if (packet == null) {
throw new NullPointerException("Packet is null.");
}
if (!done) { 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 { try {
send(ComposableBody.builder().setPayloadXML(packet.toXML()) send(ComposableBody.builder().setPayloadXML(packet.toXML())
.build()); .build());
} catch (BOSHException e) { } catch (BOSHException e) {
e.printStackTrace(); LOGGER.log(Level.SEVERE, "BOSHException in sendPacketInternal", e);
return;
} }
// 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) { protected void notifyConnectionError(Exception e) {
// Closes the connection temporary. A reconnection is possible // Closes the connection temporary. A reconnection is possible
shutdown(new Presence(Presence.Type.unavailable)); shutdown(new Presence(Presence.Type.unavailable));
// Print the stack trace to help catch the problem
e.printStackTrace();
callConnectionClosedOnErrorListener(e); callConnectionClosedOnErrorListener(e);
} }

View File

@ -335,6 +335,8 @@ public abstract class XMPPConnection {
*/ */
public abstract boolean isSecureConnection(); public abstract boolean isSecureConnection();
abstract void sendPacketInternal(Packet packet);
/** /**
* Returns if the reconnection mechanism is allowed to be used. By default * Returns if the reconnection mechanism is allowed to be used. By default
* reconnection is allowed. * reconnection is allowed.
@ -447,7 +449,21 @@ public abstract class XMPPConnection {
* *
* @param packet the packet to send. * @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. * Returns an account manager instance for this connection.
@ -771,7 +787,7 @@ public abstract class XMPPConnection {
* *
* @param packet the packet to process. * @param packet the packet to process.
*/ */
protected void firePacketSendingListeners(Packet packet) { private void firePacketSendingListeners(Packet packet) {
// Notify the listeners of the new sent packet // Notify the listeners of the new sent packet
for (ListenerWrapper listenerWrapper : sendListeners.values()) { for (ListenerWrapper listenerWrapper : sendListeners.values()) {
listenerWrapper.notifyListener(packet); listenerWrapper.notifyListener(packet);
@ -824,7 +840,7 @@ public abstract class XMPPConnection {
* *
* @param packet the packet that is going to be sent to the server * @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) { if (packet != null) {
for (InterceptorWrapper interceptorWrapper : interceptors.values()) { for (InterceptorWrapper interceptorWrapper : interceptors.values()) {
interceptorWrapper.notifyListener(packet); interceptorWrapper.notifyListener(packet);

View File

@ -185,19 +185,11 @@ public class DummyConnection extends XMPPConnection {
} }
@Override @Override
public void sendPacket(Packet packet) { void sendPacketInternal(Packet packet) {
if (!isConnected()) {
throw new IllegalStateException("Not connected to server.");
}
if (packet == null) {
throw new NullPointerException("Packet is null.");
}
firePacketInterceptors(packet);
if (DEBUG_ENABLED) { if (DEBUG_ENABLED) {
System.out.println("[SEND]: " + packet.toXML()); System.out.println("[SEND]: " + packet.toXML());
} }
queue.add(packet); queue.add(packet);
firePacketSendingListeners(packet);
} }
/** /**

View File

@ -80,10 +80,6 @@ class PacketWriter {
*/ */
public void sendPacket(Packet packet) { public void sendPacket(Packet packet) {
if (!done) { 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 { try {
queue.put(packet); queue.put(packet);
} }
@ -94,10 +90,6 @@ class PacketWriter {
synchronized (queue) { synchronized (queue) {
queue.notifyAll(); 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);
} }
} }

View File

@ -414,13 +414,7 @@ public class TCPConnection extends XMPPConnection {
wasAuthenticated = false; wasAuthenticated = false;
} }
public void sendPacket(Packet packet) { void sendPacketInternal(Packet packet) {
if (!isConnected()) {
throw new IllegalStateException("Not connected to server.");
}
if (packet == null) {
throw new NullPointerException("Packet is null.");
}
packetWriter.sendPacket(packet); packetWriter.sendPacket(packet);
} }