mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
Move duplicate code into XMPPConnection
from XMPPTCPConnection and XMPPBOSHConnection.
This commit is contained in:
parent
7d72b9b770
commit
f940d72fcd
5 changed files with 62 additions and 177 deletions
|
@ -114,13 +114,9 @@ class PacketReader {
|
|||
}
|
||||
|
||||
/**
|
||||
* Shuts the packet reader down.
|
||||
* Shuts the packet reader down. This method simply sets the 'done' flag to true.
|
||||
*/
|
||||
public void shutdown() {
|
||||
// Notify connection listeners of the connection closing if done hasn't already been set.
|
||||
if (!done) {
|
||||
connection.callConnectionClosedListener();
|
||||
}
|
||||
done = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -81,15 +81,6 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
// by XMPPTCPConnection, PacketReader, PacketWriter
|
||||
private volatile boolean socketClosed = false;
|
||||
|
||||
/**
|
||||
* Flag that indicates if the user is currently authenticated with the server.
|
||||
*/
|
||||
private boolean authenticated = false;
|
||||
/**
|
||||
* Flag that indicates if the user was authenticated with the server when the connection
|
||||
* to the server was closed (abruptly or not).
|
||||
*/
|
||||
private boolean wasAuthenticated = false;
|
||||
private boolean anonymous = false;
|
||||
private boolean usingTLS = false;
|
||||
|
||||
|
@ -353,24 +344,11 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
}
|
||||
|
||||
/**
|
||||
* Closes the connection by setting presence to unavailable then closing the stream to
|
||||
* the XMPP server. The shutdown logic will be used during a planned disconnection or when
|
||||
* dealing with an unexpected disconnection. Unlike {@link #disconnect()} the connection's
|
||||
* packet reader, packet writer, and {@link Roster} will not be removed; thus
|
||||
* connection's state is kept.
|
||||
*
|
||||
* @param unavailablePresence the presence packet to send during shutdown.
|
||||
* @throws NotConnectedException
|
||||
* Shuts the current connection down. After this method returns, the connection must be ready
|
||||
* for re-use by connect.
|
||||
*/
|
||||
protected void shutdown(Presence unavailablePresence) throws NotConnectedException {
|
||||
// Set presence to offline.
|
||||
if (packetWriter != null) {
|
||||
sendPacket(unavailablePresence);
|
||||
}
|
||||
|
||||
this.setWasAuthenticated(authenticated);
|
||||
authenticated = false;
|
||||
|
||||
@Override
|
||||
protected void shutdown() {
|
||||
if (packetReader != null) {
|
||||
packetReader.shutdown();
|
||||
}
|
||||
|
@ -378,14 +356,6 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
packetWriter.shutdown();
|
||||
}
|
||||
|
||||
// Wait 150 ms for processes to clean-up, then shutdown.
|
||||
try {
|
||||
Thread.sleep(150);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Ignore.
|
||||
}
|
||||
|
||||
// Set socketClosed to true. This will cause the PacketReader
|
||||
// and PacketWriter to ignore any Exceptions that are thrown
|
||||
// because of a read/write from/to a closed stream.
|
||||
|
@ -396,29 +366,14 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
} catch (Exception e) {
|
||||
LOGGER.log(Level.WARNING, "shutdown", e);
|
||||
}
|
||||
// In most cases the close() should be successful, so set
|
||||
// connected to false here.
|
||||
connected = false;
|
||||
|
||||
setWasAuthenticated(authenticated);
|
||||
authenticated = false;
|
||||
connected = false;
|
||||
reader = null;
|
||||
writer = null;
|
||||
}
|
||||
|
||||
public synchronized void disconnect(Presence unavailablePresence) throws NotConnectedException {
|
||||
// If not connected, ignore this request.
|
||||
if (packetReader == null || packetWriter == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
shutdown(unavailablePresence);
|
||||
|
||||
wasAuthenticated = false;
|
||||
}
|
||||
|
||||
void sendPacketInternal(Packet packet) throws NotConnectedException {
|
||||
packetWriter.sendPacket(packet);
|
||||
}
|
||||
|
@ -520,49 +475,10 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
|
||||
}
|
||||
catch (SmackException ex) {
|
||||
// An exception occurred in setting up the connection. Make sure we shut down the
|
||||
// readers and writers and close the socket.
|
||||
|
||||
if (packetWriter != null) {
|
||||
try {
|
||||
packetWriter.shutdown();
|
||||
}
|
||||
catch (Throwable ignore) { /* ignore */ }
|
||||
packetWriter = null;
|
||||
}
|
||||
if (packetReader != null) {
|
||||
try {
|
||||
packetReader.shutdown();
|
||||
}
|
||||
catch (Throwable ignore) { /* ignore */ }
|
||||
packetReader = null;
|
||||
}
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
}
|
||||
catch (Throwable ignore) { /* ignore */ }
|
||||
reader = null;
|
||||
}
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
}
|
||||
catch (Throwable ignore) { /* ignore */}
|
||||
writer = null;
|
||||
}
|
||||
if (socket != null) {
|
||||
try {
|
||||
socket.close();
|
||||
}
|
||||
catch (Exception e) { /* ignore */ }
|
||||
socket = null;
|
||||
}
|
||||
this.setWasAuthenticated(authenticated);
|
||||
authenticated = false;
|
||||
connected = false;
|
||||
|
||||
throw ex; // Everything stoppped. Now throw the exception.
|
||||
// An exception occurred in setting up the connection.
|
||||
shutdown();
|
||||
// Everything stoppped. Now throw the exception.
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -888,17 +804,6 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the connection has already logged in the server.
|
||||
*
|
||||
* @param wasAuthenticated true if the connection has already been authenticated.
|
||||
*/
|
||||
private void setWasAuthenticated(boolean wasAuthenticated) {
|
||||
if (!this.wasAuthenticated) {
|
||||
this.wasAuthenticated = wasAuthenticated;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends out a notification that there was an error with the connection
|
||||
* and closes the connection. Also prints the stack trace of the given exception
|
||||
|
@ -910,17 +815,9 @@ public class XMPPTCPConnection extends XMPPConnection {
|
|||
if ((packetReader == null || packetReader.done) &&
|
||||
(packetWriter == null || packetWriter.done)) return;
|
||||
|
||||
if (packetReader != null)
|
||||
packetReader.done = true;
|
||||
if (packetWriter != null)
|
||||
packetWriter.done = true;
|
||||
// Closes the connection temporary. A reconnection is possible
|
||||
try {
|
||||
shutdown(new Presence(Presence.Type.unavailable));
|
||||
}
|
||||
catch (NotConnectedException e1) {
|
||||
// Ignore
|
||||
}
|
||||
shutdown();
|
||||
|
||||
// Notify connection listeners of the error.
|
||||
callConnectionClosedOnErrorListener(e);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue