From 930a13cd3640422a0fd23e2952cacf798286f0bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 3 May 2008 22:30:11 +0000 Subject: [PATCH] hand reason for disconnects to listeners git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@848 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/fcp/FcpAdapter.java | 8 +-- src/net/pterodactylus/fcp/FcpConnection.java | 31 +++++---- .../pterodactylus/fcp/FcpConnectionHandler.java | 10 +-- src/net/pterodactylus/fcp/FcpListener.java | 81 +++++++++++----------- .../fcp/highlevel/HighLevelClient.java | 31 +++++++-- .../fcp/highlevel/HighLevelClientListener.java | 5 +- 6 files changed, 97 insertions(+), 69 deletions(-) diff --git a/src/net/pterodactylus/fcp/FcpAdapter.java b/src/net/pterodactylus/fcp/FcpAdapter.java index 5f35180..eb67768 100644 --- a/src/net/pterodactylus/fcp/FcpAdapter.java +++ b/src/net/pterodactylus/fcp/FcpAdapter.java @@ -21,7 +21,7 @@ package net.pterodactylus.fcp; /** * Adapter for {@link FcpListener}. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> * @version $Id$ */ @@ -85,7 +85,7 @@ public class FcpAdapter implements FcpListener { /** * {@inheritDoc} - * + * * @see FcpListener#receivedNodeData(FcpConnection, NodeData) */ public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) { @@ -94,7 +94,7 @@ public class FcpAdapter implements FcpListener { /** * {@inheritDoc} - * + * * @see FcpListener#receivedTestDDAReply(FcpConnection, TestDDAReply) */ public void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) { @@ -286,7 +286,7 @@ public class FcpAdapter implements FcpListener { /** * {@inheritDoc} */ - public void connectionClosed(FcpConnection fcpConnection) { + public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) { /* empty. */ } diff --git a/src/net/pterodactylus/fcp/FcpConnection.java b/src/net/pterodactylus/fcp/FcpConnection.java index 5757d61..d7ed4bc 100644 --- a/src/net/pterodactylus/fcp/FcpConnection.java +++ b/src/net/pterodactylus/fcp/FcpConnection.java @@ -649,11 +649,14 @@ public class FcpConnection implements Closeable { /** * Notifies all listeners that the connection to the node was closed. * - * @see FcpListener#connectionClosed(FcpConnection) + * @param throwable + * The exception that caused the disconnect, or null + * if there was no exception + * @see FcpListener#connectionClosed(FcpConnection, Throwable) */ - private void fireConnectionClosed() { + private void fireConnectionClosed(Throwable throwable) { for (FcpListener fcpListener: fcpListeners) { - fcpListener.connectionClosed(this); + fcpListener.connectionClosed(this, throwable); } } @@ -696,14 +699,7 @@ public class FcpConnection implements Closeable { * does nothing. */ public void close() { - if (connectionHandler == null) { - return; - } - logger.info("disconnecting…"); - FcpUtils.close(remoteSocket); - connectionHandler.stop(); - connectionHandler = null; - fireConnectionClosed(); + handleDisconnect(null); } /** @@ -823,13 +819,20 @@ public class FcpConnection implements Closeable { /** * Handles a disconnect from the node. + * + * @param throwable + * The exception that caused the disconnect, or null + * if there was no exception */ - synchronized void handleDisconnect() { + synchronized void handleDisconnect(Throwable throwable) { FcpUtils.close(remoteInputStream); FcpUtils.close(remoteOutputStream); FcpUtils.close(remoteSocket); - connectionHandler = null; - fireConnectionClosed(); + if (connectionHandler != null) { + connectionHandler.stop(); + connectionHandler = null; + } + fireConnectionClosed(throwable); } // diff --git a/src/net/pterodactylus/fcp/FcpConnectionHandler.java b/src/net/pterodactylus/fcp/FcpConnectionHandler.java index b6404c5..a8132c3 100644 --- a/src/net/pterodactylus/fcp/FcpConnectionHandler.java +++ b/src/net/pterodactylus/fcp/FcpConnectionHandler.java @@ -26,7 +26,7 @@ import java.nio.charset.Charset; /** * Handles an FCP connection to a node. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> * @version $Id$ */ @@ -47,7 +47,7 @@ class FcpConnectionHandler implements Runnable { /** * Creates a new connection handler that operates on the given connection * and input stream. - * + * * @param fcpConnection * The underlying FCP connection * @param remoteInputStream @@ -63,6 +63,7 @@ class FcpConnectionHandler implements Runnable { */ public void run() { FcpMessage fcpMessage = null; + Throwable throwable = null; while (true) { synchronized (this) { if (shouldStop) { @@ -97,10 +98,11 @@ class FcpConnectionHandler implements Runnable { assert fcpMessage != null: "fcp message is null"; fcpMessage.setField(field, value); } catch (IOException e) { + throwable = null; break; } } - fcpConnection.handleDisconnect(); + fcpConnection.handleDisconnect(throwable); } /** @@ -119,7 +121,7 @@ class FcpConnectionHandler implements Runnable { /** * Reads bytes from {@link #remoteInputStream} until ‘\r’ or ‘\n’ are * encountered and decodes the read bytes using UTF-8. - * + * * @return The decoded line * @throws IOException * if an I/O error occurs diff --git a/src/net/pterodactylus/fcp/FcpListener.java b/src/net/pterodactylus/fcp/FcpListener.java index 90860e5..09a5f2a 100644 --- a/src/net/pterodactylus/fcp/FcpListener.java +++ b/src/net/pterodactylus/fcp/FcpListener.java @@ -23,7 +23,7 @@ import java.util.EventListener; /** * Interface for objects that want to be notified on certain FCP events. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> * @version $Id$ */ @@ -31,7 +31,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “NodeHello” message was received. - * + * * @param fcpConnection * The connection that received the message * @param nodeHello @@ -42,7 +42,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “CloseConnectionDuplicateClientName” message * was received. - * + * * @param fcpConnection * The connection that received the message * @param closeConnectionDuplicateClientName @@ -52,7 +52,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “SSKKeypair” message was received. - * + * * @param fcpConnection * The connection that received themessage * @param sskKeypair @@ -62,7 +62,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “Peer” message was received. - * + * * @param fcpConnection * The connection that received the message * @param peer @@ -72,7 +72,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that an “EndListPeers” message was received. - * + * * @param fcpConnection * The connection that recevied the message * @param endListPeers @@ -82,7 +82,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PeerNote” message was received. - * + * * @param fcpConnection * The connection that received the message * @param peerNote @@ -92,7 +92,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that an “EndListPeerNotes” message was received. - * + * * @param fcpConnection * The connection that received the message * @param endListPeerNotes @@ -102,7 +102,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PeerRemoved” message was received. - * + * * @param fcpConnection * The connection that received the message * @param peerRemoved @@ -112,7 +112,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “NodeData” message was received. - * + * * @param fcpConnection * The connection that received the message * @param nodeData @@ -122,7 +122,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “TestDDAReply” message was received. - * + * * @param fcpConnection * The connection that received the message * @param testDDAReply @@ -132,7 +132,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “TestDDAComplete” was received. - * + * * @param fcpConnection * The connection that received the message * @param testDDAComplete @@ -142,7 +142,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PersistentGet” was received. - * + * * @param fcpConnection * The connection that received the message * @param persistentGet @@ -152,7 +152,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PersistentPut” was received. - * + * * @param fcpConnection * The connection that received the message * @param persistentPut @@ -162,7 +162,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “EndListPersistentRequests” was received. - * + * * @param fcpConnection * The connection that received the message * @param endListPersistentRequests @@ -172,7 +172,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “URIGenerated” was received. - * + * * @param fcpConnection * The connection that received the message * @param uriGenerated @@ -182,7 +182,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “DataFound” was received. - * + * * @param fcpConnection * The connection that received the message * @param dataFound @@ -192,7 +192,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that an “AllData” was received. - * + * * @param fcpConnection * The connection that received the message * @param allData @@ -202,7 +202,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “SimpleProgress” was received. - * + * * @param fcpConnection * The connection that received the message * @param simpleProgress @@ -212,7 +212,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “StartedCompression” was received. - * + * * @param fcpConnection * The connection that received the message * @param startedCompression @@ -222,7 +222,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “FinishedCompression” was received. - * + * * @param fcpConnection * The connection that received the message * @param finishedCompression @@ -232,7 +232,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that an “UnknownPeerNoteType” was received. - * + * * @param fcpConnection * The connection that received the message * @param unknownPeerNoteType @@ -242,7 +242,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “UnknownNodeIdentifier” message was received. - * + * * @param fcpConnection * The connection that received the message * @param unknownNodeIdentifier @@ -252,7 +252,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “ConfigData” message was received. - * + * * @param fcpConnection * The connection that received the message * @param configData @@ -262,7 +262,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “GetFailed” message was recevied. - * + * * @param fcpConnection * The connection that received the message * @param getFailed @@ -272,7 +272,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PutFailed” message was received. - * + * * @param fcpConnection * The connection that received the message * @param putFailed @@ -282,7 +282,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that an “IdentifierCollision” message was receied. - * + * * @param fcpConnection * The connection that received the message * @param identifierCollision @@ -292,7 +292,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PersistentPutDir” message was received. - * + * * @param fcpConnection * The connection that received the message * @param persistentPutDir @@ -303,7 +303,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PersistentRequestRemoved” message was * received. - * + * * @param fcpConnection * The connection that received the message * @param persistentRequestRemoved @@ -313,7 +313,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “SubscribedUSKUpdate” message was received. - * + * * @param fcpConnection * The connection that recevied the message * @param subscribedUSKUpdate @@ -323,7 +323,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PluginInfo” message was received. - * + * * @param fcpConnection * The connection that received the message * @param pluginInfo @@ -333,7 +333,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that an “FCPPluginReply“ message was received. - * + * * @param fcpConnection * The connection that received the message * @param fcpPluginReply @@ -344,7 +344,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PersistentRequestModified” message was * received. - * + * * @param fcpConnection * The connection that received the message * @param persistentRequestModified @@ -354,7 +354,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PutSuccessful” message was received. - * + * * @param fcpConnection * The connection that received the message * @param putSuccessful @@ -364,7 +364,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “PutFetchable” message was received. - * + * * @param fcpConnection * The connection that received the message * @param putFetchable @@ -374,7 +374,7 @@ public interface FcpListener extends EventListener { /** * Notifies a listener that a “ProtocolError” was received. - * + * * @param fcpConnection * The connection that received the message * @param protocolError @@ -386,7 +386,7 @@ public interface FcpListener extends EventListener { * Notifies a listener that a message has been received. This method is only * called if {@link FcpConnection#handleMessage(FcpMessage)} does not * recognize the message. Should that ever happen, please file a bug report! - * + * * @param fcpConnection * The connection that received the message * @param fcpMessage @@ -398,10 +398,13 @@ public interface FcpListener extends EventListener { * Notifies a listener that a connection was closed. A closed connection can * be reestablished by calling {@link FcpConnection#connect()} on the same * object again. - * + * * @param fcpConnection * The connection that was closed. + * @param throwable + * The exception that caused the disconnect, or null + * if there was no exception */ - public void connectionClosed(FcpConnection fcpConnection); + public void connectionClosed(FcpConnection fcpConnection, Throwable throwable); } diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java index 6149adf..9fd36fd 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java @@ -232,10 +232,14 @@ public class HighLevelClient { /** * Notifies all listeners that a client has disconnected. + * + * @param throwable + * The exception that caused the disconnect, or null + * if there was no exception */ - private void fireClientDisconnected() { + private void fireClientDisconnected(Throwable throwable) { for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) { - highLevelClientListener.clientDisconnected(this); + highLevelClientListener.clientDisconnected(this, throwable); } } @@ -278,8 +282,7 @@ public class HighLevelClient { * Disconnects the client from the node. */ public void disconnect() { - fcpConnection.close(); - fireClientDisconnected(); + disconnect(null); } /** @@ -434,6 +437,19 @@ public class HighLevelClient { } /** + * Disconnects the client from the node, handing the given Throwable to + * {@link #fireClientDisconnected(Throwable)}. + * + * @param throwable + * The exception that caused the disconnect, or null + * if there was no exception + */ + private void disconnect(Throwable throwable) { + fcpConnection.close(); + fireClientDisconnected(throwable); + } + + /** * FCP listener for {@link HighLevelClient}. * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> @@ -608,15 +624,16 @@ public class HighLevelClient { // /** - * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection) + * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection, + * Throwable) */ @SuppressWarnings("synthetic-access") - public void connectionClosed(FcpConnection fcpConnection) { + public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) { if (fcpConnection != HighLevelClient.this.fcpConnection) { return; } cancelIdentifier(null); - disconnect(); + disconnect(throwable); } /** diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClientListener.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClientListener.java index aef7fd4..9ea613d 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClientListener.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClientListener.java @@ -44,7 +44,10 @@ public interface HighLevelClientListener extends EventListener { * * @param highLevelClient * The client that was disconnected + * @param throwable + * The exception that caused the disconnect, or null + * if no exception occured */ - public void clientDisconnected(HighLevelClient highLevelClient); + public void clientDisconnected(HighLevelClient highLevelClient, Throwable throwable); } -- 2.7.4