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.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);
}

View File

@ -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);

View File

@ -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);
}
/**

View File

@ -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);
}
}

View File

@ -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);
}