mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 03:52:06 +01:00
Make sendPacket throw NotConnectedException
also use XMPPConnection.sendPacket() instead of PacketWriter.sendPacket() in XMPPTCPConnection.
This commit is contained in:
parent
7041e90522
commit
42f2eae8fb
5 changed files with 40 additions and 31 deletions
|
@ -336,14 +336,15 @@ public class XMPPBOSHConnection extends XMPPConnection {
|
|||
callConnectionAuthenticatedListener();
|
||||
}
|
||||
|
||||
void sendPacketInternal(Packet packet) {
|
||||
if (!done) {
|
||||
try {
|
||||
send(ComposableBody.builder().setPayloadXML(packet.toXML().toString())
|
||||
.build());
|
||||
} catch (BOSHException e) {
|
||||
LOGGER.log(Level.SEVERE, "BOSHException in sendPacketInternal", e);
|
||||
}
|
||||
void sendPacketInternal(Packet packet) throws NotConnectedException {
|
||||
if (done) {
|
||||
throw new NotConnectedException();
|
||||
}
|
||||
try {
|
||||
send(ComposableBody.builder().setPayloadXML(packet.toXML().toString()).build());
|
||||
}
|
||||
catch (BOSHException e) {
|
||||
LOGGER.log(Level.SEVERE, "BOSHException in sendPacketInternal", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -341,7 +341,7 @@ public abstract class XMPPConnection {
|
|||
*/
|
||||
public abstract boolean isSecureConnection();
|
||||
|
||||
abstract void sendPacketInternal(Packet packet);
|
||||
abstract void sendPacketInternal(Packet packet) throws NotConnectedException;
|
||||
|
||||
/**
|
||||
* Returns true if network traffic is being compressed. When using stream compression network
|
||||
|
@ -637,8 +637,9 @@ public abstract class XMPPConnection {
|
|||
* on this connection again. This is unlike the behavior during unexpected disconnects
|
||||
* (and subsequent connections). In that case, all state is preserved to allow for
|
||||
* more seamless error recovery.
|
||||
* @throws NotConnectedException
|
||||
*/
|
||||
public void disconnect() {
|
||||
public void disconnect() throws NotConnectedException {
|
||||
disconnect(new Presence(Presence.Type.unavailable));
|
||||
}
|
||||
|
||||
|
@ -657,8 +658,9 @@ public abstract class XMPPConnection {
|
|||
* more seamless error recovery.
|
||||
*
|
||||
* @param unavailablePresence the presence packet to send during shutdown.
|
||||
* @throws NotConnectedException
|
||||
*/
|
||||
public abstract void disconnect(Presence unavailablePresence);
|
||||
public abstract void disconnect(Presence unavailablePresence) throws NotConnectedException;
|
||||
|
||||
/**
|
||||
* Adds a new listener that will be notified when new Connections are created. Note
|
||||
|
|
|
@ -17,12 +17,12 @@
|
|||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
|
@ -80,19 +80,18 @@ class PacketWriter {
|
|||
* Sends the specified packet to the server.
|
||||
*
|
||||
* @param packet the packet to send.
|
||||
* @throws NotConnectedException
|
||||
*/
|
||||
public void sendPacket(Packet packet) {
|
||||
public void sendPacket(Packet packet) throws NotConnectedException {
|
||||
if (done) {
|
||||
return;
|
||||
throw new NotConnectedException();
|
||||
}
|
||||
|
||||
try {
|
||||
queue.put(packet);
|
||||
}
|
||||
catch (InterruptedException ie) {
|
||||
LOGGER.log(Level.SEVERE,
|
||||
"Failed to queue packet to send to server: " + packet.toString(), ie);
|
||||
return;
|
||||
throw new NotConnectedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -272,7 +272,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
|
||||
// Set presence to online.
|
||||
if (config.isSendPresence()) {
|
||||
packetWriter.sendPacket(new Presence(Presence.Type.available));
|
||||
sendPacket(new Presence(Presence.Type.available));
|
||||
}
|
||||
|
||||
// Stores the authentication for future reconnection
|
||||
|
@ -316,7 +316,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
}
|
||||
|
||||
// Set presence to online.
|
||||
packetWriter.sendPacket(new Presence(Presence.Type.available));
|
||||
sendPacket(new Presence(Presence.Type.available));
|
||||
|
||||
// Indicate that we're now authenticated.
|
||||
authenticated = true;
|
||||
|
@ -360,11 +360,12 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
* connection's state is kept.
|
||||
*
|
||||
* @param unavailablePresence the presence packet to send during shutdown.
|
||||
* @throws NotConnectedException
|
||||
*/
|
||||
protected void shutdown(Presence unavailablePresence) {
|
||||
protected void shutdown(Presence unavailablePresence) throws NotConnectedException {
|
||||
// Set presence to offline.
|
||||
if (packetWriter != null) {
|
||||
packetWriter.sendPacket(unavailablePresence);
|
||||
sendPacket(unavailablePresence);
|
||||
}
|
||||
|
||||
this.setWasAuthenticated(authenticated);
|
||||
|
@ -403,7 +404,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
writer = null;
|
||||
}
|
||||
|
||||
public synchronized void disconnect(Presence unavailablePresence) {
|
||||
public synchronized void disconnect(Presence unavailablePresence) throws NotConnectedException {
|
||||
// If not connected, ignore this request.
|
||||
if (packetReader == null || packetWriter == null) {
|
||||
return;
|
||||
|
@ -418,7 +419,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
wasAuthenticated = false;
|
||||
}
|
||||
|
||||
void sendPacketInternal(Packet packet) {
|
||||
void sendPacketInternal(Packet packet) throws NotConnectedException {
|
||||
packetWriter.sendPacket(packet);
|
||||
}
|
||||
|
||||
|
@ -914,7 +915,12 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
if (packetWriter != null)
|
||||
packetWriter.done = true;
|
||||
// Closes the connection temporary. A reconnection is possible
|
||||
shutdown(new Presence(Presence.Type.unavailable));
|
||||
try {
|
||||
shutdown(new Presence(Presence.Type.unavailable));
|
||||
}
|
||||
catch (NotConnectedException e1) {
|
||||
// Ignore
|
||||
}
|
||||
// Notify connection listeners of the error.
|
||||
callConnectionClosedOnErrorListener(e);
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.io.Writer;
|
|||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -41,7 +42,7 @@ public class PacketWriterTest {
|
|||
*/
|
||||
@SuppressWarnings("javadoc")
|
||||
@Test
|
||||
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException {
|
||||
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException {
|
||||
XMPPTCPConnection connection = new XMPPTCPConnection("foobar.com");
|
||||
final PacketWriter pw = new PacketWriter(connection);
|
||||
pw.setWriter(new BlockingStringWriter());
|
||||
|
@ -59,13 +60,13 @@ public class PacketWriterTest {
|
|||
public void run() {
|
||||
try {
|
||||
barrier.await();
|
||||
pw.sendPacket(new Message());
|
||||
// should only return after the pw was interrupted
|
||||
if (!shutdown) {
|
||||
prematureUnblocked = true;
|
||||
}
|
||||
}
|
||||
catch (InterruptedException | BrokenBarrierException e1) {
|
||||
}
|
||||
pw.sendPacket(new Message());
|
||||
// should only return after the pw was shutdown
|
||||
if (!shutdown) {
|
||||
prematureUnblocked = true;
|
||||
catch (Exception e) {
|
||||
}
|
||||
try {
|
||||
barrier.await();
|
||||
|
|
Loading…
Reference in a new issue