mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-25 07:42: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();
|
callConnectionAuthenticatedListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendPacketInternal(Packet packet) {
|
void sendPacketInternal(Packet packet) throws NotConnectedException {
|
||||||
if (!done) {
|
if (done) {
|
||||||
try {
|
throw new NotConnectedException();
|
||||||
send(ComposableBody.builder().setPayloadXML(packet.toXML().toString())
|
}
|
||||||
.build());
|
try {
|
||||||
} catch (BOSHException e) {
|
send(ComposableBody.builder().setPayloadXML(packet.toXML().toString()).build());
|
||||||
LOGGER.log(Level.SEVERE, "BOSHException in sendPacketInternal", e);
|
}
|
||||||
}
|
catch (BOSHException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "BOSHException in sendPacketInternal", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -341,7 +341,7 @@ public abstract class XMPPConnection {
|
||||||
*/
|
*/
|
||||||
public abstract boolean isSecureConnection();
|
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
|
* 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
|
* 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
|
* (and subsequent connections). In that case, all state is preserved to allow for
|
||||||
* more seamless error recovery.
|
* more seamless error recovery.
|
||||||
|
* @throws NotConnectedException
|
||||||
*/
|
*/
|
||||||
public void disconnect() {
|
public void disconnect() throws NotConnectedException {
|
||||||
disconnect(new Presence(Presence.Type.unavailable));
|
disconnect(new Presence(Presence.Type.unavailable));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -657,8 +658,9 @@ public abstract class XMPPConnection {
|
||||||
* more seamless error recovery.
|
* more seamless error recovery.
|
||||||
*
|
*
|
||||||
* @param unavailablePresence the presence packet to send during shutdown.
|
* @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
|
* Adds a new listener that will be notified when new Connections are created. Note
|
||||||
|
|
|
@ -17,12 +17,12 @@
|
||||||
|
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
|
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,19 +80,18 @@ class PacketWriter {
|
||||||
* Sends the specified packet to the server.
|
* Sends the specified packet to the server.
|
||||||
*
|
*
|
||||||
* @param packet the packet to send.
|
* @param packet the packet to send.
|
||||||
|
* @throws NotConnectedException
|
||||||
*/
|
*/
|
||||||
public void sendPacket(Packet packet) {
|
public void sendPacket(Packet packet) throws NotConnectedException {
|
||||||
if (done) {
|
if (done) {
|
||||||
return;
|
throw new NotConnectedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
queue.put(packet);
|
queue.put(packet);
|
||||||
}
|
}
|
||||||
catch (InterruptedException ie) {
|
catch (InterruptedException ie) {
|
||||||
LOGGER.log(Level.SEVERE,
|
throw new NotConnectedException();
|
||||||
"Failed to queue packet to send to server: " + packet.toString(), ie);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -272,7 +272,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
||||||
|
|
||||||
// Set presence to online.
|
// Set presence to online.
|
||||||
if (config.isSendPresence()) {
|
if (config.isSendPresence()) {
|
||||||
packetWriter.sendPacket(new Presence(Presence.Type.available));
|
sendPacket(new Presence(Presence.Type.available));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stores the authentication for future reconnection
|
// Stores the authentication for future reconnection
|
||||||
|
@ -316,7 +316,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set presence to online.
|
// Set presence to online.
|
||||||
packetWriter.sendPacket(new Presence(Presence.Type.available));
|
sendPacket(new Presence(Presence.Type.available));
|
||||||
|
|
||||||
// Indicate that we're now authenticated.
|
// Indicate that we're now authenticated.
|
||||||
authenticated = true;
|
authenticated = true;
|
||||||
|
@ -360,11 +360,12 @@ public class XMPPTCPConnection extends XMPPConnection {
|
||||||
* connection's state is kept.
|
* connection's state is kept.
|
||||||
*
|
*
|
||||||
* @param unavailablePresence the presence packet to send during shutdown.
|
* @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.
|
// Set presence to offline.
|
||||||
if (packetWriter != null) {
|
if (packetWriter != null) {
|
||||||
packetWriter.sendPacket(unavailablePresence);
|
sendPacket(unavailablePresence);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setWasAuthenticated(authenticated);
|
this.setWasAuthenticated(authenticated);
|
||||||
|
@ -403,7 +404,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
||||||
writer = null;
|
writer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void disconnect(Presence unavailablePresence) {
|
public synchronized void disconnect(Presence unavailablePresence) throws NotConnectedException {
|
||||||
// If not connected, ignore this request.
|
// If not connected, ignore this request.
|
||||||
if (packetReader == null || packetWriter == null) {
|
if (packetReader == null || packetWriter == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -418,7 +419,7 @@ public class XMPPTCPConnection extends XMPPConnection {
|
||||||
wasAuthenticated = false;
|
wasAuthenticated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendPacketInternal(Packet packet) {
|
void sendPacketInternal(Packet packet) throws NotConnectedException {
|
||||||
packetWriter.sendPacket(packet);
|
packetWriter.sendPacket(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -914,7 +915,12 @@ public class XMPPTCPConnection extends XMPPConnection {
|
||||||
if (packetWriter != null)
|
if (packetWriter != null)
|
||||||
packetWriter.done = true;
|
packetWriter.done = true;
|
||||||
// Closes the connection temporary. A reconnection is possible
|
// 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.
|
// Notify connection listeners of the error.
|
||||||
callConnectionClosedOnErrorListener(e);
|
callConnectionClosedOnErrorListener(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ import java.io.Writer;
|
||||||
import java.util.concurrent.BrokenBarrierException;
|
import java.util.concurrent.BrokenBarrierException;
|
||||||
import java.util.concurrent.CyclicBarrier;
|
import java.util.concurrent.CyclicBarrier;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ public class PacketWriterTest {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("javadoc")
|
@SuppressWarnings("javadoc")
|
||||||
@Test
|
@Test
|
||||||
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException {
|
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException {
|
||||||
XMPPTCPConnection connection = new XMPPTCPConnection("foobar.com");
|
XMPPTCPConnection connection = new XMPPTCPConnection("foobar.com");
|
||||||
final PacketWriter pw = new PacketWriter(connection);
|
final PacketWriter pw = new PacketWriter(connection);
|
||||||
pw.setWriter(new BlockingStringWriter());
|
pw.setWriter(new BlockingStringWriter());
|
||||||
|
@ -59,13 +60,13 @@ public class PacketWriterTest {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
barrier.await();
|
barrier.await();
|
||||||
|
pw.sendPacket(new Message());
|
||||||
|
// should only return after the pw was interrupted
|
||||||
|
if (!shutdown) {
|
||||||
|
prematureUnblocked = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (InterruptedException | BrokenBarrierException e1) {
|
catch (Exception e) {
|
||||||
}
|
|
||||||
pw.sendPacket(new Message());
|
|
||||||
// should only return after the pw was shutdown
|
|
||||||
if (!shutdown) {
|
|
||||||
prematureUnblocked = true;
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
barrier.await();
|
barrier.await();
|
||||||
|
|
Loading…
Reference in a new issue